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