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