Commit | Line | Data |
---|---|---|
3d1fc7fd MD |
1 | /* |
2 | * ltt-ring-buffer-client.h | |
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" |
eda498b8 | 27 | #include "lttng-ust-uuid.h" |
3d1fc7fd | 28 | #include "ltt-tracer.h" |
9f3fdbc6 | 29 | #include "../libringbuffer/frontend_types.h" |
3d1fc7fd | 30 | |
79dfbf42 MD |
31 | #define LTTNG_COMPACT_EVENT_BITS 5 |
32 | #define LTTNG_COMPACT_TSC_BITS 27 | |
33 | ||
3d1fc7fd MD |
34 | /* |
35 | * Keep the natural field alignment for _each field_ within this structure if | |
36 | * you ever add/remove a field from this header. Packed attribute is not used | |
37 | * because gcc generates poor code on at least powerpc and mips. Don't ever | |
38 | * let gcc add padding between the structure elements. | |
39 | */ | |
40 | ||
41 | struct packet_header { | |
42 | /* Trace packet header */ | |
43 | uint32_t magic; /* | |
44 | * Trace magic number. | |
45 | * contains endianness information. | |
46 | */ | |
19d8b1b3 | 47 | uint8_t uuid[LTTNG_UST_UUID_LEN]; |
3d1fc7fd MD |
48 | uint32_t stream_id; |
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 */ | |
18fc8d71 | 54 | unsigned long events_discarded; /* |
3d1fc7fd MD |
55 | * Events lost in this subbuffer since |
56 | * the beginning of the trace. | |
57 | * (may overflow) | |
58 | */ | |
59 | uint32_t content_size; /* Size of data in subbuffer */ | |
60 | uint32_t packet_size; /* Subbuffer size (include padding) */ | |
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 MD |
79 | return 0; |
80 | for (i = 0; i < ctx->nr_fields; i++) | |
81 | offset += ctx->fields[i].get_size(offset); | |
82 | return offset - orig_offset; | |
83 | } | |
84 | ||
85 | static inline | |
4cfec15c | 86 | void ctx_record(struct lttng_ust_lib_ring_buffer_ctx *bufctx, |
3d1fc7fd MD |
87 | struct ltt_channel *chan, |
88 | struct lttng_ctx *ctx) | |
89 | { | |
90 | int i; | |
91 | ||
b5a3dfa5 | 92 | if (caa_likely(!ctx)) |
3d1fc7fd MD |
93 | return; |
94 | for (i = 0; i < ctx->nr_fields; i++) | |
95 | ctx->fields[i].record(&ctx->fields[i], bufctx, chan); | |
96 | } | |
97 | ||
98 | /* | |
99 | * record_header_size - Calculate the header size and padding necessary. | |
100 | * @config: ring buffer instance configuration | |
101 | * @chan: channel | |
102 | * @offset: offset in the write buffer | |
103 | * @pre_header_padding: padding to add before the header (output) | |
104 | * @ctx: reservation context | |
105 | * | |
106 | * Returns the event header size (including padding). | |
107 | * | |
108 | * The payload must itself determine its own alignment from the biggest type it | |
109 | * contains. | |
110 | */ | |
111 | static __inline__ | |
4cfec15c | 112 | unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
113 | struct channel *chan, size_t offset, |
114 | size_t *pre_header_padding, | |
4cfec15c | 115 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
116 | { |
117 | struct ltt_channel *ltt_chan = channel_get_private(chan); | |
118 | struct ltt_event *event = ctx->priv; | |
119 | size_t orig_offset = offset; | |
120 | size_t padding; | |
121 | ||
122 | switch (ltt_chan->header_type) { | |
123 | case 1: /* compact */ | |
1dbfff0c | 124 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
125 | offset += padding; |
126 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
127 | offset += sizeof(uint32_t); /* id and timestamp */ | |
128 | } else { | |
79dfbf42 MD |
129 | /* Minimum space taken by LTTNG_COMPACT_EVENT_BITS id */ |
130 | offset += (LTTNG_COMPACT_EVENT_BITS + CHAR_BIT - 1) / CHAR_BIT; | |
3d1fc7fd | 131 | /* Align extended struct on largest member */ |
1dbfff0c | 132 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd | 133 | offset += sizeof(uint32_t); /* id */ |
1dbfff0c | 134 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
135 | offset += sizeof(uint64_t); /* timestamp */ |
136 | } | |
137 | break; | |
138 | case 2: /* large */ | |
1dbfff0c | 139 | padding = lib_ring_buffer_align(offset, lttng_alignof(uint16_t)); |
3d1fc7fd MD |
140 | offset += padding; |
141 | offset += sizeof(uint16_t); | |
142 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
1dbfff0c | 143 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint32_t)); |
3d1fc7fd MD |
144 | offset += sizeof(uint32_t); /* timestamp */ |
145 | } else { | |
146 | /* Align extended struct on largest member */ | |
1dbfff0c | 147 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd | 148 | offset += sizeof(uint32_t); /* id */ |
1dbfff0c | 149 | offset += lib_ring_buffer_align(offset, lttng_alignof(uint64_t)); |
3d1fc7fd MD |
150 | offset += sizeof(uint64_t); /* timestamp */ |
151 | } | |
152 | break; | |
153 | default: | |
9f3fdbc6 | 154 | padding = 0; |
3d1fc7fd MD |
155 | WARN_ON_ONCE(1); |
156 | } | |
157 | offset += ctx_get_size(offset, event->ctx); | |
158 | offset += ctx_get_size(offset, ltt_chan->ctx); | |
159 | ||
160 | *pre_header_padding = padding; | |
161 | return offset - orig_offset; | |
162 | } | |
163 | ||
9f3fdbc6 | 164 | #include "../libringbuffer/api.h" |
3d1fc7fd | 165 | |
9f3fdbc6 | 166 | static |
4cfec15c MD |
167 | void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
168 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
3d1fc7fd MD |
169 | uint32_t event_id); |
170 | ||
171 | /* | |
172 | * ltt_write_event_header | |
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__ | |
4cfec15c MD |
181 | void ltt_write_event_header(const struct lttng_ust_lib_ring_buffer_config *config, |
182 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
3d1fc7fd MD |
183 | uint32_t event_id) |
184 | { | |
185 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); | |
186 | struct ltt_event *event = ctx->priv; | |
187 | ||
b5a3dfa5 | 188 | if (caa_unlikely(ctx->rflags)) |
3d1fc7fd MD |
189 | goto slow_path; |
190 | ||
191 | switch (ltt_chan->header_type) { | |
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 | ||
3d1fc7fd | 221 | ctx_record(ctx, ltt_chan, ltt_chan->ctx); |
1ea172a1 | 222 | ctx_record(ctx, ltt_chan, event->ctx); |
d3b2c3e0 | 223 | lib_ring_buffer_align_ctx(ctx, ctx->largest_align); |
3d1fc7fd MD |
224 | |
225 | return; | |
226 | ||
227 | slow_path: | |
228 | ltt_write_event_header_slow(config, ctx, event_id); | |
229 | } | |
230 | ||
9f3fdbc6 | 231 | static |
4cfec15c MD |
232 | void ltt_write_event_header_slow(const struct lttng_ust_lib_ring_buffer_config *config, |
233 | struct lttng_ust_lib_ring_buffer_ctx *ctx, | |
3d1fc7fd MD |
234 | uint32_t event_id) |
235 | { | |
236 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); | |
237 | struct ltt_event *event = ctx->priv; | |
238 | ||
239 | switch (ltt_chan->header_type) { | |
240 | case 1: /* compact */ | |
241 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
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 | { | |
271 | if (!(ctx->rflags & (RING_BUFFER_RFLAG_FULL_TSC | LTT_RFLAG_EXTENDED))) { | |
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 | } | |
3d1fc7fd | 294 | ctx_record(ctx, ltt_chan, ltt_chan->ctx); |
1ea172a1 | 295 | ctx_record(ctx, ltt_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); | |
3d1fc7fd | 338 | struct ltt_channel *ltt_chan = channel_get_private(chan); |
3d1fc7fd MD |
339 | |
340 | header->magic = CTF_MAGIC_NUMBER; | |
a3f61e7f | 341 | memcpy(header->uuid, ltt_chan->uuid, sizeof(ltt_chan->uuid)); |
3d1fc7fd MD |
342 | header->stream_id = ltt_chan->id; |
343 | header->ctx.timestamp_begin = tsc; | |
344 | header->ctx.timestamp_end = 0; | |
345 | header->ctx.events_discarded = 0; | |
346 | header->ctx.content_size = 0xFFFFFFFF; /* for debugging */ | |
347 | header->ctx.packet_size = 0xFFFFFFFF; | |
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; | |
368 | header->ctx.content_size = data_size * CHAR_BIT; /* in bits */ | |
369 | header->ctx.packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
04489ab4 MD |
370 | |
371 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
3d1fc7fd MD |
372 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
373 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
374 | header->ctx.events_discarded = records_lost; | |
375 | } | |
376 | ||
4cfec15c | 377 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
38fae1d3 | 378 | int cpu, const char *name, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
379 | { |
380 | return 0; | |
381 | } | |
382 | ||
4cfec15c | 383 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, void *priv, int cpu, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
384 | { |
385 | } | |
386 | ||
4cfec15c | 387 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
3d1fc7fd MD |
388 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
389 | .cb.record_header_size = client_record_header_size, | |
390 | .cb.subbuffer_header_size = client_packet_header_size, | |
391 | .cb.buffer_begin = client_buffer_begin, | |
392 | .cb.buffer_end = client_buffer_end, | |
393 | .cb.buffer_create = client_buffer_create, | |
394 | .cb.buffer_finalize = client_buffer_finalize, | |
395 | ||
79dfbf42 | 396 | .tsc_bits = LTTNG_COMPACT_TSC_BITS, |
3d1fc7fd | 397 | .alloc = RING_BUFFER_ALLOC_PER_CPU, |
5d61a504 | 398 | .sync = RING_BUFFER_SYNC_GLOBAL, |
3d1fc7fd MD |
399 | .mode = RING_BUFFER_MODE_TEMPLATE, |
400 | .backend = RING_BUFFER_PAGE, | |
5d61a504 | 401 | .output = RING_BUFFER_MMAP, |
3d1fc7fd | 402 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
5d61a504 MD |
403 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
404 | .wakeup = RING_BUFFER_WAKEUP_BY_WRITER, | |
c1fca457 | 405 | .client_type = LTTNG_CLIENT_TYPE, |
3d1fc7fd MD |
406 | }; |
407 | ||
7a784989 | 408 | const struct lttng_ust_lib_ring_buffer_client_cb *LTTNG_CLIENT_CALLBACKS = &client_config.cb; |
c1fca457 | 409 | |
3d1fc7fd | 410 | static |
1d498196 | 411 | struct ltt_channel *_channel_create(const char *name, |
a3f61e7f | 412 | void *buf_addr, |
3d1fc7fd MD |
413 | size_t subbuf_size, size_t num_subbuf, |
414 | unsigned int switch_timer_interval, | |
193183fb | 415 | unsigned int read_timer_interval, |
ef9ff354 MD |
416 | int **shm_fd, int **wait_fd, |
417 | uint64_t **memory_map_size, | |
d028eddb | 418 | struct ltt_channel *chan_priv_init) |
3d1fc7fd | 419 | { |
a3f61e7f MD |
420 | void *priv; |
421 | struct ltt_channel *ltt_chan = NULL; | |
422 | struct lttng_ust_shm_handle *handle; | |
423 | ||
424 | handle = channel_create(&client_config, name, | |
425 | &priv, __alignof__(*ltt_chan), sizeof(*ltt_chan), | |
d028eddb | 426 | chan_priv_init, |
a3f61e7f MD |
427 | buf_addr, subbuf_size, num_subbuf, |
428 | switch_timer_interval, read_timer_interval, | |
429 | shm_fd, wait_fd, memory_map_size); | |
430 | if (!handle) | |
afdf9825 | 431 | return NULL; |
a3f61e7f MD |
432 | ltt_chan = priv; |
433 | ltt_chan->handle = handle; | |
dc613eb9 | 434 | ltt_chan->chan = shmp(ltt_chan->handle, ltt_chan->handle->chan); |
1d498196 | 435 | return ltt_chan; |
3d1fc7fd MD |
436 | } |
437 | ||
438 | static | |
1d498196 | 439 | void ltt_channel_destroy(struct ltt_channel *ltt_chan) |
3d1fc7fd | 440 | { |
824f40b8 | 441 | channel_destroy(ltt_chan->chan, ltt_chan->handle, 0); |
3d1fc7fd MD |
442 | } |
443 | ||
444 | static | |
4cfec15c | 445 | struct lttng_ust_lib_ring_buffer *ltt_buffer_read_open(struct channel *chan, |
38fae1d3 | 446 | struct lttng_ust_shm_handle *handle, |
ef9ff354 MD |
447 | int **shm_fd, int **wait_fd, |
448 | uint64_t **memory_map_size) | |
3d1fc7fd | 449 | { |
4cfec15c | 450 | struct lttng_ust_lib_ring_buffer *buf; |
3d1fc7fd MD |
451 | int cpu; |
452 | ||
453 | for_each_channel_cpu(cpu, chan) { | |
381c0f1e MD |
454 | buf = channel_get_ring_buffer(&client_config, chan, |
455 | cpu, handle, shm_fd, wait_fd, | |
456 | memory_map_size); | |
824f40b8 | 457 | if (!lib_ring_buffer_open_read(buf, handle, 0)) |
3d1fc7fd MD |
458 | return buf; |
459 | } | |
460 | return NULL; | |
461 | } | |
462 | ||
463 | static | |
4cfec15c | 464 | void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 465 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 466 | { |
824f40b8 | 467 | lib_ring_buffer_release_read(buf, handle, 0); |
3d1fc7fd MD |
468 | } |
469 | ||
470 | static | |
4cfec15c | 471 | int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
3d1fc7fd MD |
472 | uint32_t event_id) |
473 | { | |
474 | struct ltt_channel *ltt_chan = channel_get_private(ctx->chan); | |
475 | int ret, cpu; | |
476 | ||
477 | cpu = lib_ring_buffer_get_cpu(&client_config); | |
478 | if (cpu < 0) | |
479 | return -EPERM; | |
480 | ctx->cpu = cpu; | |
481 | ||
482 | switch (ltt_chan->header_type) { | |
483 | case 1: /* compact */ | |
484 | if (event_id > 30) | |
485 | ctx->rflags |= LTT_RFLAG_EXTENDED; | |
486 | break; | |
487 | case 2: /* large */ | |
488 | if (event_id > 65534) | |
489 | ctx->rflags |= LTT_RFLAG_EXTENDED; | |
490 | break; | |
491 | default: | |
492 | WARN_ON_ONCE(1); | |
493 | } | |
494 | ||
495 | ret = lib_ring_buffer_reserve(&client_config, ctx); | |
496 | if (ret) | |
497 | goto put; | |
498 | ltt_write_event_header(&client_config, ctx, event_id); | |
499 | return 0; | |
500 | put: | |
501 | lib_ring_buffer_put_cpu(&client_config); | |
502 | return ret; | |
503 | } | |
504 | ||
505 | static | |
4cfec15c | 506 | void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
507 | { |
508 | lib_ring_buffer_commit(&client_config, ctx); | |
509 | lib_ring_buffer_put_cpu(&client_config); | |
510 | } | |
511 | ||
512 | static | |
4cfec15c | 513 | void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
3d1fc7fd MD |
514 | size_t len) |
515 | { | |
516 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
517 | } | |
518 | ||
9f3fdbc6 | 519 | #if 0 |
3d1fc7fd MD |
520 | static |
521 | wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan) | |
522 | { | |
523 | return &chan->read_wait; | |
524 | } | |
525 | ||
526 | static | |
527 | wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan) | |
528 | { | |
529 | return &chan->hp_wait; | |
530 | } | |
9f3fdbc6 | 531 | #endif //0 |
3d1fc7fd MD |
532 | |
533 | static | |
534 | int ltt_is_finalized(struct channel *chan) | |
535 | { | |
536 | return lib_ring_buffer_channel_is_finalized(chan); | |
537 | } | |
538 | ||
539 | static | |
540 | int ltt_is_disabled(struct channel *chan) | |
541 | { | |
542 | return lib_ring_buffer_channel_is_disabled(chan); | |
543 | } | |
544 | ||
43861eab | 545 | static |
38fae1d3 | 546 | int ltt_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
43861eab | 547 | { |
4cfec15c | 548 | struct lttng_ust_lib_ring_buffer *buf; |
43861eab MD |
549 | int cpu; |
550 | ||
551 | for_each_channel_cpu(cpu, chan) { | |
ef9ff354 MD |
552 | int *shm_fd, *wait_fd; |
553 | uint64_t *memory_map_size; | |
43861eab MD |
554 | |
555 | buf = channel_get_ring_buffer(&client_config, chan, | |
556 | cpu, handle, &shm_fd, &wait_fd, | |
557 | &memory_map_size); | |
558 | lib_ring_buffer_switch(&client_config, buf, | |
559 | SWITCH_ACTIVE, handle); | |
560 | } | |
561 | return 0; | |
562 | } | |
563 | ||
3d1fc7fd | 564 | static struct ltt_transport ltt_relay_transport = { |
818173b9 | 565 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
3d1fc7fd MD |
566 | .ops = { |
567 | .channel_create = _channel_create, | |
568 | .channel_destroy = ltt_channel_destroy, | |
569 | .buffer_read_open = ltt_buffer_read_open, | |
570 | .buffer_read_close = ltt_buffer_read_close, | |
571 | .event_reserve = ltt_event_reserve, | |
572 | .event_commit = ltt_event_commit, | |
573 | .event_write = ltt_event_write, | |
574 | .packet_avail_size = NULL, /* Would be racy anyway */ | |
9f3fdbc6 MD |
575 | //.get_reader_wait_queue = ltt_get_reader_wait_queue, |
576 | //.get_hp_wait_queue = ltt_get_hp_wait_queue, | |
3d1fc7fd MD |
577 | .is_finalized = ltt_is_finalized, |
578 | .is_disabled = ltt_is_disabled, | |
43d330a4 | 579 | .flush_buffer = ltt_flush_buffer, |
3d1fc7fd MD |
580 | }, |
581 | }; | |
582 | ||
edaa1431 | 583 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
3d1fc7fd | 584 | { |
18c1cd87 | 585 | DBG("LTT : ltt ring buffer client init\n"); |
3d1fc7fd | 586 | ltt_transport_register(<t_relay_transport); |
3d1fc7fd MD |
587 | } |
588 | ||
edaa1431 | 589 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
3d1fc7fd | 590 | { |
18c1cd87 | 591 | DBG("LTT : ltt ring buffer client exit\n"); |
3d1fc7fd MD |
592 | ltt_transport_unregister(<t_relay_transport); |
593 | } |