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