Commit | Line | Data |
---|---|---|
3d1fc7fd MD |
1 | /* |
2 | * ltt-ring-buffer-client.h | |
3 | * | |
4 | * Copyright (C) 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
5 | * | |
6 | * LTTng lib ring buffer client template. | |
7 | * | |
8 | * Dual LGPL v2.1/GPL v2 license. | |
9 | */ | |
10 | ||
9f3fdbc6 | 11 | #include <stdint.h> |
4318ae1b MD |
12 | #include <lttng/ust-events.h> |
13 | #include "lttng/bitfield.h" | |
b728d87e | 14 | #include "clock.h" |
eda498b8 | 15 | #include "lttng-ust-uuid.h" |
3d1fc7fd | 16 | #include "ltt-tracer.h" |
9f3fdbc6 | 17 | #include "../libringbuffer/frontend_types.h" |
3d1fc7fd | 18 | |
79dfbf42 MD |
19 | #define LTTNG_COMPACT_EVENT_BITS 5 |
20 | #define LTTNG_COMPACT_TSC_BITS 27 | |
21 | ||
3d1fc7fd MD |
22 | /* |
23 | * Keep the natural field alignment for _each field_ within this structure if | |
24 | * you ever add/remove a field from this header. Packed attribute is not used | |
25 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
26 | * let gcc add padding between the structure elements. | |
27 | */ | |
28 | ||
29 | struct packet_header { | |
30 | /* Trace packet header */ | |
31 | uint32_t magic; /* | |
32 | * Trace magic number. | |
33 | * contains endianness information. | |
34 | */ | |
19d8b1b3 | 35 | uint8_t uuid[LTTNG_UST_UUID_LEN]; |
3d1fc7fd MD |
36 | uint32_t stream_id; |
37 | ||
38 | struct { | |
39 | /* Stream packet context */ | |
40 | uint64_t timestamp_begin; /* Cycle count at subbuffer start */ | |
41 | uint64_t timestamp_end; /* Cycle count at subbuffer end */ | |
42 | uint32_t events_discarded; /* | |
43 | * Events lost in this subbuffer since | |
44 | * the beginning of the trace. | |
45 | * (may overflow) | |
46 | */ | |
47 | uint32_t content_size; /* Size of data in subbuffer */ | |
48 | uint32_t packet_size; /* Subbuffer size (include padding) */ | |
49 | uint32_t cpu_id; /* CPU id associated with stream */ | |
50 | uint8_t header_end; /* End of header */ | |
51 | } ctx; | |
52 | }; | |
53 | ||
54 | ||
23c8854a | 55 | static inline uint64_t lib_ring_buffer_clock_read(struct channel *chan) |
3d1fc7fd MD |
56 | { |
57 | return trace_clock_read64(); | |
58 | } | |
59 | ||
60 | static inline | |
61 | size_t ctx_get_size(size_t offset, struct lttng_ctx *ctx) | |
62 | { | |
63 | int i; | |
64 | size_t orig_offset = offset; | |
65 | ||
b5a3dfa5 | 66 | if (caa_likely(!ctx)) |
3d1fc7fd MD |
67 | return 0; |
68 | for (i = 0; i < ctx->nr_fields; i++) | |
69 | offset += ctx->fields[i].get_size(offset); | |
70 | return offset - orig_offset; | |
71 | } | |
72 | ||
73 | static inline | |
4cfec15c | 74 | void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx, |
3d1fc7fd MD |
75 | struct ltt_channel *chan, |
76 | struct lttng_ctx *ctx) | |
77 | { | |
78 | int i; | |
79 | ||
b5a3dfa5 | 80 | if (caa_likely(!ctx)) |
3d1fc7fd MD |
81 | return; |
82 | for (i = 0; i < ctx->nr_fields; i++) | |
83 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); | |
84 | } | |
85 | ||
86 | /* | |
87 | * record_header_size - Calculate the header size and padding necessary. | |
88 | * @config: ring buffer instance configuration | |
89 | * @chan: channel | |
90 | * @offset: offset in the write buffer | |
91 | * @pre_header_padding: padding to add before the header (output) | |
92 | * @ctx: reservation context | |
93 | * | |
94 | * Returns the event header size (including padding). | |
95 | * | |
96 | * The payload must itself determine its own alignment from the biggest type it | |
97 | * contains. | |
98 | */ | |
99 | static __inline__ | |
4cfec15c | 100 | unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
101 | struct channel *chan, size_t offset, |
102 | size_t *pre_header_padding, | |
4cfec15c | 103 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
104 | { |
105 | struct ltt_channel *ltt_chan = channel_get_private(chan); | |
106 | struct ltt_event *event = ctx->priv; | |
107 | size_t orig_offset = offset; | |
108 | size_t padding; | |
109 | ||
110 | switch (ltt_chan->header_type) { | |
111 | case 1: /* compact */ | |
1dbfff0c | 112 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
113 | offset += padding; |
114 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
115 | offset += sizeof(uint32_t); /* id and timestamp */ | |
116 | } else { | |
79dfbf42 MD |
117 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
118 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; | |
3d1fc7fd | 119 | /* Align extended struct on largest member */ |
1dbfff0c | 120 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd | 121 | offset += sizeof(uint32_t); /* id */ |
1dbfff0c | 122 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
123 | offset += sizeof(uint64_t); /* timestamp */ |
124 | } | |
125 | break; | |
126 | case 2: /* large */ | |
1dbfff0c | 127 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
3d1fc7fd MD |
128 | offset += padding; |
129 | offset += sizeof(uint16_t); | |
130 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
1dbfff0c | 131 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
132 | offset += sizeof(uint32_t); /* timestamp */ |
133 | } else { | |
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 | default: | |
9f3fdbc6 | 142 | padding = 0; |
3d1fc7fd MD |
143 | WARN_ON_ONCE(1); |
144 | } | |
145 | offset += ctx_get_size(offset, event->ctx); | |
146 | offset += ctx_get_size(offset, ltt_chan->ctx); | |
147 | ||
148 | *pre_header_padding = padding; | |
149 | return offset - orig_offset; | |
150 | } | |
151 | ||
9f3fdbc6 | 152 | #include "../libringbuffer/api.h" |
3d1fc7fd | 153 | |
9f3fdbc6 | 154 | static |
4cfec15c MD |
155 | void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
156 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
3d1fc7fd MD |
157 | uint32_t event_id); |
158 | ||
159 | /* | |
160 | * ltt_write_event_header | |
161 | * | |
162 | * Writes the event header to the offset (already aligned on 32-bits). | |
163 | * | |
164 | * @config: ring buffer instance configuration | |
165 | * @ctx: reservation context | |
166 | * @event_id: event ID | |
167 | */ | |
168 | static __inline__ | |
4cfec15c MD |
169 | void ltt_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config, |
170 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
3d1fc7fd MD |
171 | uint32_t event_id) |
172 | { | |
173 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); | |
174 | struct ltt_event *event = ctx->priv; | |
175 | ||
b5a3dfa5 | 176 | if (caa_unlikely(ctx->rflags)) |
3d1fc7fd MD |
177 | goto slow_path; |
178 | ||
179 | switch (ltt_chan->header_type) { | |
180 | case 1: /* compact */ | |
181 | { | |
182 | uint32_t id_time = 0; | |
183 | ||
79dfbf42 MD |
184 | bt_bitfield_write(&id_time, uint32_t, |
185 | 0, | |
186 | LTTNG_COMPACT_EVENT_BITS, | |
187 | event_id); | |
188 | bt_bitfield_write(&id_time, uint32_t, | |
189 | LTTNG_COMPACT_EVENT_BITS, | |
190 | LTTNG_COMPACT_TSC_BITS, | |
191 | ctx->tsc); | |
3d1fc7fd MD |
192 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
193 | break; | |
194 | } | |
195 | case 2: /* large */ | |
196 | { | |
197 | uint32_t timestamp = (uint32_t) ctx->tsc; | |
198 | uint16_t id = event_id; | |
199 | ||
200 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
1dbfff0c | 201 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
202 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
203 | break; | |
204 | } | |
205 | default: | |
206 | WARN_ON_ONCE(1); | |
207 | } | |
208 | ||
3d1fc7fd | 209 | ctx_record(ctx, ltt_chan, ltt_chan->ctx); |
1ea172a1 | 210 | ctx_record(ctx, ltt_chan, event->ctx); |
d3b2c3e0 | 211 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
212 | |
213 | return; | |
214 | ||
215 | slow_path: | |
216 | ltt_write_event_header_slow(config, ctx, event_id); | |
217 | } | |
218 | ||
9f3fdbc6 | 219 | static |
4cfec15c MD |
220 | void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
221 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
3d1fc7fd MD |
222 | uint32_t event_id) |
223 | { | |
224 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); | |
225 | struct ltt_event *event = ctx->priv; | |
226 | ||
227 | switch (ltt_chan->header_type) { | |
228 | case 1: /* compact */ | |
229 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
230 | uint32_t id_time = 0; | |
231 | ||
79dfbf42 MD |
232 | bt_bitfield_write(&id_time, uint32_t, |
233 | 0, | |
234 | LTTNG_COMPACT_EVENT_BITS, | |
235 | event_id); | |
236 | bt_bitfield_write(&id_time, uint32_t, | |
237 | LTTNG_COMPACT_EVENT_BITS, | |
238 | LTTNG_COMPACT_TSC_BITS, | |
239 | ctx->tsc); | |
3d1fc7fd MD |
240 | lib_ring_buffer_write(config, ctx, &id_time, sizeof(id_time)); |
241 | } else { | |
242 | uint8_t id = 0; | |
243 | uint64_t timestamp = ctx->tsc; | |
244 | ||
79dfbf42 MD |
245 | bt_bitfield_write(&id, uint8_t, |
246 | 0, | |
247 | LTTNG_COMPACT_EVENT_BITS, | |
248 | 31); | |
3d1fc7fd MD |
249 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); |
250 | /* Align extended struct on largest member */ | |
1dbfff0c | 251 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd | 252 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
1dbfff0c | 253 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
254 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
255 | } | |
256 | break; | |
257 | case 2: /* large */ | |
258 | { | |
259 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
260 | uint32_t timestamp = (uint32_t) ctx->tsc; | |
261 | uint16_t id = event_id; | |
262 | ||
263 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
1dbfff0c | 264 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
265 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
266 | } else { | |
267 | uint16_t id = 65535; | |
268 | uint64_t timestamp = ctx->tsc; | |
269 | ||
270 | lib_ring_buffer_write(config, ctx, &id, sizeof(id)); | |
271 | /* Align extended struct on largest member */ | |
1dbfff0c | 272 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd | 273 | lib_ring_buffer_write(config, ctx, &event_id, sizeof(event_id)); |
1dbfff0c | 274 | lib_ring_buffer_align_ctx(ctx, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
275 | lib_ring_buffer_write(config, ctx, ×tamp, sizeof(timestamp)); |
276 | } | |
277 | break; | |
278 | } | |
279 | default: | |
280 | WARN_ON_ONCE(1); | |
281 | } | |
3d1fc7fd | 282 | ctx_record(ctx, ltt_chan, ltt_chan->ctx); |
1ea172a1 | 283 | ctx_record(ctx, ltt_chan, event->ctx); |
d3b2c3e0 | 284 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
285 | } |
286 | ||
4cfec15c | 287 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
3d1fc7fd | 288 | |
23c8854a | 289 | static uint64_t client_ring_buffer_clock_read(struct channel *chan) |
3d1fc7fd MD |
290 | { |
291 | return lib_ring_buffer_clock_read(chan); | |
292 | } | |
293 | ||
294 | static | |
4cfec15c | 295 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
296 | struct channel *chan, size_t offset, |
297 | size_t *pre_header_padding, | |
4cfec15c | 298 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
299 | { |
300 | return record_header_size(config, chan, offset, | |
301 | pre_header_padding, ctx); | |
302 | } | |
303 | ||
304 | /** | |
305 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
306 | * | |
307 | * Return header size without padding after the structure. Don't use packed | |
308 | * structure because gcc generates inefficient code on some architectures | |
309 | * (powerpc, mips..) | |
310 | */ | |
311 | static size_t client_packet_header_size(void) | |
312 | { | |
313 | return offsetof(struct packet_header, ctx.header_end); | |
314 | } | |
315 | ||
23c8854a | 316 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 317 | unsigned int subbuf_idx, |
38fae1d3 | 318 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 319 | { |
1d498196 | 320 | struct channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
321 | struct packet_header *header = |
322 | (struct packet_header *) | |
323 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
324 | subbuf_idx * chan->backend.subbuf_size, |
325 | handle); | |
3d1fc7fd | 326 | struct ltt_channel *ltt_chan = channel_get_private(chan); |
3d1fc7fd MD |
327 | |
328 | header->magic = CTF_MAGIC_NUMBER; | |
a3f61e7f | 329 | memcpy(header->uuid, ltt_chan->uuid, sizeof(ltt_chan->uuid)); |
3d1fc7fd MD |
330 | header->stream_id = ltt_chan->id; |
331 | header->ctx.timestamp_begin = tsc; | |
332 | header->ctx.timestamp_end = 0; | |
333 | header->ctx.events_discarded = 0; | |
334 | header->ctx.content_size = 0xFFFFFFFF; /* for debugging */ | |
335 | header->ctx.packet_size = 0xFFFFFFFF; | |
336 | header->ctx.cpu_id = buf->backend.cpu; | |
337 | } | |
338 | ||
339 | /* | |
340 | * offset is assumed to never be 0 here : never deliver a completely empty | |
341 | * subbuffer. data_size is between 1 and subbuf_size. | |
342 | */ | |
23c8854a | 343 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 344 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 345 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 346 | { |
1d498196 | 347 | struct channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
348 | struct packet_header *header = |
349 | (struct packet_header *) | |
350 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
351 | subbuf_idx * chan->backend.subbuf_size, |
352 | handle); | |
3d1fc7fd MD |
353 | unsigned long records_lost = 0; |
354 | ||
355 | header->ctx.timestamp_end = tsc; | |
356 | header->ctx.content_size = data_size * CHAR_BIT; /* in bits */ | |
357 | header->ctx.packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
9dcb02ef MD |
358 | /* |
359 | * We do not care about the records lost count, because the metadata | |
360 | * channel waits and retry. | |
361 | */ | |
362 | (void) lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
3d1fc7fd MD |
363 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
364 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
365 | header->ctx.events_discarded = records_lost; | |
366 | } | |
367 | ||
4cfec15c | 368 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
38fae1d3 | 369 | int cpu, const char *name, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
370 | { |
371 | return 0; | |
372 | } | |
373 | ||
4cfec15c | 374 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
375 | { |
376 | } | |
377 | ||
4cfec15c | 378 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
3d1fc7fd MD |
379 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
380 | .cb.record_header_size = client_record_header_size, | |
381 | .cb.subbuffer_header_size = client_packet_header_size, | |
382 | .cb.buffer_begin = client_buffer_begin, | |
383 | .cb.buffer_end = client_buffer_end, | |
384 | .cb.buffer_create = client_buffer_create, | |
385 | .cb.buffer_finalize = client_buffer_finalize, | |
386 | ||
79dfbf42 | 387 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
3d1fc7fd | 388 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
5d61a504 | 389 | .sync = RING_BUFFER_SYNC_GLOBAL, |
3d1fc7fd MD |
390 | .mode = RING_BUFFER_MODE_TEMPLATE, |
391 | .backend = RING_BUFFER_PAGE, | |
5d61a504 | 392 | .output = RING_BUFFER_MMAP, |
3d1fc7fd | 393 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
5d61a504 MD |
394 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
395 | .wakeup = RING_BUFFER_WAKEUP_BY_WRITER, | |
c1fca457 | 396 | .client_type = LTTNG_CLIENT_TYPE, |
3d1fc7fd MD |
397 | }; |
398 | ||
7a784989 | 399 | const struct lttng_ust_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_config.cb; |
c1fca457 | 400 | |
3d1fc7fd | 401 | static |
1d498196 | 402 | struct ltt_channel *_channel_create(const char *name, |
a3f61e7f | 403 | void *buf_addr, |
3d1fc7fd MD |
404 | size_t subbuf_size, size_t num_subbuf, |
405 | unsigned int switch_timer_interval, | |
193183fb | 406 | unsigned int read_timer_interval, |
ef9ff354 MD |
407 | int **shm_fd, int **wait_fd, |
408 | uint64_t **memory_map_size, | |
d028eddb | 409 | struct ltt_channel *chan_priv_init) |
3d1fc7fd | 410 | { |
a3f61e7f MD |
411 | void *priv; |
412 | struct ltt_channel *ltt_chan = NULL; | |
413 | struct lttng_ust_shm_handle *handle; | |
414 | ||
415 | handle = channel_create(&client_config, name, | |
416 | &priv, __alignof__(*ltt_chan), sizeof(*ltt_chan), | |
d028eddb | 417 | chan_priv_init, |
a3f61e7f MD |
418 | buf_addr, subbuf_size, num_subbuf, |
419 | switch_timer_interval, read_timer_interval, | |
420 | shm_fd, wait_fd, memory_map_size); | |
421 | if (!handle) | |
afdf9825 | 422 | return NULL; |
a3f61e7f MD |
423 | ltt_chan = priv; |
424 | ltt_chan->handle = handle; | |
dc613eb9 | 425 | ltt_chan->chan = shmp(ltt_chan->handle, ltt_chan->handle->chan); |
1d498196 | 426 | return ltt_chan; |
3d1fc7fd MD |
427 | } |
428 | ||
429 | static | |
1d498196 | 430 | void ltt_channel_destroy(struct ltt_channel *ltt_chan) |
3d1fc7fd | 431 | { |
824f40b8 | 432 | channel_destroy(ltt_chan->chan, ltt_chan->handle, 0); |
3d1fc7fd MD |
433 | } |
434 | ||
435 | static | |
4cfec15c | 436 | struct lttng_ust_lib_ring_buffer *ltt_buffer_read_open(struct channel *chan, |
38fae1d3 | 437 | struct lttng_ust_shm_handle *handle, |
ef9ff354 MD |
438 | int **shm_fd, int **wait_fd, |
439 | uint64_t **memory_map_size) | |
3d1fc7fd | 440 | { |
4cfec15c | 441 | struct lttng_ust_lib_ring_buffer *buf; |
3d1fc7fd MD |
442 | int cpu; |
443 | ||
444 | for_each_channel_cpu(cpu, chan) { | |
381c0f1e MD |
445 | buf = channel_get_ring_buffer(&client_config, chan, |
446 | cpu, handle, shm_fd, wait_fd, | |
447 | memory_map_size); | |
824f40b8 | 448 | if (!lib_ring_buffer_open_read(buf, handle, 0)) |
3d1fc7fd MD |
449 | return buf; |
450 | } | |
451 | return NULL; | |
452 | } | |
453 | ||
454 | static | |
4cfec15c | 455 | void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 456 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 457 | { |
824f40b8 | 458 | lib_ring_buffer_release_read(buf, handle, 0); |
3d1fc7fd MD |
459 | } |
460 | ||
461 | static | |
4cfec15c | 462 | int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
3d1fc7fd MD |
463 | uint32_t event_id) |
464 | { | |
465 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); | |
466 | int ret, cpu; | |
467 | ||
468 | cpu = lib_ring_buffer_get_cpu(&client_config); | |
469 | if (cpu < 0) | |
470 | return -EPERM; | |
471 | ctx->cpu = cpu; | |
472 | ||
473 | switch (ltt_chan->header_type) { | |
474 | case 1: /* compact */ | |
475 | if (event_id > 30) | |
476 | ctx->rflags |= LTT_RFLAG_EXTENDED; | |
477 | break; | |
478 | case 2: /* large */ | |
479 | if (event_id > 65534) | |
480 | ctx->rflags |= LTT_RFLAG_EXTENDED; | |
481 | break; | |
482 | default: | |
483 | WARN_ON_ONCE(1); | |
484 | } | |
485 | ||
486 | ret = lib_ring_buffer_reserve(&client_config, ctx); | |
487 | if (ret) | |
488 | goto put; | |
489 | ltt_write_event_header(&client_config, ctx, event_id); | |
490 | return 0; | |
491 | put: | |
492 | lib_ring_buffer_put_cpu(&client_config); | |
493 | return ret; | |
494 | } | |
495 | ||
496 | static | |
4cfec15c | 497 | void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
498 | { |
499 | lib_ring_buffer_commit(&client_config, ctx); | |
500 | lib_ring_buffer_put_cpu(&client_config); | |
501 | } | |
502 | ||
503 | static | |
4cfec15c | 504 | void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
3d1fc7fd MD |
505 | size_t len) |
506 | { | |
507 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
508 | } | |
509 | ||
9f3fdbc6 | 510 | #if 0 |
3d1fc7fd MD |
511 | static |
512 | wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan) | |
513 | { | |
514 | return &chan->read_wait; | |
515 | } | |
516 | ||
517 | static | |
518 | wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan) | |
519 | { | |
520 | return &chan->hp_wait; | |
521 | } | |
9f3fdbc6 | 522 | #endif //0 |
3d1fc7fd MD |
523 | |
524 | static | |
525 | int ltt_is_finalized(struct channel *chan) | |
526 | { | |
527 | return lib_ring_buffer_channel_is_finalized(chan); | |
528 | } | |
529 | ||
530 | static | |
531 | int ltt_is_disabled(struct channel *chan) | |
532 | { | |
533 | return lib_ring_buffer_channel_is_disabled(chan); | |
534 | } | |
535 | ||
43861eab | 536 | static |
38fae1d3 | 537 | int ltt_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
43861eab | 538 | { |
4cfec15c | 539 | struct lttng_ust_lib_ring_buffer *buf; |
43861eab MD |
540 | int cpu; |
541 | ||
542 | for_each_channel_cpu(cpu, chan) { | |
ef9ff354 MD |
543 | int *shm_fd, *wait_fd; |
544 | uint64_t *memory_map_size; | |
43861eab MD |
545 | |
546 | buf = channel_get_ring_buffer(&client_config, chan, | |
547 | cpu, handle, &shm_fd, &wait_fd, | |
548 | &memory_map_size); | |
549 | lib_ring_buffer_switch(&client_config, buf, | |
550 | SWITCH_ACTIVE, handle); | |
551 | } | |
552 | return 0; | |
553 | } | |
554 | ||
3d1fc7fd | 555 | static struct ltt_transport ltt_relay_transport = { |
818173b9 | 556 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
3d1fc7fd MD |
557 | .ops = { |
558 | .channel_create = _channel_create, | |
559 | .channel_destroy = ltt_channel_destroy, | |
560 | .buffer_read_open = ltt_buffer_read_open, | |
561 | .buffer_read_close = ltt_buffer_read_close, | |
562 | .event_reserve = ltt_event_reserve, | |
563 | .event_commit = ltt_event_commit, | |
564 | .event_write = ltt_event_write, | |
565 | .packet_avail_size = NULL, /* Would be racy anyway */ | |
9f3fdbc6 MD |
566 | //.get_reader_wait_queue = ltt_get_reader_wait_queue, |
567 | //.get_hp_wait_queue = ltt_get_hp_wait_queue, | |
3d1fc7fd MD |
568 | .is_finalized = ltt_is_finalized, |
569 | .is_disabled = ltt_is_disabled, | |
43d330a4 | 570 | .flush_buffer = ltt_flush_buffer, |
3d1fc7fd MD |
571 | }, |
572 | }; | |
573 | ||
edaa1431 | 574 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
3d1fc7fd | 575 | { |
18c1cd87 | 576 | DBG("LTT : ltt ring buffer client init\n"); |
3d1fc7fd | 577 | ltt_transport_register(<t_relay_transport); |
3d1fc7fd MD |
578 | } |
579 | ||
edaa1431 | 580 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
3d1fc7fd | 581 | { |
18c1cd87 | 582 | DBG("LTT : ltt ring buffer client exit\n"); |
3d1fc7fd MD |
583 | ltt_transport_unregister(<t_relay_transport); |
584 | } |