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