1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
|
= Platform example
:us: _
This barectf platform example is a stripped-down version of the
https://github.com/efficios/barectf/tree/stable-{page-component-version}/platforms/linux-fs[Linux FS demonstration platform].
== `my-platform.h`
[source,c]
----
#ifndef _MY_PLATFORM_H
#define _MY_PLATFORM_H
#ifdef __cplusplus
extern "C" {
#endif
/* Platform context */
struct my_platform_ctx;
/* barectf context */
struct barectf_my_stream_ctx;
/* Platform initialization function */
struct my_platform_ctx *my_platform_init(unsigned int buf_size,
const char *data_stream_file_path);
/* Platform finalization function */
void my_platform_fini(struct my_platform_ctx *platform_ctx);
/* barectf context pointer access function */
struct barectf_my_stream_ctx *my_platform_get_barectf_ctx(
struct my_platform_ctx *platform_ctx);
#ifdef __cplusplus
}
#endif
#endif /* _MY_PLATFORM_H */
----
== `my-platform.c`
[source,c]
----
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <time.h>
#include "barectf.h"
/* Platform context */
struct my_platform_ctx {
struct barectf_my_stream_ctx ctx;
FILE *fh;
};
/* Clock source platform function */
static uint64_t my_clock_get_value(void * const data)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
}
static void write_packet(const struct my_platform_ctx * const platform_ctx)
{
/* Append current packet to data stream file */
const size_t nmemb = fwrite(barectf_packet_buf_addr(&platform_ctx->ctx),
barectf_packet_buf_size(&platform_ctx->ctx),
1, platform_ctx->fh);
assert(nmemb == 1);
}
/* Full back end check platform function */
static int is_backend_full(void * const data)
{
return 0;
}
/* Packet opening platform function */
static void open_packet(void * const data)
{
struct my_platform_ctx * const platform_ctx = data;
barectf_my_stream_open_packet(&platform_ctx->ctx);
}
/* Packet closing platform function */
static void close_packet(void * const data)
{
struct my_platform_ctx * const platform_ctx = data;
/* Close packet now */
barectf_my_stream_close_packet(&platform_ctx->ctx);
/* Write packet to file */
write_packet(platform_ctx);
}
/* Platform initialization function */
struct my_platform_ctx *my_platform_init(const unsigned int buf_size,
const char * const data_stream_file_path)
{
char stream_path[256];
uint8_t *buf = NULL;
struct my_platform_ctx *platform_ctx;
struct barectf_platform_callbacks cbs;
/* Set platform callback functions */
cbs.my_clock_clock_get_value = my_clock_get_value;
cbs.is_backend_full = is_backend_full;
cbs.open_packet = open_packet;
cbs.close_packet = close_packet;
/* Allocate platform context (which contains a barectf context) */
platform_ctx = malloc(sizeof(*platform_ctx));
if (!platform_ctx) {
goto error;
}
/* Allocate packet buffer */
buf = malloc(buf_size);
if (!buf) {
goto error;
}
/* Open data stream file */
platform_ctx->fh = fopen(data_stream_file_path, "wb");
if (!platform_ctx->fh) {
goto error;
}
/* Initialize barectf context */
barectf_init(&platform_ctx->ctx, buf, buf_size, cbs, platform_ctx);
/* Open the first packet */
open_packet(platform_ctx);
goto end;
error:
free(platform_ctx);
free(buf);
end:
/* Return platform context to user */
return platform_ctx;
}
/* Platform finalization function */
void my_platform_fini(struct my_platform_ctx * const platform_ctx)
{
/* Close current packet if needed */
if (barectf_packet_is_open(&platform_ctx->ctx) &&
!barectf_packet_is_empty(&platform_ctx->ctx)) {
close_packet(platform_ctx);
}
/* Close data stream file */
fclose(platform_ctx->fh);
/* Deallocate packet buffer */
free(barectf_packet_buf(&platform_ctx->ctx));
/* Deallocate platform context */
free(platform_ctx);
}
/* barectf context pointer access function */
struct barectf_my_stream_ctx *my_platform_get_barectf_ctx(
struct my_platform_ctx * const platform_ctx)
{
return &platform_ctx->ctx;
}
----
== Components
In this example, you can find all the required components of a barectf
platform:
xref:api.adoc#cbs[Platform callback functions]::
xref:api.adoc#cb-clk-src[Clock source]:::
+
[source,c]
----
static uint64_t my_clock_get_value(void * const data)
{
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
}
----
xref:api.adoc#cb-open[Packet opening]:::
+
[source,c]
----
static void open_packet(void * const data)
{
struct my_platform_ctx * const platform_ctx = data;
barectf_my_stream_open_packet(&platform_ctx->ctx);
}
----
xref:api.adoc#cb-close[Packet closing]:::
+
[source,c]
----
static void close_packet(void * const data)
{
struct my_platform_ctx * const platform_ctx = data;
barectf_my_stream_close_packet(&platform_ctx->ctx);
write_packet(platform_ctx);
}
----
xref:api.adoc#cb-is-back-end-full[Is the back end full?]:::
+
[source,c]
----
static int is_backend_full(void * const data)
{
return 0;
}
----
+
This one always returns 0 as we assume that we can always append a
packet to the data stream file.
Platform initialization function::
+
[source,c]
----
struct my_platform_ctx *my_platform_init(const unsigned int buf_size,
const char * const data_stream_file_path)
{
/* ... */
}
----
Platform finalization function::
+
[source,c]
----
void my_platform_fini(struct my_platform_ctx * const platform_ctx)
{
if (barectf_packet_is_open(&platform_ctx->ctx) &&
!barectf_packet_is_empty(&platform_ctx->ctx)) {
close_packet(platform_ctx);
}
/* ... */
}
----
barectf context pointer access function::
+
[source,c]
----
struct barectf_my_stream_ctx *my_platform_get_barectf_ctx(
struct my_platform_ctx * const platform_ctx)
{
return &platform_ctx->ctx;
}
----
== Other platform examples
Have a look at:
* The
https://github.com/efficios/barectf/tree/stable-{page-component-version}/platforms/linux-fs[Linux FS platform].
* The
https://github.com/efficios/barectf/blob/{page-component-version}/examples/barectf-tracepoint/barectf-platform-qemu-arm-uart.c[`barectf-platform-qemu-arm-uart.c`]
file, which is part of the `barectf-tracepoint.h` usage example.
* The
https://github.com/efficios/barectf/tree/v2.3.1/platforms/parallella[Parallella platform].
+
The project no longer maintains this platform as it was an experiment
to trace the Epiphany cores of the
https://www.parallella.org/[Parallella board].
+
That being said, this platform can still prove interesting to understand
how to implement a barectf platform using an asynchronous
producer-consumer model.
|