Commit | Line | Data |
---|---|---|
b7cdc182 | 1 | /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only) |
9f36eaed | 2 | * |
a90917c3 | 3 | * lttng-ring-buffer-client.h |
7514523f | 4 | * |
3d084699 | 5 | * LTTng lib ring buffer client template. |
7514523f | 6 | * |
886d51a3 | 7 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
7514523f MD |
8 | */ |
9 | ||
10 | #include <linux/module.h> | |
c0e31d2e | 11 | #include <linux/types.h> |
a071f25d | 12 | #include <lttng/bitfield.h> |
263b6c88 | 13 | #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */ |
bbd023d6 | 14 | #include <wrapper/trace-clock.h> |
2df37e95 | 15 | #include <lttng/events.h> |
cefd899f | 16 | #include <lttng/events-internal.h> |
2df37e95 | 17 | #include <lttng/tracer.h> |
24591303 | 18 | #include <ringbuffer/frontend_types.h> |
7514523f | 19 | |
a2ef1c03 MD |
20 | #define LTTNG_COMPACT_EVENT_BITS 5 |
21 | #define LTTNG_COMPACT_TSC_BITS 27 | |
22 | ||
dd5a0db3 MD |
23 | static struct lttng_transport lttng_relay_transport; |
24 | ||
d793d5e1 MD |
25 | /* |
26 | * Keep the natural field alignment for _each field_ within this structure if | |
27 | * you ever add/remove a field from this header. Packed attribute is not used | |
28 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
29 | * let gcc add padding between the structure elements. | |
fcf74578 MD |
30 | * |
31 | * The guarantee we have with timestamps is that all the events in a | |
32 | * packet are included (inclusive) within the begin/end timestamps of | |
33 | * the packet. Another guarantee we have is that the "timestamp begin", | |
34 | * as well as the event timestamps, are monotonically increasing (never | |
35 | * decrease) when moving forward in a stream (physically). But this | |
36 | * guarantee does not apply to "timestamp end", because it is sampled at | |
37 | * commit time, which is not ordered with respect to space reservation. | |
d793d5e1 | 38 | */ |
9115fbdc | 39 | |
d793d5e1 | 40 | struct packet_header { |
9115fbdc | 41 | /* Trace packet header */ |
d793d5e1 MD |
42 | uint32_t magic; /* |
43 | * Trace magic number. | |
44 | * contains endianness information. | |
45 | */ | |
1ec3f75a | 46 | uint8_t uuid[16]; |
d793d5e1 | 47 | uint32_t stream_id; |
5594698f | 48 | uint64_t stream_instance_id; |
9115fbdc MD |
49 | |
50 | struct { | |
51 | /* Stream packet context */ | |
52 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ | |
53 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ | |
576ca06a MD |
54 | uint64_t content_size; /* Size of data in subbuffer */ |
55 | uint64_t packet_size; /* Subbuffer size (include padding) */ | |
5b3cf4f9 | 56 | uint64_t packet_seq_num; /* Packet sequence number */ |
a9afe705 | 57 | unsigned long events_discarded; /* |
9115fbdc MD |
58 | * Events lost in this subbuffer since |
59 | * the beginning of the trace. | |
60 | * (may overflow) | |
61 | */ | |
9115fbdc MD |
62 | uint32_t cpu_id; /* CPU id associated with stream */ |
63 | uint8_t header_end; /* End of header */ | |
64 | } ctx; | |
d793d5e1 MD |
65 | }; |
66 | ||
cc62f29e MD |
67 | struct lttng_client_ctx { |
68 | size_t packet_context_len; | |
69 | size_t event_context_len; | |
70 | }; | |
d793d5e1 | 71 | |
881833e3 MD |
72 | static inline notrace u64 lib_ring_buffer_clock_read(struct channel *chan) |
73 | { | |
74 | return trace_clock_read64(); | |
75 | } | |
76 | ||
f1676205 | 77 | static inline |
437d5aa5 | 78 | size_t ctx_get_aligned_size(size_t offset, struct lttng_kernel_ctx *ctx, |
cc62f29e | 79 | size_t ctx_len) |
f1676205 | 80 | { |
f1676205 MD |
81 | size_t orig_offset = offset; |
82 | ||
83 | if (likely(!ctx)) | |
84 | return 0; | |
a9dd15da | 85 | offset += lib_ring_buffer_align(offset, ctx->largest_align); |
cc62f29e MD |
86 | offset += ctx_len; |
87 | return offset - orig_offset; | |
88 | } | |
89 | ||
90 | static inline | |
437d5aa5 | 91 | void ctx_get_struct_size(struct lttng_kernel_ctx *ctx, size_t *ctx_len, |
1804b615 | 92 | struct lttng_channel *chan, struct lib_ring_buffer_ctx *bufctx) |
cc62f29e MD |
93 | { |
94 | int i; | |
95 | size_t offset = 0; | |
96 | ||
97 | if (likely(!ctx)) { | |
98 | *ctx_len = 0; | |
99 | return; | |
100 | } | |
1804b615 FD |
101 | for (i = 0; i < ctx->nr_fields; i++) { |
102 | if (ctx->fields[i].get_size) | |
103 | offset += ctx->fields[i].get_size(offset); | |
104 | if (ctx->fields[i].get_size_arg) | |
105 | offset += ctx->fields[i].get_size_arg(offset, | |
106 | &ctx->fields[i], bufctx, chan); | |
107 | } | |
cc62f29e | 108 | *ctx_len = offset; |
f1676205 MD |
109 | } |
110 | ||
111 | static inline | |
112 | void ctx_record(struct lib_ring_buffer_ctx *bufctx, | |
a90917c3 | 113 | struct lttng_channel *chan, |
437d5aa5 | 114 | struct lttng_kernel_ctx *ctx) |
f1676205 MD |
115 | { |
116 | int i; | |
117 | ||
118 | if (likely(!ctx)) | |
119 | return; | |
a9dd15da | 120 | lib_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
f1676205 MD |
121 | for (i = 0; i < ctx->nr_fields; i++) |
122 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); | |
123 | } | |
124 | ||
881833e3 MD |
125 | /* |
126 | * record_header_size - Calculate the header size and padding necessary. | |
127 | * @config: ring buffer instance configuration | |
128 | * @chan: channel | |
129 | * @offset: offset in the write buffer | |
881833e3 | 130 | * @pre_header_padding: padding to add before the header (output) |
881833e3 MD |
131 | * @ctx: reservation context |
132 | * | |
133 | * Returns the event header size (including padding). | |
134 | * | |
881833e3 MD |
135 | * The payload must itself determine its own alignment from the biggest type it |
136 | * contains. | |
137 | */ | |
138 | static __inline__ | |
c1cc82b8 | 139 | size_t record_header_size(const struct lib_ring_buffer_config *config, |
881833e3 | 140 | struct channel *chan, size_t offset, |
64c796d8 | 141 | size_t *pre_header_padding, |
cc62f29e MD |
142 | struct lib_ring_buffer_ctx *ctx, |
143 | struct lttng_client_ctx *client_ctx) | |
881833e3 | 144 | { |
a90917c3 | 145 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
881833e3 MD |
146 | size_t orig_offset = offset; |
147 | size_t padding; | |
148 | ||
a90917c3 | 149 | switch (lttng_chan->header_type) { |
9115fbdc | 150 | case 1: /* compact */ |
a90917c3 | 151 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
9115fbdc | 152 | offset += padding; |
c7d9db61 | 153 | if (!(ctx->priv.rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
9115fbdc MD |
154 | offset += sizeof(uint32_t); /* id and timestamp */ |
155 | } else { | |
a2ef1c03 MD |
156 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
157 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; | |
9115fbdc | 158 | /* Align extended struct on largest member */ |
a90917c3 | 159 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
9115fbdc | 160 | offset += sizeof(uint32_t); /* id */ |
a90917c3 | 161 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
9115fbdc MD |
162 | offset += sizeof(uint64_t); /* timestamp */ |
163 | } | |
164 | break; | |
165 | case 2: /* large */ | |
a90917c3 | 166 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
9115fbdc MD |
167 | offset += padding; |
168 | offset += sizeof(uint16_t); | |
c7d9db61 | 169 | if (!(ctx->priv.rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
a90917c3 | 170 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
9115fbdc MD |
171 | offset += sizeof(uint32_t); /* timestamp */ |
172 | } else { | |
173 | /* Align extended struct on largest member */ | |
a90917c3 | 174 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
9115fbdc | 175 | offset += sizeof(uint32_t); /* id */ |
a90917c3 | 176 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
9115fbdc | 177 | offset += sizeof(uint64_t); /* timestamp */ |
881833e3 | 178 | } |
9115fbdc MD |
179 | break; |
180 | default: | |
1b2e041f | 181 | padding = 0; |
64c796d8 | 182 | WARN_ON_ONCE(1); |
881833e3 | 183 | } |
cc62f29e MD |
184 | offset += ctx_get_aligned_size(offset, lttng_chan->ctx, |
185 | client_ctx->packet_context_len); | |
881833e3 MD |
186 | *pre_header_padding = padding; |
187 | return offset - orig_offset; | |
188 | } | |
189 | ||
24591303 | 190 | #include <ringbuffer/api.h> |
881833e3 | 191 | |
eb9a7857 | 192 | static |
a90917c3 | 193 | void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config, |
881833e3 | 194 | struct lib_ring_buffer_ctx *ctx, |
64c796d8 | 195 | uint32_t event_id); |
881833e3 MD |
196 | |
197 | /* | |
a90917c3 | 198 | * lttng_write_event_header |
881833e3 MD |
199 | * |
200 | * Writes the event header to the offset (already aligned on 32-bits). | |
201 | * | |
202 | * @config: ring buffer instance configuration | |
203 | * @ctx: reservation context | |
4e1f08f4 | 204 | * @event_id: event ID |
881833e3 MD |
205 | */ |
206 | static __inline__ | |
a90917c3 | 207 | void lttng_write_event_header(const struct lib_ring_buffer_config *config, |
881833e3 | 208 | struct lib_ring_buffer_ctx *ctx, |
64c796d8 | 209 | uint32_t event_id) |
881833e3 | 210 | { |
c7d9db61 | 211 | struct lttng_channel *lttng_chan = channel_get_private(ctx->priv.chan); |
881833e3 | 212 | |
c7d9db61 | 213 | if (unlikely(ctx->priv.rflags)) |
881833e3 MD |
214 | goto slow_path; |
215 | ||
a90917c3 | 216 | switch (lttng_chan->header_type) { |
9115fbdc MD |
217 | case 1: /* compact */ |
218 | { | |
219 | uint32_t id_time = 0; | |
220 | ||
a2ef1c03 MD |
221 | bt_bitfield_write(&id_time, uint32_t, |
222 | 0, | |
223 | LTTNG_COMPACT_EVENT_BITS, | |
224 | event_id); | |
225 | bt_bitfield_write(&id_time, uint32_t, | |
226 | LTTNG_COMPACT_EVENT_BITS, | |
227 | LTTNG_COMPACT_TSC_BITS, | |
c7d9db61 | 228 | ctx->priv.tsc); |
9115fbdc MD |
229 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
230 | break; | |
231 | } | |
232 | case 2: /* large */ | |
233 | { | |
c7d9db61 | 234 | uint32_t timestamp = (uint32_t) ctx->priv.tsc; |
7e855749 | 235 | uint16_t id = event_id; |
9115fbdc | 236 | |
7e855749 | 237 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
a90917c3 | 238 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
9115fbdc MD |
239 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
240 | break; | |
241 | } | |
242 | default: | |
64c796d8 | 243 | WARN_ON_ONCE(1); |
9115fbdc | 244 | } |
f1676205 | 245 | |
a90917c3 | 246 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
c595c36f | 247 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
f1676205 | 248 | |
9115fbdc | 249 | return; |
881833e3 MD |
250 | |
251 | slow_path: | |
a90917c3 | 252 | lttng_write_event_header_slow(config, ctx, event_id); |
881833e3 MD |
253 | } |
254 | ||
eb9a7857 | 255 | static |
a90917c3 | 256 | void lttng_write_event_header_slow(const struct lib_ring_buffer_config *config, |
64c796d8 MD |
257 | struct lib_ring_buffer_ctx *ctx, |
258 | uint32_t event_id) | |
881833e3 | 259 | { |
c7d9db61 | 260 | struct lttng_channel *lttng_chan = channel_get_private(ctx->priv.chan); |
9115fbdc | 261 | |
a90917c3 | 262 | switch (lttng_chan->header_type) { |
9115fbdc | 263 | case 1: /* compact */ |
c7d9db61 | 264 | if (!(ctx->priv.rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
9115fbdc MD |
265 | uint32_t id_time = 0; |
266 | ||
a2ef1c03 MD |
267 | bt_bitfield_write(&id_time, uint32_t, |
268 | 0, | |
269 | LTTNG_COMPACT_EVENT_BITS, | |
270 | event_id); | |
271 | bt_bitfield_write(&id_time, uint32_t, | |
272 | LTTNG_COMPACT_EVENT_BITS, | |
c7d9db61 | 273 | LTTNG_COMPACT_TSC_BITS, ctx->priv.tsc); |
9115fbdc MD |
274 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
275 | } else { | |
276 | uint8_t id = 0; | |
c7d9db61 | 277 | uint64_t timestamp = ctx->priv.tsc; |
9115fbdc | 278 | |
a2ef1c03 MD |
279 | bt_bitfield_write(&id, uint8_t, |
280 | 0, | |
281 | LTTNG_COMPACT_EVENT_BITS, | |
282 | 31); | |
9115fbdc MD |
283 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
284 | /* Align extended struct on largest member */ | |
a90917c3 | 285 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
9115fbdc | 286 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
a90917c3 | 287 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
9115fbdc MD |
288 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
289 | } | |
881833e3 | 290 | break; |
9115fbdc MD |
291 | case 2: /* large */ |
292 | { | |
c7d9db61 MD |
293 | if (!(ctx->priv.rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
294 | uint32_t timestamp = (uint32_t) ctx->priv.tsc; | |
7e855749 | 295 | uint16_t id = event_id; |
9115fbdc | 296 | |
7e855749 | 297 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
a90917c3 | 298 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
9115fbdc MD |
299 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
300 | } else { | |
64c796d8 | 301 | uint16_t id = 65535; |
c7d9db61 | 302 | uint64_t timestamp = ctx->priv.tsc; |
9115fbdc | 303 | |
64c796d8 | 304 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
9115fbdc | 305 | /* Align extended struct on largest member */ |
a90917c3 | 306 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
64c796d8 | 307 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
a90917c3 | 308 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
9115fbdc MD |
309 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
310 | } | |
881833e3 | 311 | break; |
881833e3 | 312 | } |
9115fbdc | 313 | default: |
64c796d8 | 314 | WARN_ON_ONCE(1); |
881833e3 | 315 | } |
a90917c3 | 316 | ctx_record(ctx, lttng_chan, lttng_chan->ctx); |
c595c36f | 317 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
881833e3 MD |
318 | } |
319 | ||
7514523f MD |
320 | static const struct lib_ring_buffer_config client_config; |
321 | ||
322 | static u64 client_ring_buffer_clock_read(struct channel *chan) | |
323 | { | |
324 | return lib_ring_buffer_clock_read(chan); | |
325 | } | |
326 | ||
1e2015dc | 327 | static |
7514523f MD |
328 | size_t client_record_header_size(const struct lib_ring_buffer_config *config, |
329 | struct channel *chan, size_t offset, | |
7514523f | 330 | size_t *pre_header_padding, |
cc62f29e MD |
331 | struct lib_ring_buffer_ctx *ctx, |
332 | void *client_ctx) | |
7514523f | 333 | { |
64c796d8 | 334 | return record_header_size(config, chan, offset, |
cc62f29e | 335 | pre_header_padding, ctx, client_ctx); |
7514523f MD |
336 | } |
337 | ||
338 | /** | |
1c25284c | 339 | * client_packet_header_size - called on buffer-switch to a new sub-buffer |
7514523f MD |
340 | * |
341 | * Return header size without padding after the structure. Don't use packed | |
342 | * structure because gcc generates inefficient code on some architectures | |
343 | * (powerpc, mips..) | |
344 | */ | |
1c25284c | 345 | static size_t client_packet_header_size(void) |
7514523f | 346 | { |
9115fbdc | 347 | return offsetof(struct packet_header, ctx.header_end); |
7514523f MD |
348 | } |
349 | ||
350 | static void client_buffer_begin(struct lib_ring_buffer *buf, u64 tsc, | |
351 | unsigned int subbuf_idx) | |
352 | { | |
353 | struct channel *chan = buf->backend.chan; | |
1c25284c MD |
354 | struct packet_header *header = |
355 | (struct packet_header *) | |
7514523f MD |
356 | lib_ring_buffer_offset_address(&buf->backend, |
357 | subbuf_idx * chan->backend.subbuf_size); | |
a90917c3 MD |
358 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
359 | struct lttng_session *session = lttng_chan->session; | |
7514523f | 360 | |
d793d5e1 | 361 | header->magic = CTF_MAGIC_NUMBER; |
1ec3f75a | 362 | memcpy(header->uuid, session->uuid.b, sizeof(session->uuid)); |
a90917c3 | 363 | header->stream_id = lttng_chan->id; |
5594698f | 364 | header->stream_instance_id = buf->backend.cpu; |
9115fbdc MD |
365 | header->ctx.timestamp_begin = tsc; |
366 | header->ctx.timestamp_end = 0; | |
576ca06a MD |
367 | header->ctx.content_size = ~0ULL; /* for debugging */ |
368 | header->ctx.packet_size = ~0ULL; | |
5b3cf4f9 JD |
369 | header->ctx.packet_seq_num = chan->backend.num_subbuf * \ |
370 | buf->backend.buf_cnt[subbuf_idx].seq_cnt + \ | |
371 | subbuf_idx; | |
9115fbdc | 372 | header->ctx.events_discarded = 0; |
9115fbdc | 373 | header->ctx.cpu_id = buf->backend.cpu; |
7514523f MD |
374 | } |
375 | ||
376 | /* | |
377 | * offset is assumed to never be 0 here : never deliver a completely empty | |
378 | * subbuffer. data_size is between 1 and subbuf_size. | |
379 | */ | |
380 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, | |
381 | unsigned int subbuf_idx, unsigned long data_size) | |
382 | { | |
383 | struct channel *chan = buf->backend.chan; | |
1c25284c MD |
384 | struct packet_header *header = |
385 | (struct packet_header *) | |
7514523f MD |
386 | lib_ring_buffer_offset_address(&buf->backend, |
387 | subbuf_idx * chan->backend.subbuf_size); | |
388 | unsigned long records_lost = 0; | |
389 | ||
9115fbdc | 390 | header->ctx.timestamp_end = tsc; |
f9e4e1b9 MD |
391 | header->ctx.content_size = |
392 | (uint64_t) data_size * CHAR_BIT; /* in bits */ | |
393 | header->ctx.packet_size = | |
394 | (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
7514523f MD |
395 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); |
396 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); | |
397 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
9115fbdc | 398 | header->ctx.events_discarded = records_lost; |
7514523f MD |
399 | } |
400 | ||
401 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, | |
402 | int cpu, const char *name) | |
403 | { | |
1c25284c | 404 | return 0; |
7514523f MD |
405 | } |
406 | ||
407 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) | |
408 | { | |
7514523f MD |
409 | } |
410 | ||
3b731ab1 JD |
411 | static struct packet_header *client_packet_header( |
412 | const struct lib_ring_buffer_config *config, | |
413 | struct lib_ring_buffer *buf) | |
414 | { | |
53700162 | 415 | return lib_ring_buffer_read_offset_address(&buf->backend, 0); |
3b731ab1 JD |
416 | } |
417 | ||
418 | static int client_timestamp_begin(const struct lib_ring_buffer_config *config, | |
419 | struct lib_ring_buffer *buf, | |
420 | uint64_t *timestamp_begin) | |
421 | { | |
422 | struct packet_header *header = client_packet_header(config, buf); | |
423 | *timestamp_begin = header->ctx.timestamp_begin; | |
424 | ||
425 | return 0; | |
426 | } | |
427 | ||
428 | static int client_timestamp_end(const struct lib_ring_buffer_config *config, | |
429 | struct lib_ring_buffer *buf, | |
430 | uint64_t *timestamp_end) | |
431 | { | |
432 | struct packet_header *header = client_packet_header(config, buf); | |
433 | *timestamp_end = header->ctx.timestamp_end; | |
434 | ||
435 | return 0; | |
436 | } | |
437 | ||
438 | static int client_events_discarded(const struct lib_ring_buffer_config *config, | |
439 | struct lib_ring_buffer *buf, | |
440 | uint64_t *events_discarded) | |
441 | { | |
442 | struct packet_header *header = client_packet_header(config, buf); | |
443 | *events_discarded = header->ctx.events_discarded; | |
444 | ||
445 | return 0; | |
446 | } | |
447 | ||
448 | static int client_content_size(const struct lib_ring_buffer_config *config, | |
449 | struct lib_ring_buffer *buf, | |
450 | uint64_t *content_size) | |
451 | { | |
452 | struct packet_header *header = client_packet_header(config, buf); | |
453 | *content_size = header->ctx.content_size; | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
458 | static int client_packet_size(const struct lib_ring_buffer_config *config, | |
459 | struct lib_ring_buffer *buf, | |
460 | uint64_t *packet_size) | |
461 | { | |
462 | struct packet_header *header = client_packet_header(config, buf); | |
463 | *packet_size = header->ctx.packet_size; | |
464 | ||
465 | return 0; | |
466 | } | |
467 | ||
468 | static int client_stream_id(const struct lib_ring_buffer_config *config, | |
469 | struct lib_ring_buffer *buf, | |
470 | uint64_t *stream_id) | |
471 | { | |
59a49244 MD |
472 | struct channel *chan = buf->backend.chan; |
473 | struct lttng_channel *lttng_chan = channel_get_private(chan); | |
3b731ab1 | 474 | |
59a49244 | 475 | *stream_id = lttng_chan->id; |
3b731ab1 JD |
476 | return 0; |
477 | } | |
478 | ||
2348ca17 JD |
479 | static int client_current_timestamp(const struct lib_ring_buffer_config *config, |
480 | struct lib_ring_buffer *bufb, | |
481 | uint64_t *ts) | |
482 | { | |
483 | *ts = config->cb.ring_buffer_clock_read(bufb->backend.chan); | |
484 | ||
485 | return 0; | |
486 | } | |
487 | ||
5b3cf4f9 JD |
488 | static int client_sequence_number(const struct lib_ring_buffer_config *config, |
489 | struct lib_ring_buffer *buf, | |
490 | uint64_t *seq) | |
491 | { | |
492 | struct packet_header *header = client_packet_header(config, buf); | |
493 | ||
494 | *seq = header->ctx.packet_seq_num; | |
495 | ||
496 | return 0; | |
497 | } | |
498 | ||
5594698f JD |
499 | static |
500 | int client_instance_id(const struct lib_ring_buffer_config *config, | |
501 | struct lib_ring_buffer *buf, | |
502 | uint64_t *id) | |
503 | { | |
59a49244 | 504 | *id = buf->backend.cpu; |
5594698f JD |
505 | |
506 | return 0; | |
507 | } | |
508 | ||
7514523f MD |
509 | static const struct lib_ring_buffer_config client_config = { |
510 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, | |
511 | .cb.record_header_size = client_record_header_size, | |
1c25284c | 512 | .cb.subbuffer_header_size = client_packet_header_size, |
7514523f MD |
513 | .cb.buffer_begin = client_buffer_begin, |
514 | .cb.buffer_end = client_buffer_end, | |
515 | .cb.buffer_create = client_buffer_create, | |
516 | .cb.buffer_finalize = client_buffer_finalize, | |
517 | ||
a2ef1c03 | 518 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
7514523f MD |
519 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
520 | .sync = RING_BUFFER_SYNC_PER_CPU, | |
3d084699 | 521 | .mode = RING_BUFFER_MODE_TEMPLATE, |
7514523f | 522 | .backend = RING_BUFFER_PAGE, |
2db1399a | 523 | .output = RING_BUFFER_OUTPUT_TEMPLATE, |
7514523f MD |
524 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
525 | .ipi = RING_BUFFER_IPI_BARRIER, | |
526 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, | |
527 | }; | |
528 | ||
dd5a0db3 MD |
529 | static |
530 | void release_priv_ops(void *priv_ops) | |
531 | { | |
532 | module_put(THIS_MODULE); | |
533 | } | |
534 | ||
535 | static | |
536 | void lttng_channel_destroy(struct channel *chan) | |
537 | { | |
538 | channel_destroy(chan); | |
539 | } | |
540 | ||
1e2015dc | 541 | static |
1c25284c | 542 | struct channel *_channel_create(const char *name, |
5cf4b87c | 543 | void *priv, void *buf_addr, |
1c25284c MD |
544 | size_t subbuf_size, size_t num_subbuf, |
545 | unsigned int switch_timer_interval, | |
546 | unsigned int read_timer_interval) | |
7514523f | 547 | { |
5cf4b87c | 548 | struct lttng_channel *lttng_chan = priv; |
dd5a0db3 MD |
549 | struct channel *chan; |
550 | ||
551 | chan = channel_create(&client_config, name, lttng_chan, buf_addr, | |
7514523f MD |
552 | subbuf_size, num_subbuf, switch_timer_interval, |
553 | read_timer_interval); | |
dd5a0db3 MD |
554 | if (chan) { |
555 | /* | |
556 | * Ensure this module is not unloaded before we finish | |
557 | * using lttng_relay_transport.ops. | |
558 | */ | |
559 | if (!try_module_get(THIS_MODULE)) { | |
5a15f70c | 560 | printk(KERN_WARNING "LTTng: Can't lock transport module.\n"); |
dd5a0db3 MD |
561 | goto error; |
562 | } | |
563 | chan->backend.priv_ops = <tng_relay_transport.ops; | |
564 | chan->backend.release_priv_ops = release_priv_ops; | |
565 | } | |
566 | return chan; | |
7514523f | 567 | |
dd5a0db3 MD |
568 | error: |
569 | lttng_channel_destroy(chan); | |
570 | return NULL; | |
7514523f MD |
571 | } |
572 | ||
ad1c05e1 | 573 | static |
a90917c3 | 574 | struct lib_ring_buffer *lttng_buffer_read_open(struct channel *chan) |
ad1c05e1 MD |
575 | { |
576 | struct lib_ring_buffer *buf; | |
577 | int cpu; | |
578 | ||
1c25284c MD |
579 | for_each_channel_cpu(cpu, chan) { |
580 | buf = channel_get_ring_buffer(&client_config, chan, cpu); | |
ad1c05e1 MD |
581 | if (!lib_ring_buffer_open_read(buf)) |
582 | return buf; | |
583 | } | |
584 | return NULL; | |
585 | } | |
586 | ||
f71ecafa | 587 | static |
a90917c3 | 588 | int lttng_buffer_has_read_closed_stream(struct channel *chan) |
f71ecafa MD |
589 | { |
590 | struct lib_ring_buffer *buf; | |
591 | int cpu; | |
592 | ||
593 | for_each_channel_cpu(cpu, chan) { | |
594 | buf = channel_get_ring_buffer(&client_config, chan, cpu); | |
595 | if (!atomic_long_read(&buf->active_readers)) | |
596 | return 1; | |
597 | } | |
598 | return 0; | |
599 | } | |
600 | ||
ad1c05e1 | 601 | static |
a90917c3 | 602 | void lttng_buffer_read_close(struct lib_ring_buffer *buf) |
ad1c05e1 MD |
603 | { |
604 | lib_ring_buffer_release_read(buf); | |
1c25284c MD |
605 | } |
606 | ||
c099397a | 607 | static |
cefd899f | 608 | int lttng_event_reserve(struct lib_ring_buffer_ctx *ctx) |
1c25284c | 609 | { |
c7d9db61 MD |
610 | struct lttng_kernel_event_recorder *event_recorder = ctx->client_priv; |
611 | struct lttng_channel *lttng_chan = event_recorder->chan; | |
cc62f29e | 612 | struct lttng_client_ctx client_ctx; |
1c25284c | 613 | int ret, cpu; |
cefd899f | 614 | uint32_t event_id; |
1c25284c MD |
615 | |
616 | cpu = lib_ring_buffer_get_cpu(&client_config); | |
5ebb028f | 617 | if (unlikely(cpu < 0)) |
1c25284c | 618 | return -EPERM; |
cefd899f | 619 | event_id = event_recorder->priv->id; |
c7d9db61 MD |
620 | memset(&ctx->priv, 0, sizeof(ctx->priv)); |
621 | ctx->priv.chan = lttng_chan->chan; | |
622 | ctx->priv.reserve_cpu = cpu; | |
e755470c FD |
623 | |
624 | /* Compute internal size of context structures. */ | |
625 | ctx_get_struct_size(lttng_chan->ctx, &client_ctx.packet_context_len, lttng_chan, ctx); | |
1c25284c | 626 | |
a90917c3 | 627 | switch (lttng_chan->header_type) { |
64c796d8 MD |
628 | case 1: /* compact */ |
629 | if (event_id > 30) | |
c7d9db61 | 630 | ctx->priv.rflags |= LTTNG_RFLAG_EXTENDED; |
64c796d8 MD |
631 | break; |
632 | case 2: /* large */ | |
633 | if (event_id > 65534) | |
c7d9db61 | 634 | ctx->priv.rflags |= LTTNG_RFLAG_EXTENDED; |
64c796d8 MD |
635 | break; |
636 | default: | |
637 | WARN_ON_ONCE(1); | |
638 | } | |
639 | ||
cc62f29e | 640 | ret = lib_ring_buffer_reserve(&client_config, ctx, &client_ctx); |
5ebb028f | 641 | if (unlikely(ret)) |
1c25284c | 642 | goto put; |
85a07c33 | 643 | lib_ring_buffer_backend_get_pages(&client_config, ctx, |
c7d9db61 | 644 | &ctx->priv.backend_pages); |
a90917c3 | 645 | lttng_write_event_header(&client_config, ctx, event_id); |
4e1f08f4 | 646 | return 0; |
1c25284c MD |
647 | put: |
648 | lib_ring_buffer_put_cpu(&client_config); | |
649 | return ret; | |
ad1c05e1 MD |
650 | } |
651 | ||
c099397a | 652 | static |
a90917c3 | 653 | void lttng_event_commit(struct lib_ring_buffer_ctx *ctx) |
1c25284c MD |
654 | { |
655 | lib_ring_buffer_commit(&client_config, ctx); | |
656 | lib_ring_buffer_put_cpu(&client_config); | |
657 | } | |
658 | ||
c099397a | 659 | static |
a90917c3 | 660 | void lttng_event_write(struct lib_ring_buffer_ctx *ctx, const void *src, |
e763dbf5 MD |
661 | size_t len) |
662 | { | |
663 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
664 | } | |
1c25284c | 665 | |
4ea00e4f | 666 | static |
a90917c3 | 667 | void lttng_event_write_from_user(struct lib_ring_buffer_ctx *ctx, |
4ea00e4f JD |
668 | const void __user *src, size_t len) |
669 | { | |
7b8ea3a5 | 670 | lib_ring_buffer_copy_from_user_inatomic(&client_config, ctx, src, len); |
4ea00e4f JD |
671 | } |
672 | ||
58aa5d24 | 673 | static |
a90917c3 | 674 | void lttng_event_memset(struct lib_ring_buffer_ctx *ctx, |
58aa5d24 MD |
675 | int c, size_t len) |
676 | { | |
677 | lib_ring_buffer_memset(&client_config, ctx, c, len); | |
678 | } | |
679 | ||
16f78f3a MD |
680 | static |
681 | void lttng_event_strcpy(struct lib_ring_buffer_ctx *ctx, const char *src, | |
682 | size_t len) | |
683 | { | |
684 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); | |
685 | } | |
686 | ||
687 | static | |
688 | void lttng_event_strcpy_from_user(struct lib_ring_buffer_ctx *ctx, | |
689 | const char __user *src, size_t len) | |
690 | { | |
691 | lib_ring_buffer_strcpy_from_user_inatomic(&client_config, ctx, src, | |
692 | len, '#'); | |
693 | } | |
694 | ||
c099397a | 695 | static |
a90917c3 | 696 | wait_queue_head_t *lttng_get_writer_buf_wait_queue(struct channel *chan, int cpu) |
c099397a | 697 | { |
71c1d843 MD |
698 | struct lib_ring_buffer *buf = channel_get_ring_buffer(&client_config, |
699 | chan, cpu); | |
700 | return &buf->write_wait; | |
24cedcfe MD |
701 | } |
702 | ||
703 | static | |
a90917c3 | 704 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
24cedcfe MD |
705 | { |
706 | return &chan->hp_wait; | |
707 | } | |
708 | ||
709 | static | |
a90917c3 | 710 | int lttng_is_finalized(struct channel *chan) |
24cedcfe MD |
711 | { |
712 | return lib_ring_buffer_channel_is_finalized(chan); | |
c099397a MD |
713 | } |
714 | ||
254ec7bc | 715 | static |
a90917c3 | 716 | int lttng_is_disabled(struct channel *chan) |
254ec7bc MD |
717 | { |
718 | return lib_ring_buffer_channel_is_disabled(chan); | |
719 | } | |
720 | ||
a90917c3 | 721 | static struct lttng_transport lttng_relay_transport = { |
3d084699 | 722 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, |
7514523f MD |
723 | .owner = THIS_MODULE, |
724 | .ops = { | |
1c25284c | 725 | .channel_create = _channel_create, |
a90917c3 MD |
726 | .channel_destroy = lttng_channel_destroy, |
727 | .buffer_read_open = lttng_buffer_read_open, | |
f71ecafa | 728 | .buffer_has_read_closed_stream = |
a90917c3 MD |
729 | lttng_buffer_has_read_closed_stream, |
730 | .buffer_read_close = lttng_buffer_read_close, | |
731 | .event_reserve = lttng_event_reserve, | |
732 | .event_commit = lttng_event_commit, | |
733 | .event_write = lttng_event_write, | |
734 | .event_write_from_user = lttng_event_write_from_user, | |
735 | .event_memset = lttng_event_memset, | |
16f78f3a MD |
736 | .event_strcpy = lttng_event_strcpy, |
737 | .event_strcpy_from_user = lttng_event_strcpy_from_user, | |
1ec3f75a | 738 | .packet_avail_size = NULL, /* Would be racy anyway */ |
a90917c3 MD |
739 | .get_writer_buf_wait_queue = lttng_get_writer_buf_wait_queue, |
740 | .get_hp_wait_queue = lttng_get_hp_wait_queue, | |
741 | .is_finalized = lttng_is_finalized, | |
742 | .is_disabled = lttng_is_disabled, | |
67a80835 MD |
743 | .timestamp_begin = client_timestamp_begin, |
744 | .timestamp_end = client_timestamp_end, | |
745 | .events_discarded = client_events_discarded, | |
746 | .content_size = client_content_size, | |
747 | .packet_size = client_packet_size, | |
748 | .stream_id = client_stream_id, | |
749 | .current_timestamp = client_current_timestamp, | |
5b3cf4f9 | 750 | .sequence_number = client_sequence_number, |
5594698f | 751 | .instance_id = client_instance_id, |
7514523f MD |
752 | }, |
753 | }; | |
754 | ||
a90917c3 | 755 | static int __init lttng_ring_buffer_client_init(void) |
7514523f | 756 | { |
a509e133 MD |
757 | /* |
758 | * This vmalloc sync all also takes care of the lib ring buffer | |
759 | * vmalloc'd module pages when it is built as a module into LTTng. | |
760 | */ | |
263b6c88 | 761 | wrapper_vmalloc_sync_mappings(); |
a90917c3 | 762 | lttng_transport_register(<tng_relay_transport); |
7514523f MD |
763 | return 0; |
764 | } | |
765 | ||
a90917c3 | 766 | module_init(lttng_ring_buffer_client_init); |
1c25284c | 767 | |
a90917c3 | 768 | static void __exit lttng_ring_buffer_client_exit(void) |
7514523f | 769 | { |
a90917c3 | 770 | lttng_transport_unregister(<tng_relay_transport); |
7514523f MD |
771 | } |
772 | ||
a90917c3 | 773 | module_exit(lttng_ring_buffer_client_exit); |
1c25284c | 774 | |
7514523f | 775 | MODULE_LICENSE("GPL and additional rights"); |
1c124020 | 776 | MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>"); |
3d084699 MD |
777 | MODULE_DESCRIPTION("LTTng ring buffer " RING_BUFFER_MODE_TEMPLATE_STRING |
778 | " client"); | |
1c124020 MJ |
779 | MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "." |
780 | __stringify(LTTNG_MODULES_MINOR_VERSION) "." | |
781 | __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION) | |
782 | LTTNG_MODULES_EXTRAVERSION); |