Commit | Line | Data |
---|---|---|
b5b073e2 PMF |
1 | /* |
2 | * buffers.c | |
3 | * LTTng userspace tracer buffering system | |
4 | * | |
5 | * Copyright (C) 2009 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca) | |
6 | * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca) | |
7 | * | |
8 | * This library is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU Lesser General Public | |
10 | * License as published by the Free Software Foundation; either | |
11 | * version 2.1 of the License, or (at your option) any later version. | |
12 | * | |
13 | * This library is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * Lesser General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU Lesser General Public | |
19 | * License along with this library; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
22 | ||
204141ee | 23 | #include <unistd.h> |
b5b073e2 PMF |
24 | #include <sys/mman.h> |
25 | #include <sys/ipc.h> | |
26 | #include <sys/shm.h> | |
27 | #include <fcntl.h> | |
28 | #include <ust/kernelcompat.h> | |
29 | #include <kcompat/kref.h> | |
909bc43f | 30 | #include <stdlib.h> |
b5b073e2 PMF |
31 | #include "buffers.h" |
32 | #include "channels.h" | |
33 | #include "tracer.h" | |
34 | #include "tracercore.h" | |
35 | #include "usterr.h" | |
36 | ||
b73a4c47 PMF |
37 | struct ltt_reserve_switch_offsets { |
38 | long begin, end, old; | |
39 | long begin_switch, end_switch_current, end_switch_old; | |
40 | size_t before_hdr_pad, size; | |
41 | }; | |
42 | ||
43 | ||
b5b073e2 PMF |
44 | static DEFINE_MUTEX(ust_buffers_channels_mutex); |
45 | static LIST_HEAD(ust_buffers_channels); | |
46 | ||
204141ee PMF |
47 | static int get_n_cpus(void) |
48 | { | |
49 | int result; | |
50 | static int n_cpus = 0; | |
51 | ||
c7dc133c PMF |
52 | if(!n_cpus) { |
53 | /* On Linux, when some processors are offline | |
54 | * _SC_NPROCESSORS_CONF counts the offline | |
55 | * processors, whereas _SC_NPROCESSORS_ONLN | |
56 | * does not. If we used _SC_NPROCESSORS_ONLN, | |
57 | * getcpu() could return a value greater than | |
58 | * this sysconf, in which case the arrays | |
59 | * indexed by processor would overflow. | |
60 | */ | |
61 | result = sysconf(_SC_NPROCESSORS_CONF); | |
62 | if(result == -1) { | |
63 | return -1; | |
64 | } | |
65 | ||
66 | n_cpus = result; | |
204141ee PMF |
67 | } |
68 | ||
c7dc133c | 69 | return n_cpus; |
204141ee PMF |
70 | } |
71 | ||
b73a4c47 PMF |
72 | /* _ust_buffers_write() |
73 | * | |
74 | * @buf: destination buffer | |
75 | * @offset: offset in destination | |
76 | * @src: source buffer | |
77 | * @len: length of source | |
78 | * @cpy: already copied | |
79 | */ | |
80 | ||
81 | void _ust_buffers_write(struct ust_buffer *buf, size_t offset, | |
82 | const void *src, size_t len, ssize_t cpy) | |
83 | { | |
84 | do { | |
85 | len -= cpy; | |
86 | src += cpy; | |
87 | offset += cpy; | |
88 | ||
89 | WARN_ON(offset >= buf->buf_size); | |
90 | ||
91 | cpy = min_t(size_t, len, buf->buf_size - offset); | |
92 | ust_buffers_do_copy(buf->buf_data + offset, src, cpy); | |
93 | } while (unlikely(len != cpy)); | |
94 | } | |
95 | ||
96 | static int ust_buffers_init_buffer(struct ust_trace *trace, | |
b5b073e2 PMF |
97 | struct ust_channel *ltt_chan, |
98 | struct ust_buffer *buf, | |
99 | unsigned int n_subbufs); | |
100 | ||
101 | static int ust_buffers_alloc_buf(struct ust_buffer *buf, size_t *size) | |
102 | { | |
103 | void *ptr; | |
104 | int result; | |
105 | ||
106 | *size = PAGE_ALIGN(*size); | |
107 | ||
108 | result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700); | |
109 | if(result == -1 && errno == EINVAL) { | |
110 | ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased."); | |
111 | return -1; | |
112 | } | |
113 | else if(result == -1) { | |
114 | PERROR("shmget"); | |
115 | return -1; | |
116 | } | |
117 | ||
204141ee | 118 | /* FIXME: should have matching call to shmdt */ |
b5b073e2 PMF |
119 | ptr = shmat(buf->shmid, NULL, 0); |
120 | if(ptr == (void *) -1) { | |
121 | perror("shmat"); | |
122 | goto destroy_shmem; | |
123 | } | |
124 | ||
125 | /* Already mark the shared memory for destruction. This will occur only | |
126 | * when all users have detached. | |
127 | */ | |
128 | result = shmctl(buf->shmid, IPC_RMID, NULL); | |
129 | if(result == -1) { | |
130 | perror("shmctl"); | |
131 | return -1; | |
132 | } | |
133 | ||
134 | buf->buf_data = ptr; | |
135 | buf->buf_size = *size; | |
136 | ||
137 | return 0; | |
138 | ||
139 | destroy_shmem: | |
140 | result = shmctl(buf->shmid, IPC_RMID, NULL); | |
141 | if(result == -1) { | |
142 | perror("shmctl"); | |
143 | } | |
144 | ||
145 | return -1; | |
146 | } | |
147 | ||
204141ee | 148 | int ust_buffers_create_buf(struct ust_channel *channel, int cpu) |
b5b073e2 PMF |
149 | { |
150 | int result; | |
204141ee | 151 | struct ust_buffer *buf = channel->buf[cpu]; |
b5b073e2 | 152 | |
204141ee PMF |
153 | buf->cpu = cpu; |
154 | result = ust_buffers_alloc_buf(buf, &channel->alloc_size); | |
b5b073e2 | 155 | if(result) |
204141ee | 156 | return -1; |
b5b073e2 | 157 | |
204141ee | 158 | buf->chan = channel; |
b5b073e2 | 159 | kref_get(&channel->kref); |
204141ee | 160 | return 0; |
b5b073e2 PMF |
161 | } |
162 | ||
163 | static void ust_buffers_destroy_channel(struct kref *kref) | |
164 | { | |
165 | struct ust_channel *chan = container_of(kref, struct ust_channel, kref); | |
166 | free(chan); | |
167 | } | |
168 | ||
169 | static void ust_buffers_destroy_buf(struct ust_buffer *buf) | |
170 | { | |
171 | struct ust_channel *chan = buf->chan; | |
172 | int result; | |
173 | ||
174 | result = munmap(buf->buf_data, buf->buf_size); | |
175 | if(result == -1) { | |
176 | PERROR("munmap"); | |
177 | } | |
178 | ||
204141ee | 179 | //ust// chan->buf[buf->cpu] = NULL; |
b5b073e2 PMF |
180 | free(buf); |
181 | kref_put(&chan->kref, ust_buffers_destroy_channel); | |
182 | } | |
183 | ||
184 | /* called from kref_put */ | |
185 | static void ust_buffers_remove_buf(struct kref *kref) | |
186 | { | |
187 | struct ust_buffer *buf = container_of(kref, struct ust_buffer, kref); | |
188 | ust_buffers_destroy_buf(buf); | |
189 | } | |
190 | ||
204141ee | 191 | int ust_buffers_open_buf(struct ust_channel *chan, int cpu) |
b5b073e2 | 192 | { |
204141ee | 193 | int result; |
b5b073e2 | 194 | |
204141ee PMF |
195 | result = ust_buffers_create_buf(chan, cpu); |
196 | if (result == -1) | |
197 | return -1; | |
b5b073e2 | 198 | |
204141ee | 199 | kref_init(&chan->buf[cpu]->kref); |
b5b073e2 | 200 | |
204141ee PMF |
201 | result = ust_buffers_init_buffer(chan->trace, chan, chan->buf[cpu], chan->subbuf_cnt); |
202 | if(result == -1) | |
203 | return -1; | |
b5b073e2 | 204 | |
204141ee | 205 | return 0; |
b5b073e2 PMF |
206 | |
207 | /* FIXME: decrementally destroy on error? */ | |
208 | } | |
209 | ||
210 | /** | |
211 | * ust_buffers_close_buf - close a channel buffer | |
212 | * @buf: buffer | |
213 | */ | |
214 | static void ust_buffers_close_buf(struct ust_buffer *buf) | |
215 | { | |
216 | kref_put(&buf->kref, ust_buffers_remove_buf); | |
217 | } | |
218 | ||
219 | int ust_buffers_channel_open(struct ust_channel *chan, size_t subbuf_size, size_t subbuf_cnt) | |
220 | { | |
204141ee PMF |
221 | int i; |
222 | int result; | |
223 | ||
b5b073e2 PMF |
224 | if(subbuf_size == 0 || subbuf_cnt == 0) |
225 | return -1; | |
226 | ||
b73a4c47 PMF |
227 | /* Check that the subbuffer size is larger than a page. */ |
228 | WARN_ON_ONCE(subbuf_size < PAGE_SIZE); | |
229 | ||
230 | /* | |
231 | * Make sure the number of subbuffers and subbuffer size are power of 2. | |
232 | */ | |
233 | WARN_ON_ONCE(hweight32(subbuf_size) != 1); | |
234 | WARN_ON(hweight32(subbuf_cnt) != 1); | |
235 | ||
b5b073e2 PMF |
236 | chan->version = UST_CHANNEL_VERSION; |
237 | chan->subbuf_cnt = subbuf_cnt; | |
238 | chan->subbuf_size = subbuf_size; | |
239 | chan->subbuf_size_order = get_count_order(subbuf_size); | |
b73a4c47 | 240 | chan->alloc_size = subbuf_size * subbuf_cnt; |
204141ee | 241 | |
b5b073e2 PMF |
242 | kref_init(&chan->kref); |
243 | ||
244 | mutex_lock(&ust_buffers_channels_mutex); | |
204141ee PMF |
245 | for(i=0; i<chan->n_cpus; i++) { |
246 | result = ust_buffers_open_buf(chan, i); | |
247 | if (result == -1) | |
248 | goto error; | |
249 | } | |
b5b073e2 PMF |
250 | list_add(&chan->list, &ust_buffers_channels); |
251 | mutex_unlock(&ust_buffers_channels_mutex); | |
252 | ||
253 | return 0; | |
254 | ||
204141ee PMF |
255 | /* Jump directly inside the loop to close the buffers that were already |
256 | * opened. */ | |
257 | for(; i>=0; i--) { | |
258 | ust_buffers_close_buf(chan->buf[i]); | |
259 | error: | |
120b0ec3 | 260 | do {} while(0); |
204141ee PMF |
261 | } |
262 | ||
b5b073e2 PMF |
263 | kref_put(&chan->kref, ust_buffers_destroy_channel); |
264 | mutex_unlock(&ust_buffers_channels_mutex); | |
265 | return -1; | |
266 | } | |
267 | ||
268 | void ust_buffers_channel_close(struct ust_channel *chan) | |
269 | { | |
204141ee PMF |
270 | int i; |
271 | if(!chan) | |
b5b073e2 PMF |
272 | return; |
273 | ||
274 | mutex_lock(&ust_buffers_channels_mutex); | |
204141ee PMF |
275 | for(i=0; i<chan->n_cpus; i++) { |
276 | /* FIXME: if we make it here, then all buffers were necessarily allocated. Moreover, we don't | |
277 | * initialize to NULL so we cannot use this check. Should we? */ | |
278 | //ust// if (chan->buf[i]) | |
279 | ust_buffers_close_buf(chan->buf[i]); | |
280 | } | |
b5b073e2 PMF |
281 | |
282 | list_del(&chan->list); | |
283 | kref_put(&chan->kref, ust_buffers_destroy_channel); | |
284 | mutex_unlock(&ust_buffers_channels_mutex); | |
285 | } | |
286 | ||
b5b073e2 PMF |
287 | /* |
288 | * ------- | |
289 | */ | |
290 | ||
204141ee | 291 | static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan, int cpu); |
b5b073e2 PMF |
292 | |
293 | static void ltt_force_switch(struct ust_buffer *buf, | |
294 | enum force_switch_mode mode); | |
295 | ||
296 | /* | |
297 | * Trace callbacks | |
298 | */ | |
b73a4c47 | 299 | static void ltt_buffer_begin(struct ust_buffer *buf, |
b5b073e2 PMF |
300 | u64 tsc, unsigned int subbuf_idx) |
301 | { | |
302 | struct ust_channel *channel = buf->chan; | |
303 | struct ltt_subbuffer_header *header = | |
304 | (struct ltt_subbuffer_header *) | |
b73a4c47 | 305 | ust_buffers_offset_address(buf, |
b5b073e2 PMF |
306 | subbuf_idx * buf->chan->subbuf_size); |
307 | ||
308 | header->cycle_count_begin = tsc; | |
02af3e60 PMF |
309 | header->data_size = 0xFFFFFFFF; /* for recognizing crashed buffers */ |
310 | header->sb_size = 0xFFFFFFFF; /* for recognizing crashed buffers */ | |
311 | /* FIXME: add memory barrier? */ | |
b5b073e2 PMF |
312 | ltt_write_trace_header(channel->trace, header); |
313 | } | |
314 | ||
315 | /* | |
316 | * offset is assumed to never be 0 here : never deliver a completely empty | |
317 | * subbuffer. The lost size is between 0 and subbuf_size-1. | |
318 | */ | |
b73a4c47 | 319 | static notrace void ltt_buffer_end(struct ust_buffer *buf, |
b5b073e2 PMF |
320 | u64 tsc, unsigned int offset, unsigned int subbuf_idx) |
321 | { | |
322 | struct ltt_subbuffer_header *header = | |
323 | (struct ltt_subbuffer_header *) | |
b73a4c47 | 324 | ust_buffers_offset_address(buf, |
b5b073e2 | 325 | subbuf_idx * buf->chan->subbuf_size); |
8c36d1ee | 326 | u32 data_size = SUBBUF_OFFSET(offset - 1, buf->chan) + 1; |
b5b073e2 | 327 | |
8c36d1ee PMF |
328 | header->data_size = data_size; |
329 | header->sb_size = PAGE_ALIGN(data_size); | |
b5b073e2 | 330 | header->cycle_count_end = tsc; |
b102c2b0 PMF |
331 | header->events_lost = uatomic_read(&buf->events_lost); |
332 | header->subbuf_corrupt = uatomic_read(&buf->corrupted_subbuffers); | |
719569e4 PMF |
333 | if(unlikely(header->events_lost > 0)) { |
334 | DBG("Some events (%d) were lost in %s_%d", header->events_lost, buf->chan->channel_name, buf->cpu); | |
335 | } | |
b5b073e2 PMF |
336 | } |
337 | ||
338 | /* | |
339 | * This function should not be called from NMI interrupt context | |
340 | */ | |
341 | static notrace void ltt_buf_unfull(struct ust_buffer *buf, | |
342 | unsigned int subbuf_idx, | |
343 | long offset) | |
344 | { | |
b5b073e2 PMF |
345 | } |
346 | ||
b73a4c47 PMF |
347 | /* |
348 | * Promote compiler barrier to a smp_mb(). | |
349 | * For the specific LTTng case, this IPI call should be removed if the | |
350 | * architecture does not reorder writes. This should eventually be provided by | |
351 | * a separate architecture-specific infrastructure. | |
352 | */ | |
e17571a5 PMF |
353 | //ust// static void remote_mb(void *info) |
354 | //ust// { | |
355 | //ust// smp_mb(); | |
356 | //ust// } | |
b73a4c47 PMF |
357 | |
358 | int ust_buffers_get_subbuf(struct ust_buffer *buf, long *consumed) | |
b5b073e2 PMF |
359 | { |
360 | struct ust_channel *channel = buf->chan; | |
361 | long consumed_old, consumed_idx, commit_count, write_offset; | |
b73a4c47 PMF |
362 | //ust// int retval; |
363 | ||
b102c2b0 | 364 | consumed_old = uatomic_read(&buf->consumed); |
b5b073e2 | 365 | consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan); |
b102c2b0 | 366 | commit_count = uatomic_read(&buf->commit_count[consumed_idx].cc_sb); |
b5b073e2 PMF |
367 | /* |
368 | * Make sure we read the commit count before reading the buffer | |
369 | * data and the write offset. Correct consumed offset ordering | |
370 | * wrt commit count is insured by the use of cmpxchg to update | |
371 | * the consumed offset. | |
b73a4c47 PMF |
372 | * smp_call_function_single can fail if the remote CPU is offline, |
373 | * this is OK because then there is no wmb to execute there. | |
374 | * If our thread is executing on the same CPU as the on the buffers | |
375 | * belongs to, we don't have to synchronize it at all. If we are | |
376 | * migrated, the scheduler will take care of the memory barriers. | |
377 | * Normally, smp_call_function_single() should ensure program order when | |
378 | * executing the remote function, which implies that it surrounds the | |
379 | * function execution with : | |
380 | * smp_mb() | |
381 | * send IPI | |
382 | * csd_lock_wait | |
383 | * recv IPI | |
384 | * smp_mb() | |
385 | * exec. function | |
386 | * smp_mb() | |
387 | * csd unlock | |
388 | * smp_mb() | |
389 | * | |
390 | * However, smp_call_function_single() does not seem to clearly execute | |
391 | * such barriers. It depends on spinlock semantic to provide the barrier | |
392 | * before executing the IPI and, when busy-looping, csd_lock_wait only | |
393 | * executes smp_mb() when it has to wait for the other CPU. | |
394 | * | |
395 | * I don't trust this code. Therefore, let's add the smp_mb() sequence | |
396 | * required ourself, even if duplicated. It has no performance impact | |
397 | * anyway. | |
398 | * | |
399 | * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs | |
400 | * read and write vs write. They do not ensure core synchronization. We | |
401 | * really have to ensure total order between the 3 barriers running on | |
402 | * the 2 CPUs. | |
403 | */ | |
404 | //ust// #ifdef LTT_NO_IPI_BARRIER | |
405 | /* | |
406 | * Local rmb to match the remote wmb to read the commit count before the | |
407 | * buffer data and the write offset. | |
b5b073e2 PMF |
408 | */ |
409 | smp_rmb(); | |
b73a4c47 PMF |
410 | //ust// #else |
411 | //ust// if (raw_smp_processor_id() != buf->cpu) { | |
412 | //ust// smp_mb(); /* Total order with IPI handler smp_mb() */ | |
413 | //ust// smp_call_function_single(buf->cpu, remote_mb, NULL, 1); | |
414 | //ust// smp_mb(); /* Total order with IPI handler smp_mb() */ | |
415 | //ust// } | |
416 | //ust// #endif | |
417 | ||
b102c2b0 | 418 | write_offset = uatomic_read(&buf->offset); |
b5b073e2 PMF |
419 | /* |
420 | * Check that the subbuffer we are trying to consume has been | |
421 | * already fully committed. | |
422 | */ | |
423 | if (((commit_count - buf->chan->subbuf_size) | |
424 | & channel->commit_count_mask) | |
425 | - (BUFFER_TRUNC(consumed_old, buf->chan) | |
426 | >> channel->n_subbufs_order) | |
427 | != 0) { | |
428 | return -EAGAIN; | |
429 | } | |
430 | /* | |
431 | * Check that we are not about to read the same subbuffer in | |
432 | * which the writer head is. | |
433 | */ | |
434 | if ((SUBBUF_TRUNC(write_offset, buf->chan) | |
435 | - SUBBUF_TRUNC(consumed_old, buf->chan)) | |
436 | == 0) { | |
437 | return -EAGAIN; | |
438 | } | |
439 | ||
b73a4c47 PMF |
440 | /* FIXME: is this ok to disable the reading feature? */ |
441 | //ust// retval = update_read_sb_index(buf, consumed_idx); | |
442 | //ust// if (retval) | |
443 | //ust// return retval; | |
444 | ||
445 | *consumed = consumed_old; | |
446 | ||
b5b073e2 PMF |
447 | return 0; |
448 | } | |
449 | ||
b73a4c47 | 450 | int ust_buffers_put_subbuf(struct ust_buffer *buf, unsigned long uconsumed_old) |
b5b073e2 PMF |
451 | { |
452 | long consumed_new, consumed_old; | |
453 | ||
b102c2b0 | 454 | consumed_old = uatomic_read(&buf->consumed); |
b5b073e2 PMF |
455 | consumed_old = consumed_old & (~0xFFFFFFFFL); |
456 | consumed_old = consumed_old | uconsumed_old; | |
457 | consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan); | |
458 | ||
459 | //ust// spin_lock(<t_buf->full_lock); | |
b102c2b0 | 460 | if (uatomic_cmpxchg(&buf->consumed, consumed_old, |
b5b073e2 PMF |
461 | consumed_new) |
462 | != consumed_old) { | |
463 | /* We have been pushed by the writer : the last | |
464 | * buffer read _is_ corrupted! It can also | |
465 | * happen if this is a buffer we never got. */ | |
466 | //ust// spin_unlock(<t_buf->full_lock); | |
467 | return -EIO; | |
468 | } else { | |
469 | /* tell the client that buffer is now unfull */ | |
470 | int index; | |
471 | long data; | |
472 | index = SUBBUF_INDEX(consumed_old, buf->chan); | |
473 | data = BUFFER_OFFSET(consumed_old, buf->chan); | |
474 | ltt_buf_unfull(buf, index, data); | |
475 | //ust// spin_unlock(<t_buf->full_lock); | |
476 | } | |
477 | return 0; | |
478 | } | |
479 | ||
b73a4c47 PMF |
480 | //ust// static void switch_buffer(unsigned long data) |
481 | //ust// { | |
482 | //ust// struct ltt_channel_buf_struct *ltt_buf = | |
483 | //ust// (struct ltt_channel_buf_struct *)data; | |
484 | //ust// struct rchan_buf *buf = ltt_buf->rbuf; | |
485 | //ust// | |
486 | //ust// if (buf) | |
487 | //ust// ltt_force_switch(buf, FORCE_ACTIVE); | |
488 | //ust// | |
489 | //ust// ltt_buf->switch_timer.expires += ltt_buf->switch_timer_interval; | |
490 | //ust// add_timer_on(<t_buf->switch_timer, smp_processor_id()); | |
491 | //ust// } | |
492 | //ust// | |
493 | //ust// static void start_switch_timer(struct ltt_channel_struct *ltt_channel) | |
494 | //ust// { | |
495 | //ust// struct rchan *rchan = ltt_channel->trans_channel_data; | |
496 | //ust// int cpu; | |
497 | //ust// | |
498 | //ust// if (!ltt_channel->switch_timer_interval) | |
499 | //ust// return; | |
500 | //ust// | |
501 | //ust// // TODO : hotplug | |
502 | //ust// for_each_online_cpu(cpu) { | |
503 | //ust// struct ltt_channel_buf_struct *ltt_buf; | |
504 | //ust// struct rchan_buf *buf; | |
505 | //ust// | |
506 | //ust// buf = rchan->buf[cpu]; | |
507 | //ust// ltt_buf = buf->chan_private; | |
508 | //ust// buf->random_access = 1; | |
509 | //ust// ltt_buf->switch_timer_interval = | |
510 | //ust// ltt_channel->switch_timer_interval; | |
511 | //ust// init_timer(<t_buf->switch_timer); | |
512 | //ust// ltt_buf->switch_timer.function = switch_buffer; | |
513 | //ust// ltt_buf->switch_timer.expires = jiffies + | |
514 | //ust// ltt_buf->switch_timer_interval; | |
515 | //ust// ltt_buf->switch_timer.data = (unsigned long)ltt_buf; | |
516 | //ust// add_timer_on(<t_buf->switch_timer, cpu); | |
517 | //ust// } | |
518 | //ust// } | |
519 | //ust// | |
520 | //ust// /* | |
521 | //ust// * Cannot use del_timer_sync with add_timer_on, so use an IPI to locally | |
522 | //ust// * delete the timer. | |
523 | //ust// */ | |
524 | //ust// static void stop_switch_timer_ipi(void *info) | |
525 | //ust// { | |
526 | //ust// struct ltt_channel_buf_struct *ltt_buf = | |
527 | //ust// (struct ltt_channel_buf_struct *)info; | |
528 | //ust// | |
529 | //ust// del_timer(<t_buf->switch_timer); | |
530 | //ust// } | |
531 | //ust// | |
532 | //ust// static void stop_switch_timer(struct ltt_channel_struct *ltt_channel) | |
533 | //ust// { | |
534 | //ust// struct rchan *rchan = ltt_channel->trans_channel_data; | |
535 | //ust// int cpu; | |
536 | //ust// | |
537 | //ust// if (!ltt_channel->switch_timer_interval) | |
538 | //ust// return; | |
539 | //ust// | |
540 | //ust// // TODO : hotplug | |
541 | //ust// for_each_online_cpu(cpu) { | |
542 | //ust// struct ltt_channel_buf_struct *ltt_buf; | |
543 | //ust// struct rchan_buf *buf; | |
544 | //ust// | |
545 | //ust// buf = rchan->buf[cpu]; | |
546 | //ust// ltt_buf = buf->chan_private; | |
547 | //ust// smp_call_function(stop_switch_timer_ipi, ltt_buf, 1); | |
548 | //ust// buf->random_access = 0; | |
549 | //ust// } | |
550 | //ust// } | |
551 | ||
e17571a5 PMF |
552 | //ust// static void ust_buffers_print_written(struct ust_channel *chan, |
553 | //ust// long cons_off, unsigned int cpu) | |
554 | //ust// { | |
555 | //ust// struct ust_buffer *buf = chan->buf[cpu]; | |
556 | //ust// long cons_idx, events_count; | |
557 | //ust// | |
558 | //ust// cons_idx = SUBBUF_INDEX(cons_off, chan); | |
b102c2b0 | 559 | //ust// events_count = uatomic_read(&buf->commit_count[cons_idx].events); |
e17571a5 PMF |
560 | //ust// |
561 | //ust// if (events_count) | |
562 | //ust// printk(KERN_INFO | |
563 | //ust// "channel %s: %lu events written (cpu %u, index %lu)\n", | |
564 | //ust// chan->channel_name, events_count, cpu, cons_idx); | |
565 | //ust// } | |
b73a4c47 | 566 | |
b5b073e2 PMF |
567 | static void ltt_relay_print_subbuffer_errors( |
568 | struct ust_channel *channel, | |
204141ee | 569 | long cons_off, int cpu) |
b5b073e2 | 570 | { |
204141ee | 571 | struct ust_buffer *ltt_buf = channel->buf[cpu]; |
b73a4c47 | 572 | long cons_idx, commit_count, commit_count_sb, write_offset; |
b5b073e2 PMF |
573 | |
574 | cons_idx = SUBBUF_INDEX(cons_off, channel); | |
b102c2b0 PMF |
575 | commit_count = uatomic_read(<t_buf->commit_count[cons_idx].cc); |
576 | commit_count_sb = uatomic_read(<t_buf->commit_count[cons_idx].cc_sb); | |
b73a4c47 | 577 | |
b5b073e2 PMF |
578 | /* |
579 | * No need to order commit_count and write_offset reads because we | |
580 | * execute after trace is stopped when there are no readers left. | |
581 | */ | |
b102c2b0 | 582 | write_offset = uatomic_read(<t_buf->offset); |
b5b073e2 | 583 | WARN( "LTT : unread channel %s offset is %ld " |
b73a4c47 PMF |
584 | "and cons_off : %ld (cpu %d)\n", |
585 | channel->channel_name, write_offset, cons_off, cpu); | |
b5b073e2 PMF |
586 | /* Check each sub-buffer for non filled commit count */ |
587 | if (((commit_count - channel->subbuf_size) & channel->commit_count_mask) | |
588 | - (BUFFER_TRUNC(cons_off, channel) >> channel->n_subbufs_order) != 0) { | |
589 | ERR("LTT : %s : subbuffer %lu has non filled " | |
b73a4c47 PMF |
590 | "commit count [cc, cc_sb] [%lu,%lu].\n", |
591 | channel->channel_name, cons_idx, commit_count, commit_count_sb); | |
b5b073e2 PMF |
592 | } |
593 | ERR("LTT : %s : commit count : %lu, subbuf size %zd\n", | |
594 | channel->channel_name, commit_count, | |
595 | channel->subbuf_size); | |
596 | } | |
597 | ||
b73a4c47 | 598 | static void ltt_relay_print_errors(struct ust_trace *trace, |
204141ee | 599 | struct ust_channel *channel, int cpu) |
b5b073e2 | 600 | { |
204141ee | 601 | struct ust_buffer *ltt_buf = channel->buf[cpu]; |
b5b073e2 PMF |
602 | long cons_off; |
603 | ||
4292ed8a PMF |
604 | /* |
605 | * Can be called in the error path of allocation when | |
606 | * trans_channel_data is not yet set. | |
607 | */ | |
608 | if (!channel) | |
609 | return; | |
610 | ||
e17571a5 PMF |
611 | //ust// for (cons_off = 0; cons_off < rchan->alloc_size; |
612 | //ust// cons_off = SUBBUF_ALIGN(cons_off, rchan)) | |
613 | //ust// ust_buffers_print_written(ltt_chan, cons_off, cpu); | |
b102c2b0 PMF |
614 | for (cons_off = uatomic_read(<t_buf->consumed); |
615 | (SUBBUF_TRUNC(uatomic_read(<t_buf->offset), | |
b5b073e2 PMF |
616 | channel) |
617 | - cons_off) > 0; | |
618 | cons_off = SUBBUF_ALIGN(cons_off, channel)) | |
204141ee | 619 | ltt_relay_print_subbuffer_errors(channel, cons_off, cpu); |
b5b073e2 PMF |
620 | } |
621 | ||
204141ee | 622 | static void ltt_relay_print_buffer_errors(struct ust_channel *channel, int cpu) |
b5b073e2 | 623 | { |
b73a4c47 | 624 | struct ust_trace *trace = channel->trace; |
204141ee | 625 | struct ust_buffer *ltt_buf = channel->buf[cpu]; |
b5b073e2 | 626 | |
b102c2b0 | 627 | if (uatomic_read(<t_buf->events_lost)) |
b73a4c47 | 628 | ERR("channel %s: %ld events lost (cpu %d)", |
b5b073e2 | 629 | channel->channel_name, |
b102c2b0 PMF |
630 | uatomic_read(<t_buf->events_lost), cpu); |
631 | if (uatomic_read(<t_buf->corrupted_subbuffers)) | |
b73a4c47 | 632 | ERR("channel %s : %ld corrupted subbuffers (cpu %d)", |
b5b073e2 | 633 | channel->channel_name, |
b102c2b0 | 634 | uatomic_read(<t_buf->corrupted_subbuffers), cpu); |
b5b073e2 | 635 | |
204141ee | 636 | ltt_relay_print_errors(trace, channel, cpu); |
b5b073e2 PMF |
637 | } |
638 | ||
639 | static void ltt_relay_release_channel(struct kref *kref) | |
640 | { | |
641 | struct ust_channel *ltt_chan = container_of(kref, | |
642 | struct ust_channel, kref); | |
643 | free(ltt_chan->buf); | |
644 | } | |
645 | ||
646 | /* | |
647 | * Create ltt buffer. | |
648 | */ | |
b73a4c47 | 649 | //ust// static int ltt_relay_create_buffer(struct ust_trace *trace, |
b5b073e2 PMF |
650 | //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf, |
651 | //ust// unsigned int cpu, unsigned int n_subbufs) | |
652 | //ust// { | |
653 | //ust// struct ltt_channel_buf_struct *ltt_buf = | |
654 | //ust// percpu_ptr(ltt_chan->buf, cpu); | |
655 | //ust// unsigned int j; | |
b73a4c47 | 656 | //ust// |
b5b073e2 PMF |
657 | //ust// ltt_buf->commit_count = |
658 | //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs, | |
659 | //ust// GFP_KERNEL, cpu_to_node(cpu)); | |
660 | //ust// if (!ltt_buf->commit_count) | |
661 | //ust// return -ENOMEM; | |
662 | //ust// kref_get(&trace->kref); | |
663 | //ust// kref_get(&trace->ltt_transport_kref); | |
664 | //ust// kref_get(<t_chan->kref); | |
b102c2b0 PMF |
665 | //ust// uatomic_set(<t_buf->offset, ltt_subbuffer_header_size()); |
666 | //ust// uatomic_set(<t_buf->consumed, 0); | |
667 | //ust// uatomic_set(<t_buf->active_readers, 0); | |
b5b073e2 | 668 | //ust// for (j = 0; j < n_subbufs; j++) |
b102c2b0 | 669 | //ust// uatomic_set(<t_buf->commit_count[j], 0); |
b5b073e2 | 670 | //ust// init_waitqueue_head(<t_buf->write_wait); |
b102c2b0 | 671 | //ust// uatomic_set(<t_buf->wakeup_readers, 0); |
b5b073e2 | 672 | //ust// spin_lock_init(<t_buf->full_lock); |
b73a4c47 | 673 | //ust// |
b5b073e2 PMF |
674 | //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0); |
675 | //ust// /* atomic_add made on local variable on data that belongs to | |
676 | //ust// * various CPUs : ok because tracing not started (for this cpu). */ | |
b102c2b0 | 677 | //ust// uatomic_add(<t_buf->commit_count[0], ltt_subbuffer_header_size()); |
b73a4c47 | 678 | //ust// |
b102c2b0 PMF |
679 | //ust// uatomic_set(<t_buf->events_lost, 0); |
680 | //ust// uatomic_set(<t_buf->corrupted_subbuffers, 0); | |
b73a4c47 | 681 | //ust// |
b5b073e2 PMF |
682 | //ust// return 0; |
683 | //ust// } | |
684 | ||
b73a4c47 | 685 | static int ust_buffers_init_buffer(struct ust_trace *trace, |
b5b073e2 PMF |
686 | struct ust_channel *ltt_chan, struct ust_buffer *buf, |
687 | unsigned int n_subbufs) | |
688 | { | |
689 | unsigned int j; | |
690 | int fds[2]; | |
691 | int result; | |
692 | ||
693 | buf->commit_count = | |
b73a4c47 | 694 | zmalloc(sizeof(*buf->commit_count) * n_subbufs); |
b5b073e2 PMF |
695 | if (!buf->commit_count) |
696 | return -ENOMEM; | |
697 | kref_get(&trace->kref); | |
698 | kref_get(&trace->ltt_transport_kref); | |
699 | kref_get(<t_chan->kref); | |
b102c2b0 PMF |
700 | uatomic_set(&buf->offset, ltt_subbuffer_header_size()); |
701 | uatomic_set(&buf->consumed, 0); | |
702 | uatomic_set(&buf->active_readers, 0); | |
b73a4c47 | 703 | for (j = 0; j < n_subbufs; j++) { |
b102c2b0 PMF |
704 | uatomic_set(&buf->commit_count[j].cc, 0); |
705 | uatomic_set(&buf->commit_count[j].cc_sb, 0); | |
b73a4c47 | 706 | } |
b5b073e2 | 707 | //ust// init_waitqueue_head(&buf->write_wait); |
b102c2b0 | 708 | //ust// uatomic_set(&buf->wakeup_readers, 0); |
b5b073e2 PMF |
709 | //ust// spin_lock_init(&buf->full_lock); |
710 | ||
b73a4c47 | 711 | ltt_buffer_begin(buf, trace->start_tsc, 0); |
b5b073e2 | 712 | |
b102c2b0 | 713 | uatomic_add(&buf->commit_count[0].cc, ltt_subbuffer_header_size()); |
b5b073e2 | 714 | |
b102c2b0 PMF |
715 | uatomic_set(&buf->events_lost, 0); |
716 | uatomic_set(&buf->corrupted_subbuffers, 0); | |
b5b073e2 PMF |
717 | |
718 | result = pipe(fds); | |
719 | if(result == -1) { | |
720 | PERROR("pipe"); | |
721 | return -1; | |
722 | } | |
723 | buf->data_ready_fd_read = fds[0]; | |
724 | buf->data_ready_fd_write = fds[1]; | |
725 | ||
726 | /* FIXME: do we actually need this? */ | |
727 | result = fcntl(fds[0], F_SETFL, O_NONBLOCK); | |
728 | if(result == -1) { | |
729 | PERROR("fcntl"); | |
730 | } | |
731 | ||
732 | //ust// buf->commit_seq = malloc(sizeof(buf->commit_seq) * n_subbufs); | |
733 | //ust// if(!ltt_buf->commit_seq) { | |
734 | //ust// return -1; | |
735 | //ust// } | |
37315729 | 736 | memset(buf->commit_seq, 0, sizeof(buf->commit_seq[0]) * n_subbufs); |
b5b073e2 PMF |
737 | |
738 | /* FIXME: decrementally destroy on error */ | |
739 | ||
740 | return 0; | |
741 | } | |
742 | ||
743 | /* FIXME: use this function */ | |
204141ee | 744 | static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan, int cpu) |
b5b073e2 | 745 | { |
b73a4c47 | 746 | struct ust_trace *trace = ltt_chan->trace; |
204141ee | 747 | struct ust_buffer *ltt_buf = ltt_chan->buf[cpu]; |
b5b073e2 PMF |
748 | |
749 | kref_put(<t_chan->trace->ltt_transport_kref, | |
750 | ltt_release_transport); | |
204141ee | 751 | ltt_relay_print_buffer_errors(ltt_chan, cpu); |
b5b073e2 | 752 | //ust// free(ltt_buf->commit_seq); |
909bc43f | 753 | free(ltt_buf->commit_count); |
b5b073e2 PMF |
754 | ltt_buf->commit_count = NULL; |
755 | kref_put(<t_chan->kref, ltt_relay_release_channel); | |
756 | kref_put(&trace->kref, ltt_release_trace); | |
757 | //ust// wake_up_interruptible(&trace->kref_wq); | |
758 | } | |
759 | ||
204141ee | 760 | static int ust_buffers_alloc_channel_buf_structs(struct ust_channel *chan) |
b5b073e2 PMF |
761 | { |
762 | void *ptr; | |
763 | int result; | |
204141ee PMF |
764 | size_t size; |
765 | int i; | |
b5b073e2 | 766 | |
204141ee | 767 | size = PAGE_ALIGN(1); |
b5b073e2 | 768 | |
204141ee | 769 | for(i=0; i<chan->n_cpus; i++) { |
b5b073e2 | 770 | |
204141ee PMF |
771 | result = chan->buf_struct_shmids[i] = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700); |
772 | if(result == -1) { | |
773 | PERROR("shmget"); | |
774 | goto destroy_previous; | |
775 | } | |
b5b073e2 | 776 | |
204141ee PMF |
777 | /* FIXME: should have matching call to shmdt */ |
778 | ptr = shmat(chan->buf_struct_shmids[i], NULL, 0); | |
779 | if(ptr == (void *) -1) { | |
780 | perror("shmat"); | |
781 | goto destroy_shm; | |
782 | } | |
783 | ||
784 | /* Already mark the shared memory for destruction. This will occur only | |
785 | * when all users have detached. | |
786 | */ | |
787 | result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL); | |
788 | if(result == -1) { | |
789 | perror("shmctl"); | |
790 | goto destroy_previous; | |
791 | } | |
792 | ||
793 | chan->buf[i] = ptr; | |
b5b073e2 PMF |
794 | } |
795 | ||
204141ee | 796 | return 0; |
b5b073e2 | 797 | |
204141ee PMF |
798 | /* Jumping inside this loop occurs from within the other loop above with i as |
799 | * counter, so it unallocates the structures for the cpu = current_i down to | |
800 | * zero. */ | |
801 | for(; i>=0; i--) { | |
802 | destroy_shm: | |
803 | result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL); | |
804 | if(result == -1) { | |
805 | perror("shmctl"); | |
806 | } | |
b5b073e2 | 807 | |
204141ee PMF |
808 | destroy_previous: |
809 | continue; | |
b5b073e2 PMF |
810 | } |
811 | ||
204141ee | 812 | return -1; |
b5b073e2 PMF |
813 | } |
814 | ||
815 | /* | |
816 | * Create channel. | |
817 | */ | |
b73a4c47 | 818 | static int ust_buffers_create_channel(const char *trace_name, struct ust_trace *trace, |
b5b073e2 PMF |
819 | const char *channel_name, struct ust_channel *ltt_chan, |
820 | unsigned int subbuf_size, unsigned int n_subbufs, int overwrite) | |
821 | { | |
b5b073e2 PMF |
822 | int result; |
823 | ||
824 | kref_init(<t_chan->kref); | |
825 | ||
826 | ltt_chan->trace = trace; | |
b5b073e2 PMF |
827 | ltt_chan->overwrite = overwrite; |
828 | ltt_chan->n_subbufs_order = get_count_order(n_subbufs); | |
829 | ltt_chan->commit_count_mask = (~0UL >> ltt_chan->n_subbufs_order); | |
204141ee | 830 | ltt_chan->n_cpus = get_n_cpus(); |
b5b073e2 | 831 | //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map); |
204141ee PMF |
832 | ltt_chan->buf = (void *) malloc(ltt_chan->n_cpus * sizeof(void *)); |
833 | if(ltt_chan->buf == NULL) { | |
834 | goto error; | |
835 | } | |
836 | ltt_chan->buf_struct_shmids = (int *) malloc(ltt_chan->n_cpus * sizeof(int)); | |
837 | if(ltt_chan->buf_struct_shmids == NULL) | |
838 | goto free_buf; | |
b5b073e2 | 839 | |
204141ee PMF |
840 | result = ust_buffers_alloc_channel_buf_structs(ltt_chan); |
841 | if(result != 0) { | |
842 | goto free_buf_struct_shmids; | |
843 | } | |
b5b073e2 | 844 | |
b5b073e2 | 845 | result = ust_buffers_channel_open(ltt_chan, subbuf_size, n_subbufs); |
204141ee | 846 | if (result != 0) { |
c1f20530 | 847 | ERR("Cannot open channel for trace %s", trace_name); |
204141ee | 848 | goto unalloc_buf_structs; |
b5b073e2 PMF |
849 | } |
850 | ||
204141ee PMF |
851 | return 0; |
852 | ||
853 | unalloc_buf_structs: | |
854 | /* FIXME: put a call here to unalloc the buf structs! */ | |
855 | ||
856 | free_buf_struct_shmids: | |
857 | free(ltt_chan->buf_struct_shmids); | |
b5b073e2 | 858 | |
204141ee PMF |
859 | free_buf: |
860 | free(ltt_chan->buf); | |
861 | ||
862 | error: | |
863 | return -1; | |
b5b073e2 PMF |
864 | } |
865 | ||
866 | /* | |
867 | * LTTng channel flush function. | |
868 | * | |
869 | * Must be called when no tracing is active in the channel, because of | |
870 | * accesses across CPUs. | |
871 | */ | |
872 | static notrace void ltt_relay_buffer_flush(struct ust_buffer *buf) | |
873 | { | |
874 | int result; | |
875 | ||
876 | //ust// buf->finalized = 1; | |
877 | ltt_force_switch(buf, FORCE_FLUSH); | |
878 | ||
879 | result = write(buf->data_ready_fd_write, "1", 1); | |
880 | if(result == -1) { | |
881 | PERROR("write (in ltt_relay_buffer_flush)"); | |
882 | ERR("this should never happen!"); | |
883 | } | |
884 | } | |
885 | ||
886 | static void ltt_relay_async_wakeup_chan(struct ust_channel *ltt_channel) | |
887 | { | |
888 | //ust// unsigned int i; | |
889 | //ust// struct rchan *rchan = ltt_channel->trans_channel_data; | |
890 | //ust// | |
891 | //ust// for_each_possible_cpu(i) { | |
892 | //ust// struct ltt_channel_buf_struct *ltt_buf = | |
893 | //ust// percpu_ptr(ltt_channel->buf, i); | |
894 | //ust// | |
b102c2b0 PMF |
895 | //ust// if (uatomic_read(<t_buf->wakeup_readers) == 1) { |
896 | //ust// uatomic_set(<t_buf->wakeup_readers, 0); | |
b5b073e2 PMF |
897 | //ust// wake_up_interruptible(&rchan->buf[i]->read_wait); |
898 | //ust// } | |
899 | //ust// } | |
900 | } | |
901 | ||
204141ee | 902 | static void ltt_relay_finish_buffer(struct ust_channel *channel, unsigned int cpu) |
b5b073e2 PMF |
903 | { |
904 | // int result; | |
905 | ||
204141ee PMF |
906 | if (channel->buf[cpu]) { |
907 | struct ust_buffer *buf = channel->buf[cpu]; | |
b5b073e2 PMF |
908 | ltt_relay_buffer_flush(buf); |
909 | //ust// ltt_relay_wake_writers(ltt_buf); | |
910 | /* closing the pipe tells the consumer the buffer is finished */ | |
911 | ||
912 | //result = write(ltt_buf->data_ready_fd_write, "D", 1); | |
913 | //if(result == -1) { | |
914 | // PERROR("write (in ltt_relay_finish_buffer)"); | |
915 | // ERR("this should never happen!"); | |
916 | //} | |
917 | close(buf->data_ready_fd_write); | |
918 | } | |
919 | } | |
920 | ||
921 | ||
922 | static void ltt_relay_finish_channel(struct ust_channel *channel) | |
923 | { | |
204141ee | 924 | unsigned int i; |
b5b073e2 | 925 | |
204141ee PMF |
926 | for(i=0; i<channel->n_cpus; i++) { |
927 | ltt_relay_finish_buffer(channel, i); | |
928 | } | |
b5b073e2 PMF |
929 | } |
930 | ||
931 | static void ltt_relay_remove_channel(struct ust_channel *channel) | |
932 | { | |
933 | ust_buffers_channel_close(channel); | |
934 | kref_put(&channel->kref, ltt_relay_release_channel); | |
935 | } | |
936 | ||
b73a4c47 PMF |
937 | //ust// /* |
938 | //ust// * Returns : | |
939 | //ust// * 0 if ok | |
940 | //ust// * !0 if execution must be aborted. | |
941 | //ust// */ | |
942 | //ust// static inline int ltt_relay_try_reserve( | |
943 | //ust// struct ust_channel *channel, struct ust_buffer *buf, | |
944 | //ust// struct ltt_reserve_switch_offsets *offsets, size_t data_size, | |
945 | //ust// u64 *tsc, unsigned int *rflags, int largest_align) | |
946 | //ust// { | |
b102c2b0 | 947 | //ust// offsets->begin = uatomic_read(&buf->offset); |
b73a4c47 PMF |
948 | //ust// offsets->old = offsets->begin; |
949 | //ust// offsets->begin_switch = 0; | |
950 | //ust// offsets->end_switch_current = 0; | |
951 | //ust// offsets->end_switch_old = 0; | |
952 | //ust// | |
953 | //ust// *tsc = trace_clock_read64(); | |
954 | //ust// if (last_tsc_overflow(buf, *tsc)) | |
955 | //ust// *rflags = LTT_RFLAG_ID_SIZE_TSC; | |
956 | //ust// | |
957 | //ust// if (SUBBUF_OFFSET(offsets->begin, buf->chan) == 0) { | |
958 | //ust// offsets->begin_switch = 1; /* For offsets->begin */ | |
959 | //ust// } else { | |
960 | //ust// offsets->size = ust_get_header_size(channel, | |
961 | //ust// offsets->begin, data_size, | |
962 | //ust// &offsets->before_hdr_pad, *rflags); | |
963 | //ust// offsets->size += ltt_align(offsets->begin + offsets->size, | |
964 | //ust// largest_align) | |
965 | //ust// + data_size; | |
966 | //ust// if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size) | |
967 | //ust// > buf->chan->subbuf_size) { | |
968 | //ust// offsets->end_switch_old = 1; /* For offsets->old */ | |
969 | //ust// offsets->begin_switch = 1; /* For offsets->begin */ | |
970 | //ust// } | |
971 | //ust// } | |
972 | //ust// if (offsets->begin_switch) { | |
973 | //ust// long subbuf_index; | |
974 | //ust// | |
975 | //ust// if (offsets->end_switch_old) | |
976 | //ust// offsets->begin = SUBBUF_ALIGN(offsets->begin, | |
977 | //ust// buf->chan); | |
978 | //ust// offsets->begin = offsets->begin + ltt_subbuffer_header_size(); | |
979 | //ust// /* Test new buffer integrity */ | |
980 | //ust// subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan); | |
981 | //ust// offsets->reserve_commit_diff = | |
982 | //ust// (BUFFER_TRUNC(offsets->begin, buf->chan) | |
983 | //ust// >> channel->n_subbufs_order) | |
b102c2b0 | 984 | //ust// - (uatomic_read(&buf->commit_count[subbuf_index]) |
b73a4c47 PMF |
985 | //ust// & channel->commit_count_mask); |
986 | //ust// if (offsets->reserve_commit_diff == 0) { | |
987 | //ust// long consumed; | |
988 | //ust// | |
b102c2b0 | 989 | //ust// consumed = uatomic_read(&buf->consumed); |
b73a4c47 PMF |
990 | //ust// |
991 | //ust// /* Next buffer not corrupted. */ | |
992 | //ust// if (!channel->overwrite && | |
993 | //ust// (SUBBUF_TRUNC(offsets->begin, buf->chan) | |
994 | //ust// - SUBBUF_TRUNC(consumed, buf->chan)) | |
995 | //ust// >= channel->alloc_size) { | |
996 | //ust// | |
997 | //ust// long consumed_idx = SUBBUF_INDEX(consumed, buf->chan); | |
b102c2b0 | 998 | //ust// long commit_count = uatomic_read(&buf->commit_count[consumed_idx]); |
b73a4c47 PMF |
999 | //ust// if(((commit_count - buf->chan->subbuf_size) & channel->commit_count_mask) - (BUFFER_TRUNC(consumed, buf->chan) >> channel->n_subbufs_order) != 0) { |
1000 | //ust// WARN("Event dropped. Caused by non-committed event."); | |
1001 | //ust// } | |
1002 | //ust// else { | |
1003 | //ust// WARN("Event dropped. Caused by non-consumed buffer."); | |
1004 | //ust// } | |
1005 | //ust// /* | |
1006 | //ust// * We do not overwrite non consumed buffers | |
1007 | //ust// * and we are full : event is lost. | |
1008 | //ust// */ | |
b102c2b0 | 1009 | //ust// uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1010 | //ust// return -1; |
1011 | //ust// } else { | |
1012 | //ust// /* | |
1013 | //ust// * next buffer not corrupted, we are either in | |
1014 | //ust// * overwrite mode or the buffer is not full. | |
1015 | //ust// * It's safe to write in this new subbuffer. | |
1016 | //ust// */ | |
1017 | //ust// } | |
1018 | //ust// } else { | |
1019 | //ust// /* | |
1020 | //ust// * Next subbuffer corrupted. Force pushing reader even | |
1021 | //ust// * in normal mode. It's safe to write in this new | |
1022 | //ust// * subbuffer. | |
1023 | //ust// */ | |
1024 | //ust// } | |
1025 | //ust// offsets->size = ust_get_header_size(channel, | |
1026 | //ust// offsets->begin, data_size, | |
1027 | //ust// &offsets->before_hdr_pad, *rflags); | |
1028 | //ust// offsets->size += ltt_align(offsets->begin + offsets->size, | |
1029 | //ust// largest_align) | |
1030 | //ust// + data_size; | |
1031 | //ust// if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size) | |
1032 | //ust// > buf->chan->subbuf_size) { | |
1033 | //ust// /* | |
1034 | //ust// * Event too big for subbuffers, report error, don't | |
1035 | //ust// * complete the sub-buffer switch. | |
1036 | //ust// */ | |
b102c2b0 | 1037 | //ust// uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1038 | //ust// return -1; |
1039 | //ust// } else { | |
1040 | //ust// /* | |
1041 | //ust// * We just made a successful buffer switch and the event | |
1042 | //ust// * fits in the new subbuffer. Let's write. | |
1043 | //ust// */ | |
1044 | //ust// } | |
1045 | //ust// } else { | |
1046 | //ust// /* | |
1047 | //ust// * Event fits in the current buffer and we are not on a switch | |
1048 | //ust// * boundary. It's safe to write. | |
1049 | //ust// */ | |
1050 | //ust// } | |
1051 | //ust// offsets->end = offsets->begin + offsets->size; | |
1052 | //ust// | |
1053 | //ust// if ((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0) { | |
1054 | //ust// /* | |
1055 | //ust// * The offset_end will fall at the very beginning of the next | |
1056 | //ust// * subbuffer. | |
1057 | //ust// */ | |
1058 | //ust// offsets->end_switch_current = 1; /* For offsets->begin */ | |
1059 | //ust// } | |
1060 | //ust// return 0; | |
1061 | //ust// } | |
1062 | //ust// | |
1063 | //ust// /* | |
1064 | //ust// * Returns : | |
1065 | //ust// * 0 if ok | |
1066 | //ust// * !0 if execution must be aborted. | |
1067 | //ust// */ | |
1068 | //ust// static inline int ltt_relay_try_switch( | |
1069 | //ust// enum force_switch_mode mode, | |
1070 | //ust// struct ust_channel *channel, | |
1071 | //ust// struct ust_buffer *buf, | |
1072 | //ust// struct ltt_reserve_switch_offsets *offsets, | |
1073 | //ust// u64 *tsc) | |
1074 | //ust// { | |
1075 | //ust// long subbuf_index; | |
1076 | //ust// | |
b102c2b0 | 1077 | //ust// offsets->begin = uatomic_read(&buf->offset); |
b73a4c47 PMF |
1078 | //ust// offsets->old = offsets->begin; |
1079 | //ust// offsets->begin_switch = 0; | |
1080 | //ust// offsets->end_switch_old = 0; | |
1081 | //ust// | |
1082 | //ust// *tsc = trace_clock_read64(); | |
1083 | //ust// | |
1084 | //ust// if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) { | |
1085 | //ust// offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan); | |
1086 | //ust// offsets->end_switch_old = 1; | |
1087 | //ust// } else { | |
1088 | //ust// /* we do not have to switch : buffer is empty */ | |
1089 | //ust// return -1; | |
1090 | //ust// } | |
1091 | //ust// if (mode == FORCE_ACTIVE) | |
1092 | //ust// offsets->begin += ltt_subbuffer_header_size(); | |
1093 | //ust// /* | |
1094 | //ust// * Always begin_switch in FORCE_ACTIVE mode. | |
1095 | //ust// * Test new buffer integrity | |
1096 | //ust// */ | |
1097 | //ust// subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan); | |
1098 | //ust// offsets->reserve_commit_diff = | |
1099 | //ust// (BUFFER_TRUNC(offsets->begin, buf->chan) | |
1100 | //ust// >> channel->n_subbufs_order) | |
b102c2b0 | 1101 | //ust// - (uatomic_read(&buf->commit_count[subbuf_index]) |
b73a4c47 PMF |
1102 | //ust// & channel->commit_count_mask); |
1103 | //ust// if (offsets->reserve_commit_diff == 0) { | |
1104 | //ust// /* Next buffer not corrupted. */ | |
1105 | //ust// if (mode == FORCE_ACTIVE | |
1106 | //ust// && !channel->overwrite | |
b102c2b0 | 1107 | //ust// && offsets->begin - uatomic_read(&buf->consumed) |
b73a4c47 PMF |
1108 | //ust// >= channel->alloc_size) { |
1109 | //ust// /* | |
1110 | //ust// * We do not overwrite non consumed buffers and we are | |
1111 | //ust// * full : ignore switch while tracing is active. | |
1112 | //ust// */ | |
1113 | //ust// return -1; | |
1114 | //ust// } | |
1115 | //ust// } else { | |
1116 | //ust// /* | |
1117 | //ust// * Next subbuffer corrupted. Force pushing reader even in normal | |
1118 | //ust// * mode | |
1119 | //ust// */ | |
1120 | //ust// } | |
1121 | //ust// offsets->end = offsets->begin; | |
1122 | //ust// return 0; | |
1123 | //ust// } | |
1124 | //ust// | |
1125 | //ust// static inline void ltt_reserve_push_reader( | |
1126 | //ust// struct ust_channel *channel, | |
1127 | //ust// struct ust_buffer *buf, | |
1128 | //ust// struct ltt_reserve_switch_offsets *offsets) | |
1129 | //ust// { | |
1130 | //ust// long consumed_old, consumed_new; | |
1131 | //ust// | |
1132 | //ust// do { | |
b102c2b0 | 1133 | //ust// consumed_old = uatomic_read(&buf->consumed); |
b73a4c47 PMF |
1134 | //ust// /* |
1135 | //ust// * If buffer is in overwrite mode, push the reader consumed | |
1136 | //ust// * count if the write position has reached it and we are not | |
1137 | //ust// * at the first iteration (don't push the reader farther than | |
1138 | //ust// * the writer). This operation can be done concurrently by many | |
1139 | //ust// * writers in the same buffer, the writer being at the farthest | |
1140 | //ust// * write position sub-buffer index in the buffer being the one | |
1141 | //ust// * which will win this loop. | |
1142 | //ust// * If the buffer is not in overwrite mode, pushing the reader | |
1143 | //ust// * only happens if a sub-buffer is corrupted. | |
1144 | //ust// */ | |
1145 | //ust// if ((SUBBUF_TRUNC(offsets->end-1, buf->chan) | |
1146 | //ust// - SUBBUF_TRUNC(consumed_old, buf->chan)) | |
1147 | //ust// >= channel->alloc_size) | |
1148 | //ust// consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan); | |
1149 | //ust// else { | |
1150 | //ust// consumed_new = consumed_old; | |
1151 | //ust// break; | |
1152 | //ust// } | |
b102c2b0 | 1153 | //ust// } while (uatomic_cmpxchg(&buf->consumed, consumed_old, |
b73a4c47 PMF |
1154 | //ust// consumed_new) != consumed_old); |
1155 | //ust// | |
1156 | //ust// if (consumed_old != consumed_new) { | |
1157 | //ust// /* | |
1158 | //ust// * Reader pushed : we are the winner of the push, we can | |
1159 | //ust// * therefore reequilibrate reserve and commit. Atomic increment | |
1160 | //ust// * of the commit count permits other writers to play around | |
1161 | //ust// * with this variable before us. We keep track of | |
1162 | //ust// * corrupted_subbuffers even in overwrite mode : | |
1163 | //ust// * we never want to write over a non completely committed | |
1164 | //ust// * sub-buffer : possible causes : the buffer size is too low | |
1165 | //ust// * compared to the unordered data input, or there is a writer | |
1166 | //ust// * that died between the reserve and the commit. | |
1167 | //ust// */ | |
1168 | //ust// if (offsets->reserve_commit_diff) { | |
1169 | //ust// /* | |
1170 | //ust// * We have to alter the sub-buffer commit count. | |
1171 | //ust// * We do not deliver the previous subbuffer, given it | |
1172 | //ust// * was either corrupted or not consumed (overwrite | |
1173 | //ust// * mode). | |
1174 | //ust// */ | |
b102c2b0 PMF |
1175 | //ust// uatomic_add(&buf->commit_count[SUBBUF_INDEX(offsets->begin, buf->chan)], |
1176 | //ust// offsets->reserve_commit_diff); | |
b73a4c47 PMF |
1177 | //ust// if (!channel->overwrite |
1178 | //ust// || offsets->reserve_commit_diff | |
1179 | //ust// != channel->subbuf_size) { | |
1180 | //ust// /* | |
1181 | //ust// * The reserve commit diff was not subbuf_size : | |
1182 | //ust// * it means the subbuffer was partly written to | |
1183 | //ust// * and is therefore corrupted. If it is multiple | |
1184 | //ust// * of subbuffer size and we are in flight | |
1185 | //ust// * recorder mode, we are skipping over a whole | |
1186 | //ust// * subbuffer. | |
1187 | //ust// */ | |
b102c2b0 | 1188 | //ust// uatomic_inc(&buf->corrupted_subbuffers); |
b73a4c47 PMF |
1189 | //ust// } |
1190 | //ust// } | |
1191 | //ust// } | |
1192 | //ust// } | |
1193 | //ust// | |
1194 | //ust// /** | |
1195 | //ust// * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer. | |
1196 | //ust// * @trace: the trace structure to log to. | |
1197 | //ust// * @ltt_channel: channel structure | |
1198 | //ust// * @transport_data: data structure specific to ltt relay | |
1199 | //ust// * @data_size: size of the variable length data to log. | |
1200 | //ust// * @slot_size: pointer to total size of the slot (out) | |
1201 | //ust// * @buf_offset : pointer to reserved buffer offset (out) | |
1202 | //ust// * @tsc: pointer to the tsc at the slot reservation (out) | |
1203 | //ust// * @cpu: cpuid | |
1204 | //ust// * | |
1205 | //ust// * Return : -ENOSPC if not enough space, else returns 0. | |
1206 | //ust// * It will take care of sub-buffer switching. | |
1207 | //ust// */ | |
1208 | //ust// static notrace int ltt_relay_reserve_slot(struct ust_trace *trace, | |
1209 | //ust// struct ust_channel *channel, void **transport_data, | |
1210 | //ust// size_t data_size, size_t *slot_size, long *buf_offset, u64 *tsc, | |
1211 | //ust// unsigned int *rflags, int largest_align, int cpu) | |
1212 | //ust// { | |
1213 | //ust// struct ust_buffer *buf = *transport_data = channel->buf[cpu]; | |
1214 | //ust// struct ltt_reserve_switch_offsets offsets; | |
1215 | //ust// | |
1216 | //ust// offsets.reserve_commit_diff = 0; | |
1217 | //ust// offsets.size = 0; | |
1218 | //ust// | |
1219 | //ust// /* | |
1220 | //ust// * Perform retryable operations. | |
1221 | //ust// */ | |
1222 | //ust// if (ltt_nesting > 4) { | |
b102c2b0 | 1223 | //ust// uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1224 | //ust// return -EPERM; |
1225 | //ust// } | |
1226 | //ust// do { | |
1227 | //ust// if (ltt_relay_try_reserve(channel, buf, &offsets, data_size, tsc, rflags, | |
1228 | //ust// largest_align)) | |
1229 | //ust// return -ENOSPC; | |
b102c2b0 | 1230 | //ust// } while (uatomic_cmpxchg(&buf->offset, offsets.old, |
b73a4c47 PMF |
1231 | //ust// offsets.end) != offsets.old); |
1232 | //ust// | |
1233 | //ust// /* | |
1234 | //ust// * Atomically update last_tsc. This update races against concurrent | |
1235 | //ust// * atomic updates, but the race will always cause supplementary full TSC | |
1236 | //ust// * events, never the opposite (missing a full TSC event when it would be | |
1237 | //ust// * needed). | |
1238 | //ust// */ | |
1239 | //ust// save_last_tsc(buf, *tsc); | |
1240 | //ust// | |
1241 | //ust// /* | |
1242 | //ust// * Push the reader if necessary | |
1243 | //ust// */ | |
1244 | //ust// ltt_reserve_push_reader(channel, buf, &offsets); | |
1245 | //ust// | |
1246 | //ust// /* | |
1247 | //ust// * Switch old subbuffer if needed. | |
1248 | //ust// */ | |
1249 | //ust// if (offsets.end_switch_old) | |
1250 | //ust// ltt_reserve_switch_old_subbuf(channel, buf, &offsets, tsc); | |
1251 | //ust// | |
1252 | //ust// /* | |
1253 | //ust// * Populate new subbuffer. | |
1254 | //ust// */ | |
1255 | //ust// if (offsets.begin_switch) | |
1256 | //ust// ltt_reserve_switch_new_subbuf(channel, buf, &offsets, tsc); | |
1257 | //ust// | |
1258 | //ust// if (offsets.end_switch_current) | |
1259 | //ust// ltt_reserve_end_switch_current(channel, buf, &offsets, tsc); | |
1260 | //ust// | |
1261 | //ust// *slot_size = offsets.size; | |
1262 | //ust// *buf_offset = offsets.begin + offsets.before_hdr_pad; | |
1263 | //ust// return 0; | |
1264 | //ust// } | |
1265 | //ust// | |
1266 | //ust// /* | |
1267 | //ust// * Force a sub-buffer switch for a per-cpu buffer. This operation is | |
1268 | //ust// * completely reentrant : can be called while tracing is active with | |
1269 | //ust// * absolutely no lock held. | |
b73a4c47 PMF |
1270 | //ust// */ |
1271 | //ust// static notrace void ltt_force_switch(struct ust_buffer *buf, | |
1272 | //ust// enum force_switch_mode mode) | |
1273 | //ust// { | |
1274 | //ust// struct ust_channel *channel = buf->chan; | |
1275 | //ust// struct ltt_reserve_switch_offsets offsets; | |
1276 | //ust// u64 tsc; | |
1277 | //ust// | |
1278 | //ust// offsets.reserve_commit_diff = 0; | |
1279 | //ust// offsets.size = 0; | |
1280 | //ust// | |
1281 | //ust// /* | |
1282 | //ust// * Perform retryable operations. | |
1283 | //ust// */ | |
1284 | //ust// do { | |
1285 | //ust// if (ltt_relay_try_switch(mode, channel, buf, &offsets, &tsc)) | |
1286 | //ust// return; | |
b102c2b0 | 1287 | //ust// } while (uatomic_cmpxchg(&buf->offset, offsets.old, |
b73a4c47 PMF |
1288 | //ust// offsets.end) != offsets.old); |
1289 | //ust// | |
1290 | //ust// /* | |
1291 | //ust// * Atomically update last_tsc. This update races against concurrent | |
1292 | //ust// * atomic updates, but the race will always cause supplementary full TSC | |
1293 | //ust// * events, never the opposite (missing a full TSC event when it would be | |
1294 | //ust// * needed). | |
1295 | //ust// */ | |
1296 | //ust// save_last_tsc(buf, tsc); | |
1297 | //ust// | |
1298 | //ust// /* | |
1299 | //ust// * Push the reader if necessary | |
1300 | //ust// */ | |
1301 | //ust// if (mode == FORCE_ACTIVE) | |
1302 | //ust// ltt_reserve_push_reader(channel, buf, &offsets); | |
1303 | //ust// | |
1304 | //ust// /* | |
1305 | //ust// * Switch old subbuffer if needed. | |
1306 | //ust// */ | |
1307 | //ust// if (offsets.end_switch_old) | |
1308 | //ust// ltt_reserve_switch_old_subbuf(channel, buf, &offsets, &tsc); | |
1309 | //ust// | |
1310 | //ust// /* | |
1311 | //ust// * Populate new subbuffer. | |
1312 | //ust// */ | |
1313 | //ust// if (mode == FORCE_ACTIVE) | |
1314 | //ust// ltt_reserve_switch_new_subbuf(channel, buf, &offsets, &tsc); | |
1315 | //ust// } | |
b5b073e2 PMF |
1316 | |
1317 | /* | |
b73a4c47 PMF |
1318 | * ltt_reserve_switch_old_subbuf: switch old subbuffer |
1319 | * | |
1320 | * Concurrency safe because we are the last and only thread to alter this | |
1321 | * sub-buffer. As long as it is not delivered and read, no other thread can | |
1322 | * alter the offset, alter the reserve_count or call the | |
1323 | * client_buffer_end_callback on this sub-buffer. | |
1324 | * | |
1325 | * The only remaining threads could be the ones with pending commits. They will | |
1326 | * have to do the deliver themselves. Not concurrency safe in overwrite mode. | |
1327 | * We detect corrupted subbuffers with commit and reserve counts. We keep a | |
1328 | * corrupted sub-buffers count and push the readers across these sub-buffers. | |
1329 | * | |
1330 | * Not concurrency safe if a writer is stalled in a subbuffer and another writer | |
1331 | * switches in, finding out it's corrupted. The result will be than the old | |
1332 | * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer | |
1333 | * will be declared corrupted too because of the commit count adjustment. | |
1334 | * | |
1335 | * Note : offset_old should never be 0 here. | |
b5b073e2 | 1336 | */ |
b73a4c47 PMF |
1337 | static void ltt_reserve_switch_old_subbuf( |
1338 | struct ust_channel *chan, struct ust_buffer *buf, | |
1339 | struct ltt_reserve_switch_offsets *offsets, u64 *tsc) | |
b5b073e2 | 1340 | { |
b73a4c47 PMF |
1341 | long oldidx = SUBBUF_INDEX(offsets->old - 1, chan); |
1342 | long commit_count, padding_size; | |
b5b073e2 | 1343 | |
b73a4c47 PMF |
1344 | padding_size = chan->subbuf_size |
1345 | - (SUBBUF_OFFSET(offsets->old - 1, chan) + 1); | |
1346 | ltt_buffer_end(buf, *tsc, offsets->old, oldidx); | |
b5b073e2 | 1347 | |
b73a4c47 PMF |
1348 | /* |
1349 | * Must write slot data before incrementing commit count. | |
1350 | * This compiler barrier is upgraded into a smp_wmb() by the IPI | |
1351 | * sent by get_subbuf() when it does its smp_rmb(). | |
1352 | */ | |
1353 | barrier(); | |
b102c2b0 PMF |
1354 | uatomic_add(&buf->commit_count[oldidx].cc, padding_size); |
1355 | commit_count = uatomic_read(&buf->commit_count[oldidx].cc); | |
b73a4c47 | 1356 | ltt_check_deliver(chan, buf, offsets->old - 1, commit_count, oldidx); |
1e8c9e7b | 1357 | ltt_write_commit_counter(chan, buf, oldidx, |
b73a4c47 PMF |
1358 | offsets->old, commit_count, padding_size); |
1359 | } | |
b5b073e2 | 1360 | |
b73a4c47 PMF |
1361 | /* |
1362 | * ltt_reserve_switch_new_subbuf: Populate new subbuffer. | |
1363 | * | |
1364 | * This code can be executed unordered : writers may already have written to the | |
1365 | * sub-buffer before this code gets executed, caution. The commit makes sure | |
1366 | * that this code is executed before the deliver of this sub-buffer. | |
1367 | */ | |
1368 | static void ltt_reserve_switch_new_subbuf( | |
1369 | struct ust_channel *chan, struct ust_buffer *buf, | |
1370 | struct ltt_reserve_switch_offsets *offsets, u64 *tsc) | |
1371 | { | |
1372 | long beginidx = SUBBUF_INDEX(offsets->begin, chan); | |
1373 | long commit_count; | |
b5b073e2 | 1374 | |
b73a4c47 | 1375 | ltt_buffer_begin(buf, *tsc, beginidx); |
b5b073e2 | 1376 | |
b73a4c47 PMF |
1377 | /* |
1378 | * Must write slot data before incrementing commit count. | |
1379 | * This compiler barrier is upgraded into a smp_wmb() by the IPI | |
1380 | * sent by get_subbuf() when it does its smp_rmb(). | |
1381 | */ | |
1382 | barrier(); | |
b102c2b0 PMF |
1383 | uatomic_add(&buf->commit_count[beginidx].cc, ltt_subbuffer_header_size()); |
1384 | commit_count = uatomic_read(&buf->commit_count[beginidx].cc); | |
b73a4c47 PMF |
1385 | /* Check if the written buffer has to be delivered */ |
1386 | ltt_check_deliver(chan, buf, offsets->begin, commit_count, beginidx); | |
1e8c9e7b | 1387 | ltt_write_commit_counter(chan, buf, beginidx, |
b73a4c47 PMF |
1388 | offsets->begin, commit_count, ltt_subbuffer_header_size()); |
1389 | } | |
b5b073e2 | 1390 | |
b73a4c47 PMF |
1391 | /* |
1392 | * ltt_reserve_end_switch_current: finish switching current subbuffer | |
1393 | * | |
1394 | * Concurrency safe because we are the last and only thread to alter this | |
1395 | * sub-buffer. As long as it is not delivered and read, no other thread can | |
1396 | * alter the offset, alter the reserve_count or call the | |
1397 | * client_buffer_end_callback on this sub-buffer. | |
1398 | * | |
1399 | * The only remaining threads could be the ones with pending commits. They will | |
1400 | * have to do the deliver themselves. Not concurrency safe in overwrite mode. | |
1401 | * We detect corrupted subbuffers with commit and reserve counts. We keep a | |
1402 | * corrupted sub-buffers count and push the readers across these sub-buffers. | |
1403 | * | |
1404 | * Not concurrency safe if a writer is stalled in a subbuffer and another writer | |
1405 | * switches in, finding out it's corrupted. The result will be than the old | |
1406 | * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer | |
1407 | * will be declared corrupted too because of the commit count adjustment. | |
1408 | */ | |
1409 | static void ltt_reserve_end_switch_current( | |
1410 | struct ust_channel *chan, | |
1411 | struct ust_buffer *buf, | |
1412 | struct ltt_reserve_switch_offsets *offsets, u64 *tsc) | |
1413 | { | |
1414 | long endidx = SUBBUF_INDEX(offsets->end - 1, chan); | |
1415 | long commit_count, padding_size; | |
1416 | ||
1417 | padding_size = chan->subbuf_size | |
1418 | - (SUBBUF_OFFSET(offsets->end - 1, chan) + 1); | |
1419 | ||
1420 | ltt_buffer_end(buf, *tsc, offsets->end, endidx); | |
1421 | ||
1422 | /* | |
1423 | * Must write slot data before incrementing commit count. | |
1424 | * This compiler barrier is upgraded into a smp_wmb() by the IPI | |
1425 | * sent by get_subbuf() when it does its smp_rmb(). | |
1426 | */ | |
1427 | barrier(); | |
b102c2b0 PMF |
1428 | uatomic_add(&buf->commit_count[endidx].cc, padding_size); |
1429 | commit_count = uatomic_read(&buf->commit_count[endidx].cc); | |
b73a4c47 PMF |
1430 | ltt_check_deliver(chan, buf, |
1431 | offsets->end - 1, commit_count, endidx); | |
1e8c9e7b | 1432 | ltt_write_commit_counter(chan, buf, endidx, |
b73a4c47 | 1433 | offsets->end, commit_count, padding_size); |
b5b073e2 PMF |
1434 | } |
1435 | ||
1436 | /* | |
1437 | * Returns : | |
1438 | * 0 if ok | |
1439 | * !0 if execution must be aborted. | |
1440 | */ | |
b73a4c47 | 1441 | static int ltt_relay_try_switch_slow( |
b5b073e2 | 1442 | enum force_switch_mode mode, |
b73a4c47 | 1443 | struct ust_channel *chan, |
b5b073e2 PMF |
1444 | struct ust_buffer *buf, |
1445 | struct ltt_reserve_switch_offsets *offsets, | |
1446 | u64 *tsc) | |
1447 | { | |
1448 | long subbuf_index; | |
b73a4c47 | 1449 | long reserve_commit_diff; |
b5b073e2 | 1450 | |
b102c2b0 | 1451 | offsets->begin = uatomic_read(&buf->offset); |
b5b073e2 PMF |
1452 | offsets->old = offsets->begin; |
1453 | offsets->begin_switch = 0; | |
1454 | offsets->end_switch_old = 0; | |
1455 | ||
1456 | *tsc = trace_clock_read64(); | |
1457 | ||
1458 | if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) { | |
1459 | offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan); | |
1460 | offsets->end_switch_old = 1; | |
1461 | } else { | |
1462 | /* we do not have to switch : buffer is empty */ | |
1463 | return -1; | |
1464 | } | |
1465 | if (mode == FORCE_ACTIVE) | |
1466 | offsets->begin += ltt_subbuffer_header_size(); | |
1467 | /* | |
1468 | * Always begin_switch in FORCE_ACTIVE mode. | |
1469 | * Test new buffer integrity | |
1470 | */ | |
1471 | subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan); | |
b73a4c47 | 1472 | reserve_commit_diff = |
b5b073e2 | 1473 | (BUFFER_TRUNC(offsets->begin, buf->chan) |
b73a4c47 | 1474 | >> chan->n_subbufs_order) |
b102c2b0 | 1475 | - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb) |
b73a4c47 PMF |
1476 | & chan->commit_count_mask); |
1477 | if (reserve_commit_diff == 0) { | |
b5b073e2 PMF |
1478 | /* Next buffer not corrupted. */ |
1479 | if (mode == FORCE_ACTIVE | |
b73a4c47 | 1480 | && !chan->overwrite |
b102c2b0 | 1481 | && offsets->begin - uatomic_read(&buf->consumed) |
b73a4c47 | 1482 | >= chan->alloc_size) { |
b5b073e2 PMF |
1483 | /* |
1484 | * We do not overwrite non consumed buffers and we are | |
1485 | * full : ignore switch while tracing is active. | |
1486 | */ | |
1487 | return -1; | |
1488 | } | |
1489 | } else { | |
1490 | /* | |
1491 | * Next subbuffer corrupted. Force pushing reader even in normal | |
1492 | * mode | |
1493 | */ | |
1494 | } | |
1495 | offsets->end = offsets->begin; | |
1496 | return 0; | |
1497 | } | |
1498 | ||
b5b073e2 | 1499 | /* |
b73a4c47 PMF |
1500 | * Force a sub-buffer switch for a per-cpu buffer. This operation is |
1501 | * completely reentrant : can be called while tracing is active with | |
1502 | * absolutely no lock held. | |
b5b073e2 | 1503 | */ |
b73a4c47 PMF |
1504 | void ltt_force_switch_lockless_slow(struct ust_buffer *buf, |
1505 | enum force_switch_mode mode) | |
b5b073e2 | 1506 | { |
b73a4c47 | 1507 | struct ust_channel *chan = buf->chan; |
b5b073e2 | 1508 | struct ltt_reserve_switch_offsets offsets; |
b73a4c47 | 1509 | u64 tsc; |
b5b073e2 | 1510 | |
b5b073e2 PMF |
1511 | offsets.size = 0; |
1512 | ||
10dd3941 | 1513 | DBG("Switching (forced) %s_%d", chan->channel_name, buf->cpu); |
b5b073e2 PMF |
1514 | /* |
1515 | * Perform retryable operations. | |
1516 | */ | |
b5b073e2 | 1517 | do { |
b73a4c47 PMF |
1518 | if (ltt_relay_try_switch_slow(mode, chan, buf, |
1519 | &offsets, &tsc)) | |
1520 | return; | |
b102c2b0 | 1521 | } while (uatomic_cmpxchg(&buf->offset, offsets.old, |
b5b073e2 PMF |
1522 | offsets.end) != offsets.old); |
1523 | ||
1524 | /* | |
1525 | * Atomically update last_tsc. This update races against concurrent | |
1526 | * atomic updates, but the race will always cause supplementary full TSC | |
1527 | * events, never the opposite (missing a full TSC event when it would be | |
1528 | * needed). | |
1529 | */ | |
b73a4c47 | 1530 | save_last_tsc(buf, tsc); |
b5b073e2 PMF |
1531 | |
1532 | /* | |
1533 | * Push the reader if necessary | |
1534 | */ | |
b73a4c47 PMF |
1535 | if (mode == FORCE_ACTIVE) { |
1536 | ltt_reserve_push_reader(chan, buf, offsets.end - 1); | |
1537 | //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan)); | |
1538 | } | |
b5b073e2 PMF |
1539 | |
1540 | /* | |
1541 | * Switch old subbuffer if needed. | |
1542 | */ | |
b73a4c47 PMF |
1543 | if (offsets.end_switch_old) { |
1544 | //ust// ltt_clear_noref_flag(rchan, buf, SUBBUF_INDEX(offsets.old - 1, rchan)); | |
1545 | ltt_reserve_switch_old_subbuf(chan, buf, &offsets, &tsc); | |
1546 | } | |
b5b073e2 PMF |
1547 | |
1548 | /* | |
1549 | * Populate new subbuffer. | |
1550 | */ | |
b73a4c47 PMF |
1551 | if (mode == FORCE_ACTIVE) |
1552 | ltt_reserve_switch_new_subbuf(chan, buf, &offsets, &tsc); | |
1553 | } | |
b5b073e2 | 1554 | |
b73a4c47 PMF |
1555 | /* |
1556 | * Returns : | |
1557 | * 0 if ok | |
1558 | * !0 if execution must be aborted. | |
1559 | */ | |
1560 | static int ltt_relay_try_reserve_slow(struct ust_channel *chan, struct ust_buffer *buf, | |
1561 | struct ltt_reserve_switch_offsets *offsets, size_t data_size, | |
1562 | u64 *tsc, unsigned int *rflags, int largest_align) | |
1563 | { | |
1564 | long reserve_commit_diff; | |
b5b073e2 | 1565 | |
b102c2b0 | 1566 | offsets->begin = uatomic_read(&buf->offset); |
b73a4c47 PMF |
1567 | offsets->old = offsets->begin; |
1568 | offsets->begin_switch = 0; | |
1569 | offsets->end_switch_current = 0; | |
1570 | offsets->end_switch_old = 0; | |
1571 | ||
1572 | *tsc = trace_clock_read64(); | |
1573 | if (last_tsc_overflow(buf, *tsc)) | |
1574 | *rflags = LTT_RFLAG_ID_SIZE_TSC; | |
1575 | ||
1576 | if (unlikely(SUBBUF_OFFSET(offsets->begin, buf->chan) == 0)) { | |
1577 | offsets->begin_switch = 1; /* For offsets->begin */ | |
1578 | } else { | |
1579 | offsets->size = ust_get_header_size(chan, | |
1580 | offsets->begin, data_size, | |
1581 | &offsets->before_hdr_pad, *rflags); | |
1582 | offsets->size += ltt_align(offsets->begin + offsets->size, | |
1583 | largest_align) | |
1584 | + data_size; | |
1585 | if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) + | |
1586 | offsets->size) > buf->chan->subbuf_size)) { | |
1587 | offsets->end_switch_old = 1; /* For offsets->old */ | |
1588 | offsets->begin_switch = 1; /* For offsets->begin */ | |
1589 | } | |
1590 | } | |
1591 | if (unlikely(offsets->begin_switch)) { | |
1592 | long subbuf_index; | |
1593 | ||
1594 | /* | |
1595 | * We are typically not filling the previous buffer completely. | |
1596 | */ | |
1597 | if (likely(offsets->end_switch_old)) | |
1598 | offsets->begin = SUBBUF_ALIGN(offsets->begin, | |
1599 | buf->chan); | |
1600 | offsets->begin = offsets->begin + ltt_subbuffer_header_size(); | |
1601 | /* Test new buffer integrity */ | |
1602 | subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan); | |
1603 | reserve_commit_diff = | |
1604 | (BUFFER_TRUNC(offsets->begin, buf->chan) | |
1605 | >> chan->n_subbufs_order) | |
b102c2b0 | 1606 | - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb) |
b73a4c47 PMF |
1607 | & chan->commit_count_mask); |
1608 | if (likely(reserve_commit_diff == 0)) { | |
1609 | /* Next buffer not corrupted. */ | |
1610 | if (unlikely(!chan->overwrite && | |
1611 | (SUBBUF_TRUNC(offsets->begin, buf->chan) | |
b102c2b0 | 1612 | - SUBBUF_TRUNC(uatomic_read( |
b73a4c47 PMF |
1613 | &buf->consumed), |
1614 | buf->chan)) | |
1615 | >= chan->alloc_size)) { | |
1616 | /* | |
1617 | * We do not overwrite non consumed buffers | |
1618 | * and we are full : event is lost. | |
1619 | */ | |
b102c2b0 | 1620 | uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1621 | return -1; |
1622 | } else { | |
1623 | /* | |
1624 | * next buffer not corrupted, we are either in | |
1625 | * overwrite mode or the buffer is not full. | |
1626 | * It's safe to write in this new subbuffer. | |
1627 | */ | |
1628 | } | |
1629 | } else { | |
1630 | /* | |
1631 | * Next subbuffer corrupted. Drop event in normal and | |
1632 | * overwrite mode. Caused by either a writer OOPS or | |
1633 | * too many nested writes over a reserve/commit pair. | |
1634 | */ | |
b102c2b0 | 1635 | uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1636 | return -1; |
1637 | } | |
1638 | offsets->size = ust_get_header_size(chan, | |
1639 | offsets->begin, data_size, | |
1640 | &offsets->before_hdr_pad, *rflags); | |
1641 | offsets->size += ltt_align(offsets->begin + offsets->size, | |
1642 | largest_align) | |
1643 | + data_size; | |
1644 | if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) | |
1645 | + offsets->size) > buf->chan->subbuf_size)) { | |
1646 | /* | |
1647 | * Event too big for subbuffers, report error, don't | |
1648 | * complete the sub-buffer switch. | |
1649 | */ | |
b102c2b0 | 1650 | uatomic_inc(&buf->events_lost); |
b73a4c47 PMF |
1651 | return -1; |
1652 | } else { | |
1653 | /* | |
1654 | * We just made a successful buffer switch and the event | |
1655 | * fits in the new subbuffer. Let's write. | |
1656 | */ | |
1657 | } | |
1658 | } else { | |
1659 | /* | |
1660 | * Event fits in the current buffer and we are not on a switch | |
1661 | * boundary. It's safe to write. | |
1662 | */ | |
1663 | } | |
1664 | offsets->end = offsets->begin + offsets->size; | |
1665 | ||
1666 | if (unlikely((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0)) { | |
1667 | /* | |
1668 | * The offset_end will fall at the very beginning of the next | |
1669 | * subbuffer. | |
1670 | */ | |
1671 | offsets->end_switch_current = 1; /* For offsets->begin */ | |
1672 | } | |
b5b073e2 PMF |
1673 | return 0; |
1674 | } | |
1675 | ||
b73a4c47 PMF |
1676 | /** |
1677 | * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer. | |
1678 | * @trace: the trace structure to log to. | |
1679 | * @ltt_channel: channel structure | |
1680 | * @transport_data: data structure specific to ltt relay | |
1681 | * @data_size: size of the variable length data to log. | |
1682 | * @slot_size: pointer to total size of the slot (out) | |
1683 | * @buf_offset : pointer to reserved buffer offset (out) | |
1684 | * @tsc: pointer to the tsc at the slot reservation (out) | |
1685 | * @cpu: cpuid | |
b5b073e2 | 1686 | * |
b73a4c47 PMF |
1687 | * Return : -ENOSPC if not enough space, else returns 0. |
1688 | * It will take care of sub-buffer switching. | |
b5b073e2 | 1689 | */ |
b73a4c47 PMF |
1690 | int ltt_reserve_slot_lockless_slow(struct ust_trace *trace, |
1691 | struct ust_channel *chan, void **transport_data, | |
1692 | size_t data_size, size_t *slot_size, long *buf_offset, u64 *tsc, | |
1693 | unsigned int *rflags, int largest_align, int cpu) | |
b5b073e2 | 1694 | { |
b73a4c47 | 1695 | struct ust_buffer *buf = chan->buf[cpu]; |
b5b073e2 | 1696 | struct ltt_reserve_switch_offsets offsets; |
b5b073e2 | 1697 | |
b5b073e2 PMF |
1698 | offsets.size = 0; |
1699 | ||
b5b073e2 | 1700 | do { |
b73a4c47 PMF |
1701 | if (unlikely(ltt_relay_try_reserve_slow(chan, buf, &offsets, |
1702 | data_size, tsc, rflags, largest_align))) | |
1703 | return -ENOSPC; | |
b102c2b0 | 1704 | } while (unlikely(uatomic_cmpxchg(&buf->offset, offsets.old, |
b73a4c47 | 1705 | offsets.end) != offsets.old)); |
b5b073e2 PMF |
1706 | |
1707 | /* | |
1708 | * Atomically update last_tsc. This update races against concurrent | |
1709 | * atomic updates, but the race will always cause supplementary full TSC | |
1710 | * events, never the opposite (missing a full TSC event when it would be | |
1711 | * needed). | |
1712 | */ | |
b73a4c47 | 1713 | save_last_tsc(buf, *tsc); |
b5b073e2 PMF |
1714 | |
1715 | /* | |
1716 | * Push the reader if necessary | |
1717 | */ | |
b73a4c47 PMF |
1718 | ltt_reserve_push_reader(chan, buf, offsets.end - 1); |
1719 | ||
1720 | /* | |
1721 | * Clear noref flag for this subbuffer. | |
1722 | */ | |
1723 | //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan)); | |
b5b073e2 PMF |
1724 | |
1725 | /* | |
1726 | * Switch old subbuffer if needed. | |
1727 | */ | |
b73a4c47 PMF |
1728 | if (unlikely(offsets.end_switch_old)) { |
1729 | //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.old - 1, chan)); | |
1730 | ltt_reserve_switch_old_subbuf(chan, buf, &offsets, tsc); | |
10dd3941 | 1731 | DBG("Switching %s_%d", chan->channel_name, cpu); |
b73a4c47 | 1732 | } |
b5b073e2 PMF |
1733 | |
1734 | /* | |
1735 | * Populate new subbuffer. | |
1736 | */ | |
b73a4c47 PMF |
1737 | if (unlikely(offsets.begin_switch)) |
1738 | ltt_reserve_switch_new_subbuf(chan, buf, &offsets, tsc); | |
1739 | ||
1740 | if (unlikely(offsets.end_switch_current)) | |
1741 | ltt_reserve_end_switch_current(chan, buf, &offsets, tsc); | |
1742 | ||
1743 | *slot_size = offsets.size; | |
1744 | *buf_offset = offsets.begin + offsets.before_hdr_pad; | |
1745 | return 0; | |
b5b073e2 PMF |
1746 | } |
1747 | ||
b5b073e2 PMF |
1748 | static struct ltt_transport ust_relay_transport = { |
1749 | .name = "ustrelay", | |
1750 | .ops = { | |
1751 | .create_channel = ust_buffers_create_channel, | |
1752 | .finish_channel = ltt_relay_finish_channel, | |
1753 | .remove_channel = ltt_relay_remove_channel, | |
1754 | .wakeup_channel = ltt_relay_async_wakeup_chan, | |
b5b073e2 PMF |
1755 | }, |
1756 | }; | |
1757 | ||
b5b073e2 PMF |
1758 | static char initialized = 0; |
1759 | ||
1760 | void __attribute__((constructor)) init_ustrelay_transport(void) | |
1761 | { | |
1762 | if(!initialized) { | |
1763 | ltt_transport_register(&ust_relay_transport); | |
1764 | initialized = 1; | |
1765 | } | |
1766 | } | |
1767 | ||
b73a4c47 | 1768 | static void __attribute__((destructor)) ust_buffers_exit(void) |
b5b073e2 PMF |
1769 | { |
1770 | ltt_transport_unregister(&ust_relay_transport); | |
1771 | } | |
b73a4c47 PMF |
1772 | |
1773 | size_t ltt_write_event_header_slow(struct ust_trace *trace, | |
1774 | struct ust_channel *channel, | |
1775 | struct ust_buffer *buf, long buf_offset, | |
1776 | u16 eID, u32 event_size, | |
1777 | u64 tsc, unsigned int rflags) | |
1778 | { | |
1779 | struct ltt_event_header header; | |
1780 | u16 small_size; | |
1781 | ||
1782 | switch (rflags) { | |
1783 | case LTT_RFLAG_ID_SIZE_TSC: | |
1784 | header.id_time = 29 << LTT_TSC_BITS; | |
1785 | break; | |
1786 | case LTT_RFLAG_ID_SIZE: | |
1787 | header.id_time = 30 << LTT_TSC_BITS; | |
1788 | break; | |
1789 | case LTT_RFLAG_ID: | |
1790 | header.id_time = 31 << LTT_TSC_BITS; | |
1791 | break; | |
1792 | } | |
1793 | ||
1794 | header.id_time |= (u32)tsc & LTT_TSC_MASK; | |
1795 | ust_buffers_write(buf, buf_offset, &header, sizeof(header)); | |
1796 | buf_offset += sizeof(header); | |
1797 | ||
1798 | switch (rflags) { | |
1799 | case LTT_RFLAG_ID_SIZE_TSC: | |
1800 | small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE); | |
1801 | ust_buffers_write(buf, buf_offset, | |
1802 | &eID, sizeof(u16)); | |
1803 | buf_offset += sizeof(u16); | |
1804 | ust_buffers_write(buf, buf_offset, | |
1805 | &small_size, sizeof(u16)); | |
1806 | buf_offset += sizeof(u16); | |
1807 | if (small_size == LTT_MAX_SMALL_SIZE) { | |
1808 | ust_buffers_write(buf, buf_offset, | |
1809 | &event_size, sizeof(u32)); | |
1810 | buf_offset += sizeof(u32); | |
1811 | } | |
1812 | buf_offset += ltt_align(buf_offset, sizeof(u64)); | |
1813 | ust_buffers_write(buf, buf_offset, | |
1814 | &tsc, sizeof(u64)); | |
1815 | buf_offset += sizeof(u64); | |
1816 | break; | |
1817 | case LTT_RFLAG_ID_SIZE: | |
1818 | small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE); | |
1819 | ust_buffers_write(buf, buf_offset, | |
1820 | &eID, sizeof(u16)); | |
1821 | buf_offset += sizeof(u16); | |
1822 | ust_buffers_write(buf, buf_offset, | |
1823 | &small_size, sizeof(u16)); | |
1824 | buf_offset += sizeof(u16); | |
1825 | if (small_size == LTT_MAX_SMALL_SIZE) { | |
1826 | ust_buffers_write(buf, buf_offset, | |
1827 | &event_size, sizeof(u32)); | |
1828 | buf_offset += sizeof(u32); | |
1829 | } | |
1830 | break; | |
1831 | case LTT_RFLAG_ID: | |
1832 | ust_buffers_write(buf, buf_offset, | |
1833 | &eID, sizeof(u16)); | |
1834 | buf_offset += sizeof(u16); | |
1835 | break; | |
1836 | } | |
1837 | ||
1838 | return buf_offset; | |
1839 | } |