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