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