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