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