cleanup: Remove redefinition of CHAR_BIT
[lttng-ust.git] / liblttng-ust / lttng-ring-buffer-metadata-client.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * LTTng lib ring buffer client template.
7 */
8
9 #include <limits.h>
10 #include <stddef.h>
11 #include <stdint.h>
12
13 #include <ust-events-internal.h>
14 #include "ust-bitfield.h"
15 #include "ust-compat.h"
16 #include "lttng-tracer.h"
17 #include "../libringbuffer/frontend_types.h"
18
19 struct metadata_packet_header {
20 uint32_t magic; /* 0x75D11D57 */
21 uint8_t uuid[LTTNG_UST_UUID_LEN]; /* Unique Universal Identifier */
22 uint32_t checksum; /* 0 if unused */
23 uint32_t content_size; /* in bits */
24 uint32_t packet_size; /* in bits */
25 uint8_t compression_scheme; /* 0 if unused */
26 uint8_t encryption_scheme; /* 0 if unused */
27 uint8_t checksum_scheme; /* 0 if unused */
28 uint8_t major; /* CTF spec major version number */
29 uint8_t minor; /* CTF spec minor version number */
30 uint8_t header_end[0];
31 };
32
33 struct metadata_record_header {
34 uint8_t header_end[0]; /* End of header */
35 };
36
37 static const struct lttng_ust_lib_ring_buffer_config client_config;
38
39 static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan)
40 {
41 return 0;
42 }
43
44 static inline
45 size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
46 struct lttng_ust_lib_ring_buffer_channel *chan,
47 size_t offset,
48 size_t *pre_header_padding,
49 struct lttng_ust_lib_ring_buffer_ctx *ctx,
50 void *client_ctx)
51 {
52 return 0;
53 }
54
55 #include "../libringbuffer/api.h"
56 #include "lttng-rb-clients.h"
57
58 static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan)
59 {
60 return 0;
61 }
62
63 static
64 size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config,
65 struct lttng_ust_lib_ring_buffer_channel *chan,
66 size_t offset,
67 size_t *pre_header_padding,
68 struct lttng_ust_lib_ring_buffer_ctx *ctx,
69 void *client_ctx)
70 {
71 return 0;
72 }
73
74 /**
75 * client_packet_header_size - called on buffer-switch to a new sub-buffer
76 *
77 * Return header size without padding after the structure. Don't use packed
78 * structure because gcc generates inefficient code on some architectures
79 * (powerpc, mips..)
80 */
81 static size_t client_packet_header_size(void)
82 {
83 return offsetof(struct metadata_packet_header, header_end);
84 }
85
86 static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
87 unsigned int subbuf_idx,
88 struct lttng_ust_shm_handle *handle)
89 {
90 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan);
91 struct metadata_packet_header *header =
92 (struct metadata_packet_header *)
93 lib_ring_buffer_offset_address(&buf->backend,
94 subbuf_idx * chan->backend.subbuf_size,
95 handle);
96 struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan);
97
98 assert(header);
99 if (!header)
100 return;
101 header->magic = TSDL_MAGIC_NUMBER;
102 memcpy(header->uuid, lttng_chan->priv->uuid, sizeof(lttng_chan->priv->uuid));
103 header->checksum = 0; /* 0 if unused */
104 header->content_size = 0xFFFFFFFF; /* in bits, for debugging */
105 header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */
106 header->compression_scheme = 0; /* 0 if unused */
107 header->encryption_scheme = 0; /* 0 if unused */
108 header->checksum_scheme = 0; /* 0 if unused */
109 header->major = CTF_SPEC_MAJOR;
110 header->minor = CTF_SPEC_MINOR;
111 }
112
113 /*
114 * offset is assumed to never be 0 here : never deliver a completely empty
115 * subbuffer. data_size is between 1 and subbuf_size.
116 */
117 static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc,
118 unsigned int subbuf_idx, unsigned long data_size,
119 struct lttng_ust_shm_handle *handle)
120 {
121 struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan);
122 struct metadata_packet_header *header =
123 (struct metadata_packet_header *)
124 lib_ring_buffer_offset_address(&buf->backend,
125 subbuf_idx * chan->backend.subbuf_size,
126 handle);
127 unsigned long records_lost = 0;
128
129 assert(header);
130 if (!header)
131 return;
132 header->content_size = data_size * CHAR_BIT; /* in bits */
133 header->packet_size = LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */
134 /*
135 * We do not care about the records lost count, because the metadata
136 * channel waits and retry.
137 */
138 (void) lib_ring_buffer_get_records_lost_full(&client_config, buf);
139 records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf);
140 records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf);
141 WARN_ON_ONCE(records_lost != 0);
142 }
143
144 static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv,
145 int cpu, const char *name,
146 struct lttng_ust_shm_handle *handle)
147 {
148 return 0;
149 }
150
151 static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf,
152 void *priv, int cpu,
153 struct lttng_ust_shm_handle *handle)
154 {
155 }
156
157 static const
158 struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = {
159 .parent = {
160 .ring_buffer_clock_read = client_ring_buffer_clock_read,
161 .record_header_size = client_record_header_size,
162 .subbuffer_header_size = client_packet_header_size,
163 .buffer_begin = client_buffer_begin,
164 .buffer_end = client_buffer_end,
165 .buffer_create = client_buffer_create,
166 .buffer_finalize = client_buffer_finalize,
167 },
168 };
169
170 static const struct lttng_ust_lib_ring_buffer_config client_config = {
171 .cb.ring_buffer_clock_read = client_ring_buffer_clock_read,
172 .cb.record_header_size = client_record_header_size,
173 .cb.subbuffer_header_size = client_packet_header_size,
174 .cb.buffer_begin = client_buffer_begin,
175 .cb.buffer_end = client_buffer_end,
176 .cb.buffer_create = client_buffer_create,
177 .cb.buffer_finalize = client_buffer_finalize,
178
179 .tsc_bits = 0,
180 .alloc = RING_BUFFER_ALLOC_GLOBAL,
181 .sync = RING_BUFFER_SYNC_GLOBAL,
182 .mode = RING_BUFFER_MODE_TEMPLATE,
183 .backend = RING_BUFFER_PAGE,
184 .output = RING_BUFFER_MMAP,
185 .oops = RING_BUFFER_OOPS_CONSISTENCY,
186 .ipi = RING_BUFFER_NO_IPI_BARRIER,
187 .wakeup = RING_BUFFER_WAKEUP_BY_WRITER,
188 .client_type = LTTNG_CLIENT_TYPE,
189
190 .cb_ptr = &client_cb.parent,
191 };
192
193 static
194 struct lttng_ust_channel_buffer *_channel_create(const char *name,
195 void *buf_addr,
196 size_t subbuf_size, size_t num_subbuf,
197 unsigned int switch_timer_interval,
198 unsigned int read_timer_interval,
199 unsigned char *uuid,
200 uint32_t chan_id,
201 const int *stream_fds, int nr_stream_fds,
202 int64_t blocking_timeout)
203 {
204 struct lttng_ust_abi_channel_config chan_priv_init;
205 struct lttng_ust_shm_handle *handle;
206 struct lttng_ust_channel_buffer *lttng_chan_buf;
207
208 lttng_chan_buf = lttng_ust_alloc_channel_buffer();
209 if (!lttng_chan_buf)
210 return NULL;
211 memcpy(lttng_chan_buf->priv->uuid, uuid, LTTNG_UST_UUID_LEN);
212 lttng_chan_buf->priv->id = chan_id;
213
214 memset(&chan_priv_init, 0, sizeof(chan_priv_init));
215 memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN);
216 chan_priv_init.id = chan_id;
217
218 handle = channel_create(&client_config, name,
219 __alignof__(struct lttng_ust_channel_buffer),
220 sizeof(struct lttng_ust_channel_buffer),
221 &chan_priv_init,
222 lttng_chan_buf, buf_addr, subbuf_size, num_subbuf,
223 switch_timer_interval, read_timer_interval,
224 stream_fds, nr_stream_fds, blocking_timeout);
225 if (!handle)
226 goto error;
227 lttng_chan_buf->priv->rb_chan = shmp(handle, handle->chan);
228 return lttng_chan_buf;
229
230 error:
231 lttng_ust_free_channel_common(lttng_chan_buf->parent);
232 return NULL;
233 }
234
235 static
236 void lttng_channel_destroy(struct lttng_ust_channel_buffer *lttng_chan_buf)
237 {
238 channel_destroy(lttng_chan_buf->priv->rb_chan, lttng_chan_buf->priv->rb_chan->handle, 1);
239 lttng_ust_free_channel_common(lttng_chan_buf->parent);
240 }
241
242 static
243 int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, uint32_t event_id)
244 {
245 int ret;
246
247 ret = lib_ring_buffer_reserve(&client_config, ctx, NULL);
248 if (ret)
249 return ret;
250 if (lib_ring_buffer_backend_get_pages(&client_config, ctx,
251 &ctx->backend_pages))
252 return -EPERM;
253 return 0;
254 }
255
256 static
257 void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx)
258 {
259 lib_ring_buffer_commit(&client_config, ctx);
260 }
261
262 static
263 void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src,
264 size_t len)
265 {
266 lib_ring_buffer_write(&client_config, ctx, src, len);
267 }
268
269 static
270 size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer *chan)
271 {
272 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
273 unsigned long o_begin;
274 struct lttng_ust_lib_ring_buffer *buf;
275
276 buf = shmp(rb_chan->handle, rb_chan->backend.buf[0].shmp); /* Only for global buffer ! */
277 o_begin = v_read(&client_config, &buf->offset);
278 if (subbuf_offset(o_begin, rb_chan) != 0) {
279 return rb_chan->backend.subbuf_size - subbuf_offset(o_begin, rb_chan);
280 } else {
281 return rb_chan->backend.subbuf_size - subbuf_offset(o_begin, rb_chan)
282 - sizeof(struct metadata_packet_header);
283 }
284 }
285
286 static
287 int lttng_is_finalized(struct lttng_ust_channel_buffer *chan)
288 {
289 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
290
291 return lib_ring_buffer_channel_is_finalized(rb_chan);
292 }
293
294 static
295 int lttng_is_disabled(struct lttng_ust_channel_buffer *chan)
296 {
297 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
298
299 return lib_ring_buffer_channel_is_disabled(rb_chan);
300 }
301
302 static
303 int lttng_flush_buffer(struct lttng_ust_channel_buffer *chan)
304 {
305 struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan;
306 struct lttng_ust_lib_ring_buffer *buf;
307 int shm_fd, wait_fd, wakeup_fd;
308 uint64_t memory_map_size;
309
310 buf = channel_get_ring_buffer(&client_config, rb_chan,
311 0, rb_chan->handle, &shm_fd, &wait_fd, &wakeup_fd,
312 &memory_map_size);
313 lib_ring_buffer_switch(&client_config, buf,
314 SWITCH_ACTIVE, rb_chan->handle);
315 return 0;
316 }
317
318 static struct lttng_transport lttng_relay_transport = {
319 .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap",
320 .ops = {
321 .struct_size = sizeof(struct lttng_ust_channel_buffer_ops),
322
323 .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private, {
324 .pub = &lttng_relay_transport.ops,
325 .channel_create = _channel_create,
326 .channel_destroy = lttng_channel_destroy,
327 .packet_avail_size = lttng_packet_avail_size,
328 .is_finalized = lttng_is_finalized,
329 .is_disabled = lttng_is_disabled,
330 .flush_buffer = lttng_flush_buffer,
331 }),
332 .event_reserve = lttng_event_reserve,
333 .event_commit = lttng_event_commit,
334 .event_write = lttng_event_write,
335 },
336 .client_config = &client_config,
337 };
338
339 void RING_BUFFER_MODE_TEMPLATE_INIT(void)
340 {
341 DBG("LTT : ltt ring buffer client \"%s\" init\n",
342 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
343 lttng_transport_register(&lttng_relay_transport);
344 }
345
346 void RING_BUFFER_MODE_TEMPLATE_EXIT(void)
347 {
348 DBG("LTT : ltt ring buffer client \"%s\" exit\n",
349 "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap");
350 lttng_transport_unregister(&lttng_relay_transport);
351 }
This page took 0.04681 seconds and 5 git commands to generate.