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