barectf-platform-linux-fs.c: write_packet(): use `const size_t`
[barectf.git] / platforms / linux-fs / barectf-platform-linux-fs.c
1 /*
2 * Copyright (c) 2015 EfficiOS Inc. and Linux Foundation
3 * Copyright (c) 2015-2020 Philippe Proulx <pproulx@efficios.com>
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdint.h>
29 #include <assert.h>
30 #include <time.h>
31
32 #include "barectf-platform-linux-fs.h"
33 #include "barectf.h"
34
35 #ifdef __cplusplus
36 # define _FROM_VOID_PTR(_type, _value) static_cast<_type *>(_value)
37 #else
38 # define _FROM_VOID_PTR(_type, _value) ((_type *) (_value))
39 #endif
40
41 struct barectf_platform_linux_fs_ctx {
42 struct barectf_default_ctx ctx;
43 FILE *fh;
44 int simulate_full_backend;
45 unsigned int full_backend_rand_lt;
46 unsigned int full_backend_rand_max;
47 };
48
49 static uint64_t get_clock(void * const data)
50 {
51 struct timespec ts;
52
53 clock_gettime(CLOCK_REALTIME, &ts);
54 return ts.tv_sec * 1000000000ULL + ts.tv_nsec;
55 }
56
57 static void write_packet(const struct barectf_platform_linux_fs_ctx * const platform_ctx)
58 {
59 const size_t nmemb = fwrite(barectf_packet_buf(&platform_ctx->ctx),
60 barectf_packet_buf_size(&platform_ctx->ctx), 1, platform_ctx->fh);
61
62 assert(nmemb == 1);
63 }
64
65 static int is_backend_full(void * const data)
66 {
67 int is_backend_full = 0;
68 const struct barectf_platform_linux_fs_ctx * const platform_ctx =
69 _FROM_VOID_PTR(const struct barectf_platform_linux_fs_ctx, data);
70
71 if (platform_ctx->simulate_full_backend) {
72 if (rand() % platform_ctx->full_backend_rand_max <
73 platform_ctx->full_backend_rand_lt) {
74 is_backend_full = 1;
75 goto end;
76 }
77 }
78
79 end:
80 return is_backend_full;
81 }
82
83 static void open_packet(void * const data)
84 {
85 struct barectf_platform_linux_fs_ctx * const platform_ctx =
86 _FROM_VOID_PTR(struct barectf_platform_linux_fs_ctx, data);
87
88 barectf_default_open_packet(&platform_ctx->ctx);
89 }
90
91 static void close_packet(void * const data)
92 {
93 struct barectf_platform_linux_fs_ctx * const platform_ctx =
94 _FROM_VOID_PTR(struct barectf_platform_linux_fs_ctx, data);
95
96 /* Close packet now */
97 barectf_default_close_packet(&platform_ctx->ctx);
98
99 /* Write packet to file */
100 write_packet(platform_ctx);
101 }
102
103 struct barectf_platform_linux_fs_ctx *barectf_platform_linux_fs_init(
104 const unsigned int buf_size, const char * const trace_dir,
105 const int simulate_full_backend,
106 const unsigned int full_backend_rand_lt,
107 const unsigned int full_backend_rand_max)
108 {
109 char stream_path[256];
110 uint8_t *buf = NULL;
111 struct barectf_platform_linux_fs_ctx *platform_ctx;
112 struct barectf_platform_callbacks cbs;
113
114 cbs.default_clock_get_value = get_clock;
115 cbs.is_backend_full = is_backend_full;
116 cbs.open_packet = open_packet;
117 cbs.close_packet = close_packet;
118 platform_ctx = _FROM_VOID_PTR(struct barectf_platform_linux_fs_ctx,
119 malloc(sizeof(*platform_ctx)));
120
121 if (!platform_ctx) {
122 goto error;
123 }
124
125 buf = _FROM_VOID_PTR(uint8_t, malloc(buf_size));
126
127 if (!buf) {
128 goto error;
129 }
130
131 sprintf(stream_path, "%s/stream", trace_dir);
132 platform_ctx->fh = fopen(stream_path, "wb");
133
134 if (!platform_ctx->fh) {
135 goto error;
136 }
137
138 platform_ctx->simulate_full_backend = simulate_full_backend;
139 platform_ctx->full_backend_rand_lt = full_backend_rand_lt;
140 platform_ctx->full_backend_rand_max = full_backend_rand_max;
141 barectf_init(&platform_ctx->ctx, buf, buf_size, cbs, platform_ctx);
142 open_packet(platform_ctx);
143 goto end;
144
145 error:
146 free(platform_ctx);
147 free(buf);
148
149 end:
150 return platform_ctx;
151 }
152
153 void barectf_platform_linux_fs_fini(struct barectf_platform_linux_fs_ctx * const platform_ctx)
154 {
155 if (barectf_packet_is_open(&platform_ctx->ctx) &&
156 !barectf_packet_is_empty(&platform_ctx->ctx)) {
157 close_packet(platform_ctx);
158 }
159
160 fclose(platform_ctx->fh);
161 free(barectf_packet_buf(&platform_ctx->ctx));
162 free(platform_ctx);
163 }
164
165 struct barectf_default_ctx *barectf_platform_linux_fs_get_barectf_ctx(
166 struct barectf_platform_linux_fs_ctx * const platform_ctx)
167 {
168 return &platform_ctx->ctx;
169 }
This page took 0.039924 seconds and 4 git commands to generate.