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