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