Commit | Line | Data |
---|---|---|
3d1fc7fd | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
3d1fc7fd | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * LTTng lib ring buffer client template. |
3d1fc7fd MD |
7 | */ |
8 | ||
9af5d97a | 9 | #include <limits.h> |
b4051ad8 | 10 | #include <stddef.h> |
9f3fdbc6 | 11 | #include <stdint.h> |
7753d283 | 12 | |
0950190a | 13 | #include <lttng/urcu/pointer.h> |
8cd08025 MJ |
14 | #include <urcu/tls-compat.h> |
15 | ||
16 | #include "common/events.h" | |
9d315d6d MJ |
17 | #include "common/bitfield.h" |
18 | #include "common/align.h" | |
f41a6b5f | 19 | #include "common/clock.h" |
e4db8f98 | 20 | #include "common/ringbuffer/frontend_types.h" |
3d1fc7fd | 21 | |
373ea80a MD |
22 | #define LTTNG_COMPACT_EVENT_BITS 5 |
23 | #define LTTNG_COMPACT_TIMESTAMP_BITS 27 | |
79dfbf42 | 24 | |
3d1fc7fd MD |
25 | /* |
26 | * Keep the natural field alignment for _each field_ within this structure if | |
27 | * you ever add/remove a field from this header. Packed attribute is not used | |
28 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
29 | * let gcc add padding between the structure elements. | |
30 | */ | |
31 | ||
32 | struct packet_header { | |
33 | /* Trace packet header */ | |
34 | uint32_t magic; /* | |
35 | * Trace magic number. | |
36 | * contains endianness information. | |
37 | */ | |
19d8b1b3 | 38 | uint8_t uuid[LTTNG_UST_UUID_LEN]; |
3d1fc7fd | 39 | uint32_t stream_id; |
2d94adc5 | 40 | uint64_t stream_instance_id; |
3d1fc7fd MD |
41 | |
42 | struct { | |
43 | /* Stream packet context */ | |
44 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ | |
45 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ | |
0c00a107 MD |
46 | uint64_t content_size; /* Size of data in subbuffer */ |
47 | uint64_t packet_size; /* Subbuffer size (include padding) */ | |
1ff31389 | 48 | uint64_t packet_seq_num; /* Packet sequence number */ |
18fc8d71 | 49 | unsigned long events_discarded; /* |
3d1fc7fd MD |
50 | * Events lost in this subbuffer since |
51 | * the beginning of the trace. | |
52 | * (may overflow) | |
53 | */ | |
3d1fc7fd MD |
54 | uint32_t cpu_id; /* CPU id associated with stream */ |
55 | uint8_t header_end; /* End of header */ | |
56 | } ctx; | |
57 | }; | |
58 | ||
e56bb47c MD |
59 | struct lttng_client_ctx { |
60 | size_t packet_context_len; | |
61 | size_t event_context_len; | |
0950190a | 62 | struct lttng_ust_ctx *chan_ctx; |
e56bb47c | 63 | }; |
3d1fc7fd | 64 | |
8936b6c0 MD |
65 | /* |
66 | * Indexed by lib_ring_buffer_nesting_count(). | |
67 | */ | |
b5457df5 | 68 | typedef struct lttng_ust_ring_buffer_ctx_private private_ctx_stack_t[LIB_RING_BUFFER_MAX_NESTING]; |
8936b6c0 MD |
69 | static DEFINE_URCU_TLS(private_ctx_stack_t, private_ctx_stack); |
70 | ||
71 | /* | |
a9fd951a | 72 | * Force a read (imply TLS allocation for dlopen) of TLS variables. |
8936b6c0 | 73 | */ |
a9fd951a | 74 | void RING_BUFFER_MODE_TEMPLATE_ALLOC_TLS(void) |
8936b6c0 | 75 | { |
742ac92a | 76 | __asm__ __volatile__ ("" : : "m" (URCU_TLS(private_ctx_stack))); |
8936b6c0 MD |
77 | } |
78 | ||
2208d8b5 | 79 | static inline uint64_t lib_ring_buffer_clock_read( |
b5457df5 | 80 | struct lttng_ust_ring_buffer_channel *chan __attribute__((unused))) |
3d1fc7fd MD |
81 | { |
82 | return trace_clock_read64(); | |
83 | } | |
84 | ||
85 | static inline | |
daacdbfc | 86 | size_t ctx_get_aligned_size(size_t offset, struct lttng_ust_ctx *ctx, |
e56bb47c | 87 | size_t ctx_len) |
3d1fc7fd | 88 | { |
3d1fc7fd MD |
89 | size_t orig_offset = offset; |
90 | ||
b5a3dfa5 | 91 | if (caa_likely(!ctx)) |
3d1fc7fd | 92 | return 0; |
b5457df5 | 93 | offset += lttng_ust_ring_buffer_align(offset, ctx->largest_align); |
e56bb47c MD |
94 | offset += ctx_len; |
95 | return offset - orig_offset; | |
96 | } | |
97 | ||
98 | static inline | |
b2e37d27 MD |
99 | void ctx_get_struct_size(struct lttng_ust_ring_buffer_ctx *bufctx, |
100 | struct lttng_ust_ctx *ctx, size_t *ctx_len) | |
e56bb47c MD |
101 | { |
102 | int i; | |
103 | size_t offset = 0; | |
104 | ||
105 | if (caa_likely(!ctx)) { | |
106 | *ctx_len = 0; | |
107 | return; | |
108 | } | |
69e21fdf | 109 | for (i = 0; i < ctx->nr_fields; i++) |
b2e37d27 | 110 | offset += ctx->fields[i].get_size(ctx->fields[i].priv, bufctx->probe_ctx, offset); |
e56bb47c | 111 | *ctx_len = offset; |
3d1fc7fd MD |
112 | } |
113 | ||
114 | static inline | |
b5457df5 | 115 | void ctx_record(struct lttng_ust_ring_buffer_ctx *bufctx, |
e7bc0ef6 | 116 | struct lttng_ust_channel_buffer *chan, |
69e21fdf | 117 | struct lttng_ust_ctx *ctx) |
3d1fc7fd MD |
118 | { |
119 | int i; | |
120 | ||
b5a3dfa5 | 121 | if (caa_likely(!ctx)) |
3d1fc7fd | 122 | return; |
b5457df5 | 123 | lttng_ust_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
69e21fdf | 124 | for (i = 0; i < ctx->nr_fields; i++) |
b2e37d27 | 125 | ctx->fields[i].record(ctx->fields[i].priv, bufctx->probe_ctx, bufctx, chan); |
3d1fc7fd MD |
126 | } |
127 | ||
128 | /* | |
129 | * record_header_size - Calculate the header size and padding necessary. | |
130 | * @config: ring buffer instance configuration | |
131 | * @chan: channel | |
132 | * @offset: offset in the write buffer | |
133 | * @pre_header_padding: padding to add before the header (output) | |
134 | * @ctx: reservation context | |
135 | * | |
136 | * Returns the event header size (including padding). | |
137 | * | |
138 | * The payload must itself determine its own alignment from the biggest type it | |
139 | * contains. | |
140 | */ | |
141 | static __inline__ | |
2208d8b5 | 142 | size_t record_header_size( |
b5457df5 MD |
143 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
144 | struct lttng_ust_ring_buffer_channel *chan, | |
2208d8b5 MJ |
145 | size_t offset, |
146 | size_t *pre_header_padding, | |
b5457df5 | 147 | struct lttng_ust_ring_buffer_ctx *ctx, |
2208d8b5 | 148 | struct lttng_client_ctx *client_ctx) |
3d1fc7fd | 149 | { |
e7bc0ef6 | 150 | struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan); |
3d1fc7fd MD |
151 | size_t orig_offset = offset; |
152 | size_t padding; | |
153 | ||
e7bc0ef6 | 154 | switch (lttng_chan->priv->header_type) { |
3d1fc7fd | 155 | case 1: /* compact */ |
b5457df5 | 156 | padding = lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint32_t)); |
3d1fc7fd | 157 | offset += padding; |
373ea80a | 158 | if (!(ctx->priv->rflags & (RING_BUFFER_RFLAG_FULL_TIMESTAMP | LTTNG_RFLAG_EXTENDED))) { |
3d1fc7fd MD |
159 | offset += sizeof(uint32_t); /* id and timestamp */ |
160 | } else { | |
79dfbf42 MD |
161 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
162 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; | |
3d1fc7fd | 163 | /* Align extended struct on largest member */ |
b5457df5 | 164 | offset += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd | 165 | offset += sizeof(uint32_t); /* id */ |
b5457df5 | 166 | offset += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd MD |
167 | offset += sizeof(uint64_t); /* timestamp */ |
168 | } | |
169 | break; | |
170 | case 2: /* large */ | |
b5457df5 | 171 | padding = lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint16_t)); |
3d1fc7fd MD |
172 | offset += padding; |
173 | offset += sizeof(uint16_t); | |
373ea80a | 174 | if (!(ctx->priv->rflags & (RING_BUFFER_RFLAG_FULL_TIMESTAMP | LTTNG_RFLAG_EXTENDED))) { |
b5457df5 | 175 | offset += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint32_t)); |
3d1fc7fd MD |
176 | offset += sizeof(uint32_t); /* timestamp */ |
177 | } else { | |
178 | /* Align extended struct on largest member */ | |
b5457df5 | 179 | offset += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd | 180 | offset += sizeof(uint32_t); /* id */ |
b5457df5 | 181 | offset += lttng_ust_ring_buffer_align(offset, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd MD |
182 | offset += sizeof(uint64_t); /* timestamp */ |
183 | } | |
184 | break; | |
185 | default: | |
9f3fdbc6 | 186 | padding = 0; |
3d1fc7fd MD |
187 | WARN_ON_ONCE(1); |
188 | } | |
0950190a | 189 | offset += ctx_get_aligned_size(offset, client_ctx->chan_ctx, |
2b7080aa | 190 | client_ctx->packet_context_len); |
3d1fc7fd MD |
191 | *pre_header_padding = padding; |
192 | return offset - orig_offset; | |
193 | } | |
194 | ||
e4db8f98 | 195 | #include "common/ringbuffer/api.h" |
8cd08025 | 196 | #include "common/ringbuffer-clients/clients.h" |
3d1fc7fd | 197 | |
9f3fdbc6 | 198 | static |
b5457df5 MD |
199 | void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config *config, |
200 | struct lttng_ust_ring_buffer_ctx *ctx, | |
0950190a | 201 | struct lttng_client_ctx *client_ctx, |
3d1fc7fd MD |
202 | uint32_t event_id); |
203 | ||
204 | /* | |
7dd08bec | 205 | * lttng_write_event_header |
3d1fc7fd MD |
206 | * |
207 | * Writes the event header to the offset (already aligned on 32-bits). | |
208 | * | |
209 | * @config: ring buffer instance configuration | |
210 | * @ctx: reservation context | |
211 | * @event_id: event ID | |
212 | */ | |
213 | static __inline__ | |
b5457df5 MD |
214 | void lttng_write_event_header(const struct lttng_ust_ring_buffer_config *config, |
215 | struct lttng_ust_ring_buffer_ctx *ctx, | |
0950190a | 216 | struct lttng_client_ctx *client_ctx, |
3d1fc7fd MD |
217 | uint32_t event_id) |
218 | { | |
8936b6c0 | 219 | struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(ctx->priv->chan); |
3d1fc7fd | 220 | |
8936b6c0 | 221 | if (caa_unlikely(ctx->priv->rflags)) |
3d1fc7fd MD |
222 | goto slow_path; |
223 | ||
e7bc0ef6 | 224 | switch (lttng_chan->priv->header_type) { |
3d1fc7fd MD |
225 | case 1: /* compact */ |
226 | { | |
227 | uint32_t id_time = 0; | |
228 | ||
79dfbf42 MD |
229 | bt_bitfield_write(&id_time, uint32_t, |
230 | 0, | |
231 | LTTNG_COMPACT_EVENT_BITS, | |
232 | event_id); | |
233 | bt_bitfield_write(&id_time, uint32_t, | |
234 | LTTNG_COMPACT_EVENT_BITS, | |
373ea80a MD |
235 | LTTNG_COMPACT_TIMESTAMP_BITS, |
236 | ctx->priv->timestamp); | |
3d1fc7fd MD |
237 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
238 | break; | |
239 | } | |
240 | case 2: /* large */ | |
241 | { | |
373ea80a | 242 | uint32_t timestamp = (uint32_t) ctx->priv->timestamp; |
3d1fc7fd MD |
243 | uint16_t id = event_id; |
244 | ||
245 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
b5457df5 | 246 | lttng_ust_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(uint32_t)); |
3d1fc7fd MD |
247 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
248 | break; | |
249 | } | |
250 | default: | |
251 | WARN_ON_ONCE(1); | |
252 | } | |
253 | ||
69e21fdf | 254 | ctx_record(ctx, lttng_chan, client_ctx->chan_ctx); |
b5457df5 | 255 | lttng_ust_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
256 | |
257 | return; | |
258 | ||
259 | slow_path: | |
0950190a | 260 | lttng_write_event_header_slow(config, ctx, client_ctx, event_id); |
3d1fc7fd MD |
261 | } |
262 | ||
9f3fdbc6 | 263 | static |
b5457df5 MD |
264 | void lttng_write_event_header_slow(const struct lttng_ust_ring_buffer_config *config, |
265 | struct lttng_ust_ring_buffer_ctx *ctx, | |
0950190a | 266 | struct lttng_client_ctx *client_ctx, |
3d1fc7fd MD |
267 | uint32_t event_id) |
268 | { | |
b5457df5 | 269 | struct lttng_ust_ring_buffer_ctx_private *ctx_private = ctx->priv; |
8936b6c0 | 270 | struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(ctx->priv->chan); |
3d1fc7fd | 271 | |
e7bc0ef6 | 272 | switch (lttng_chan->priv->header_type) { |
3d1fc7fd | 273 | case 1: /* compact */ |
373ea80a | 274 | if (!(ctx_private->rflags & (RING_BUFFER_RFLAG_FULL_TIMESTAMP | LTTNG_RFLAG_EXTENDED))) { |
3d1fc7fd MD |
275 | uint32_t id_time = 0; |
276 | ||
79dfbf42 MD |
277 | bt_bitfield_write(&id_time, uint32_t, |
278 | 0, | |
279 | LTTNG_COMPACT_EVENT_BITS, | |
280 | event_id); | |
281 | bt_bitfield_write(&id_time, uint32_t, | |
282 | LTTNG_COMPACT_EVENT_BITS, | |
373ea80a MD |
283 | LTTNG_COMPACT_TIMESTAMP_BITS, |
284 | ctx_private->timestamp); | |
3d1fc7fd MD |
285 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
286 | } else { | |
287 | uint8_t id = 0; | |
373ea80a | 288 | uint64_t timestamp = ctx_private->timestamp; |
3d1fc7fd | 289 | |
79dfbf42 MD |
290 | bt_bitfield_write(&id, uint8_t, |
291 | 0, | |
292 | LTTNG_COMPACT_EVENT_BITS, | |
293 | 31); | |
3d1fc7fd MD |
294 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
295 | /* Align extended struct on largest member */ | |
b5457df5 | 296 | lttng_ust_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd | 297 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
b5457df5 | 298 | lttng_ust_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd MD |
299 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
300 | } | |
301 | break; | |
302 | case 2: /* large */ | |
303 | { | |
373ea80a MD |
304 | if (!(ctx_private->rflags & (RING_BUFFER_RFLAG_FULL_TIMESTAMP | LTTNG_RFLAG_EXTENDED))) { |
305 | uint32_t timestamp = (uint32_t) ctx_private->timestamp; | |
3d1fc7fd MD |
306 | uint16_t id = event_id; |
307 | ||
308 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
b5457df5 | 309 | lttng_ust_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(uint32_t)); |
3d1fc7fd MD |
310 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
311 | } else { | |
312 | uint16_t id = 65535; | |
373ea80a | 313 | uint64_t timestamp = ctx_private->timestamp; |
3d1fc7fd MD |
314 | |
315 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
316 | /* Align extended struct on largest member */ | |
b5457df5 | 317 | lttng_ust_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd | 318 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
b5457df5 | 319 | lttng_ust_ring_buffer_align_ctx(ctx, lttng_ust_rb_alignof(uint64_t)); |
3d1fc7fd MD |
320 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
321 | } | |
322 | break; | |
323 | } | |
324 | default: | |
325 | WARN_ON_ONCE(1); | |
326 | } | |
69e21fdf | 327 | ctx_record(ctx, lttng_chan, client_ctx->chan_ctx); |
b5457df5 | 328 | lttng_ust_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
329 | } |
330 | ||
b5457df5 | 331 | static const struct lttng_ust_ring_buffer_config client_config; |
3d1fc7fd | 332 | |
b5457df5 | 333 | static uint64_t client_ring_buffer_clock_read(struct lttng_ust_ring_buffer_channel *chan) |
3d1fc7fd MD |
334 | { |
335 | return lib_ring_buffer_clock_read(chan); | |
336 | } | |
337 | ||
338 | static | |
b5457df5 MD |
339 | size_t client_record_header_size(const struct lttng_ust_ring_buffer_config *config, |
340 | struct lttng_ust_ring_buffer_channel *chan, | |
5198080d | 341 | size_t offset, |
3d1fc7fd | 342 | size_t *pre_header_padding, |
b5457df5 | 343 | struct lttng_ust_ring_buffer_ctx *ctx, |
e56bb47c | 344 | void *client_ctx) |
3d1fc7fd MD |
345 | { |
346 | return record_header_size(config, chan, offset, | |
e56bb47c | 347 | pre_header_padding, ctx, client_ctx); |
3d1fc7fd MD |
348 | } |
349 | ||
350 | /** | |
351 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
352 | * | |
353 | * Return header size without padding after the structure. Don't use packed | |
354 | * structure because gcc generates inefficient code on some architectures | |
355 | * (powerpc, mips..) | |
356 | */ | |
357 | static size_t client_packet_header_size(void) | |
358 | { | |
359 | return offsetof(struct packet_header, ctx.header_end); | |
360 | } | |
361 | ||
373ea80a | 362 | static void client_buffer_begin(struct lttng_ust_ring_buffer *buf, uint64_t timestamp, |
1d498196 | 363 | unsigned int subbuf_idx, |
38fae1d3 | 364 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 365 | { |
b5457df5 | 366 | struct lttng_ust_ring_buffer_channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
367 | struct packet_header *header = |
368 | (struct packet_header *) | |
369 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
370 | subbuf_idx * chan->backend.subbuf_size, |
371 | handle); | |
e7bc0ef6 | 372 | struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan); |
1ff31389 | 373 | uint64_t cnt = shmp_index(handle, buf->backend.buf_cnt, subbuf_idx)->seq_cnt; |
3d1fc7fd | 374 | |
34daae3e MD |
375 | assert(header); |
376 | if (!header) | |
377 | return; | |
3d1fc7fd | 378 | header->magic = CTF_MAGIC_NUMBER; |
e7bc0ef6 MD |
379 | memcpy(header->uuid, lttng_chan->priv->uuid, sizeof(lttng_chan->priv->uuid)); |
380 | header->stream_id = lttng_chan->priv->id; | |
2d94adc5 | 381 | header->stream_instance_id = buf->backend.cpu; |
373ea80a | 382 | header->ctx.timestamp_begin = timestamp; |
3d1fc7fd | 383 | header->ctx.timestamp_end = 0; |
0c00a107 MD |
384 | header->ctx.content_size = ~0ULL; /* for debugging */ |
385 | header->ctx.packet_size = ~0ULL; | |
1ff31389 | 386 | header->ctx.packet_seq_num = chan->backend.num_subbuf * cnt + subbuf_idx; |
3d1fc7fd | 387 | header->ctx.events_discarded = 0; |
3d1fc7fd MD |
388 | header->ctx.cpu_id = buf->backend.cpu; |
389 | } | |
390 | ||
391 | /* | |
392 | * offset is assumed to never be 0 here : never deliver a completely empty | |
393 | * subbuffer. data_size is between 1 and subbuf_size. | |
394 | */ | |
373ea80a | 395 | static void client_buffer_end(struct lttng_ust_ring_buffer *buf, uint64_t timestamp, |
1d498196 | 396 | unsigned int subbuf_idx, unsigned long data_size, |
f27b0770 MD |
397 | struct lttng_ust_shm_handle *handle, |
398 | const struct lttng_ust_ring_buffer_ctx *ctx) | |
3d1fc7fd | 399 | { |
b5457df5 | 400 | struct lttng_ust_ring_buffer_channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
401 | struct packet_header *header = |
402 | (struct packet_header *) | |
403 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
404 | subbuf_idx * chan->backend.subbuf_size, |
405 | handle); | |
3d1fc7fd MD |
406 | unsigned long records_lost = 0; |
407 | ||
34daae3e MD |
408 | assert(header); |
409 | if (!header) | |
410 | return; | |
373ea80a | 411 | header->ctx.timestamp_end = timestamp; |
b1830883 MD |
412 | header->ctx.content_size = |
413 | (uint64_t) data_size * CHAR_BIT; /* in bits */ | |
414 | header->ctx.packet_size = | |
b72687b8 | 415 | (uint64_t) LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
04489ab4 | 416 | |
f27b0770 MD |
417 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, ctx); |
418 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, ctx); | |
419 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, ctx); | |
3d1fc7fd MD |
420 | header->ctx.events_discarded = records_lost; |
421 | } | |
422 | ||
2208d8b5 | 423 | static int client_buffer_create( |
b5457df5 | 424 | struct lttng_ust_ring_buffer *buf __attribute__((unused)), |
2208d8b5 MJ |
425 | void *priv __attribute__((unused)), |
426 | int cpu __attribute__((unused)), | |
427 | const char *name __attribute__((unused)), | |
428 | struct lttng_ust_shm_handle *handle __attribute__((unused))) | |
3d1fc7fd MD |
429 | { |
430 | return 0; | |
431 | } | |
432 | ||
2208d8b5 | 433 | static void client_buffer_finalize( |
b5457df5 | 434 | struct lttng_ust_ring_buffer *buf __attribute__((unused)), |
2208d8b5 MJ |
435 | void *priv __attribute__((unused)), |
436 | int cpu __attribute__((unused)), | |
437 | struct lttng_ust_shm_handle *handle __attribute__((unused))) | |
3d1fc7fd MD |
438 | { |
439 | } | |
440 | ||
2208d8b5 | 441 | static void client_content_size_field( |
b5457df5 | 442 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
2208d8b5 | 443 | size_t *offset, size_t *length) |
a9ff648c MD |
444 | { |
445 | *offset = offsetof(struct packet_header, ctx.content_size); | |
446 | *length = sizeof(((struct packet_header *) NULL)->ctx.content_size); | |
447 | } | |
448 | ||
2208d8b5 | 449 | static void client_packet_size_field( |
b5457df5 | 450 | const struct lttng_ust_ring_buffer_config *config __attribute__((unused)), |
2208d8b5 | 451 | size_t *offset, size_t *length) |
a9ff648c MD |
452 | { |
453 | *offset = offsetof(struct packet_header, ctx.packet_size); | |
454 | *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size); | |
455 | } | |
456 | ||
b5457df5 | 457 | static struct packet_header *client_packet_header(struct lttng_ust_ring_buffer *buf, |
b2f3252a JD |
458 | struct lttng_ust_shm_handle *handle) |
459 | { | |
c4d6bd10 | 460 | return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle); |
b2f3252a JD |
461 | } |
462 | ||
b5457df5 MD |
463 | static int client_timestamp_begin(struct lttng_ust_ring_buffer *buf, |
464 | struct lttng_ust_ring_buffer_channel *chan, | |
b2f3252a JD |
465 | uint64_t *timestamp_begin) |
466 | { | |
07539b34 | 467 | struct lttng_ust_shm_handle *handle = chan->handle; |
b2f3252a JD |
468 | struct packet_header *header; |
469 | ||
470 | header = client_packet_header(buf, handle); | |
34daae3e MD |
471 | if (!header) |
472 | return -1; | |
b2f3252a JD |
473 | *timestamp_begin = header->ctx.timestamp_begin; |
474 | return 0; | |
475 | } | |
476 | ||
b5457df5 MD |
477 | static int client_timestamp_end(struct lttng_ust_ring_buffer *buf, |
478 | struct lttng_ust_ring_buffer_channel *chan, | |
b2f3252a JD |
479 | uint64_t *timestamp_end) |
480 | { | |
07539b34 | 481 | struct lttng_ust_shm_handle *handle = chan->handle; |
b2f3252a JD |
482 | struct packet_header *header; |
483 | ||
484 | header = client_packet_header(buf, handle); | |
34daae3e MD |
485 | if (!header) |
486 | return -1; | |
b2f3252a JD |
487 | *timestamp_end = header->ctx.timestamp_end; |
488 | return 0; | |
489 | } | |
490 | ||
b5457df5 MD |
491 | static int client_events_discarded(struct lttng_ust_ring_buffer *buf, |
492 | struct lttng_ust_ring_buffer_channel *chan, | |
b2f3252a JD |
493 | uint64_t *events_discarded) |
494 | { | |
07539b34 | 495 | struct lttng_ust_shm_handle *handle = chan->handle; |
b2f3252a JD |
496 | struct packet_header *header; |
497 | ||
498 | header = client_packet_header(buf, handle); | |
34daae3e MD |
499 | if (!header) |
500 | return -1; | |
b2f3252a JD |
501 | *events_discarded = header->ctx.events_discarded; |
502 | return 0; | |
503 | } | |
504 | ||
b5457df5 MD |
505 | static int client_content_size(struct lttng_ust_ring_buffer *buf, |
506 | struct lttng_ust_ring_buffer_channel *chan, | |
b2f3252a JD |
507 | uint64_t *content_size) |
508 | { | |
07539b34 | 509 | struct lttng_ust_shm_handle *handle = chan->handle; |
b2f3252a JD |
510 | struct packet_header *header; |
511 | ||
512 | header = client_packet_header(buf, handle); | |
34daae3e MD |
513 | if (!header) |
514 | return -1; | |
b2f3252a JD |
515 | *content_size = header->ctx.content_size; |
516 | return 0; | |
517 | } | |
518 | ||
b5457df5 MD |
519 | static int client_packet_size(struct lttng_ust_ring_buffer *buf, |
520 | struct lttng_ust_ring_buffer_channel *chan, | |
b2f3252a JD |
521 | uint64_t *packet_size) |
522 | { | |
07539b34 | 523 | struct lttng_ust_shm_handle *handle = chan->handle; |
b2f3252a JD |
524 | struct packet_header *header; |
525 | ||
526 | header = client_packet_header(buf, handle); | |
34daae3e MD |
527 | if (!header) |
528 | return -1; | |
b2f3252a JD |
529 | *packet_size = header->ctx.packet_size; |
530 | return 0; | |
531 | } | |
532 | ||
b5457df5 MD |
533 | static int client_stream_id(struct lttng_ust_ring_buffer *buf __attribute__((unused)), |
534 | struct lttng_ust_ring_buffer_channel *chan, | |
b2f3252a JD |
535 | uint64_t *stream_id) |
536 | { | |
e7bc0ef6 | 537 | struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan); |
db95cf8b | 538 | |
e7bc0ef6 | 539 | *stream_id = lttng_chan->priv->id; |
b2f3252a | 540 | |
b2f3252a JD |
541 | return 0; |
542 | } | |
543 | ||
2208d8b5 | 544 | static int client_current_timestamp( |
b5457df5 MD |
545 | struct lttng_ust_ring_buffer *buf __attribute__((unused)), |
546 | struct lttng_ust_ring_buffer_channel *chan, | |
fca361e8 JD |
547 | uint64_t *ts) |
548 | { | |
fca361e8 JD |
549 | *ts = client_ring_buffer_clock_read(chan); |
550 | ||
551 | return 0; | |
552 | } | |
553 | ||
b5457df5 MD |
554 | static int client_sequence_number(struct lttng_ust_ring_buffer *buf, |
555 | struct lttng_ust_ring_buffer_channel *chan, | |
1ff31389 JD |
556 | uint64_t *seq) |
557 | { | |
07539b34 | 558 | struct lttng_ust_shm_handle *handle = chan->handle; |
1ff31389 JD |
559 | struct packet_header *header; |
560 | ||
561 | header = client_packet_header(buf, handle); | |
3e1db88d MD |
562 | if (!header) |
563 | return -1; | |
1ff31389 JD |
564 | *seq = header->ctx.packet_seq_num; |
565 | return 0; | |
566 | } | |
567 | ||
b5457df5 MD |
568 | static int client_instance_id(struct lttng_ust_ring_buffer *buf, |
569 | struct lttng_ust_ring_buffer_channel *chan __attribute__((unused)), | |
45a00b05 JD |
570 | uint64_t *id) |
571 | { | |
db95cf8b | 572 | *id = buf->backend.cpu; |
45a00b05 | 573 | |
45a00b05 JD |
574 | return 0; |
575 | } | |
576 | ||
82b9bde8 JD |
577 | static const |
578 | struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = { | |
579 | .parent = { | |
580 | .ring_buffer_clock_read = client_ring_buffer_clock_read, | |
581 | .record_header_size = client_record_header_size, | |
582 | .subbuffer_header_size = client_packet_header_size, | |
583 | .buffer_begin = client_buffer_begin, | |
584 | .buffer_end = client_buffer_end, | |
585 | .buffer_create = client_buffer_create, | |
586 | .buffer_finalize = client_buffer_finalize, | |
a9ff648c MD |
587 | .content_size_field = client_content_size_field, |
588 | .packet_size_field = client_packet_size_field, | |
82b9bde8 | 589 | }, |
b2f3252a JD |
590 | .timestamp_begin = client_timestamp_begin, |
591 | .timestamp_end = client_timestamp_end, | |
592 | .events_discarded = client_events_discarded, | |
593 | .content_size = client_content_size, | |
594 | .packet_size = client_packet_size, | |
595 | .stream_id = client_stream_id, | |
fca361e8 | 596 | .current_timestamp = client_current_timestamp, |
1ff31389 | 597 | .sequence_number = client_sequence_number, |
45a00b05 | 598 | .instance_id = client_instance_id, |
82b9bde8 JD |
599 | }; |
600 | ||
b5457df5 | 601 | static const struct lttng_ust_ring_buffer_config client_config = { |
3d1fc7fd MD |
602 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
603 | .cb.record_header_size = client_record_header_size, | |
604 | .cb.subbuffer_header_size = client_packet_header_size, | |
605 | .cb.buffer_begin = client_buffer_begin, | |
606 | .cb.buffer_end = client_buffer_end, | |
607 | .cb.buffer_create = client_buffer_create, | |
608 | .cb.buffer_finalize = client_buffer_finalize, | |
a9ff648c MD |
609 | .cb.content_size_field = client_content_size_field, |
610 | .cb.packet_size_field = client_packet_size_field, | |
3d1fc7fd | 611 | |
373ea80a | 612 | .timestamp_bits = LTTNG_COMPACT_TIMESTAMP_BITS, |
3d1fc7fd | 613 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
5d61a504 | 614 | .sync = RING_BUFFER_SYNC_GLOBAL, |
3d1fc7fd MD |
615 | .mode = RING_BUFFER_MODE_TEMPLATE, |
616 | .backend = RING_BUFFER_PAGE, | |
5d61a504 | 617 | .output = RING_BUFFER_MMAP, |
3d1fc7fd | 618 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
5d61a504 | 619 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
34a91bdb | 620 | .wakeup = LTTNG_CLIENT_WAKEUP, |
c1fca457 | 621 | .client_type = LTTNG_CLIENT_TYPE, |
82b9bde8 JD |
622 | |
623 | .cb_ptr = &client_cb.parent, | |
3d1fc7fd MD |
624 | }; |
625 | ||
626 | static | |
e7bc0ef6 | 627 | struct lttng_ust_channel_buffer *_channel_create(const char *name, |
a3f61e7f | 628 | void *buf_addr, |
3d1fc7fd MD |
629 | size_t subbuf_size, size_t num_subbuf, |
630 | unsigned int switch_timer_interval, | |
193183fb | 631 | unsigned int read_timer_interval, |
6ca18e66 | 632 | unsigned char *uuid, |
a9ff648c | 633 | uint32_t chan_id, |
b2c5f61a MD |
634 | const int *stream_fds, int nr_stream_fds, |
635 | int64_t blocking_timeout) | |
3d1fc7fd | 636 | { |
f0fde1c3 | 637 | struct lttng_ust_abi_channel_config chan_priv_init; |
a3f61e7f | 638 | struct lttng_ust_shm_handle *handle; |
e7bc0ef6 | 639 | struct lttng_ust_channel_buffer *lttng_chan_buf; |
f0fde1c3 | 640 | |
e7bc0ef6 MD |
641 | lttng_chan_buf = lttng_ust_alloc_channel_buffer(); |
642 | if (!lttng_chan_buf) | |
f0fde1c3 | 643 | return NULL; |
e7bc0ef6 MD |
644 | memcpy(lttng_chan_buf->priv->uuid, uuid, LTTNG_UST_UUID_LEN); |
645 | lttng_chan_buf->priv->id = chan_id; | |
a3f61e7f | 646 | |
74d81a6c MD |
647 | memset(&chan_priv_init, 0, sizeof(chan_priv_init)); |
648 | memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN); | |
6ca18e66 | 649 | chan_priv_init.id = chan_id; |
f0fde1c3 | 650 | |
a3f61e7f | 651 | handle = channel_create(&client_config, name, |
f0fde1c3 MD |
652 | __alignof__(struct lttng_ust_abi_channel_config), |
653 | sizeof(struct lttng_ust_abi_channel_config), | |
74d81a6c | 654 | &chan_priv_init, |
e7bc0ef6 | 655 | lttng_chan_buf, buf_addr, subbuf_size, num_subbuf, |
a9ff648c | 656 | switch_timer_interval, read_timer_interval, |
b2c5f61a | 657 | stream_fds, nr_stream_fds, blocking_timeout); |
a3f61e7f | 658 | if (!handle) |
f0fde1c3 | 659 | goto error; |
07539b34 | 660 | lttng_chan_buf->priv->rb_chan = shmp(handle, handle->chan); |
e7bc0ef6 | 661 | return lttng_chan_buf; |
f0fde1c3 MD |
662 | |
663 | error: | |
e7bc0ef6 | 664 | lttng_ust_free_channel_common(lttng_chan_buf->parent); |
f0fde1c3 | 665 | return NULL; |
3d1fc7fd MD |
666 | } |
667 | ||
668 | static | |
e7bc0ef6 | 669 | void lttng_channel_destroy(struct lttng_ust_channel_buffer *lttng_chan_buf) |
3d1fc7fd | 670 | { |
07539b34 | 671 | channel_destroy(lttng_chan_buf->priv->rb_chan, lttng_chan_buf->priv->rb_chan->handle, 1); |
e7bc0ef6 | 672 | lttng_ust_free_channel_common(lttng_chan_buf->parent); |
3d1fc7fd MD |
673 | } |
674 | ||
675 | static | |
b5457df5 | 676 | int lttng_event_reserve(struct lttng_ust_ring_buffer_ctx *ctx) |
3d1fc7fd | 677 | { |
8936b6c0 | 678 | struct lttng_ust_event_recorder *event_recorder = ctx->client_priv; |
07539b34 | 679 | struct lttng_ust_channel_buffer *lttng_chan = event_recorder->chan; |
e56bb47c | 680 | struct lttng_client_ctx client_ctx; |
8936b6c0 | 681 | int ret, nesting; |
b5457df5 | 682 | struct lttng_ust_ring_buffer_ctx_private *private_ctx; |
8936b6c0 | 683 | uint32_t event_id; |
3d1fc7fd | 684 | |
b93e147b | 685 | event_id = (uint32_t) event_recorder->priv->parent.id; |
0950190a | 686 | client_ctx.chan_ctx = lttng_ust_rcu_dereference(lttng_chan->priv->ctx); |
e56bb47c | 687 | /* Compute internal size of context structures. */ |
b2e37d27 | 688 | ctx_get_struct_size(ctx, client_ctx.chan_ctx, &client_ctx.packet_context_len); |
e56bb47c | 689 | |
8936b6c0 MD |
690 | nesting = lib_ring_buffer_nesting_inc(&client_config); |
691 | if (nesting < 0) | |
3d1fc7fd | 692 | return -EPERM; |
3d1fc7fd | 693 | |
f37bd904 | 694 | private_ctx = &URCU_TLS(private_ctx_stack)[nesting]; |
8936b6c0 MD |
695 | memset(private_ctx, 0, sizeof(*private_ctx)); |
696 | private_ctx->pub = ctx; | |
697 | private_ctx->chan = lttng_chan->priv->rb_chan; | |
698 | ||
699 | ctx->priv = private_ctx; | |
700 | ||
e7bc0ef6 | 701 | switch (lttng_chan->priv->header_type) { |
3d1fc7fd MD |
702 | case 1: /* compact */ |
703 | if (event_id > 30) | |
8936b6c0 | 704 | private_ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
3d1fc7fd MD |
705 | break; |
706 | case 2: /* large */ | |
707 | if (event_id > 65534) | |
8936b6c0 | 708 | private_ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
3d1fc7fd MD |
709 | break; |
710 | default: | |
711 | WARN_ON_ONCE(1); | |
712 | } | |
713 | ||
e56bb47c | 714 | ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx); |
630d6836 | 715 | if (caa_unlikely(ret)) |
3d1fc7fd | 716 | goto put; |
2b7080aa | 717 | if (lib_ring_buffer_backend_get_pages(&client_config, ctx, |
8936b6c0 | 718 | &private_ctx->backend_pages)) { |
2b7080aa MD |
719 | ret = -EPERM; |
720 | goto put; | |
a3492932 | 721 | } |
0950190a | 722 | lttng_write_event_header(&client_config, ctx, &client_ctx, event_id); |
3d1fc7fd MD |
723 | return 0; |
724 | put: | |
7489fcb4 | 725 | lib_ring_buffer_nesting_dec(&client_config); |
3d1fc7fd MD |
726 | return ret; |
727 | } | |
728 | ||
729 | static | |
b5457df5 | 730 | void lttng_event_commit(struct lttng_ust_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
731 | { |
732 | lib_ring_buffer_commit(&client_config, ctx); | |
7489fcb4 | 733 | lib_ring_buffer_nesting_dec(&client_config); |
3d1fc7fd MD |
734 | } |
735 | ||
736 | static | |
b5457df5 | 737 | void lttng_event_write(struct lttng_ust_ring_buffer_ctx *ctx, |
8936b6c0 | 738 | const void *src, size_t len, size_t alignment) |
3d1fc7fd | 739 | { |
b5457df5 | 740 | lttng_ust_ring_buffer_align_ctx(ctx, alignment); |
3d1fc7fd MD |
741 | lib_ring_buffer_write(&client_config, ctx, src, len); |
742 | } | |
743 | ||
a44c74d9 | 744 | static |
b5457df5 | 745 | void lttng_event_strcpy(struct lttng_ust_ring_buffer_ctx *ctx, |
8936b6c0 | 746 | const char *src, size_t len) |
a44c74d9 MD |
747 | { |
748 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); | |
749 | } | |
750 | ||
27927814 | 751 | static |
b5457df5 | 752 | void lttng_event_pstrcpy_pad(struct lttng_ust_ring_buffer_ctx *ctx, |
8936b6c0 | 753 | const char *src, size_t len) |
27927814 | 754 | { |
879f9b0a | 755 | lib_ring_buffer_pstrcpy(&client_config, ctx, src, len, '\0'); |
27927814 MD |
756 | } |
757 | ||
3d1fc7fd | 758 | static |
07539b34 | 759 | int lttng_is_finalized(struct lttng_ust_channel_buffer *chan) |
3d1fc7fd | 760 | { |
b5457df5 | 761 | struct lttng_ust_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
3d1fc7fd | 762 | |
07539b34 | 763 | return lib_ring_buffer_channel_is_finalized(rb_chan); |
3d1fc7fd MD |
764 | } |
765 | ||
766 | static | |
07539b34 | 767 | int lttng_is_disabled(struct lttng_ust_channel_buffer *chan) |
3d1fc7fd | 768 | { |
b5457df5 | 769 | struct lttng_ust_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
3d1fc7fd | 770 | |
07539b34 | 771 | return lib_ring_buffer_channel_is_disabled(rb_chan); |
3d1fc7fd MD |
772 | } |
773 | ||
43861eab | 774 | static |
07539b34 | 775 | int lttng_flush_buffer(struct lttng_ust_channel_buffer *chan) |
43861eab | 776 | { |
b5457df5 MD |
777 | struct lttng_ust_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
778 | struct lttng_ust_ring_buffer *buf; | |
43861eab MD |
779 | int cpu; |
780 | ||
07539b34 | 781 | for_each_channel_cpu(cpu, rb_chan) { |
74d81a6c MD |
782 | int shm_fd, wait_fd, wakeup_fd; |
783 | uint64_t memory_map_size; | |
63b3205f | 784 | void *memory_map_addr; |
43861eab | 785 | |
07539b34 MD |
786 | buf = channel_get_ring_buffer(&client_config, rb_chan, |
787 | cpu, rb_chan->handle, &shm_fd, &wait_fd, | |
63b3205f | 788 | &wakeup_fd, &memory_map_size, &memory_map_addr); |
43861eab | 789 | lib_ring_buffer_switch(&client_config, buf, |
07539b34 | 790 | SWITCH_ACTIVE, rb_chan->handle); |
43861eab MD |
791 | } |
792 | return 0; | |
793 | } | |
794 | ||
7dd08bec | 795 | static struct lttng_transport lttng_relay_transport = { |
818173b9 | 796 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
3d1fc7fd | 797 | .ops = { |
14b6f891 | 798 | .struct_size = sizeof(struct lttng_ust_channel_buffer_ops), |
5defa774 | 799 | .priv = LTTNG_UST_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private, { |
a880bae5 MD |
800 | .pub = <tng_relay_transport.ops, |
801 | .channel_create = _channel_create, | |
802 | .channel_destroy = lttng_channel_destroy, | |
803 | .packet_avail_size = NULL, /* Would be racy anyway */ | |
804 | .is_finalized = lttng_is_finalized, | |
805 | .is_disabled = lttng_is_disabled, | |
806 | .flush_buffer = lttng_flush_buffer, | |
807 | }), | |
7dd08bec MD |
808 | .event_reserve = lttng_event_reserve, |
809 | .event_commit = lttng_event_commit, | |
810 | .event_write = lttng_event_write, | |
a44c74d9 | 811 | .event_strcpy = lttng_event_strcpy, |
879f9b0a | 812 | .event_pstrcpy_pad = lttng_event_pstrcpy_pad, |
3d1fc7fd | 813 | }, |
74d81a6c | 814 | .client_config = &client_config, |
3d1fc7fd MD |
815 | }; |
816 | ||
edaa1431 | 817 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
3d1fc7fd | 818 | { |
34a91bdb MD |
819 | DBG("LTT : ltt ring buffer client \"%s\" init\n", |
820 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); | |
7dd08bec | 821 | lttng_transport_register(<tng_relay_transport); |
3d1fc7fd MD |
822 | } |
823 | ||
edaa1431 | 824 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
3d1fc7fd | 825 | { |
34a91bdb MD |
826 | DBG("LTT : ltt ring buffer client \"%s\" exit\n", |
827 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); | |
7dd08bec | 828 | lttng_transport_unregister(<tng_relay_transport); |
3d1fc7fd | 829 | } |