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