Commit | Line | Data |
---|---|---|
3d1fc7fd | 1 | /* |
7dd08bec | 2 | * lttng-ring-buffer-client.h |
3d1fc7fd | 3 | * |
3d1fc7fd MD |
4 | * LTTng lib ring buffer client template. |
5 | * | |
e92f3e28 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 | |
3d1fc7fd MD |
21 | */ |
22 | ||
9f3fdbc6 | 23 | #include <stdint.h> |
4318ae1b MD |
24 | #include <lttng/ust-events.h> |
25 | #include "lttng/bitfield.h" | |
b728d87e | 26 | #include "clock.h" |
7dd08bec | 27 | #include "lttng-tracer.h" |
9f3fdbc6 | 28 | #include "../libringbuffer/frontend_types.h" |
3d1fc7fd | 29 | |
79dfbf42 MD |
30 | #define LTTNG_COMPACT_EVENT_BITS 5 |
31 | #define LTTNG_COMPACT_TSC_BITS 27 | |
32 | ||
3d1fc7fd MD |
33 | /* |
34 | * Keep the natural field alignment for _each field_ within this structure if | |
35 | * you ever add/remove a field from this header. Packed attribute is not used | |
36 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
37 | * let gcc add padding between the structure elements. | |
38 | */ | |
39 | ||
40 | struct packet_header { | |
41 | /* Trace packet header */ | |
42 | uint32_t magic; /* | |
43 | * Trace magic number. | |
44 | * contains endianness information. | |
45 | */ | |
19d8b1b3 | 46 | uint8_t uuid[LTTNG_UST_UUID_LEN]; |
3d1fc7fd | 47 | uint32_t stream_id; |
2d94adc5 | 48 | uint64_t stream_instance_id; |
3d1fc7fd MD |
49 | |
50 | struct { | |
51 | /* Stream packet context */ | |
52 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ | |
53 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ | |
0c00a107 MD |
54 | uint64_t content_size; /* Size of data in subbuffer */ |
55 | uint64_t packet_size; /* Subbuffer size (include padding) */ | |
18fc8d71 | 56 | unsigned long events_discarded; /* |
3d1fc7fd MD |
57 | * Events lost in this subbuffer since |
58 | * the beginning of the trace. | |
59 | * (may overflow) | |
60 | */ | |
3d1fc7fd MD |
61 | uint32_t cpu_id; /* CPU id associated with stream */ |
62 | uint8_t header_end; /* End of header */ | |
63 | } ctx; | |
64 | }; | |
65 | ||
66 | ||
23c8854a | 67 | static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan) |
3d1fc7fd MD |
68 | { |
69 | return trace_clock_read64(); | |
70 | } | |
71 | ||
72 | static inline | |
73 | size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx) | |
74 | { | |
75 | int i; | |
76 | size_t orig_offset = offset; | |
77 | ||
b5a3dfa5 | 78 | if (caa_likely(!ctx)) |
3d1fc7fd | 79 | return 0; |
b2cc986a | 80 | offset += lib_ring_buffer_align(offset, ctx->largest_align); |
3d1fc7fd | 81 | for (i = 0; i < ctx->nr_fields; i++) |
53569322 | 82 | offset += ctx->fields[i].get_size(&ctx->fields[i], offset); |
3d1fc7fd MD |
83 | return offset - orig_offset; |
84 | } | |
85 | ||
86 | static inline | |
4cfec15c | 87 | void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx, |
7dd08bec | 88 | struct lttng_channel *chan, |
3d1fc7fd MD |
89 | struct lttng_ctx *ctx) |
90 | { | |
91 | int i; | |
92 | ||
b5a3dfa5 | 93 | if (caa_likely(!ctx)) |
3d1fc7fd | 94 | return; |
b2cc986a | 95 | lib_ring_buffer_align_ctx(bufctx, ctx->largest_align); |
3d1fc7fd MD |
96 | for (i = 0; i < ctx->nr_fields; i++) |
97 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); | |
98 | } | |
99 | ||
100 | /* | |
101 | * record_header_size - Calculate the header size and padding necessary. | |
102 | * @config: ring buffer instance configuration | |
103 | * @chan: channel | |
104 | * @offset: offset in the write buffer | |
105 | * @pre_header_padding: padding to add before the header (output) | |
106 | * @ctx: reservation context | |
107 | * | |
108 | * Returns the event header size (including padding). | |
109 | * | |
110 | * The payload must itself determine its own alignment from the biggest type it | |
111 | * contains. | |
112 | */ | |
113 | static __inline__ | |
8ca68914 | 114 | size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
115 | struct channel *chan, size_t offset, |
116 | size_t *pre_header_padding, | |
4cfec15c | 117 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd | 118 | { |
7dd08bec MD |
119 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
120 | struct lttng_event *event = ctx->priv; | |
3d1fc7fd MD |
121 | size_t orig_offset = offset; |
122 | size_t padding; | |
123 | ||
7dd08bec | 124 | switch (lttng_chan->header_type) { |
3d1fc7fd | 125 | case 1: /* compact */ |
1dbfff0c | 126 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
3d1fc7fd | 127 | offset += padding; |
7dd08bec | 128 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
3d1fc7fd MD |
129 | offset += sizeof(uint32_t); /* id and timestamp */ |
130 | } else { | |
79dfbf42 MD |
131 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
132 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; | |
3d1fc7fd | 133 | /* Align extended struct on largest member */ |
1dbfff0c | 134 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd | 135 | offset += sizeof(uint32_t); /* id */ |
1dbfff0c | 136 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
137 | offset += sizeof(uint64_t); /* timestamp */ |
138 | } | |
139 | break; | |
140 | case 2: /* large */ | |
1dbfff0c | 141 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
3d1fc7fd MD |
142 | offset += padding; |
143 | offset += sizeof(uint16_t); | |
7dd08bec | 144 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
1dbfff0c | 145 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
146 | offset += sizeof(uint32_t); /* timestamp */ |
147 | } else { | |
148 | /* Align extended struct on largest member */ | |
1dbfff0c | 149 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd | 150 | offset += sizeof(uint32_t); /* id */ |
1dbfff0c | 151 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
152 | offset += sizeof(uint64_t); /* timestamp */ |
153 | } | |
154 | break; | |
155 | default: | |
9f3fdbc6 | 156 | padding = 0; |
3d1fc7fd MD |
157 | WARN_ON_ONCE(1); |
158 | } | |
159 | offset += ctx_get_size(offset, event->ctx); | |
7dd08bec | 160 | offset += ctx_get_size(offset, lttng_chan->ctx); |
3d1fc7fd MD |
161 | |
162 | *pre_header_padding = padding; | |
163 | return offset - orig_offset; | |
164 | } | |
165 | ||
9f3fdbc6 | 166 | #include "../libringbuffer/api.h" |
82b9bde8 | 167 | #include "lttng-rb-clients.h" |
3d1fc7fd | 168 | |
9f3fdbc6 | 169 | static |
7dd08bec | 170 | void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
4cfec15c | 171 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
3d1fc7fd MD |
172 | uint32_t event_id); |
173 | ||
174 | /* | |
7dd08bec | 175 | * lttng_write_event_header |
3d1fc7fd MD |
176 | * |
177 | * Writes the event header to the offset (already aligned on 32-bits). | |
178 | * | |
179 | * @config: ring buffer instance configuration | |
180 | * @ctx: reservation context | |
181 | * @event_id: event ID | |
182 | */ | |
183 | static __inline__ | |
7dd08bec | 184 | void lttng_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config, |
4cfec15c | 185 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
3d1fc7fd MD |
186 | uint32_t event_id) |
187 | { | |
7dd08bec | 188 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
53569322 | 189 | struct lttng_stack_ctx *lttng_ctx = ctx->priv2; |
3d1fc7fd | 190 | |
b5a3dfa5 | 191 | if (caa_unlikely(ctx->rflags)) |
3d1fc7fd MD |
192 | goto slow_path; |
193 | ||
7dd08bec | 194 | switch (lttng_chan->header_type) { |
3d1fc7fd MD |
195 | case 1: /* compact */ |
196 | { | |
197 | uint32_t id_time = 0; | |
198 | ||
79dfbf42 MD |
199 | bt_bitfield_write(&id_time, uint32_t, |
200 | 0, | |
201 | LTTNG_COMPACT_EVENT_BITS, | |
202 | event_id); | |
203 | bt_bitfield_write(&id_time, uint32_t, | |
204 | LTTNG_COMPACT_EVENT_BITS, | |
205 | LTTNG_COMPACT_TSC_BITS, | |
206 | ctx->tsc); | |
3d1fc7fd MD |
207 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
208 | break; | |
209 | } | |
210 | case 2: /* large */ | |
211 | { | |
212 | uint32_t timestamp = (uint32_t) ctx->tsc; | |
213 | uint16_t id = event_id; | |
214 | ||
215 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
1dbfff0c | 216 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
217 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
218 | break; | |
219 | } | |
220 | default: | |
221 | WARN_ON_ONCE(1); | |
222 | } | |
223 | ||
53569322 MD |
224 | ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx); |
225 | ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx); | |
d3b2c3e0 | 226 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
227 | |
228 | return; | |
229 | ||
230 | slow_path: | |
7dd08bec | 231 | lttng_write_event_header_slow(config, ctx, event_id); |
3d1fc7fd MD |
232 | } |
233 | ||
9f3fdbc6 | 234 | static |
7dd08bec | 235 | void lttng_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
4cfec15c | 236 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
3d1fc7fd MD |
237 | uint32_t event_id) |
238 | { | |
7dd08bec | 239 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
53569322 | 240 | struct lttng_stack_ctx *lttng_ctx = ctx->priv2; |
3d1fc7fd | 241 | |
7dd08bec | 242 | switch (lttng_chan->header_type) { |
3d1fc7fd | 243 | case 1: /* compact */ |
7dd08bec | 244 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
3d1fc7fd MD |
245 | uint32_t id_time = 0; |
246 | ||
79dfbf42 MD |
247 | bt_bitfield_write(&id_time, uint32_t, |
248 | 0, | |
249 | LTTNG_COMPACT_EVENT_BITS, | |
250 | event_id); | |
251 | bt_bitfield_write(&id_time, uint32_t, | |
252 | LTTNG_COMPACT_EVENT_BITS, | |
253 | LTTNG_COMPACT_TSC_BITS, | |
254 | ctx->tsc); | |
3d1fc7fd MD |
255 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
256 | } else { | |
257 | uint8_t id = 0; | |
258 | uint64_t timestamp = ctx->tsc; | |
259 | ||
79dfbf42 MD |
260 | bt_bitfield_write(&id, uint8_t, |
261 | 0, | |
262 | LTTNG_COMPACT_EVENT_BITS, | |
263 | 31); | |
3d1fc7fd MD |
264 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
265 | /* Align extended struct on largest member */ | |
1dbfff0c | 266 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd | 267 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
1dbfff0c | 268 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
269 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
270 | } | |
271 | break; | |
272 | case 2: /* large */ | |
273 | { | |
7dd08bec | 274 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTTNG_RFLAG_EXTENDED))) { |
3d1fc7fd MD |
275 | uint32_t timestamp = (uint32_t) ctx->tsc; |
276 | uint16_t id = event_id; | |
277 | ||
278 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
1dbfff0c | 279 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
280 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
281 | } else { | |
282 | uint16_t id = 65535; | |
283 | uint64_t timestamp = ctx->tsc; | |
284 | ||
285 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
286 | /* Align extended struct on largest member */ | |
1dbfff0c | 287 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd | 288 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
1dbfff0c | 289 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
290 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
291 | } | |
292 | break; | |
293 | } | |
294 | default: | |
295 | WARN_ON_ONCE(1); | |
296 | } | |
53569322 MD |
297 | ctx_record(ctx, lttng_chan, lttng_ctx->chan_ctx); |
298 | ctx_record(ctx, lttng_chan, lttng_ctx->event_ctx); | |
d3b2c3e0 | 299 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
300 | } |
301 | ||
4cfec15c | 302 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
3d1fc7fd | 303 | |
23c8854a | 304 | static uint64_t client_ring_buffer_clock_read(struct channel *chan) |
3d1fc7fd MD |
305 | { |
306 | return lib_ring_buffer_clock_read(chan); | |
307 | } | |
308 | ||
309 | static | |
4cfec15c | 310 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
311 | struct channel *chan, size_t offset, |
312 | size_t *pre_header_padding, | |
4cfec15c | 313 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
314 | { |
315 | return record_header_size(config, chan, offset, | |
316 | pre_header_padding, ctx); | |
317 | } | |
318 | ||
319 | /** | |
320 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
321 | * | |
322 | * Return header size without padding after the structure. Don't use packed | |
323 | * structure because gcc generates inefficient code on some architectures | |
324 | * (powerpc, mips..) | |
325 | */ | |
326 | static size_t client_packet_header_size(void) | |
327 | { | |
328 | return offsetof(struct packet_header, ctx.header_end); | |
329 | } | |
330 | ||
23c8854a | 331 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 332 | unsigned int subbuf_idx, |
38fae1d3 | 333 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 334 | { |
1d498196 | 335 | struct channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
336 | struct packet_header *header = |
337 | (struct packet_header *) | |
338 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
339 | subbuf_idx * chan->backend.subbuf_size, |
340 | handle); | |
7dd08bec | 341 | struct lttng_channel *lttng_chan = channel_get_private(chan); |
3d1fc7fd | 342 | |
34daae3e MD |
343 | assert(header); |
344 | if (!header) | |
345 | return; | |
3d1fc7fd | 346 | header->magic = CTF_MAGIC_NUMBER; |
7dd08bec MD |
347 | memcpy(header->uuid, lttng_chan->uuid, sizeof(lttng_chan->uuid)); |
348 | header->stream_id = lttng_chan->id; | |
2d94adc5 | 349 | header->stream_instance_id = buf->backend.cpu; |
3d1fc7fd MD |
350 | header->ctx.timestamp_begin = tsc; |
351 | header->ctx.timestamp_end = 0; | |
0c00a107 MD |
352 | header->ctx.content_size = ~0ULL; /* for debugging */ |
353 | header->ctx.packet_size = ~0ULL; | |
3d1fc7fd | 354 | header->ctx.events_discarded = 0; |
3d1fc7fd MD |
355 | header->ctx.cpu_id = buf->backend.cpu; |
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 | */ | |
23c8854a | 362 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 363 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 364 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 365 | { |
1d498196 | 366 | struct channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
367 | struct packet_header *header = |
368 | (struct packet_header *) | |
369 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
370 | subbuf_idx * chan->backend.subbuf_size, |
371 | handle); | |
3d1fc7fd MD |
372 | unsigned long records_lost = 0; |
373 | ||
34daae3e MD |
374 | assert(header); |
375 | if (!header) | |
376 | return; | |
3d1fc7fd | 377 | header->ctx.timestamp_end = tsc; |
b1830883 MD |
378 | header->ctx.content_size = |
379 | (uint64_t) data_size * CHAR_BIT; /* in bits */ | |
380 | header->ctx.packet_size = | |
381 | (uint64_t) PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
04489ab4 MD |
382 | |
383 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
3d1fc7fd MD |
384 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
385 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
386 | header->ctx.events_discarded = records_lost; | |
387 | } | |
388 | ||
4cfec15c | 389 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
38fae1d3 | 390 | int cpu, const char *name, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
391 | { |
392 | return 0; | |
393 | } | |
394 | ||
4cfec15c | 395 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
396 | { |
397 | } | |
398 | ||
a9ff648c MD |
399 | static void client_content_size_field(const struct lttng_ust_lib_ring_buffer_config *config, |
400 | size_t *offset, size_t *length) | |
401 | { | |
402 | *offset = offsetof(struct packet_header, ctx.content_size); | |
403 | *length = sizeof(((struct packet_header *) NULL)->ctx.content_size); | |
404 | } | |
405 | ||
406 | static void client_packet_size_field(const struct lttng_ust_lib_ring_buffer_config *config, | |
407 | size_t *offset, size_t *length) | |
408 | { | |
409 | *offset = offsetof(struct packet_header, ctx.packet_size); | |
410 | *length = sizeof(((struct packet_header *) NULL)->ctx.packet_size); | |
411 | } | |
412 | ||
b2f3252a JD |
413 | static struct packet_header *client_packet_header(struct lttng_ust_lib_ring_buffer *buf, |
414 | struct lttng_ust_shm_handle *handle) | |
415 | { | |
c4d6bd10 | 416 | return lib_ring_buffer_read_offset_address(&buf->backend, 0, handle); |
b2f3252a JD |
417 | } |
418 | ||
419 | static int client_timestamp_begin(struct lttng_ust_lib_ring_buffer *buf, | |
420 | struct lttng_ust_shm_handle *handle, | |
421 | uint64_t *timestamp_begin) | |
422 | { | |
423 | struct packet_header *header; | |
424 | ||
425 | header = client_packet_header(buf, handle); | |
34daae3e MD |
426 | if (!header) |
427 | return -1; | |
b2f3252a JD |
428 | *timestamp_begin = header->ctx.timestamp_begin; |
429 | return 0; | |
430 | } | |
431 | ||
432 | static int client_timestamp_end(struct lttng_ust_lib_ring_buffer *buf, | |
433 | struct lttng_ust_shm_handle *handle, | |
434 | uint64_t *timestamp_end) | |
435 | { | |
436 | struct packet_header *header; | |
437 | ||
438 | header = client_packet_header(buf, handle); | |
34daae3e MD |
439 | if (!header) |
440 | return -1; | |
b2f3252a JD |
441 | *timestamp_end = header->ctx.timestamp_end; |
442 | return 0; | |
443 | } | |
444 | ||
445 | static int client_events_discarded(struct lttng_ust_lib_ring_buffer *buf, | |
446 | struct lttng_ust_shm_handle *handle, | |
447 | uint64_t *events_discarded) | |
448 | { | |
449 | struct packet_header *header; | |
450 | ||
451 | header = client_packet_header(buf, handle); | |
34daae3e MD |
452 | if (!header) |
453 | return -1; | |
b2f3252a JD |
454 | *events_discarded = header->ctx.events_discarded; |
455 | return 0; | |
456 | } | |
457 | ||
458 | static int client_content_size(struct lttng_ust_lib_ring_buffer *buf, | |
459 | struct lttng_ust_shm_handle *handle, | |
460 | uint64_t *content_size) | |
461 | { | |
462 | struct packet_header *header; | |
463 | ||
464 | header = client_packet_header(buf, handle); | |
34daae3e MD |
465 | if (!header) |
466 | return -1; | |
b2f3252a JD |
467 | *content_size = header->ctx.content_size; |
468 | return 0; | |
469 | } | |
470 | ||
471 | static int client_packet_size(struct lttng_ust_lib_ring_buffer *buf, | |
472 | struct lttng_ust_shm_handle *handle, | |
473 | uint64_t *packet_size) | |
474 | { | |
475 | struct packet_header *header; | |
476 | ||
477 | header = client_packet_header(buf, handle); | |
34daae3e MD |
478 | if (!header) |
479 | return -1; | |
b2f3252a JD |
480 | *packet_size = header->ctx.packet_size; |
481 | return 0; | |
482 | } | |
483 | ||
484 | static int client_stream_id(struct lttng_ust_lib_ring_buffer *buf, | |
485 | struct lttng_ust_shm_handle *handle, | |
486 | uint64_t *stream_id) | |
487 | { | |
488 | struct packet_header *header; | |
489 | ||
490 | header = client_packet_header(buf, handle); | |
34daae3e MD |
491 | if (!header) |
492 | return -1; | |
b2f3252a JD |
493 | *stream_id = header->stream_id; |
494 | return 0; | |
495 | } | |
496 | ||
fca361e8 JD |
497 | static int client_current_timestamp(struct lttng_ust_lib_ring_buffer *buf, |
498 | struct lttng_ust_shm_handle *handle, | |
499 | uint64_t *ts) | |
500 | { | |
501 | struct channel *chan; | |
502 | ||
503 | chan = shmp(handle, handle->chan); | |
504 | *ts = client_ring_buffer_clock_read(chan); | |
505 | ||
506 | return 0; | |
507 | } | |
508 | ||
82b9bde8 JD |
509 | static const |
510 | struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = { | |
511 | .parent = { | |
512 | .ring_buffer_clock_read = client_ring_buffer_clock_read, | |
513 | .record_header_size = client_record_header_size, | |
514 | .subbuffer_header_size = client_packet_header_size, | |
515 | .buffer_begin = client_buffer_begin, | |
516 | .buffer_end = client_buffer_end, | |
517 | .buffer_create = client_buffer_create, | |
518 | .buffer_finalize = client_buffer_finalize, | |
a9ff648c MD |
519 | .content_size_field = client_content_size_field, |
520 | .packet_size_field = client_packet_size_field, | |
82b9bde8 | 521 | }, |
b2f3252a JD |
522 | .timestamp_begin = client_timestamp_begin, |
523 | .timestamp_end = client_timestamp_end, | |
524 | .events_discarded = client_events_discarded, | |
525 | .content_size = client_content_size, | |
526 | .packet_size = client_packet_size, | |
527 | .stream_id = client_stream_id, | |
fca361e8 | 528 | .current_timestamp = client_current_timestamp, |
82b9bde8 JD |
529 | }; |
530 | ||
4cfec15c | 531 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
3d1fc7fd MD |
532 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
533 | .cb.record_header_size = client_record_header_size, | |
534 | .cb.subbuffer_header_size = client_packet_header_size, | |
535 | .cb.buffer_begin = client_buffer_begin, | |
536 | .cb.buffer_end = client_buffer_end, | |
537 | .cb.buffer_create = client_buffer_create, | |
538 | .cb.buffer_finalize = client_buffer_finalize, | |
a9ff648c MD |
539 | .cb.content_size_field = client_content_size_field, |
540 | .cb.packet_size_field = client_packet_size_field, | |
3d1fc7fd | 541 | |
79dfbf42 | 542 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
3d1fc7fd | 543 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
5d61a504 | 544 | .sync = RING_BUFFER_SYNC_GLOBAL, |
3d1fc7fd MD |
545 | .mode = RING_BUFFER_MODE_TEMPLATE, |
546 | .backend = RING_BUFFER_PAGE, | |
5d61a504 | 547 | .output = RING_BUFFER_MMAP, |
3d1fc7fd | 548 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
5d61a504 | 549 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
34a91bdb | 550 | .wakeup = LTTNG_CLIENT_WAKEUP, |
c1fca457 | 551 | .client_type = LTTNG_CLIENT_TYPE, |
82b9bde8 JD |
552 | |
553 | .cb_ptr = &client_cb.parent, | |
3d1fc7fd MD |
554 | }; |
555 | ||
82b9bde8 | 556 | const struct lttng_ust_client_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_cb; |
c1fca457 | 557 | |
3d1fc7fd | 558 | static |
7dd08bec | 559 | struct lttng_channel *_channel_create(const char *name, |
a3f61e7f | 560 | void *buf_addr, |
3d1fc7fd MD |
561 | size_t subbuf_size, size_t num_subbuf, |
562 | unsigned int switch_timer_interval, | |
193183fb | 563 | unsigned int read_timer_interval, |
6ca18e66 | 564 | unsigned char *uuid, |
a9ff648c | 565 | uint32_t chan_id, |
5ea386c3 | 566 | const int *stream_fds, int nr_stream_fds) |
3d1fc7fd | 567 | { |
74d81a6c | 568 | struct lttng_channel chan_priv_init; |
a3f61e7f | 569 | struct lttng_ust_shm_handle *handle; |
74d81a6c MD |
570 | struct lttng_channel *lttng_chan; |
571 | void *priv; | |
a3f61e7f | 572 | |
74d81a6c MD |
573 | memset(&chan_priv_init, 0, sizeof(chan_priv_init)); |
574 | memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN); | |
6ca18e66 | 575 | chan_priv_init.id = chan_id; |
a3f61e7f | 576 | handle = channel_create(&client_config, name, |
8824f307 MD |
577 | &priv, __alignof__(struct lttng_channel), |
578 | sizeof(struct lttng_channel), | |
74d81a6c | 579 | &chan_priv_init, |
a3f61e7f | 580 | buf_addr, subbuf_size, num_subbuf, |
a9ff648c | 581 | switch_timer_interval, read_timer_interval, |
5ea386c3 | 582 | stream_fds, nr_stream_fds); |
a3f61e7f | 583 | if (!handle) |
afdf9825 | 584 | return NULL; |
7dd08bec MD |
585 | lttng_chan = priv; |
586 | lttng_chan->handle = handle; | |
74d81a6c | 587 | lttng_chan->chan = shmp(handle, handle->chan); |
7dd08bec | 588 | return lttng_chan; |
3d1fc7fd MD |
589 | } |
590 | ||
591 | static | |
74d81a6c | 592 | void lttng_channel_destroy(struct lttng_channel *chan) |
3d1fc7fd | 593 | { |
74d81a6c | 594 | channel_destroy(chan->chan, chan->handle, 1); |
3d1fc7fd MD |
595 | } |
596 | ||
597 | static | |
7dd08bec | 598 | int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
3d1fc7fd MD |
599 | uint32_t event_id) |
600 | { | |
7dd08bec | 601 | struct lttng_channel *lttng_chan = channel_get_private(ctx->chan); |
3d1fc7fd MD |
602 | int ret, cpu; |
603 | ||
604 | cpu = lib_ring_buffer_get_cpu(&client_config); | |
605 | if (cpu < 0) | |
606 | return -EPERM; | |
607 | ctx->cpu = cpu; | |
608 | ||
7dd08bec | 609 | switch (lttng_chan->header_type) { |
3d1fc7fd MD |
610 | case 1: /* compact */ |
611 | if (event_id > 30) | |
7dd08bec | 612 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
3d1fc7fd MD |
613 | break; |
614 | case 2: /* large */ | |
615 | if (event_id > 65534) | |
7dd08bec | 616 | ctx->rflags |= LTTNG_RFLAG_EXTENDED; |
3d1fc7fd MD |
617 | break; |
618 | default: | |
619 | WARN_ON_ONCE(1); | |
620 | } | |
621 | ||
622 | ret = lib_ring_buffer_reserve(&client_config, ctx); | |
623 | if (ret) | |
624 | goto put; | |
7dd08bec | 625 | lttng_write_event_header(&client_config, ctx, event_id); |
3d1fc7fd MD |
626 | return 0; |
627 | put: | |
628 | lib_ring_buffer_put_cpu(&client_config); | |
629 | return ret; | |
630 | } | |
631 | ||
632 | static | |
7dd08bec | 633 | void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
634 | { |
635 | lib_ring_buffer_commit(&client_config, ctx); | |
636 | lib_ring_buffer_put_cpu(&client_config); | |
637 | } | |
638 | ||
639 | static | |
7dd08bec | 640 | void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
3d1fc7fd MD |
641 | size_t len) |
642 | { | |
643 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
644 | } | |
645 | ||
a44c74d9 MD |
646 | static |
647 | void lttng_event_strcpy(struct lttng_ust_lib_ring_buffer_ctx *ctx, const char *src, | |
648 | size_t len) | |
649 | { | |
650 | lib_ring_buffer_strcpy(&client_config, ctx, src, len, '#'); | |
651 | } | |
652 | ||
9f3fdbc6 | 653 | #if 0 |
3d1fc7fd | 654 | static |
7dd08bec | 655 | wait_queue_head_t *lttng_get_reader_wait_queue(struct channel *chan) |
3d1fc7fd MD |
656 | { |
657 | return &chan->read_wait; | |
658 | } | |
659 | ||
660 | static | |
7dd08bec | 661 | wait_queue_head_t *lttng_get_hp_wait_queue(struct channel *chan) |
3d1fc7fd MD |
662 | { |
663 | return &chan->hp_wait; | |
664 | } | |
9f3fdbc6 | 665 | #endif //0 |
3d1fc7fd MD |
666 | |
667 | static | |
7dd08bec | 668 | int lttng_is_finalized(struct channel *chan) |
3d1fc7fd MD |
669 | { |
670 | return lib_ring_buffer_channel_is_finalized(chan); | |
671 | } | |
672 | ||
673 | static | |
7dd08bec | 674 | int lttng_is_disabled(struct channel *chan) |
3d1fc7fd MD |
675 | { |
676 | return lib_ring_buffer_channel_is_disabled(chan); | |
677 | } | |
678 | ||
43861eab | 679 | static |
7dd08bec | 680 | int lttng_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
43861eab | 681 | { |
4cfec15c | 682 | struct lttng_ust_lib_ring_buffer *buf; |
43861eab MD |
683 | int cpu; |
684 | ||
685 | for_each_channel_cpu(cpu, chan) { | |
74d81a6c MD |
686 | int shm_fd, wait_fd, wakeup_fd; |
687 | uint64_t memory_map_size; | |
43861eab MD |
688 | |
689 | buf = channel_get_ring_buffer(&client_config, chan, | |
690 | cpu, handle, &shm_fd, &wait_fd, | |
74d81a6c | 691 | &wakeup_fd, &memory_map_size); |
43861eab MD |
692 | lib_ring_buffer_switch(&client_config, buf, |
693 | SWITCH_ACTIVE, handle); | |
694 | } | |
695 | return 0; | |
696 | } | |
697 | ||
7dd08bec | 698 | static struct lttng_transport lttng_relay_transport = { |
818173b9 | 699 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
3d1fc7fd MD |
700 | .ops = { |
701 | .channel_create = _channel_create, | |
7dd08bec | 702 | .channel_destroy = lttng_channel_destroy, |
a44c74d9 | 703 | .u.has_strcpy = 1, |
7dd08bec MD |
704 | .event_reserve = lttng_event_reserve, |
705 | .event_commit = lttng_event_commit, | |
706 | .event_write = lttng_event_write, | |
3d1fc7fd | 707 | .packet_avail_size = NULL, /* Would be racy anyway */ |
7dd08bec MD |
708 | //.get_reader_wait_queue = lttng_get_reader_wait_queue, |
709 | //.get_hp_wait_queue = lttng_get_hp_wait_queue, | |
710 | .is_finalized = lttng_is_finalized, | |
711 | .is_disabled = lttng_is_disabled, | |
712 | .flush_buffer = lttng_flush_buffer, | |
a44c74d9 | 713 | .event_strcpy = lttng_event_strcpy, |
3d1fc7fd | 714 | }, |
74d81a6c | 715 | .client_config = &client_config, |
3d1fc7fd MD |
716 | }; |
717 | ||
edaa1431 | 718 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
3d1fc7fd | 719 | { |
34a91bdb MD |
720 | DBG("LTT : ltt ring buffer client \"%s\" init\n", |
721 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); | |
7dd08bec | 722 | lttng_transport_register(<tng_relay_transport); |
3d1fc7fd MD |
723 | } |
724 | ||
edaa1431 | 725 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
3d1fc7fd | 726 | { |
34a91bdb MD |
727 | DBG("LTT : ltt ring buffer client \"%s\" exit\n", |
728 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); | |
7dd08bec | 729 | lttng_transport_unregister(<tng_relay_transport); |
3d1fc7fd | 730 | } |