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 MD |
11 | #include <stdint.h> |
12 | #include "ust/bitfield.h" | |
3d1fc7fd MD |
13 | #include "ltt-events.h" |
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, | |
79 | unsigned int subbuf_idx) | |
80 | { | |
9f3fdbc6 | 81 | struct channel *chan = shmp(buf->backend.chan); |
3d1fc7fd MD |
82 | struct metadata_packet_header *header = |
83 | (struct metadata_packet_header *) | |
84 | lib_ring_buffer_offset_address(&buf->backend, | |
85 | subbuf_idx * chan->backend.subbuf_size); | |
86 | struct ltt_channel *ltt_chan = channel_get_private(chan); | |
87 | struct ltt_session *session = ltt_chan->session; | |
88 | ||
89 | header->magic = TSDL_MAGIC_NUMBER; | |
9f3fdbc6 | 90 | memcpy(header->uuid, session->uuid, sizeof(session->uuid)); |
3d1fc7fd MD |
91 | header->checksum = 0; /* 0 if unused */ |
92 | header->content_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
93 | header->packet_size = 0xFFFFFFFF; /* in bits, for debugging */ | |
94 | header->compression_scheme = 0; /* 0 if unused */ | |
95 | header->encryption_scheme = 0; /* 0 if unused */ | |
96 | header->checksum_scheme = 0; /* 0 if unused */ | |
97 | } | |
98 | ||
99 | /* | |
100 | * offset is assumed to never be 0 here : never deliver a completely empty | |
101 | * subbuffer. data_size is between 1 and subbuf_size. | |
102 | */ | |
103 | static void client_buffer_end(struct lib_ring_buffer *buf, u64 tsc, | |
104 | unsigned int subbuf_idx, unsigned long data_size) | |
105 | { | |
9f3fdbc6 | 106 | struct channel *chan = shmp(buf->backend.chan); |
3d1fc7fd MD |
107 | struct metadata_packet_header *header = |
108 | (struct metadata_packet_header *) | |
109 | lib_ring_buffer_offset_address(&buf->backend, | |
110 | subbuf_idx * chan->backend.subbuf_size); | |
111 | unsigned long records_lost = 0; | |
112 | ||
113 | header->content_size = data_size * CHAR_BIT; /* in bits */ | |
114 | header->packet_size = PAGE_ALIGN(data_size) * CHAR_BIT; /* in bits */ | |
115 | records_lost += lib_ring_buffer_get_records_lost_full(&client_config, buf); | |
116 | records_lost += lib_ring_buffer_get_records_lost_wrap(&client_config, buf); | |
117 | records_lost += lib_ring_buffer_get_records_lost_big(&client_config, buf); | |
118 | WARN_ON_ONCE(records_lost != 0); | |
119 | } | |
120 | ||
121 | static int client_buffer_create(struct lib_ring_buffer *buf, void *priv, | |
122 | int cpu, const char *name) | |
123 | { | |
124 | return 0; | |
125 | } | |
126 | ||
127 | static void client_buffer_finalize(struct lib_ring_buffer *buf, void *priv, int cpu) | |
128 | { | |
129 | } | |
130 | ||
131 | static const struct lib_ring_buffer_config client_config = { | |
132 | .cb.ring_buffer_clock_read = client_ring_buffer_clock_read, | |
133 | .cb.record_header_size = client_record_header_size, | |
134 | .cb.subbuffer_header_size = client_packet_header_size, | |
135 | .cb.buffer_begin = client_buffer_begin, | |
136 | .cb.buffer_end = client_buffer_end, | |
137 | .cb.buffer_create = client_buffer_create, | |
138 | .cb.buffer_finalize = client_buffer_finalize, | |
139 | ||
140 | .tsc_bits = 0, | |
141 | .alloc = RING_BUFFER_ALLOC_GLOBAL, | |
142 | .sync = RING_BUFFER_SYNC_GLOBAL, | |
143 | .mode = RING_BUFFER_MODE_TEMPLATE, | |
144 | .backend = RING_BUFFER_PAGE, | |
145 | .output = RING_BUFFER_SPLICE, | |
146 | .oops = RING_BUFFER_OOPS_CONSISTENCY, | |
147 | .ipi = RING_BUFFER_IPI_BARRIER, | |
148 | .wakeup = RING_BUFFER_WAKEUP_BY_TIMER, | |
149 | }; | |
150 | ||
151 | static | |
152 | struct channel *_channel_create(const char *name, | |
153 | struct ltt_channel *ltt_chan, void *buf_addr, | |
154 | size_t subbuf_size, size_t num_subbuf, | |
155 | unsigned int switch_timer_interval, | |
9f3fdbc6 MD |
156 | unsigned int read_timer_interval, |
157 | int *shmid) | |
3d1fc7fd MD |
158 | { |
159 | return channel_create(&client_config, name, ltt_chan, buf_addr, | |
160 | subbuf_size, num_subbuf, switch_timer_interval, | |
9f3fdbc6 | 161 | read_timer_interval, shmid); |
3d1fc7fd MD |
162 | } |
163 | ||
164 | static | |
165 | void ltt_channel_destroy(struct channel *chan) | |
166 | { | |
167 | channel_destroy(chan); | |
168 | } | |
169 | ||
170 | static | |
171 | struct lib_ring_buffer *ltt_buffer_read_open(struct channel *chan) | |
172 | { | |
173 | struct lib_ring_buffer *buf; | |
174 | ||
175 | buf = channel_get_ring_buffer(&client_config, chan, 0); | |
176 | if (!lib_ring_buffer_open_read(buf)) | |
177 | return buf; | |
178 | return NULL; | |
179 | } | |
180 | ||
181 | static | |
182 | void ltt_buffer_read_close(struct lib_ring_buffer *buf) | |
183 | { | |
184 | lib_ring_buffer_release_read(buf); | |
185 | } | |
186 | ||
187 | static | |
188 | int ltt_event_reserve(struct lib_ring_buffer_ctx *ctx, uint32_t event_id) | |
189 | { | |
190 | return lib_ring_buffer_reserve(&client_config, ctx); | |
191 | } | |
192 | ||
193 | static | |
194 | void ltt_event_commit(struct lib_ring_buffer_ctx *ctx) | |
195 | { | |
196 | lib_ring_buffer_commit(&client_config, ctx); | |
197 | } | |
198 | ||
199 | static | |
200 | void ltt_event_write(struct lib_ring_buffer_ctx *ctx, const void *src, | |
201 | size_t len) | |
202 | { | |
203 | lib_ring_buffer_write(&client_config, ctx, src, len); | |
204 | } | |
205 | ||
206 | static | |
207 | size_t ltt_packet_avail_size(struct channel *chan) | |
208 | ||
209 | { | |
210 | unsigned long o_begin; | |
211 | struct lib_ring_buffer *buf; | |
212 | ||
9f3fdbc6 | 213 | buf = shmp(chan->backend.buf); /* Only for global buffer ! */ |
3d1fc7fd MD |
214 | o_begin = v_read(&client_config, &buf->offset); |
215 | if (subbuf_offset(o_begin, chan) != 0) { | |
216 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan); | |
217 | } else { | |
218 | return chan->backend.subbuf_size - subbuf_offset(o_begin, chan) | |
219 | - sizeof(struct metadata_packet_header); | |
220 | } | |
221 | } | |
222 | ||
9f3fdbc6 | 223 | #if 0 |
3d1fc7fd MD |
224 | static |
225 | wait_queue_head_t *ltt_get_reader_wait_queue(struct channel *chan) | |
226 | { | |
227 | return &chan->read_wait; | |
228 | } | |
229 | ||
230 | static | |
231 | wait_queue_head_t *ltt_get_hp_wait_queue(struct channel *chan) | |
232 | { | |
233 | return &chan->hp_wait; | |
234 | } | |
9f3fdbc6 | 235 | #endif //0 |
3d1fc7fd MD |
236 | |
237 | static | |
238 | int ltt_is_finalized(struct channel *chan) | |
239 | { | |
240 | return lib_ring_buffer_channel_is_finalized(chan); | |
241 | } | |
242 | ||
243 | static | |
244 | int ltt_is_disabled(struct channel *chan) | |
245 | { | |
246 | return lib_ring_buffer_channel_is_disabled(chan); | |
247 | } | |
248 | ||
249 | static struct ltt_transport ltt_relay_transport = { | |
250 | .name = "relay-" RING_BUFFER_MODE_TEMPLATE_STRING, | |
3d1fc7fd MD |
251 | .ops = { |
252 | .channel_create = _channel_create, | |
253 | .channel_destroy = ltt_channel_destroy, | |
254 | .buffer_read_open = ltt_buffer_read_open, | |
255 | .buffer_read_close = ltt_buffer_read_close, | |
256 | .event_reserve = ltt_event_reserve, | |
257 | .event_commit = ltt_event_commit, | |
258 | .event_write = ltt_event_write, | |
259 | .packet_avail_size = ltt_packet_avail_size, | |
9f3fdbc6 MD |
260 | //.get_reader_wait_queue = ltt_get_reader_wait_queue, |
261 | //.get_hp_wait_queue = ltt_get_hp_wait_queue, | |
3d1fc7fd MD |
262 | .is_finalized = ltt_is_finalized, |
263 | .is_disabled = ltt_is_disabled, | |
264 | }, | |
265 | }; | |
266 | ||
9f3fdbc6 MD |
267 | static |
268 | void __attribute__((constructor)) ltt_ring_buffer_client_init(void) | |
3d1fc7fd | 269 | { |
9f3fdbc6 | 270 | printf("LTT : ltt ring buffer client init\n"); |
3d1fc7fd | 271 | ltt_transport_register(<t_relay_transport); |
3d1fc7fd MD |
272 | } |
273 | ||
9f3fdbc6 MD |
274 | static |
275 | void __attribute__((destructor)) ltt_ring_buffer_client_exit(void) | |
3d1fc7fd | 276 | { |
9f3fdbc6 | 277 | printf("LTT : ltt ring buffer client exit\n"); |
3d1fc7fd MD |
278 | ltt_transport_unregister(<t_relay_transport); |
279 | } |