Add UST_DEBUG env. var. support
[lttng-ust.git] / libust / buffers.c
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-2011 - 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; version 2.1 of
11 * the License.
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
23 /*
24 * Note: this code does not support the ref/noref flag and reader-owned
25 * subbuffer scheme. Therefore, flight recorder mode uses a mechanism
26 * where the reader can read corrupted data (and detect this), thus
27 * returning -EIO.
28 */
29
30 #include <unistd.h>
31 #include <sys/mman.h>
32 #include <sys/ipc.h>
33 #include <sys/shm.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36
37 #include <ust/clock.h>
38
39 #include "buffers.h"
40 #include "channels.h"
41 #include "tracer.h"
42 #include "tracercore.h"
43 #include "usterr_signal_safe.h"
44
45 struct ltt_reserve_switch_offsets {
46 long begin, end, old;
47 long begin_switch, end_switch_current, end_switch_old;
48 size_t before_hdr_pad, size;
49 };
50
51
52 static DEFINE_MUTEX(ust_buffers_channels_mutex);
53 static CDS_LIST_HEAD(ust_buffers_channels);
54
55 static void ltt_force_switch(struct ust_buffer *buf,
56 enum force_switch_mode mode);
57
58 /**
59 * _ust_buffers_strncpy_fixup - Fix an incomplete string in a ltt_relay buffer.
60 * @buf : buffer
61 * @offset : offset within the buffer
62 * @len : length to write
63 * @copied: string actually copied
64 * @terminated: does string end with \0
65 *
66 * Fills string with "X" if incomplete.
67 */
68 void _ust_buffers_strncpy_fixup(struct ust_buffer *buf, size_t offset,
69 size_t len, size_t copied, int terminated)
70 {
71 size_t buf_offset, cpy;
72
73 if (copied == len) {
74 /*
75 * Deal with non-terminated string.
76 */
77 assert(!terminated);
78 offset += copied - 1;
79 buf_offset = BUFFER_OFFSET(offset, buf->chan);
80 /*
81 * Underlying layer should never ask for writes across
82 * subbuffers.
83 */
84 assert(buf_offset
85 < buf->chan->subbuf_size*buf->chan->subbuf_cnt);
86 ust_buffers_do_memset(buf->buf_data + buf_offset, '\0', 1);
87 return;
88 }
89
90 /*
91 * Deal with incomplete string.
92 * Overwrite string's \0 with X too.
93 */
94 cpy = copied - 1;
95 assert(terminated);
96 len -= cpy;
97 offset += cpy;
98 buf_offset = BUFFER_OFFSET(offset, buf->chan);
99
100 /*
101 * Underlying layer should never ask for writes across subbuffers.
102 */
103 assert(buf_offset
104 < buf->chan->subbuf_size*buf->chan->subbuf_cnt);
105
106 ust_buffers_do_memset(buf->buf_data + buf_offset,
107 'X', len);
108
109 /*
110 * Overwrite last 'X' with '\0'.
111 */
112 offset += len - 1;
113 buf_offset = BUFFER_OFFSET(offset, buf->chan);
114 /*
115 * Underlying layer should never ask for writes across subbuffers.
116 */
117 assert(buf_offset
118 < buf->chan->subbuf_size*buf->chan->subbuf_cnt);
119 ust_buffers_do_memset(buf->buf_data + buf_offset, '\0', 1);
120 }
121
122 static void ltt_buffer_begin(struct ust_buffer *buf,
123 u64 tsc, unsigned int subbuf_idx)
124 {
125 struct ust_channel *channel = buf->chan;
126 struct ltt_subbuffer_header *header =
127 (struct ltt_subbuffer_header *)
128 ust_buffers_offset_address(buf,
129 subbuf_idx * buf->chan->subbuf_size);
130
131 header->cycle_count_begin = tsc;
132 header->data_size = 0xFFFFFFFF; /* for recognizing crashed buffers */
133 header->sb_size = 0xFFFFFFFF; /* for recognizing crashed buffers */
134 /*
135 * No memory barrier needed to order data_data/sb_size vs commit count
136 * update, because commit count update contains a compiler barrier that
137 * ensures the order of the writes are OK from a program POV. It only
138 * matters for crash dump recovery which is not executed concurrently,
139 * so memory write order does not matter.
140 */
141 ltt_write_trace_header(channel->trace, header);
142 }
143
144 static int map_buf_data(struct ust_buffer *buf, size_t *size)
145 {
146 void *ptr;
147 int result;
148
149 *size = PAGE_ALIGN(*size);
150
151 result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700);
152 if (result < 0 && errno == EINVAL) {
153 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
154 return -1;
155 } else if (result < 0) {
156 PERROR("shmget");
157 return -1;
158 }
159
160 ptr = shmat(buf->shmid, NULL, 0);
161 if (ptr == (void *) -1) {
162 perror("shmat");
163 goto destroy_shmem;
164 }
165
166 /* Already mark the shared memory for destruction. This will occur only
167 * when all users have detached.
168 */
169 result = shmctl(buf->shmid, IPC_RMID, NULL);
170 if(result == -1) {
171 perror("shmctl");
172 return -1;
173 }
174
175 buf->buf_data = ptr;
176 buf->buf_size = *size;
177
178 return 0;
179
180 destroy_shmem:
181 result = shmctl(buf->shmid, IPC_RMID, NULL);
182 if(result == -1) {
183 perror("shmctl");
184 }
185
186 return -1;
187 }
188
189 static int open_buf(struct ust_channel *chan, int cpu)
190 {
191 int result, fds[2];
192 unsigned int j;
193 struct ust_trace *trace = chan->trace;
194 struct ust_buffer *buf = chan->buf[cpu];
195 unsigned int n_subbufs = chan->subbuf_cnt;
196
197
198 result = map_buf_data(buf, &chan->alloc_size);
199 if (result < 0)
200 return -1;
201
202 buf->commit_count =
203 zmalloc(sizeof(*buf->commit_count) * n_subbufs);
204 if (!buf->commit_count)
205 goto unmap_buf;
206
207 result = pipe(fds);
208 if (result < 0) {
209 PERROR("pipe");
210 goto free_commit_count;
211 }
212 buf->data_ready_fd_read = fds[0];
213 buf->data_ready_fd_write = fds[1];
214
215 buf->cpu = cpu;
216 buf->chan = chan;
217
218 uatomic_set(&buf->offset, ltt_subbuffer_header_size());
219 uatomic_set(&buf->consumed, 0);
220 uatomic_set(&buf->active_readers, 0);
221 for (j = 0; j < n_subbufs; j++) {
222 uatomic_set(&buf->commit_count[j].cc, 0);
223 uatomic_set(&buf->commit_count[j].cc_sb, 0);
224 }
225
226 ltt_buffer_begin(buf, trace->start_tsc, 0);
227
228 uatomic_add(&buf->commit_count[0].cc, ltt_subbuffer_header_size());
229
230 uatomic_set(&buf->events_lost, 0);
231 uatomic_set(&buf->corrupted_subbuffers, 0);
232
233 memset(buf->commit_seq, 0, sizeof(buf->commit_seq[0]) * n_subbufs);
234
235 return 0;
236
237 free_commit_count:
238 free(buf->commit_count);
239
240 unmap_buf:
241 if (shmdt(buf->buf_data) < 0) {
242 PERROR("shmdt failed");
243 }
244
245 return -1;
246 }
247
248 static void close_buf(struct ust_buffer *buf)
249 {
250 int result;
251
252 result = shmdt(buf->buf_data);
253 if (result < 0) {
254 PERROR("shmdt");
255 }
256
257 result = close(buf->data_ready_fd_read);
258 if (result < 0) {
259 PERROR("close");
260 }
261
262 result = close(buf->data_ready_fd_write);
263 if (result < 0 && errno != EBADF) {
264 PERROR("close");
265 }
266 }
267
268
269 static int open_channel(struct ust_channel *chan, size_t subbuf_size,
270 size_t subbuf_cnt)
271 {
272 int i;
273 int result;
274
275 if(subbuf_size == 0 || subbuf_cnt == 0)
276 return -1;
277
278 /* Check that the subbuffer size is larger than a page. */
279 WARN_ON_ONCE(subbuf_size < PAGE_SIZE);
280
281 /*
282 * Make sure the number of subbuffers and subbuffer size are power of 2.
283 */
284 WARN_ON_ONCE(hweight32(subbuf_size) != 1);
285 WARN_ON(hweight32(subbuf_cnt) != 1);
286
287 chan->version = UST_CHANNEL_VERSION;
288 chan->subbuf_cnt = subbuf_cnt;
289 chan->subbuf_size = subbuf_size;
290 chan->subbuf_size_order = get_count_order(subbuf_size);
291 chan->alloc_size = subbuf_size * subbuf_cnt;
292
293 pthread_mutex_lock(&ust_buffers_channels_mutex);
294 for (i=0; i < chan->n_cpus; i++) {
295 result = open_buf(chan, i);
296 if (result == -1)
297 goto error;
298 }
299 cds_list_add(&chan->list, &ust_buffers_channels);
300 pthread_mutex_unlock(&ust_buffers_channels_mutex);
301
302 return 0;
303
304 /* Error handling */
305 error:
306 for(i--; i >= 0; i--)
307 close_buf(chan->buf[i]);
308
309 pthread_mutex_unlock(&ust_buffers_channels_mutex);
310 return -1;
311 }
312
313 static void close_channel(struct ust_channel *chan)
314 {
315 int i;
316 if(!chan)
317 return;
318
319 pthread_mutex_lock(&ust_buffers_channels_mutex);
320 /*
321 * checking for chan->buf[i] being NULL or not is useless in
322 * practice because we allocate buffers for all possible cpus.
323 * However, should we decide to change this and only allocate
324 * for online cpus, this check becomes useful.
325 */
326 for (i=0; i<chan->n_cpus; i++) {
327 if (chan->buf[i])
328 close_buf(chan->buf[i]);
329 }
330
331 cds_list_del(&chan->list);
332
333 pthread_mutex_unlock(&ust_buffers_channels_mutex);
334 }
335
336 /*
337 * offset is assumed to never be 0 here : never deliver a completely empty
338 * subbuffer. The lost size is between 0 and subbuf_size-1.
339 */
340 static notrace void ltt_buffer_end(struct ust_buffer *buf,
341 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
342 {
343 struct ltt_subbuffer_header *header =
344 (struct ltt_subbuffer_header *)
345 ust_buffers_offset_address(buf,
346 subbuf_idx * buf->chan->subbuf_size);
347 u32 data_size = SUBBUF_OFFSET(offset - 1, buf->chan) + 1;
348
349 header->sb_size = PAGE_ALIGN(data_size);
350 header->cycle_count_end = tsc;
351 header->events_lost = uatomic_read(&buf->events_lost);
352 header->subbuf_corrupt = uatomic_read(&buf->corrupted_subbuffers);
353 if(unlikely(header->events_lost > 0)) {
354 DBG("Some events (%d) were lost in %s_%d", header->events_lost, buf->chan->channel_name, buf->cpu);
355 }
356 /*
357 * Makes sure data_size write happens after write of the rest of the
358 * buffer end data, because data_size is used to identify a completely
359 * written subbuffer in a crash dump.
360 */
361 cmm_barrier();
362 header->data_size = data_size;
363 }
364
365 /*
366 * This function should not be called from NMI interrupt context
367 */
368 static notrace void ltt_buf_unfull(struct ust_buffer *buf,
369 unsigned int subbuf_idx,
370 long offset)
371 {
372 }
373
374 /*
375 * Promote compiler cmm_barrier to a smp_mb().
376 * For the specific LTTng case, this IPI call should be removed if the
377 * architecture does not reorder writes. This should eventually be provided by
378 * a separate architecture-specific infrastructure.
379 */
380 //ust// static void remote_mb(void *info)
381 //ust// {
382 //ust// smp_mb();
383 //ust// }
384
385 int ust_buffers_get_subbuf(struct ust_buffer *buf, long *consumed)
386 {
387 struct ust_channel *channel = buf->chan;
388 long consumed_old, consumed_idx, commit_count, write_offset;
389 //ust// int retval;
390
391 consumed_old = uatomic_read(&buf->consumed);
392 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
393 commit_count = uatomic_read(&buf->commit_count[consumed_idx].cc_sb);
394 /*
395 * Make sure we read the commit count before reading the buffer
396 * data and the write offset. Correct consumed offset ordering
397 * wrt commit count is insured by the use of cmpxchg to update
398 * the consumed offset.
399 */
400
401 /*
402 * Local rmb to match the remote wmb to read the commit count before the
403 * buffer data and the write offset.
404 */
405 cmm_smp_rmb();
406
407 write_offset = uatomic_read(&buf->offset);
408 /*
409 * Check that the subbuffer we are trying to consume has been
410 * already fully committed.
411 */
412 if (((commit_count - buf->chan->subbuf_size)
413 & channel->commit_count_mask)
414 - (BUFFER_TRUNC(consumed_old, buf->chan)
415 >> channel->n_subbufs_order)
416 != 0) {
417 return -EAGAIN;
418 }
419 /*
420 * Check that we are not about to read the same subbuffer in
421 * which the writer head is.
422 */
423 if ((SUBBUF_TRUNC(write_offset, buf->chan)
424 - SUBBUF_TRUNC(consumed_old, buf->chan))
425 == 0) {
426 return -EAGAIN;
427 }
428 *consumed = consumed_old;
429
430 return 0;
431 }
432
433 int ust_buffers_put_subbuf(struct ust_buffer *buf, unsigned long uconsumed_old)
434 {
435 long consumed_new, consumed_old;
436
437 consumed_old = uatomic_read(&buf->consumed);
438 consumed_old = consumed_old & (~0xFFFFFFFFL);
439 consumed_old = consumed_old | uconsumed_old;
440 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
441
442 if (uatomic_cmpxchg(&buf->consumed, consumed_old,
443 consumed_new)
444 != consumed_old) {
445 /* We have been pushed by the writer : the last
446 * buffer read _is_ corrupted! It can also
447 * happen if this is a buffer we never got. */
448 return -EIO;
449 } else {
450 /* tell the client that buffer is now unfull */
451 int index;
452 long data;
453 index = SUBBUF_INDEX(consumed_old, buf->chan);
454 data = BUFFER_OFFSET(consumed_old, buf->chan);
455 ltt_buf_unfull(buf, index, data);
456 }
457 return 0;
458 }
459
460 static int map_buf_structs(struct ust_channel *chan)
461 {
462 void *ptr;
463 int result;
464 size_t size;
465 int i;
466
467 size = PAGE_ALIGN(1);
468
469 for(i=0; i<chan->n_cpus; i++) {
470
471 result = chan->buf_struct_shmids[i] = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700);
472 if(result == -1) {
473 PERROR("shmget");
474 goto destroy_previous;
475 }
476
477 ptr = shmat(chan->buf_struct_shmids[i], NULL, 0);
478 if(ptr == (void *) -1) {
479 perror("shmat");
480 goto destroy_shm;
481 }
482
483 /* Already mark the shared memory for destruction. This will occur only
484 * when all users have detached.
485 */
486 result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL);
487 if(result == -1) {
488 perror("shmctl");
489 goto destroy_previous;
490 }
491
492 chan->buf[i] = ptr;
493 }
494
495 return 0;
496
497 /* Jumping inside this loop occurs from within the other loop above with i as
498 * counter, so it unallocates the structures for the cpu = current_i down to
499 * zero. */
500 for(; i>=0; i--) {
501 destroy_shm:
502 result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL);
503 if(result == -1) {
504 perror("shmctl");
505 }
506
507 destroy_previous:
508 continue;
509 }
510
511 return -1;
512 }
513
514 static int unmap_buf_structs(struct ust_channel *chan)
515 {
516 int i;
517
518 for (i=0; i < chan->n_cpus; i++) {
519 if (shmdt(chan->buf[i]) < 0) {
520 PERROR("shmdt");
521 }
522 }
523 return 0;
524 }
525
526 /*
527 * Create channel.
528 */
529 static int create_channel(const char *trace_name, struct ust_trace *trace,
530 const char *channel_name, struct ust_channel *chan,
531 unsigned int subbuf_size, unsigned int n_subbufs, int overwrite)
532 {
533 int i, result;
534
535 chan->trace = trace;
536 chan->overwrite = overwrite;
537 chan->n_subbufs_order = get_count_order(n_subbufs);
538 chan->commit_count_mask = (~0UL >> chan->n_subbufs_order);
539 chan->n_cpus = get_n_cpus();
540
541 /* These mappings should ideall be per-cpu, if somebody can do that
542 * from userspace, that would be cool!
543 */
544 chan->buf = (void *) zmalloc(chan->n_cpus * sizeof(void *));
545 if(chan->buf == NULL) {
546 goto error;
547 }
548 chan->buf_struct_shmids = (int *) zmalloc(chan->n_cpus * sizeof(int));
549 if(chan->buf_struct_shmids == NULL)
550 goto free_buf;
551
552 result = map_buf_structs(chan);
553 if(result != 0) {
554 goto free_buf_struct_shmids;
555 }
556
557 result = open_channel(chan, subbuf_size, n_subbufs);
558 if (result != 0) {
559 ERR("Cannot open channel for trace %s", trace_name);
560 goto unmap_buf_structs;
561 }
562
563 return 0;
564
565 unmap_buf_structs:
566 for (i=0; i < chan->n_cpus; i++) {
567 if (shmdt(chan->buf[i]) < 0) {
568 PERROR("shmdt bufstruct");
569 }
570 }
571
572 free_buf_struct_shmids:
573 free(chan->buf_struct_shmids);
574
575 free_buf:
576 free(chan->buf);
577
578 error:
579 return -1;
580 }
581
582
583 static void remove_channel(struct ust_channel *chan)
584 {
585 close_channel(chan);
586
587 unmap_buf_structs(chan);
588
589 free(chan->buf_struct_shmids);
590
591 free(chan->buf);
592 }
593
594 static void ltt_relay_async_wakeup_chan(struct ust_channel *ltt_channel)
595 {
596 }
597
598 static void ltt_relay_finish_buffer(struct ust_channel *channel, unsigned int cpu)
599 {
600 if (channel->buf[cpu]) {
601 struct ust_buffer *buf = channel->buf[cpu];
602 ltt_force_switch(buf, FORCE_FLUSH);
603
604 /* closing the pipe tells the consumer the buffer is finished */
605 close(buf->data_ready_fd_write);
606 }
607 }
608
609
610 static void finish_channel(struct ust_channel *channel)
611 {
612 unsigned int i;
613
614 for (i=0; i<channel->n_cpus; i++) {
615 ltt_relay_finish_buffer(channel, i);
616 }
617 }
618
619
620 /*
621 * ltt_reserve_switch_old_subbuf: switch old subbuffer
622 *
623 * Concurrency safe because we are the last and only thread to alter this
624 * sub-buffer. As long as it is not delivered and read, no other thread can
625 * alter the offset, alter the reserve_count or call the
626 * client_buffer_end_callback on this sub-buffer.
627 *
628 * The only remaining threads could be the ones with pending commits. They will
629 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
630 * We detect corrupted subbuffers with commit and reserve counts. We keep a
631 * corrupted sub-buffers count and push the readers across these sub-buffers.
632 *
633 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
634 * switches in, finding out it's corrupted. The result will be than the old
635 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
636 * will be declared corrupted too because of the commit count adjustment.
637 *
638 * Note : offset_old should never be 0 here.
639 */
640 static void ltt_reserve_switch_old_subbuf(
641 struct ust_channel *chan, struct ust_buffer *buf,
642 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
643 {
644 long oldidx = SUBBUF_INDEX(offsets->old - 1, chan);
645 long commit_count, padding_size;
646
647 padding_size = chan->subbuf_size
648 - (SUBBUF_OFFSET(offsets->old - 1, chan) + 1);
649 ltt_buffer_end(buf, *tsc, offsets->old, oldidx);
650
651 /*
652 * Must write slot data before incrementing commit count.
653 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
654 * sent by get_subbuf() when it does its cmm_smp_rmb().
655 */
656 cmm_smp_wmb();
657 uatomic_add(&buf->commit_count[oldidx].cc, padding_size);
658 commit_count = uatomic_read(&buf->commit_count[oldidx].cc);
659 ltt_check_deliver(chan, buf, offsets->old - 1, commit_count, oldidx);
660 ltt_write_commit_counter(chan, buf, oldidx,
661 offsets->old, commit_count, padding_size);
662 }
663
664 /*
665 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
666 *
667 * This code can be executed unordered : writers may already have written to the
668 * sub-buffer before this code gets executed, caution. The commit makes sure
669 * that this code is executed before the deliver of this sub-buffer.
670 */
671 static void ltt_reserve_switch_new_subbuf(
672 struct ust_channel *chan, struct ust_buffer *buf,
673 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
674 {
675 long beginidx = SUBBUF_INDEX(offsets->begin, chan);
676 long commit_count;
677
678 ltt_buffer_begin(buf, *tsc, beginidx);
679
680 /*
681 * Must write slot data before incrementing commit count.
682 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
683 * sent by get_subbuf() when it does its cmm_smp_rmb().
684 */
685 cmm_smp_wmb();
686 uatomic_add(&buf->commit_count[beginidx].cc, ltt_subbuffer_header_size());
687 commit_count = uatomic_read(&buf->commit_count[beginidx].cc);
688 /* Check if the written buffer has to be delivered */
689 ltt_check_deliver(chan, buf, offsets->begin, commit_count, beginidx);
690 ltt_write_commit_counter(chan, buf, beginidx,
691 offsets->begin, commit_count, ltt_subbuffer_header_size());
692 }
693
694 /*
695 * ltt_reserve_end_switch_current: finish switching current subbuffer
696 *
697 * Concurrency safe because we are the last and only thread to alter this
698 * sub-buffer. As long as it is not delivered and read, no other thread can
699 * alter the offset, alter the reserve_count or call the
700 * client_buffer_end_callback on this sub-buffer.
701 *
702 * The only remaining threads could be the ones with pending commits. They will
703 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
704 * We detect corrupted subbuffers with commit and reserve counts. We keep a
705 * corrupted sub-buffers count and push the readers across these sub-buffers.
706 *
707 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
708 * switches in, finding out it's corrupted. The result will be than the old
709 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
710 * will be declared corrupted too because of the commit count adjustment.
711 */
712 static void ltt_reserve_end_switch_current(
713 struct ust_channel *chan,
714 struct ust_buffer *buf,
715 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
716 {
717 long endidx = SUBBUF_INDEX(offsets->end - 1, chan);
718 long commit_count, padding_size;
719
720 padding_size = chan->subbuf_size
721 - (SUBBUF_OFFSET(offsets->end - 1, chan) + 1);
722
723 ltt_buffer_end(buf, *tsc, offsets->end, endidx);
724
725 /*
726 * Must write slot data before incrementing commit count.
727 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
728 * sent by get_subbuf() when it does its cmm_smp_rmb().
729 */
730 cmm_smp_wmb();
731 uatomic_add(&buf->commit_count[endidx].cc, padding_size);
732 commit_count = uatomic_read(&buf->commit_count[endidx].cc);
733 ltt_check_deliver(chan, buf,
734 offsets->end - 1, commit_count, endidx);
735 ltt_write_commit_counter(chan, buf, endidx,
736 offsets->end, commit_count, padding_size);
737 }
738
739 /*
740 * Returns :
741 * 0 if ok
742 * !0 if execution must be aborted.
743 */
744 static int ltt_relay_try_switch_slow(
745 enum force_switch_mode mode,
746 struct ust_channel *chan,
747 struct ust_buffer *buf,
748 struct ltt_reserve_switch_offsets *offsets,
749 u64 *tsc)
750 {
751 long subbuf_index;
752 long reserve_commit_diff;
753
754 offsets->begin = uatomic_read(&buf->offset);
755 offsets->old = offsets->begin;
756 offsets->begin_switch = 0;
757 offsets->end_switch_old = 0;
758
759 *tsc = trace_clock_read64();
760
761 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
762 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
763 offsets->end_switch_old = 1;
764 } else {
765 /* we do not have to switch : buffer is empty */
766 return -1;
767 }
768 if (mode == FORCE_ACTIVE)
769 offsets->begin += ltt_subbuffer_header_size();
770 /*
771 * Always begin_switch in FORCE_ACTIVE mode.
772 * Test new buffer integrity
773 */
774 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
775 reserve_commit_diff =
776 (BUFFER_TRUNC(offsets->begin, buf->chan)
777 >> chan->n_subbufs_order)
778 - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb)
779 & chan->commit_count_mask);
780 if (reserve_commit_diff == 0) {
781 /* Next buffer not corrupted. */
782 if (mode == FORCE_ACTIVE
783 && !chan->overwrite
784 && offsets->begin - uatomic_read(&buf->consumed)
785 >= chan->alloc_size) {
786 /*
787 * We do not overwrite non consumed buffers and we are
788 * full : ignore switch while tracing is active.
789 */
790 return -1;
791 }
792 } else {
793 /*
794 * Next subbuffer corrupted. Force pushing reader even in normal
795 * mode
796 */
797 }
798 offsets->end = offsets->begin;
799 return 0;
800 }
801
802 /*
803 * Force a sub-buffer switch for a per-cpu buffer. This operation is
804 * completely reentrant : can be called while tracing is active with
805 * absolutely no lock held.
806 */
807 void ltt_force_switch_lockless_slow(struct ust_buffer *buf,
808 enum force_switch_mode mode)
809 {
810 struct ust_channel *chan = buf->chan;
811 struct ltt_reserve_switch_offsets offsets;
812 u64 tsc;
813
814 offsets.size = 0;
815
816 DBG("Switching (forced) %s_%d", chan->channel_name, buf->cpu);
817 /*
818 * Perform retryable operations.
819 */
820 do {
821 if (ltt_relay_try_switch_slow(mode, chan, buf,
822 &offsets, &tsc))
823 return;
824 } while (uatomic_cmpxchg(&buf->offset, offsets.old,
825 offsets.end) != offsets.old);
826
827 /*
828 * Atomically update last_tsc. This update races against concurrent
829 * atomic updates, but the race will always cause supplementary full TSC
830 * events, never the opposite (missing a full TSC event when it would be
831 * needed).
832 */
833 save_last_tsc(buf, tsc);
834
835 /*
836 * Push the reader if necessary
837 */
838 if (mode == FORCE_ACTIVE) {
839 ltt_reserve_push_reader(chan, buf, offsets.end - 1);
840 }
841
842 /*
843 * Switch old subbuffer if needed.
844 */
845 if (offsets.end_switch_old) {
846 ltt_reserve_switch_old_subbuf(chan, buf, &offsets, &tsc);
847 }
848
849 /*
850 * Populate new subbuffer.
851 */
852 if (mode == FORCE_ACTIVE)
853 ltt_reserve_switch_new_subbuf(chan, buf, &offsets, &tsc);
854 }
855
856 /*
857 * Returns :
858 * 0 if ok
859 * !0 if execution must be aborted.
860 */
861 static int ltt_relay_try_reserve_slow(struct ust_channel *chan, struct ust_buffer *buf,
862 struct ltt_reserve_switch_offsets *offsets, size_t data_size,
863 u64 *tsc, unsigned int *rflags, int largest_align)
864 {
865 long reserve_commit_diff;
866
867 offsets->begin = uatomic_read(&buf->offset);
868 offsets->old = offsets->begin;
869 offsets->begin_switch = 0;
870 offsets->end_switch_current = 0;
871 offsets->end_switch_old = 0;
872
873 *tsc = trace_clock_read64();
874 if (last_tsc_overflow(buf, *tsc))
875 *rflags = LTT_RFLAG_ID_SIZE_TSC;
876
877 if (unlikely(SUBBUF_OFFSET(offsets->begin, buf->chan) == 0)) {
878 offsets->begin_switch = 1; /* For offsets->begin */
879 } else {
880 offsets->size = ust_get_header_size(chan,
881 offsets->begin, data_size,
882 &offsets->before_hdr_pad, *rflags);
883 offsets->size += ltt_align(offsets->begin + offsets->size,
884 largest_align)
885 + data_size;
886 if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) +
887 offsets->size) > buf->chan->subbuf_size)) {
888 offsets->end_switch_old = 1; /* For offsets->old */
889 offsets->begin_switch = 1; /* For offsets->begin */
890 }
891 }
892 if (unlikely(offsets->begin_switch)) {
893 long subbuf_index;
894
895 /*
896 * We are typically not filling the previous buffer completely.
897 */
898 if (likely(offsets->end_switch_old))
899 offsets->begin = SUBBUF_ALIGN(offsets->begin,
900 buf->chan);
901 offsets->begin = offsets->begin + ltt_subbuffer_header_size();
902 /* Test new buffer integrity */
903 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
904 reserve_commit_diff =
905 (BUFFER_TRUNC(offsets->begin, buf->chan)
906 >> chan->n_subbufs_order)
907 - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb)
908 & chan->commit_count_mask);
909 if (likely(reserve_commit_diff == 0)) {
910 /* Next buffer not corrupted. */
911 if (unlikely(!chan->overwrite &&
912 (SUBBUF_TRUNC(offsets->begin, buf->chan)
913 - SUBBUF_TRUNC(uatomic_read(
914 &buf->consumed),
915 buf->chan))
916 >= chan->alloc_size)) {
917 /*
918 * We do not overwrite non consumed buffers
919 * and we are full : event is lost.
920 */
921 uatomic_inc(&buf->events_lost);
922 return -1;
923 } else {
924 /*
925 * next buffer not corrupted, we are either in
926 * overwrite mode or the buffer is not full.
927 * It's safe to write in this new subbuffer.
928 */
929 }
930 } else {
931 /*
932 * Next subbuffer corrupted. Drop event in normal and
933 * overwrite mode. Caused by either a writer OOPS or
934 * too many nested writes over a reserve/commit pair.
935 */
936 uatomic_inc(&buf->events_lost);
937 return -1;
938 }
939 offsets->size = ust_get_header_size(chan,
940 offsets->begin, data_size,
941 &offsets->before_hdr_pad, *rflags);
942 offsets->size += ltt_align(offsets->begin + offsets->size,
943 largest_align)
944 + data_size;
945 if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan)
946 + offsets->size) > buf->chan->subbuf_size)) {
947 /*
948 * Event too big for subbuffers, report error, don't
949 * complete the sub-buffer switch.
950 */
951 uatomic_inc(&buf->events_lost);
952 return -1;
953 } else {
954 /*
955 * We just made a successful buffer switch and the event
956 * fits in the new subbuffer. Let's write.
957 */
958 }
959 } else {
960 /*
961 * Event fits in the current buffer and we are not on a switch
962 * boundary. It's safe to write.
963 */
964 }
965 offsets->end = offsets->begin + offsets->size;
966
967 if (unlikely((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0)) {
968 /*
969 * The offset_end will fall at the very beginning of the next
970 * subbuffer.
971 */
972 offsets->end_switch_current = 1; /* For offsets->begin */
973 }
974 return 0;
975 }
976
977 /**
978 * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer.
979 * @trace: the trace structure to log to.
980 * @ltt_channel: channel structure
981 * @transport_data: data structure specific to ltt relay
982 * @data_size: size of the variable length data to log.
983 * @slot_size: pointer to total size of the slot (out)
984 * @buf_offset : pointer to reserved buffer offset (out)
985 * @tsc: pointer to the tsc at the slot reservation (out)
986 * @cpu: cpuid
987 *
988 * Return : -ENOSPC if not enough space, else returns 0.
989 * It will take care of sub-buffer switching.
990 */
991 int ltt_reserve_slot_lockless_slow(struct ust_channel *chan,
992 struct ust_trace *trace, size_t data_size,
993 int largest_align, int cpu,
994 struct ust_buffer **ret_buf,
995 size_t *slot_size, long *buf_offset,
996 u64 *tsc, unsigned int *rflags)
997 {
998 struct ust_buffer *buf = *ret_buf = chan->buf[cpu];
999 struct ltt_reserve_switch_offsets offsets;
1000
1001 offsets.size = 0;
1002
1003 do {
1004 if (unlikely(ltt_relay_try_reserve_slow(chan, buf, &offsets,
1005 data_size, tsc, rflags, largest_align)))
1006 return -ENOSPC;
1007 } while (unlikely(uatomic_cmpxchg(&buf->offset, offsets.old,
1008 offsets.end) != offsets.old));
1009
1010 /*
1011 * Atomically update last_tsc. This update races against concurrent
1012 * atomic updates, but the race will always cause supplementary full TSC
1013 * events, never the opposite (missing a full TSC event when it would be
1014 * needed).
1015 */
1016 save_last_tsc(buf, *tsc);
1017
1018 /*
1019 * Push the reader if necessary
1020 */
1021 ltt_reserve_push_reader(chan, buf, offsets.end - 1);
1022
1023 /*
1024 * Switch old subbuffer if needed.
1025 */
1026 if (unlikely(offsets.end_switch_old)) {
1027 ltt_reserve_switch_old_subbuf(chan, buf, &offsets, tsc);
1028 DBG("Switching %s_%d", chan->channel_name, cpu);
1029 }
1030
1031 /*
1032 * Populate new subbuffer.
1033 */
1034 if (unlikely(offsets.begin_switch))
1035 ltt_reserve_switch_new_subbuf(chan, buf, &offsets, tsc);
1036
1037 if (unlikely(offsets.end_switch_current))
1038 ltt_reserve_end_switch_current(chan, buf, &offsets, tsc);
1039
1040 *slot_size = offsets.size;
1041 *buf_offset = offsets.begin + offsets.before_hdr_pad;
1042 return 0;
1043 }
1044
1045 static struct ltt_transport ust_relay_transport = {
1046 .name = "ustrelay",
1047 .ops = {
1048 .create_channel = create_channel,
1049 .finish_channel = finish_channel,
1050 .remove_channel = remove_channel,
1051 .wakeup_channel = ltt_relay_async_wakeup_chan,
1052 },
1053 };
1054
1055 static char initialized = 0;
1056
1057 void __attribute__((constructor)) init_ustrelay_transport(void)
1058 {
1059 init_usterr();
1060 if(!initialized) {
1061 ltt_transport_register(&ust_relay_transport);
1062 initialized = 1;
1063 }
1064 }
1065
1066 static void __attribute__((destructor)) ust_buffers_exit(void)
1067 {
1068 ltt_transport_unregister(&ust_relay_transport);
1069 }
1070
1071 size_t ltt_write_event_header_slow(struct ust_channel *channel,
1072 struct ust_buffer *buf, long buf_offset,
1073 u16 eID, u32 event_size,
1074 u64 tsc, unsigned int rflags)
1075 {
1076 struct ltt_event_header header;
1077 u16 small_size;
1078
1079 switch (rflags) {
1080 case LTT_RFLAG_ID_SIZE_TSC:
1081 header.id_time = 29 << LTT_TSC_BITS;
1082 break;
1083 case LTT_RFLAG_ID_SIZE:
1084 header.id_time = 30 << LTT_TSC_BITS;
1085 break;
1086 case LTT_RFLAG_ID:
1087 header.id_time = 31 << LTT_TSC_BITS;
1088 break;
1089 default:
1090 WARN_ON_ONCE(1);
1091 header.id_time = 0;
1092 break;
1093 }
1094
1095 header.id_time |= (u32)tsc & LTT_TSC_MASK;
1096 ust_buffers_write(buf, buf_offset, &header, sizeof(header));
1097 buf_offset += sizeof(header);
1098
1099 switch (rflags) {
1100 case LTT_RFLAG_ID_SIZE_TSC:
1101 small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE);
1102 ust_buffers_write(buf, buf_offset,
1103 &eID, sizeof(u16));
1104 buf_offset += sizeof(u16);
1105 ust_buffers_write(buf, buf_offset,
1106 &small_size, sizeof(u16));
1107 buf_offset += sizeof(u16);
1108 if (small_size == LTT_MAX_SMALL_SIZE) {
1109 ust_buffers_write(buf, buf_offset,
1110 &event_size, sizeof(u32));
1111 buf_offset += sizeof(u32);
1112 }
1113 buf_offset += ltt_align(buf_offset, sizeof(u64));
1114 ust_buffers_write(buf, buf_offset,
1115 &tsc, sizeof(u64));
1116 buf_offset += sizeof(u64);
1117 break;
1118 case LTT_RFLAG_ID_SIZE:
1119 small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE);
1120 ust_buffers_write(buf, buf_offset,
1121 &eID, sizeof(u16));
1122 buf_offset += sizeof(u16);
1123 ust_buffers_write(buf, buf_offset,
1124 &small_size, sizeof(u16));
1125 buf_offset += sizeof(u16);
1126 if (small_size == LTT_MAX_SMALL_SIZE) {
1127 ust_buffers_write(buf, buf_offset,
1128 &event_size, sizeof(u32));
1129 buf_offset += sizeof(u32);
1130 }
1131 break;
1132 case LTT_RFLAG_ID:
1133 ust_buffers_write(buf, buf_offset,
1134 &eID, sizeof(u16));
1135 buf_offset += sizeof(u16);
1136 break;
1137 }
1138
1139 return buf_offset;
1140 }
This page took 0.063264 seconds and 4 git commands to generate.