4 This barectf platform example is a stripped-down version of the
5 https://github.com/efficios/barectf/tree/stable-{page-component-version}/platforms/linux-fs[Linux FS]
6 demonstration platform.
13 #ifndef _MY_PLATFORM_H
14 #define _MY_PLATFORM_H
20 /* Platform context */
21 struct my_platform_ctx;
24 struct barectf_my_stream_ctx;
26 /* Platform initialization function */
27 struct my_platform_ctx *my_platform_init(unsigned int buf_size,
28 const char *data_stream_file_path);
30 /* Platform finalization function */
31 void my_platform_fini(struct my_platform_ctx *platform_ctx);
33 /* barectf context pointer access function */
34 struct barectf_my_stream_ctx *my_platform_get_barectf_ctx(
35 struct my_platform_ctx *platform_ctx);
41 #endif /* _MY_PLATFORM_H */
57 /* Platform context */
58 struct my_platform_ctx {
59 struct barectf_my_stream_ctx ctx;
63 /* Clock source platform function */
64 static uint64_t my_clock_get_value(void * const data)
68 clock_gettime(CLOCK_REALTIME, &ts);
69 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
72 static void write_packet(const struct my_platform_ctx * const platform_ctx)
74 /* Append current packet to data stream file */
75 const size_t nmemb = fwrite(barectf_packet_buf_addr(&platform_ctx->ctx),
76 barectf_packet_buf_size(&platform_ctx->ctx),
82 /* Full back end check platform function */
83 static int is_backend_full(void * const data)
88 /* Packet opening platform function */
89 static void open_packet(void * const data)
91 struct my_platform_ctx * const platform_ctx = data;
93 barectf_my_stream_open_packet(&platform_ctx->ctx);
96 /* Packet closing platform function */
97 static void close_packet(void * const data)
99 struct my_platform_ctx * const platform_ctx = data;
101 /* Close packet now */
102 barectf_my_stream_close_packet(&platform_ctx->ctx);
104 /* Write packet to file */
105 write_packet(platform_ctx);
108 /* Platform initialization function */
109 struct my_platform_ctx *my_platform_init(const unsigned int buf_size,
110 const char * const data_stream_file_path)
112 char stream_path[256];
114 struct my_platform_ctx *platform_ctx;
115 struct barectf_platform_callbacks cbs;
117 /* Set platform callback functions */
118 cbs.my_clock_clock_get_value = my_clock_get_value;
119 cbs.is_backend_full = is_backend_full;
120 cbs.open_packet = open_packet;
121 cbs.close_packet = close_packet;
123 /* Allocate platform context (which contains a barectf context) */
124 platform_ctx = malloc(sizeof(*platform_ctx));
130 /* Allocate packet buffer */
131 buf = malloc(buf_size);
137 /* Open data stream file */
138 platform_ctx->fh = fopen(data_stream_file_path, "wb");
140 if (!platform_ctx->fh) {
144 /* Initialize barectf context */
145 barectf_init(&platform_ctx->ctx, buf, buf_size, cbs, platform_ctx);
147 /* Open the first packet */
148 open_packet(platform_ctx);
157 /* Return platform context to user */
161 /* Platform finalization function */
162 void my_platform_fini(struct my_platform_ctx * const platform_ctx)
164 /* Close current packet if needed */
165 if (barectf_packet_is_open(&platform_ctx->ctx) &&
166 !barectf_packet_is_empty(&platform_ctx->ctx)) {
167 close_packet(platform_ctx);
170 /* Close data stream file */
171 fclose(platform_ctx->fh);
173 /* Deallocate packet buffer */
174 free(barectf_packet_buf(&platform_ctx->ctx));
176 /* Deallocate platform context */
180 /* barectf context pointer access function */
181 struct barectf_my_stream_ctx *my_platform_get_barectf_ctx(
182 struct my_platform_ctx * const platform_ctx)
184 return &platform_ctx->ctx;
190 In this example, you can find all the required components of a barectf
193 xref:api.adoc#cbs[Platform callback functions]::
194 xref:api.adoc#cb-clk-src[Clock source]:::
198 static uint64_t my_clock_get_value(void * const data)
202 clock_gettime(CLOCK_REALTIME, &ts);
203 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
207 xref:api.adoc#cb-open[Packet opening]:::
211 static void open_packet(void * const data)
213 struct my_platform_ctx * const platform_ctx = data;
215 barectf_my_stream_open_packet(&platform_ctx->ctx);
219 xref:api.adoc#cb-close[Packet closing]:::
223 static void close_packet(void * const data)
225 struct my_platform_ctx * const platform_ctx = data;
227 barectf_my_stream_close_packet(&platform_ctx->ctx);
228 write_packet(platform_ctx);
232 xref:api.adoc#cb-is-back-end-full[Is the back end full?]:::
236 static int is_backend_full(void * const data)
242 This one always returns 0 as we assume that we can always append a
243 packet to the data stream file.
245 Platform initialization function::
249 struct my_platform_ctx *my_platform_init(const unsigned int buf_size,
250 const char * const data_stream_file_path)
256 Platform finalization function::
260 void my_platform_fini(struct my_platform_ctx * const platform_ctx)
262 if (barectf_packet_is_open(&platform_ctx->ctx) &&
263 !barectf_packet_is_empty(&platform_ctx->ctx)) {
264 close_packet(platform_ctx);
271 barectf context pointer access function::
275 struct barectf_my_stream_ctx *my_platform_get_barectf_ctx(
276 struct my_platform_ctx * const platform_ctx)
278 return &platform_ctx->ctx;