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