Commit | Line | Data |
---|---|---|
3d1fc7fd | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
3d1fc7fd | 3 | * |
e92f3e28 MD |
4 | * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
5 | * | |
c0c0989a | 6 | * LTTng lib ring buffer client template. |
3d1fc7fd MD |
7 | */ |
8 | ||
9af5d97a | 9 | #include <limits.h> |
b4051ad8 | 10 | #include <stddef.h> |
9f3fdbc6 | 11 | #include <stdint.h> |
7753d283 MJ |
12 | |
13 | #include <ust-events-internal.h> | |
ae4b659d | 14 | #include "ust-bitfield.h" |
cd61d9bf | 15 | #include "ust-compat.h" |
7dd08bec | 16 | #include "lttng-tracer.h" |
9f3fdbc6 | 17 | #include "../libringbuffer/frontend_types.h" |
8936b6c0 | 18 | #include <urcu/tls-compat.h> |
3d1fc7fd MD |
19 | |
20 | struct metadata_packet_header { | |
21 | uint32_t magic; /* 0x75D11D57 */ | |
19d8b1b3 | 22 | uint8_t uuid[LTTNG_UST_UUID_LEN]; /* Unique Universal Identifier */ |
3d1fc7fd MD |
23 | uint32_t checksum; /* 0 if unused */ |
24 | uint32_t content_size; /* in bits */ | |
25 | uint32_t packet_size; /* in bits */ | |
26 | uint8_t compression_scheme; /* 0 if unused */ | |
27 | uint8_t encryption_scheme; /* 0 if unused */ | |
28 | uint8_t checksum_scheme; /* 0 if unused */ | |
8a98a75d MD |
29 | uint8_t major; /* CTF spec major version number */ |
30 | uint8_t minor; /* CTF spec minor version number */ | |
3d1fc7fd MD |
31 | uint8_t header_end[0]; |
32 | }; | |
33 | ||
34 | struct metadata_record_header { | |
35 | uint8_t header_end[0]; /* End of header */ | |
36 | }; | |
37 | ||
4cfec15c | 38 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
3d1fc7fd | 39 | |
8936b6c0 MD |
40 | /* No nested use supported for metadata ring buffer. */ |
41 | static DEFINE_URCU_TLS(struct lttng_ust_lib_ring_buffer_ctx_private, private_ctx); | |
42 | ||
5198080d | 43 | static inline uint64_t lib_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan) |
3d1fc7fd MD |
44 | { |
45 | return 0; | |
46 | } | |
47 | ||
48 | static inline | |
8ca68914 | 49 | size_t record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
5198080d MJ |
50 | struct lttng_ust_lib_ring_buffer_channel *chan, |
51 | size_t offset, | |
3d1fc7fd | 52 | size_t *pre_header_padding, |
e56bb47c MD |
53 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
54 | void *client_ctx) | |
3d1fc7fd MD |
55 | { |
56 | return 0; | |
57 | } | |
58 | ||
9f3fdbc6 | 59 | #include "../libringbuffer/api.h" |
82b9bde8 | 60 | #include "lttng-rb-clients.h" |
3d1fc7fd | 61 | |
5198080d | 62 | static uint64_t client_ring_buffer_clock_read(struct lttng_ust_lib_ring_buffer_channel *chan) |
3d1fc7fd MD |
63 | { |
64 | return 0; | |
65 | } | |
66 | ||
67 | static | |
4cfec15c | 68 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
5198080d MJ |
69 | struct lttng_ust_lib_ring_buffer_channel *chan, |
70 | size_t offset, | |
3d1fc7fd | 71 | size_t *pre_header_padding, |
e56bb47c MD |
72 | struct lttng_ust_lib_ring_buffer_ctx *ctx, |
73 | void *client_ctx) | |
3d1fc7fd MD |
74 | { |
75 | return 0; | |
76 | } | |
77 | ||
78 | /** | |
79 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
80 | * | |
81 | * Return header size without padding after the structure. Don't use packed | |
82 | * structure because gcc generates inefficient code on some architectures | |
83 | * (powerpc, mips..) | |
84 | */ | |
85 | static size_t client_packet_header_size(void) | |
86 | { | |
87 | return offsetof(struct metadata_packet_header, header_end); | |
88 | } | |
89 | ||
23c8854a | 90 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 91 | unsigned int subbuf_idx, |
38fae1d3 | 92 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 93 | { |
5198080d | 94 | struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
95 | struct metadata_packet_header *header = |
96 | (struct metadata_packet_header *) | |
97 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
98 | subbuf_idx * chan->backend.subbuf_size, |
99 | handle); | |
e7bc0ef6 | 100 | struct lttng_ust_channel_buffer *lttng_chan = channel_get_private(chan); |
3d1fc7fd | 101 | |
34daae3e MD |
102 | assert(header); |
103 | if (!header) | |
104 | return; | |
3d1fc7fd | 105 | header->magic = TSDL_MAGIC_NUMBER; |
e7bc0ef6 | 106 | memcpy(header->uuid, lttng_chan->priv->uuid, sizeof(lttng_chan->priv->uuid)); |
3d1fc7fd MD |
107 | header->checksum = 0; /* 0 if unused */ |
108 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
109 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
110 | header->compression_scheme = 0; /* 0 if unused */ | |
111 | header->encryption_scheme = 0; /* 0 if unused */ | |
112 | header->checksum_scheme = 0; /* 0 if unused */ | |
8a98a75d MD |
113 | header->major = CTF_SPEC_MAJOR; |
114 | header->minor = CTF_SPEC_MINOR; | |
3d1fc7fd MD |
115 | } |
116 | ||
117 | /* | |
118 | * offset is assumed to never be 0 here : never deliver a completely empty | |
119 | * subbuffer. data_size is between 1 and subbuf_size. | |
120 | */ | |
23c8854a | 121 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, uint64_t tsc, |
1d498196 | 122 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 123 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 124 | { |
5198080d | 125 | struct lttng_ust_lib_ring_buffer_channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
126 | struct metadata_packet_header *header = |
127 | (struct metadata_packet_header *) | |
128 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
129 | subbuf_idx * chan->backend.subbuf_size, |
130 | handle); | |
3d1fc7fd MD |
131 | unsigned long records_lost = 0; |
132 | ||
34daae3e MD |
133 | assert(header); |
134 | if (!header) | |
135 | return; | |
3d1fc7fd | 136 | header->content_size = data_size * CHAR_BIT; /* in bits */ |
b72687b8 | 137 | header->packet_size = LTTNG_UST_PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ |
04489ab4 MD |
138 | /* |
139 | * We do not care about the records lost count, because the metadata | |
140 | * channel waits and retry. | |
141 | */ | |
142 | (void) lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
3d1fc7fd MD |
143 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); |
144 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
145 | WARN_ON_ONCE(records_lost != 0); | |
146 | } | |
147 | ||
4cfec15c | 148 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
1d498196 | 149 | int cpu, const char *name, |
38fae1d3 | 150 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
151 | { |
152 | return 0; | |
153 | } | |
154 | ||
4cfec15c | 155 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 156 | void *priv, int cpu, |
38fae1d3 | 157 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
158 | { |
159 | } | |
160 | ||
82b9bde8 JD |
161 | static const |
162 | struct lttng_ust_client_lib_ring_buffer_client_cb client_cb = { | |
163 | .parent = { | |
164 | .ring_buffer_clock_read = client_ring_buffer_clock_read, | |
165 | .record_header_size = client_record_header_size, | |
166 | .subbuffer_header_size = client_packet_header_size, | |
167 | .buffer_begin = client_buffer_begin, | |
168 | .buffer_end = client_buffer_end, | |
169 | .buffer_create = client_buffer_create, | |
170 | .buffer_finalize = client_buffer_finalize, | |
171 | }, | |
172 | }; | |
173 | ||
4cfec15c | 174 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
3d1fc7fd MD |
175 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
176 | .cb.record_header_size = client_record_header_size, | |
177 | .cb.subbuffer_header_size = client_packet_header_size, | |
178 | .cb.buffer_begin = client_buffer_begin, | |
179 | .cb.buffer_end = client_buffer_end, | |
180 | .cb.buffer_create = client_buffer_create, | |
181 | .cb.buffer_finalize = client_buffer_finalize, | |
182 | ||
183 | .tsc_bits = 0, | |
184 | .alloc = RING_BUFFER_ALLOC_GLOBAL, | |
185 | .sync = RING_BUFFER_SYNC_GLOBAL, | |
186 | .mode = RING_BUFFER_MODE_TEMPLATE, | |
187 | .backend = RING_BUFFER_PAGE, | |
5d61a504 | 188 | .output = RING_BUFFER_MMAP, |
3d1fc7fd | 189 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
5d61a504 MD |
190 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
191 | .wakeup = RING_BUFFER_WAKEUP_BY_WRITER, | |
c1fca457 | 192 | .client_type = LTTNG_CLIENT_TYPE, |
82b9bde8 JD |
193 | |
194 | .cb_ptr = &client_cb.parent, | |
3d1fc7fd MD |
195 | }; |
196 | ||
197 | static | |
e7bc0ef6 | 198 | struct lttng_ust_channel_buffer *_channel_create(const char *name, |
a3f61e7f | 199 | void *buf_addr, |
3d1fc7fd MD |
200 | size_t subbuf_size, size_t num_subbuf, |
201 | unsigned int switch_timer_interval, | |
193183fb | 202 | unsigned int read_timer_interval, |
6ca18e66 | 203 | unsigned char *uuid, |
a9ff648c | 204 | uint32_t chan_id, |
b2c5f61a MD |
205 | const int *stream_fds, int nr_stream_fds, |
206 | int64_t blocking_timeout) | |
3d1fc7fd | 207 | { |
f0fde1c3 | 208 | struct lttng_ust_abi_channel_config chan_priv_init; |
a3f61e7f | 209 | struct lttng_ust_shm_handle *handle; |
e7bc0ef6 | 210 | struct lttng_ust_channel_buffer *lttng_chan_buf; |
f0fde1c3 | 211 | |
e7bc0ef6 MD |
212 | lttng_chan_buf = lttng_ust_alloc_channel_buffer(); |
213 | if (!lttng_chan_buf) | |
f0fde1c3 | 214 | return NULL; |
e7bc0ef6 MD |
215 | memcpy(lttng_chan_buf->priv->uuid, uuid, LTTNG_UST_UUID_LEN); |
216 | lttng_chan_buf->priv->id = chan_id; | |
a3f61e7f | 217 | |
74d81a6c MD |
218 | memset(&chan_priv_init, 0, sizeof(chan_priv_init)); |
219 | memcpy(chan_priv_init.uuid, uuid, LTTNG_UST_UUID_LEN); | |
6ca18e66 | 220 | chan_priv_init.id = chan_id; |
f0fde1c3 | 221 | |
a3f61e7f | 222 | handle = channel_create(&client_config, name, |
e7bc0ef6 MD |
223 | __alignof__(struct lttng_ust_channel_buffer), |
224 | sizeof(struct lttng_ust_channel_buffer), | |
74d81a6c | 225 | &chan_priv_init, |
e7bc0ef6 | 226 | lttng_chan_buf, buf_addr, subbuf_size, num_subbuf, |
a9ff648c | 227 | switch_timer_interval, read_timer_interval, |
b2c5f61a | 228 | stream_fds, nr_stream_fds, blocking_timeout); |
a3f61e7f | 229 | if (!handle) |
f0fde1c3 | 230 | goto error; |
07539b34 | 231 | lttng_chan_buf->priv->rb_chan = shmp(handle, handle->chan); |
e7bc0ef6 | 232 | return lttng_chan_buf; |
f0fde1c3 MD |
233 | |
234 | error: | |
e7bc0ef6 | 235 | lttng_ust_free_channel_common(lttng_chan_buf->parent); |
f0fde1c3 | 236 | return NULL; |
3d1fc7fd MD |
237 | } |
238 | ||
239 | static | |
e7bc0ef6 | 240 | void lttng_channel_destroy(struct lttng_ust_channel_buffer *lttng_chan_buf) |
3d1fc7fd | 241 | { |
07539b34 | 242 | channel_destroy(lttng_chan_buf->priv->rb_chan, lttng_chan_buf->priv->rb_chan->handle, 1); |
e7bc0ef6 | 243 | lttng_ust_free_channel_common(lttng_chan_buf->parent); |
3d1fc7fd MD |
244 | } |
245 | ||
246 | static | |
8936b6c0 | 247 | int lttng_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd | 248 | { |
a3492932 MD |
249 | int ret; |
250 | ||
8936b6c0 MD |
251 | memset(&private_ctx, 0, sizeof(private_ctx)); |
252 | private_ctx.pub = ctx; | |
253 | private_ctx.chan = ctx->client_priv; | |
254 | ctx->priv = &private_ctx; | |
e56bb47c | 255 | ret = lib_ring_buffer_reserve(&client_config, ctx, NULL); |
a3492932 MD |
256 | if (ret) |
257 | return ret; | |
2b7080aa | 258 | if (lib_ring_buffer_backend_get_pages(&client_config, ctx, |
8936b6c0 | 259 | &ctx->priv->backend_pages)) |
2b7080aa | 260 | return -EPERM; |
a3492932 | 261 | return 0; |
3d1fc7fd MD |
262 | } |
263 | ||
264 | static | |
7dd08bec | 265 | void lttng_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
266 | { |
267 | lib_ring_buffer_commit(&client_config, ctx); | |
268 | } | |
269 | ||
270 | static | |
8936b6c0 MD |
271 | void lttng_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, |
272 | const void *src, size_t len, size_t alignment) | |
3d1fc7fd | 273 | { |
8936b6c0 | 274 | lttng_ust_lib_ring_buffer_align_ctx(ctx, alignment); |
3d1fc7fd MD |
275 | lib_ring_buffer_write(&client_config, ctx, src, len); |
276 | } | |
277 | ||
278 | static | |
07539b34 | 279 | size_t lttng_packet_avail_size(struct lttng_ust_channel_buffer *chan) |
3d1fc7fd | 280 | { |
07539b34 | 281 | struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
3d1fc7fd | 282 | unsigned long o_begin; |
4cfec15c | 283 | struct lttng_ust_lib_ring_buffer *buf; |
3d1fc7fd | 284 | |
07539b34 | 285 | buf = shmp(rb_chan->handle, rb_chan->backend.buf[0].shmp); /* Only for global buffer ! */ |
3d1fc7fd | 286 | o_begin = v_read(&client_config, &buf->offset); |
07539b34 MD |
287 | if (subbuf_offset(o_begin, rb_chan) != 0) { |
288 | return rb_chan->backend.subbuf_size - subbuf_offset(o_begin, rb_chan); | |
3d1fc7fd | 289 | } else { |
07539b34 | 290 | return rb_chan->backend.subbuf_size - subbuf_offset(o_begin, rb_chan) |
3d1fc7fd MD |
291 | - sizeof(struct metadata_packet_header); |
292 | } | |
293 | } | |
294 | ||
295 | static | |
07539b34 | 296 | int lttng_is_finalized(struct lttng_ust_channel_buffer *chan) |
3d1fc7fd | 297 | { |
07539b34 | 298 | struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
3d1fc7fd | 299 | |
07539b34 | 300 | return lib_ring_buffer_channel_is_finalized(rb_chan); |
3d1fc7fd MD |
301 | } |
302 | ||
303 | static | |
07539b34 | 304 | int lttng_is_disabled(struct lttng_ust_channel_buffer *chan) |
3d1fc7fd | 305 | { |
07539b34 | 306 | struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
3d1fc7fd | 307 | |
07539b34 | 308 | return lib_ring_buffer_channel_is_disabled(rb_chan); |
3d1fc7fd MD |
309 | } |
310 | ||
43861eab | 311 | static |
07539b34 | 312 | int lttng_flush_buffer(struct lttng_ust_channel_buffer *chan) |
43861eab | 313 | { |
07539b34 | 314 | struct lttng_ust_lib_ring_buffer_channel *rb_chan = chan->priv->rb_chan; |
4cfec15c | 315 | struct lttng_ust_lib_ring_buffer *buf; |
74d81a6c MD |
316 | int shm_fd, wait_fd, wakeup_fd; |
317 | uint64_t memory_map_size; | |
43861eab | 318 | |
07539b34 MD |
319 | buf = channel_get_ring_buffer(&client_config, rb_chan, |
320 | 0, rb_chan->handle, &shm_fd, &wait_fd, &wakeup_fd, | |
43861eab MD |
321 | &memory_map_size); |
322 | lib_ring_buffer_switch(&client_config, buf, | |
07539b34 | 323 | SWITCH_ACTIVE, rb_chan->handle); |
43861eab MD |
324 | return 0; |
325 | } | |
326 | ||
7dd08bec | 327 | static struct lttng_transport lttng_relay_transport = { |
818173b9 | 328 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
3d1fc7fd | 329 | .ops = { |
14b6f891 | 330 | .struct_size = sizeof(struct lttng_ust_channel_buffer_ops), |
49926dbd | 331 | |
14b6f891 | 332 | .priv = __LTTNG_COMPOUND_LITERAL(struct lttng_ust_channel_buffer_ops_private, { |
a880bae5 MD |
333 | .pub = <tng_relay_transport.ops, |
334 | .channel_create = _channel_create, | |
335 | .channel_destroy = lttng_channel_destroy, | |
336 | .packet_avail_size = lttng_packet_avail_size, | |
337 | .is_finalized = lttng_is_finalized, | |
338 | .is_disabled = lttng_is_disabled, | |
339 | .flush_buffer = lttng_flush_buffer, | |
340 | }), | |
7dd08bec MD |
341 | .event_reserve = lttng_event_reserve, |
342 | .event_commit = lttng_event_commit, | |
343 | .event_write = lttng_event_write, | |
3d1fc7fd | 344 | }, |
74d81a6c | 345 | .client_config = &client_config, |
3d1fc7fd MD |
346 | }; |
347 | ||
edaa1431 | 348 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
3d1fc7fd | 349 | { |
34a91bdb MD |
350 | DBG("LTT : ltt ring buffer client \"%s\" init\n", |
351 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); | |
7dd08bec | 352 | lttng_transport_register(<tng_relay_transport); |
3d1fc7fd MD |
353 | } |
354 | ||
edaa1431 | 355 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
3d1fc7fd | 356 | { |
34a91bdb MD |
357 | DBG("LTT : ltt ring buffer client \"%s\" exit\n", |
358 | "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap"); | |
7dd08bec | 359 | lttng_transport_unregister(<tng_relay_transport); |
3d1fc7fd | 360 | } |