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