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" | |
3d1fc7fd | 14 | #include "ltt-tracer.h" |
9f3fdbc6 | 15 | #include "../libringbuffer/frontend_types.h" |
3d1fc7fd MD |
16 | |
17 | struct metadata_packet_header { | |
18 | uint32_t magic; /* 0x75D11D57 */ | |
19 | uint8_t uuid[16]; /* Unique Universal Identifier */ | |
20 | uint32_t checksum; /* 0 if unused */ | |
21 | uint32_t content_size; /* in bits */ | |
22 | uint32_t packet_size; /* in bits */ | |
23 | uint8_t compression_scheme; /* 0 if unused */ | |
24 | uint8_t encryption_scheme; /* 0 if unused */ | |
25 | uint8_t checksum_scheme; /* 0 if unused */ | |
8a98a75d MD |
26 | uint8_t major; /* CTF spec major version number */ |
27 | uint8_t minor; /* CTF spec minor version number */ | |
3d1fc7fd MD |
28 | uint8_t header_end[0]; |
29 | }; | |
30 | ||
31 | struct metadata_record_header { | |
32 | uint8_t header_end[0]; /* End of header */ | |
33 | }; | |
34 | ||
4cfec15c | 35 | static const struct lttng_ust_lib_ring_buffer_config client_config; |
3d1fc7fd MD |
36 | |
37 | static inline | |
38 | u64 lib_ring_buffer_clock_read(struct channel *chan) | |
39 | { | |
40 | return 0; | |
41 | } | |
42 | ||
43 | static inline | |
4cfec15c | 44 | unsigned char record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
45 | struct channel *chan, size_t offset, |
46 | size_t *pre_header_padding, | |
4cfec15c | 47 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
48 | { |
49 | return 0; | |
50 | } | |
51 | ||
9f3fdbc6 | 52 | #include "../libringbuffer/api.h" |
3d1fc7fd MD |
53 | |
54 | static u64 client_ring_buffer_clock_read(struct channel *chan) | |
55 | { | |
56 | return 0; | |
57 | } | |
58 | ||
59 | static | |
4cfec15c | 60 | size_t client_record_header_size(const struct lttng_ust_lib_ring_buffer_config *config, |
3d1fc7fd MD |
61 | struct channel *chan, size_t offset, |
62 | size_t *pre_header_padding, | |
4cfec15c | 63 | struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
64 | { |
65 | return 0; | |
66 | } | |
67 | ||
68 | /** | |
69 | * client_packet_header_size - called on buffer-switch to a new sub-buffer | |
70 | * | |
71 | * Return header size without padding after the structure. Don't use packed | |
72 | * structure because gcc generates inefficient code on some architectures | |
73 | * (powerpc, mips..) | |
74 | */ | |
75 | static size_t client_packet_header_size(void) | |
76 | { | |
77 | return offsetof(struct metadata_packet_header, header_end); | |
78 | } | |
79 | ||
4cfec15c | 80 | static void client_buffer_begin(struct lttng_ust_lib_ring_buffer *buf, u64 tsc, |
1d498196 | 81 | unsigned int subbuf_idx, |
38fae1d3 | 82 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 83 | { |
1d498196 | 84 | struct channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
85 | struct metadata_packet_header *header = |
86 | (struct metadata_packet_header *) | |
87 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
88 | subbuf_idx * chan->backend.subbuf_size, |
89 | handle); | |
3d1fc7fd MD |
90 | struct ltt_channel *ltt_chan = channel_get_private(chan); |
91 | struct ltt_session *session = ltt_chan->session; | |
92 | ||
93 | header->magic = TSDL_MAGIC_NUMBER; | |
9f3fdbc6 | 94 | memcpy(header->uuid, session->uuid, sizeof(session->uuid)); |
3d1fc7fd MD |
95 | header->checksum = 0; /* 0 if unused */ |
96 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
97 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
98 | header->compression_scheme = 0; /* 0 if unused */ | |
99 | header->encryption_scheme = 0; /* 0 if unused */ | |
100 | header->checksum_scheme = 0; /* 0 if unused */ | |
8a98a75d MD |
101 | header->major = CTF_SPEC_MAJOR; |
102 | header->minor = CTF_SPEC_MINOR; | |
103 | ||
3d1fc7fd MD |
104 | } |
105 | ||
106 | /* | |
107 | * offset is assumed to never be 0 here : never deliver a completely empty | |
108 | * subbuffer. data_size is between 1 and subbuf_size. | |
109 | */ | |
4cfec15c | 110 | static void client_buffer_end(struct lttng_ust_lib_ring_buffer *buf, u64 tsc, |
1d498196 | 111 | unsigned int subbuf_idx, unsigned long data_size, |
38fae1d3 | 112 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 113 | { |
1d498196 | 114 | struct channel *chan = shmp(handle, buf->backend.chan); |
3d1fc7fd MD |
115 | struct metadata_packet_header *header = |
116 | (struct metadata_packet_header *) | |
117 | lib_ring_buffer_offset_address(&buf->backend, | |
1d498196 MD |
118 | subbuf_idx * chan->backend.subbuf_size, |
119 | handle); | |
3d1fc7fd MD |
120 | unsigned long records_lost = 0; |
121 | ||
122 | header->content_size = data_size * CHAR_BIT; /* in bits */ | |
123 | header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
124 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
125 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); | |
126 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
127 | WARN_ON_ONCE(records_lost != 0); | |
128 | } | |
129 | ||
4cfec15c | 130 | static int client_buffer_create(struct lttng_ust_lib_ring_buffer *buf, void *priv, |
1d498196 | 131 | int cpu, const char *name, |
38fae1d3 | 132 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
133 | { |
134 | return 0; | |
135 | } | |
136 | ||
4cfec15c | 137 | static void client_buffer_finalize(struct lttng_ust_lib_ring_buffer *buf, |
1d498196 | 138 | void *priv, int cpu, |
38fae1d3 | 139 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
140 | { |
141 | } | |
142 | ||
4cfec15c | 143 | static const struct lttng_ust_lib_ring_buffer_config client_config = { |
3d1fc7fd MD |
144 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, |
145 | .cb.record_header_size = client_record_header_size, | |
146 | .cb.subbuffer_header_size = client_packet_header_size, | |
147 | .cb.buffer_begin = client_buffer_begin, | |
148 | .cb.buffer_end = client_buffer_end, | |
149 | .cb.buffer_create = client_buffer_create, | |
150 | .cb.buffer_finalize = client_buffer_finalize, | |
151 | ||
152 | .tsc_bits = 0, | |
153 | .alloc = RING_BUFFER_ALLOC_GLOBAL, | |
154 | .sync = RING_BUFFER_SYNC_GLOBAL, | |
155 | .mode = RING_BUFFER_MODE_TEMPLATE, | |
156 | .backend = RING_BUFFER_PAGE, | |
5d61a504 | 157 | .output = RING_BUFFER_MMAP, |
3d1fc7fd | 158 | .oops = RING_BUFFER_OOPS_CONSISTENCY, |
5d61a504 MD |
159 | .ipi = RING_BUFFER_NO_IPI_BARRIER, |
160 | .wakeup = RING_BUFFER_WAKEUP_BY_WRITER, | |
c1fca457 | 161 | .client_type = LTTNG_CLIENT_TYPE, |
3d1fc7fd MD |
162 | }; |
163 | ||
c1fca457 MD |
164 | const struct lttng_ust_lib_ring_buffer_config *LTTNG_CLIENT_CALLBACKS = &client_config; |
165 | ||
3d1fc7fd | 166 | static |
1d498196 | 167 | struct ltt_channel *_channel_create(const char *name, |
3d1fc7fd MD |
168 | struct ltt_channel *ltt_chan, void *buf_addr, |
169 | size_t subbuf_size, size_t num_subbuf, | |
170 | unsigned int switch_timer_interval, | |
193183fb MD |
171 | unsigned int read_timer_interval, |
172 | int *shm_fd, int *wait_fd, | |
173 | uint64_t *memory_map_size) | |
3d1fc7fd | 174 | { |
1d498196 | 175 | ltt_chan->handle = channel_create(&client_config, name, ltt_chan, buf_addr, |
3d1fc7fd | 176 | subbuf_size, num_subbuf, switch_timer_interval, |
193183fb MD |
177 | read_timer_interval, shm_fd, wait_fd, |
178 | memory_map_size); | |
1b2c2540 MD |
179 | if (!ltt_chan->handle) |
180 | return NULL; | |
181 | ltt_chan->chan = shmp(ltt_chan->handle, ltt_chan->handle->chan); | |
1d498196 | 182 | return ltt_chan; |
3d1fc7fd MD |
183 | } |
184 | ||
185 | static | |
1d498196 | 186 | void ltt_channel_destroy(struct ltt_channel *ltt_chan) |
3d1fc7fd | 187 | { |
824f40b8 | 188 | channel_destroy(ltt_chan->chan, ltt_chan->handle, 0); |
3d1fc7fd MD |
189 | } |
190 | ||
191 | static | |
4cfec15c | 192 | struct lttng_ust_lib_ring_buffer *ltt_buffer_read_open(struct channel *chan, |
38fae1d3 | 193 | struct lttng_ust_shm_handle *handle, |
381c0f1e MD |
194 | int *shm_fd, int *wait_fd, |
195 | uint64_t *memory_map_size) | |
3d1fc7fd | 196 | { |
4cfec15c | 197 | struct lttng_ust_lib_ring_buffer *buf; |
3d1fc7fd | 198 | |
381c0f1e MD |
199 | buf = channel_get_ring_buffer(&client_config, chan, |
200 | 0, handle, shm_fd, wait_fd, memory_map_size); | |
824f40b8 | 201 | if (!lib_ring_buffer_open_read(buf, handle, 0)) |
3d1fc7fd MD |
202 | return buf; |
203 | return NULL; | |
204 | } | |
205 | ||
206 | static | |
4cfec15c | 207 | void ltt_buffer_read_close(struct lttng_ust_lib_ring_buffer *buf, |
38fae1d3 | 208 | struct lttng_ust_shm_handle *handle) |
3d1fc7fd | 209 | { |
824f40b8 | 210 | lib_ring_buffer_release_read(buf, handle, 0); |
3d1fc7fd MD |
211 | } |
212 | ||
213 | static | |
4cfec15c | 214 | int ltt_event_reserve(struct lttng_ust_lib_ring_buffer_ctx *ctx, uint32_t event_id) |
3d1fc7fd MD |
215 | { |
216 | return lib_ring_buffer_reserve(&client_config, ctx); | |
217 | } | |
218 | ||
219 | static | |
4cfec15c | 220 | void ltt_event_commit(struct lttng_ust_lib_ring_buffer_ctx *ctx) |
3d1fc7fd MD |
221 | { |
222 | lib_ring_buffer_commit(&client_config, ctx); | |
223 | } | |
224 | ||
225 | static | |
4cfec15c | 226 | void ltt_event_write(struct lttng_ust_lib_ring_buffer_ctx *ctx, const void *src, |
3d1fc7fd MD |
227 | size_t len) |
228 | { | |
229 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
230 | } | |
231 | ||
232 | static | |
38fae1d3 | 233 | size_t ltt_packet_avail_size(struct channel *chan, struct lttng_ust_shm_handle *handle) |
3d1fc7fd MD |
234 | |
235 | { | |
236 | unsigned long o_begin; | |
4cfec15c | 237 | struct lttng_ust_lib_ring_buffer *buf; |
3d1fc7fd | 238 | |
1d498196 | 239 | buf = shmp(handle, chan->backend.buf[0].shmp); /* Only for global buffer ! */ |
3d1fc7fd MD |
240 | o_begin = v_read(&client_config, &buf->offset); |
241 | if (subbuf_offset(o_begin, chan) != 0) { | |
242 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan); | |
243 | } else { | |
244 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan) | |
245 | - sizeof(struct metadata_packet_header); | |
246 | } | |
247 | } | |
248 | ||
9f3fdbc6 | 249 | #if 0 |
3d1fc7fd MD |
250 | static |
251 | wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan) | |
252 | { | |
253 | return &chan->read_wait; | |
254 | } | |
255 | ||
256 | static | |
257 | wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan) | |
258 | { | |
259 | return &chan->hp_wait; | |
260 | } | |
9f3fdbc6 | 261 | #endif //0 |
3d1fc7fd MD |
262 | |
263 | static | |
264 | int ltt_is_finalized(struct channel *chan) | |
265 | { | |
266 | return lib_ring_buffer_channel_is_finalized(chan); | |
267 | } | |
268 | ||
269 | static | |
270 | int ltt_is_disabled(struct channel *chan) | |
271 | { | |
272 | return lib_ring_buffer_channel_is_disabled(chan); | |
273 | } | |
274 | ||
43861eab | 275 | static |
38fae1d3 | 276 | int ltt_flush_buffer(struct channel *chan, struct lttng_ust_shm_handle *handle) |
43861eab | 277 | { |
4cfec15c | 278 | struct lttng_ust_lib_ring_buffer *buf; |
43861eab MD |
279 | int shm_fd, wait_fd; |
280 | uint64_t memory_map_size; | |
281 | ||
282 | buf = channel_get_ring_buffer(&client_config, chan, | |
283 | 0, handle, &shm_fd, &wait_fd, | |
284 | &memory_map_size); | |
285 | lib_ring_buffer_switch(&client_config, buf, | |
286 | SWITCH_ACTIVE, handle); | |
287 | return 0; | |
288 | } | |
289 | ||
3d1fc7fd | 290 | static struct ltt_transport ltt_relay_transport = { |
818173b9 | 291 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING "-mmap", |
3d1fc7fd MD |
292 | .ops = { |
293 | .channel_create = _channel_create, | |
294 | .channel_destroy = ltt_channel_destroy, | |
295 | .buffer_read_open = ltt_buffer_read_open, | |
296 | .buffer_read_close = ltt_buffer_read_close, | |
297 | .event_reserve = ltt_event_reserve, | |
298 | .event_commit = ltt_event_commit, | |
299 | .event_write = ltt_event_write, | |
300 | .packet_avail_size = ltt_packet_avail_size, | |
9f3fdbc6 MD |
301 | //.get_reader_wait_queue = ltt_get_reader_wait_queue, |
302 | //.get_hp_wait_queue = ltt_get_hp_wait_queue, | |
3d1fc7fd MD |
303 | .is_finalized = ltt_is_finalized, |
304 | .is_disabled = ltt_is_disabled, | |
43d330a4 | 305 | .flush_buffer = ltt_flush_buffer, |
3d1fc7fd MD |
306 | }, |
307 | }; | |
308 | ||
edaa1431 | 309 | void RING_BUFFER_MODE_TEMPLATE_INIT(void) |
3d1fc7fd | 310 | { |
18c1cd87 | 311 | DBG("LTT : ltt ring buffer client init\n"); |
3d1fc7fd | 312 | ltt_transport_register(<t_relay_transport); |
3d1fc7fd MD |
313 | } |
314 | ||
edaa1431 | 315 | void RING_BUFFER_MODE_TEMPLATE_EXIT(void) |
3d1fc7fd | 316 | { |
18c1cd87 | 317 | DBG("LTT : ltt ring buffer client exit\n"); |
3d1fc7fd MD |
318 | ltt_transport_unregister(<t_relay_transport); |
319 | } |