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