Add comment on manual registration of "ust" channel
[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 - 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
23 #include <sys/mman.h>
24 #include <sys/ipc.h>
25 #include <sys/shm.h>
26 #include <fcntl.h>
27 #include <ust/kernelcompat.h>
28 #include <kcompat/kref.h>
29 #include "buffers.h"
30 #include "channels.h"
31 #include "tracer.h"
32 #include "tracercore.h"
33 #include "usterr.h"
34
35 static DEFINE_MUTEX(ust_buffers_channels_mutex);
36 static LIST_HEAD(ust_buffers_channels);
37
38 static int ust_buffers_init_buffer(struct ltt_trace_struct *trace,
39 struct ust_channel *ltt_chan,
40 struct ust_buffer *buf,
41 unsigned int n_subbufs);
42
43 static int ust_buffers_alloc_buf(struct ust_buffer *buf, size_t *size)
44 {
45 void *ptr;
46 int result;
47
48 *size = PAGE_ALIGN(*size);
49
50 result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700);
51 if(result == -1 && errno == EINVAL) {
52 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
53 return -1;
54 }
55 else if(result == -1) {
56 PERROR("shmget");
57 return -1;
58 }
59
60 ptr = shmat(buf->shmid, NULL, 0);
61 if(ptr == (void *) -1) {
62 perror("shmat");
63 goto destroy_shmem;
64 }
65
66 /* Already mark the shared memory for destruction. This will occur only
67 * when all users have detached.
68 */
69 result = shmctl(buf->shmid, IPC_RMID, NULL);
70 if(result == -1) {
71 perror("shmctl");
72 return -1;
73 }
74
75 buf->buf_data = ptr;
76 buf->buf_size = *size;
77
78 return 0;
79
80 destroy_shmem:
81 result = shmctl(buf->shmid, IPC_RMID, NULL);
82 if(result == -1) {
83 perror("shmctl");
84 }
85
86 return -1;
87 }
88
89 static struct ust_buffer *ust_buffers_create_buf(struct ust_channel *channel)
90 {
91 int result;
92
93 result = ust_buffers_alloc_buf(channel->buf, &channel->alloc_size);
94 if(result)
95 goto free_buf;
96
97 ((struct ust_buffer *)channel->buf)->chan = channel;
98 kref_get(&channel->kref);
99 return channel->buf;
100
101 free_buf:
102 return NULL;
103 }
104
105 static void ust_buffers_destroy_channel(struct kref *kref)
106 {
107 struct ust_channel *chan = container_of(kref, struct ust_channel, kref);
108 free(chan);
109 }
110
111 static void ust_buffers_destroy_buf(struct ust_buffer *buf)
112 {
113 struct ust_channel *chan = buf->chan;
114 int result;
115
116 result = munmap(buf->buf_data, buf->buf_size);
117 if(result == -1) {
118 PERROR("munmap");
119 }
120
121 free(buf);
122 kref_put(&chan->kref, ust_buffers_destroy_channel);
123 }
124
125 /* called from kref_put */
126 static void ust_buffers_remove_buf(struct kref *kref)
127 {
128 struct ust_buffer *buf = container_of(kref, struct ust_buffer, kref);
129 ust_buffers_destroy_buf(buf);
130 }
131
132 static struct ust_buffer *ust_buffers_open_buf(struct ust_channel *chan)
133 {
134 struct ust_buffer *buf = NULL;
135 int err;
136
137 buf = ust_buffers_create_buf(chan);
138 if (!buf)
139 return NULL;
140
141 kref_init(&buf->kref);
142
143 err = ust_buffers_init_buffer(chan->trace, chan, buf, chan->subbuf_cnt);
144
145 if (err)
146 return ERR_PTR(err);
147
148 return buf;
149
150 /* FIXME: decrementally destroy on error? */
151 }
152
153 /**
154 * ust_buffers_close_buf - close a channel buffer
155 * @buf: buffer
156 */
157 static void ust_buffers_close_buf(struct ust_buffer *buf)
158 {
159 kref_put(&buf->kref, ust_buffers_remove_buf);
160 }
161
162 int ust_buffers_channel_open(struct ust_channel *chan, size_t subbuf_size, size_t subbuf_cnt)
163 {
164 if(subbuf_size == 0 || subbuf_cnt == 0)
165 return -1;
166
167 chan->version = UST_CHANNEL_VERSION;
168 chan->subbuf_cnt = subbuf_cnt;
169 chan->subbuf_size = subbuf_size;
170 chan->subbuf_size_order = get_count_order(subbuf_size);
171 chan->alloc_size = FIX_SIZE(subbuf_size * subbuf_cnt);
172 kref_init(&chan->kref);
173
174 mutex_lock(&ust_buffers_channels_mutex);
175 chan->buf = ust_buffers_open_buf(chan);
176 if (!chan->buf)
177 goto error;
178 list_add(&chan->list, &ust_buffers_channels);
179 mutex_unlock(&ust_buffers_channels_mutex);
180
181 return 0;
182
183 error:
184 kref_put(&chan->kref, ust_buffers_destroy_channel);
185 mutex_unlock(&ust_buffers_channels_mutex);
186 return -1;
187 }
188
189 void ust_buffers_channel_close(struct ust_channel *chan)
190 {
191 if (!chan)
192 return;
193
194 mutex_lock(&ust_buffers_channels_mutex);
195 if (chan->buf)
196 ust_buffers_close_buf(chan->buf);
197
198 list_del(&chan->list);
199 kref_put(&chan->kref, ust_buffers_destroy_channel);
200 mutex_unlock(&ust_buffers_channels_mutex);
201 }
202
203 /* _ust_buffers_write()
204 *
205 * @buf: destination buffer
206 * @offset: offset in destination
207 * @src: source buffer
208 * @len: length of source
209 * @cpy: already copied
210 */
211
212 void _ust_buffers_write(struct ust_buffer *buf, size_t offset,
213 const void *src, size_t len, ssize_t cpy)
214 {
215 do {
216 len -= cpy;
217 src += cpy;
218 offset += cpy;
219 WARN_ON(offset >= buf->buf_size);
220
221 cpy = min_t(size_t, len, buf->buf_size - offset);
222 ust_buffers_do_copy(buf->buf_data + offset, src, cpy);
223 } while (unlikely(len != cpy));
224 }
225
226 /**
227 * ltt_buffers_offset_address - get address of a location within the buffer
228 * @buf : buffer
229 * @offset : offset within the buffer.
230 *
231 * Return the address where a given offset is located.
232 * Should be used to get the current subbuffer header pointer. Given we know
233 * it's never on a page boundary, it's safe to write directly to this address,
234 * as long as the write is never bigger than a page size.
235 */
236 void *ltt_buffers_offset_address(struct ust_buffer *buf, size_t offset)
237 {
238 return ((char *)buf->buf_data)+offset;
239 }
240
241 /*
242 * -------
243 */
244
245 /*
246 * Last TSC comparison functions. Check if the current TSC overflows
247 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
248 * atomically.
249 */
250
251 /* FIXME: does this test work properly? */
252 #if (BITS_PER_LONG == 32)
253 static inline void save_last_tsc(struct ust_buffer *ltt_buf,
254 u64 tsc)
255 {
256 ltt_buf->last_tsc = (unsigned long)(tsc >> LTT_TSC_BITS);
257 }
258
259 static inline int last_tsc_overflow(struct ust_buffer *ltt_buf,
260 u64 tsc)
261 {
262 unsigned long tsc_shifted = (unsigned long)(tsc >> LTT_TSC_BITS);
263
264 if (unlikely((tsc_shifted - ltt_buf->last_tsc)))
265 return 1;
266 else
267 return 0;
268 }
269 #else
270 static inline void save_last_tsc(struct ust_buffer *ltt_buf,
271 u64 tsc)
272 {
273 ltt_buf->last_tsc = (unsigned long)tsc;
274 }
275
276 static inline int last_tsc_overflow(struct ust_buffer *ltt_buf,
277 u64 tsc)
278 {
279 if (unlikely((tsc - ltt_buf->last_tsc) >> LTT_TSC_BITS))
280 return 1;
281 else
282 return 0;
283 }
284 #endif
285
286 /*
287 * A switch is done during tracing or as a final flush after tracing (so it
288 * won't write in the new sub-buffer).
289 */
290 enum force_switch_mode { FORCE_ACTIVE, FORCE_FLUSH };
291
292 static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan);
293
294 static void ltt_force_switch(struct ust_buffer *buf,
295 enum force_switch_mode mode);
296
297 /*
298 * Trace callbacks
299 */
300 static void ltt_buffer_begin_callback(struct ust_buffer *buf,
301 u64 tsc, unsigned int subbuf_idx)
302 {
303 struct ust_channel *channel = buf->chan;
304 struct ltt_subbuffer_header *header =
305 (struct ltt_subbuffer_header *)
306 ltt_buffers_offset_address(buf,
307 subbuf_idx * buf->chan->subbuf_size);
308
309 header->cycle_count_begin = tsc;
310 header->lost_size = 0xFFFFFFFF; /* for debugging */
311 header->buf_size = buf->chan->subbuf_size;
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 */
319 static notrace void ltt_buffer_end_callback(struct ust_buffer *buf,
320 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
321 {
322 struct ltt_subbuffer_header *header =
323 (struct ltt_subbuffer_header *)
324 ltt_buffers_offset_address(buf,
325 subbuf_idx * buf->chan->subbuf_size);
326
327 header->lost_size = SUBBUF_OFFSET((buf->chan->subbuf_size - offset),
328 buf->chan);
329 header->cycle_count_end = tsc;
330 header->events_lost = local_read(&buf->events_lost);
331 header->subbuf_corrupt = local_read(&buf->corrupted_subbuffers);
332
333 }
334
335 void (*wake_consumer)(void *, int) = NULL;
336
337 void relay_set_wake_consumer(void (*wake)(void *, int))
338 {
339 wake_consumer = wake;
340 }
341
342 void relay_wake_consumer(void *arg, int finished)
343 {
344 if(wake_consumer)
345 wake_consumer(arg, finished);
346 }
347
348 static notrace void ltt_deliver(struct ust_buffer *buf, unsigned int subbuf_idx,
349 long commit_count)
350 {
351 int result;
352
353 //ust// #ifdef CONFIG_LTT_VMCORE
354 local_set(&buf->commit_seq[subbuf_idx], commit_count);
355 //ust// #endif
356
357 /* wakeup consumer */
358 result = write(buf->data_ready_fd_write, "1", 1);
359 if(result == -1) {
360 PERROR("write (in ltt_relay_buffer_flush)");
361 ERR("this should never happen!");
362 }
363 //ust// atomic_set(&ltt_buf->wakeup_readers, 1);
364 }
365
366 /*
367 * This function should not be called from NMI interrupt context
368 */
369 static notrace void ltt_buf_unfull(struct ust_buffer *buf,
370 unsigned int subbuf_idx,
371 long offset)
372 {
373 //ust// struct ltt_channel_struct *ltt_channel =
374 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
375 //ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
376 //ust//
377 //ust// ltt_relay_wake_writers(ltt_buf);
378 }
379
380 int ust_buffers_do_get_subbuf(struct ust_buffer *buf, long *pconsumed_old)
381 {
382 struct ust_channel *channel = buf->chan;
383 long consumed_old, consumed_idx, commit_count, write_offset;
384 consumed_old = atomic_long_read(&buf->consumed);
385 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
386 commit_count = local_read(&buf->commit_count[consumed_idx]);
387 /*
388 * Make sure we read the commit count before reading the buffer
389 * data and the write offset. Correct consumed offset ordering
390 * wrt commit count is insured by the use of cmpxchg to update
391 * the consumed offset.
392 */
393 smp_rmb();
394 write_offset = local_read(&buf->offset);
395 /*
396 * Check that the subbuffer we are trying to consume has been
397 * already fully committed.
398 */
399 if (((commit_count - buf->chan->subbuf_size)
400 & channel->commit_count_mask)
401 - (BUFFER_TRUNC(consumed_old, buf->chan)
402 >> channel->n_subbufs_order)
403 != 0) {
404 return -EAGAIN;
405 }
406 /*
407 * Check that we are not about to read the same subbuffer in
408 * which the writer head is.
409 */
410 if ((SUBBUF_TRUNC(write_offset, buf->chan)
411 - SUBBUF_TRUNC(consumed_old, buf->chan))
412 == 0) {
413 return -EAGAIN;
414 }
415
416 *pconsumed_old = consumed_old;
417 return 0;
418 }
419
420 int ust_buffers_do_put_subbuf(struct ust_buffer *buf, u32 uconsumed_old)
421 {
422 long consumed_new, consumed_old;
423
424 consumed_old = atomic_long_read(&buf->consumed);
425 consumed_old = consumed_old & (~0xFFFFFFFFL);
426 consumed_old = consumed_old | uconsumed_old;
427 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
428
429 //ust// spin_lock(&ltt_buf->full_lock);
430 if (atomic_long_cmpxchg(&buf->consumed, consumed_old,
431 consumed_new)
432 != consumed_old) {
433 /* We have been pushed by the writer : the last
434 * buffer read _is_ corrupted! It can also
435 * happen if this is a buffer we never got. */
436 //ust// spin_unlock(&ltt_buf->full_lock);
437 return -EIO;
438 } else {
439 /* tell the client that buffer is now unfull */
440 int index;
441 long data;
442 index = SUBBUF_INDEX(consumed_old, buf->chan);
443 data = BUFFER_OFFSET(consumed_old, buf->chan);
444 ltt_buf_unfull(buf, index, data);
445 //ust// spin_unlock(&ltt_buf->full_lock);
446 }
447 return 0;
448 }
449
450 static void ltt_relay_print_subbuffer_errors(
451 struct ust_channel *channel,
452 long cons_off)
453 {
454 struct ust_buffer *ltt_buf = channel->buf;
455 long cons_idx, commit_count, write_offset;
456
457 cons_idx = SUBBUF_INDEX(cons_off, channel);
458 commit_count = local_read(&ltt_buf->commit_count[cons_idx]);
459 /*
460 * No need to order commit_count and write_offset reads because we
461 * execute after trace is stopped when there are no readers left.
462 */
463 write_offset = local_read(&ltt_buf->offset);
464 WARN( "LTT : unread channel %s offset is %ld "
465 "and cons_off : %ld\n",
466 channel->channel_name, write_offset, cons_off);
467 /* Check each sub-buffer for non filled commit count */
468 if (((commit_count - channel->subbuf_size) & channel->commit_count_mask)
469 - (BUFFER_TRUNC(cons_off, channel) >> channel->n_subbufs_order) != 0) {
470 ERR("LTT : %s : subbuffer %lu has non filled "
471 "commit count %lu.\n",
472 channel->channel_name, cons_idx, commit_count);
473 }
474 ERR("LTT : %s : commit count : %lu, subbuf size %zd\n",
475 channel->channel_name, commit_count,
476 channel->subbuf_size);
477 }
478
479 static void ltt_relay_print_errors(struct ltt_trace_struct *trace,
480 struct ust_channel *channel)
481 {
482 struct ust_buffer *ltt_buf = channel->buf;
483 long cons_off;
484
485 /*
486 * Can be called in the error path of allocation when
487 * trans_channel_data is not yet set.
488 */
489 if (!channel)
490 return;
491
492 for (cons_off = atomic_long_read(&ltt_buf->consumed);
493 (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
494 channel)
495 - cons_off) > 0;
496 cons_off = SUBBUF_ALIGN(cons_off, channel))
497 ltt_relay_print_subbuffer_errors(channel, cons_off);
498 }
499
500 static void ltt_relay_print_buffer_errors(struct ust_channel *channel)
501 {
502 struct ltt_trace_struct *trace = channel->trace;
503 struct ust_buffer *ltt_buf = channel->buf;
504
505 if (local_read(&ltt_buf->events_lost))
506 printk(KERN_ALERT
507 "LTT : %s : %ld events lost "
508 "in %s channel.\n",
509 channel->channel_name,
510 local_read(&ltt_buf->events_lost),
511 channel->channel_name);
512 if (local_read(&ltt_buf->corrupted_subbuffers))
513 printk(KERN_ALERT
514 "LTT : %s : %ld corrupted subbuffers "
515 "in %s channel.\n",
516 channel->channel_name,
517 local_read(&ltt_buf->corrupted_subbuffers),
518 channel->channel_name);
519
520 ltt_relay_print_errors(trace, channel);
521 }
522
523 static void ltt_relay_release_channel(struct kref *kref)
524 {
525 struct ust_channel *ltt_chan = container_of(kref,
526 struct ust_channel, kref);
527 free(ltt_chan->buf);
528 }
529
530 /*
531 * Create ltt buffer.
532 */
533 //ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
534 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
535 //ust// unsigned int cpu, unsigned int n_subbufs)
536 //ust// {
537 //ust// struct ltt_channel_buf_struct *ltt_buf =
538 //ust// percpu_ptr(ltt_chan->buf, cpu);
539 //ust// unsigned int j;
540 //ust//
541 //ust// ltt_buf->commit_count =
542 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
543 //ust// GFP_KERNEL, cpu_to_node(cpu));
544 //ust// if (!ltt_buf->commit_count)
545 //ust// return -ENOMEM;
546 //ust// kref_get(&trace->kref);
547 //ust// kref_get(&trace->ltt_transport_kref);
548 //ust// kref_get(&ltt_chan->kref);
549 //ust// local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
550 //ust// atomic_long_set(&ltt_buf->consumed, 0);
551 //ust// atomic_long_set(&ltt_buf->active_readers, 0);
552 //ust// for (j = 0; j < n_subbufs; j++)
553 //ust// local_set(&ltt_buf->commit_count[j], 0);
554 //ust// init_waitqueue_head(&ltt_buf->write_wait);
555 //ust// atomic_set(&ltt_buf->wakeup_readers, 0);
556 //ust// spin_lock_init(&ltt_buf->full_lock);
557 //ust//
558 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
559 //ust// /* atomic_add made on local variable on data that belongs to
560 //ust// * various CPUs : ok because tracing not started (for this cpu). */
561 //ust// local_add(ltt_subbuffer_header_size(), &ltt_buf->commit_count[0]);
562 //ust//
563 //ust// local_set(&ltt_buf->events_lost, 0);
564 //ust// local_set(&ltt_buf->corrupted_subbuffers, 0);
565 //ust//
566 //ust// return 0;
567 //ust// }
568
569 static int ust_buffers_init_buffer(struct ltt_trace_struct *trace,
570 struct ust_channel *ltt_chan, struct ust_buffer *buf,
571 unsigned int n_subbufs)
572 {
573 unsigned int j;
574 int fds[2];
575 int result;
576
577 buf->commit_count =
578 zmalloc(sizeof(buf->commit_count) * n_subbufs);
579 if (!buf->commit_count)
580 return -ENOMEM;
581 kref_get(&trace->kref);
582 kref_get(&trace->ltt_transport_kref);
583 kref_get(&ltt_chan->kref);
584 local_set(&buf->offset, ltt_subbuffer_header_size());
585 atomic_long_set(&buf->consumed, 0);
586 atomic_long_set(&buf->active_readers, 0);
587 for (j = 0; j < n_subbufs; j++)
588 local_set(&buf->commit_count[j], 0);
589 //ust// init_waitqueue_head(&buf->write_wait);
590 //ust// atomic_set(&buf->wakeup_readers, 0);
591 //ust// spin_lock_init(&buf->full_lock);
592
593 ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
594
595 local_add(ltt_subbuffer_header_size(), &buf->commit_count[0]);
596
597 local_set(&buf->events_lost, 0);
598 local_set(&buf->corrupted_subbuffers, 0);
599
600 result = pipe(fds);
601 if(result == -1) {
602 PERROR("pipe");
603 return -1;
604 }
605 buf->data_ready_fd_read = fds[0];
606 buf->data_ready_fd_write = fds[1];
607
608 /* FIXME: do we actually need this? */
609 result = fcntl(fds[0], F_SETFL, O_NONBLOCK);
610 if(result == -1) {
611 PERROR("fcntl");
612 }
613
614 //ust// buf->commit_seq = malloc(sizeof(buf->commit_seq) * n_subbufs);
615 //ust// if(!ltt_buf->commit_seq) {
616 //ust// return -1;
617 //ust// }
618
619 /* FIXME: decrementally destroy on error */
620
621 return 0;
622 }
623
624 /* FIXME: use this function */
625 static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan)
626 {
627 struct ltt_trace_struct *trace = ltt_chan->trace;
628 struct ust_buffer *ltt_buf = ltt_chan->buf;
629
630 kref_put(&ltt_chan->trace->ltt_transport_kref,
631 ltt_release_transport);
632 ltt_relay_print_buffer_errors(ltt_chan);
633 //ust// free(ltt_buf->commit_seq);
634 kfree(ltt_buf->commit_count);
635 ltt_buf->commit_count = NULL;
636 kref_put(&ltt_chan->kref, ltt_relay_release_channel);
637 kref_put(&trace->kref, ltt_release_trace);
638 //ust// wake_up_interruptible(&trace->kref_wq);
639 }
640
641 static void ltt_chan_alloc_ltt_buf(struct ust_channel *chan)
642 {
643 void *ptr;
644 int result;
645
646 /* Get one page */
647 /* FIXME: increase size if we have a seq_commit array that overflows the page */
648 size_t size = PAGE_ALIGN(1);
649
650 result = chan->buf_shmid = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700);
651 if(chan->buf_shmid == -1) {
652 PERROR("shmget");
653 return;
654 }
655
656 ptr = shmat(chan->buf_shmid, NULL, 0);
657 if(ptr == (void *) -1) {
658 perror("shmat");
659 goto destroy_shmem;
660 }
661
662 /* Already mark the shared memory for destruction. This will occur only
663 * when all users have detached.
664 */
665 result = shmctl(chan->buf_shmid, IPC_RMID, NULL);
666 if(result == -1) {
667 perror("shmctl");
668 return;
669 }
670
671 chan->buf = ptr;
672
673 return;
674
675 destroy_shmem:
676 result = shmctl(chan->buf_shmid, IPC_RMID, NULL);
677 if(result == -1) {
678 perror("shmctl");
679 }
680
681 return;
682 }
683
684 /*
685 * Create channel.
686 */
687 static int ust_buffers_create_channel(const char *trace_name, struct ltt_trace_struct *trace,
688 const char *channel_name, struct ust_channel *ltt_chan,
689 unsigned int subbuf_size, unsigned int n_subbufs, int overwrite)
690 {
691 int err = 0;
692 int result;
693
694 kref_init(&ltt_chan->kref);
695
696 ltt_chan->trace = trace;
697 ltt_chan->buffer_begin = ltt_buffer_begin_callback;
698 ltt_chan->buffer_end = ltt_buffer_end_callback;
699 ltt_chan->overwrite = overwrite;
700 ltt_chan->n_subbufs_order = get_count_order(n_subbufs);
701 ltt_chan->commit_count_mask = (~0UL >> ltt_chan->n_subbufs_order);
702 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
703
704 ltt_chan_alloc_ltt_buf(ltt_chan);
705
706 //ust// ltt_chan->buf = malloc(sizeof(struct ltt_channel_buf_struct));
707 if (!ltt_chan->buf)
708 goto alloc_error;
709 /* FIXME: handle error of this call */
710 result = ust_buffers_channel_open(ltt_chan, subbuf_size, n_subbufs);
711 if (result == -1) {
712 printk(KERN_ERR "LTT : Can't open channel for trace %s\n",
713 trace_name);
714 goto relay_open_error;
715 }
716
717 err = 0;
718 goto end;
719
720 relay_open_error:
721 //ust// percpu_free(ltt_chan->buf);
722 alloc_error:
723 err = EPERM;
724 end:
725 return err;
726 }
727
728 /*
729 * LTTng channel flush function.
730 *
731 * Must be called when no tracing is active in the channel, because of
732 * accesses across CPUs.
733 */
734 static notrace void ltt_relay_buffer_flush(struct ust_buffer *buf)
735 {
736 int result;
737
738 //ust// buf->finalized = 1;
739 ltt_force_switch(buf, FORCE_FLUSH);
740
741 result = write(buf->data_ready_fd_write, "1", 1);
742 if(result == -1) {
743 PERROR("write (in ltt_relay_buffer_flush)");
744 ERR("this should never happen!");
745 }
746 }
747
748 static void ltt_relay_async_wakeup_chan(struct ust_channel *ltt_channel)
749 {
750 //ust// unsigned int i;
751 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
752 //ust//
753 //ust// for_each_possible_cpu(i) {
754 //ust// struct ltt_channel_buf_struct *ltt_buf =
755 //ust// percpu_ptr(ltt_channel->buf, i);
756 //ust//
757 //ust// if (atomic_read(&ltt_buf->wakeup_readers) == 1) {
758 //ust// atomic_set(&ltt_buf->wakeup_readers, 0);
759 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
760 //ust// }
761 //ust// }
762 }
763
764 static void ltt_relay_finish_buffer(struct ust_channel *channel)
765 {
766 // int result;
767
768 if (channel->buf) {
769 struct ust_buffer *buf = channel->buf;
770 ltt_relay_buffer_flush(buf);
771 //ust// ltt_relay_wake_writers(ltt_buf);
772 /* closing the pipe tells the consumer the buffer is finished */
773
774 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
775 //if(result == -1) {
776 // PERROR("write (in ltt_relay_finish_buffer)");
777 // ERR("this should never happen!");
778 //}
779 close(buf->data_ready_fd_write);
780 }
781 }
782
783
784 static void ltt_relay_finish_channel(struct ust_channel *channel)
785 {
786 //ust// unsigned int i;
787
788 //ust// for_each_possible_cpu(i)
789 ltt_relay_finish_buffer(channel);
790 }
791
792 static void ltt_relay_remove_channel(struct ust_channel *channel)
793 {
794 ust_buffers_channel_close(channel);
795 kref_put(&channel->kref, ltt_relay_release_channel);
796 }
797
798 struct ltt_reserve_switch_offsets {
799 long begin, end, old;
800 long begin_switch, end_switch_current, end_switch_old;
801 long commit_count, reserve_commit_diff;
802 size_t before_hdr_pad, size;
803 };
804
805 /*
806 * Returns :
807 * 0 if ok
808 * !0 if execution must be aborted.
809 */
810 static inline int ltt_relay_try_reserve(
811 struct ust_channel *channel, struct ust_buffer *buf,
812 struct ltt_reserve_switch_offsets *offsets, size_t data_size,
813 u64 *tsc, unsigned int *rflags, int largest_align)
814 {
815 offsets->begin = local_read(&buf->offset);
816 offsets->old = offsets->begin;
817 offsets->begin_switch = 0;
818 offsets->end_switch_current = 0;
819 offsets->end_switch_old = 0;
820
821 *tsc = trace_clock_read64();
822 if (last_tsc_overflow(buf, *tsc))
823 *rflags = LTT_RFLAG_ID_SIZE_TSC;
824
825 if (SUBBUF_OFFSET(offsets->begin, buf->chan) == 0) {
826 offsets->begin_switch = 1; /* For offsets->begin */
827 } else {
828 offsets->size = ust_get_header_size(channel,
829 offsets->begin, data_size,
830 &offsets->before_hdr_pad, *rflags);
831 offsets->size += ltt_align(offsets->begin + offsets->size,
832 largest_align)
833 + data_size;
834 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
835 > buf->chan->subbuf_size) {
836 offsets->end_switch_old = 1; /* For offsets->old */
837 offsets->begin_switch = 1; /* For offsets->begin */
838 }
839 }
840 if (offsets->begin_switch) {
841 long subbuf_index;
842
843 if (offsets->end_switch_old)
844 offsets->begin = SUBBUF_ALIGN(offsets->begin,
845 buf->chan);
846 offsets->begin = offsets->begin + ltt_subbuffer_header_size();
847 /* Test new buffer integrity */
848 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
849 offsets->reserve_commit_diff =
850 (BUFFER_TRUNC(offsets->begin, buf->chan)
851 >> channel->n_subbufs_order)
852 - (local_read(&buf->commit_count[subbuf_index])
853 & channel->commit_count_mask);
854 if (offsets->reserve_commit_diff == 0) {
855 long consumed;
856
857 consumed = atomic_long_read(&buf->consumed);
858
859 /* Next buffer not corrupted. */
860 if (!channel->overwrite &&
861 (SUBBUF_TRUNC(offsets->begin, buf->chan)
862 - SUBBUF_TRUNC(consumed, buf->chan))
863 >= channel->alloc_size) {
864
865 long consumed_idx = SUBBUF_INDEX(consumed, buf->chan);
866 long commit_count = local_read(&buf->commit_count[consumed_idx]);
867 if(((commit_count - buf->chan->subbuf_size) & channel->commit_count_mask) - (BUFFER_TRUNC(consumed, buf->chan) >> channel->n_subbufs_order) != 0) {
868 WARN("Event dropped. Caused by non-committed event.");
869 }
870 else {
871 WARN("Event dropped. Caused by non-consumed buffer.");
872 }
873 /*
874 * We do not overwrite non consumed buffers
875 * and we are full : event is lost.
876 */
877 local_inc(&buf->events_lost);
878 return -1;
879 } else {
880 /*
881 * next buffer not corrupted, we are either in
882 * overwrite mode or the buffer is not full.
883 * It's safe to write in this new subbuffer.
884 */
885 }
886 } else {
887 /*
888 * Next subbuffer corrupted. Force pushing reader even
889 * in normal mode. It's safe to write in this new
890 * subbuffer.
891 */
892 }
893 offsets->size = ust_get_header_size(channel,
894 offsets->begin, data_size,
895 &offsets->before_hdr_pad, *rflags);
896 offsets->size += ltt_align(offsets->begin + offsets->size,
897 largest_align)
898 + data_size;
899 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
900 > buf->chan->subbuf_size) {
901 /*
902 * Event too big for subbuffers, report error, don't
903 * complete the sub-buffer switch.
904 */
905 local_inc(&buf->events_lost);
906 return -1;
907 } else {
908 /*
909 * We just made a successful buffer switch and the event
910 * fits in the new subbuffer. Let's write.
911 */
912 }
913 } else {
914 /*
915 * Event fits in the current buffer and we are not on a switch
916 * boundary. It's safe to write.
917 */
918 }
919 offsets->end = offsets->begin + offsets->size;
920
921 if ((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0) {
922 /*
923 * The offset_end will fall at the very beginning of the next
924 * subbuffer.
925 */
926 offsets->end_switch_current = 1; /* For offsets->begin */
927 }
928 return 0;
929 }
930
931 /*
932 * Returns :
933 * 0 if ok
934 * !0 if execution must be aborted.
935 */
936 static inline int ltt_relay_try_switch(
937 enum force_switch_mode mode,
938 struct ust_channel *channel,
939 struct ust_buffer *buf,
940 struct ltt_reserve_switch_offsets *offsets,
941 u64 *tsc)
942 {
943 long subbuf_index;
944
945 offsets->begin = local_read(&buf->offset);
946 offsets->old = offsets->begin;
947 offsets->begin_switch = 0;
948 offsets->end_switch_old = 0;
949
950 *tsc = trace_clock_read64();
951
952 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
953 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
954 offsets->end_switch_old = 1;
955 } else {
956 /* we do not have to switch : buffer is empty */
957 return -1;
958 }
959 if (mode == FORCE_ACTIVE)
960 offsets->begin += ltt_subbuffer_header_size();
961 /*
962 * Always begin_switch in FORCE_ACTIVE mode.
963 * Test new buffer integrity
964 */
965 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
966 offsets->reserve_commit_diff =
967 (BUFFER_TRUNC(offsets->begin, buf->chan)
968 >> channel->n_subbufs_order)
969 - (local_read(&buf->commit_count[subbuf_index])
970 & channel->commit_count_mask);
971 if (offsets->reserve_commit_diff == 0) {
972 /* Next buffer not corrupted. */
973 if (mode == FORCE_ACTIVE
974 && !channel->overwrite
975 && offsets->begin - atomic_long_read(&buf->consumed)
976 >= channel->alloc_size) {
977 /*
978 * We do not overwrite non consumed buffers and we are
979 * full : ignore switch while tracing is active.
980 */
981 return -1;
982 }
983 } else {
984 /*
985 * Next subbuffer corrupted. Force pushing reader even in normal
986 * mode
987 */
988 }
989 offsets->end = offsets->begin;
990 return 0;
991 }
992
993 static inline void ltt_reserve_push_reader(
994 struct ust_channel *channel,
995 struct ust_buffer *buf,
996 struct ltt_reserve_switch_offsets *offsets)
997 {
998 long consumed_old, consumed_new;
999
1000 do {
1001 consumed_old = atomic_long_read(&buf->consumed);
1002 /*
1003 * If buffer is in overwrite mode, push the reader consumed
1004 * count if the write position has reached it and we are not
1005 * at the first iteration (don't push the reader farther than
1006 * the writer). This operation can be done concurrently by many
1007 * writers in the same buffer, the writer being at the farthest
1008 * write position sub-buffer index in the buffer being the one
1009 * which will win this loop.
1010 * If the buffer is not in overwrite mode, pushing the reader
1011 * only happens if a sub-buffer is corrupted.
1012 */
1013 if ((SUBBUF_TRUNC(offsets->end-1, buf->chan)
1014 - SUBBUF_TRUNC(consumed_old, buf->chan))
1015 >= channel->alloc_size)
1016 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
1017 else {
1018 consumed_new = consumed_old;
1019 break;
1020 }
1021 } while (atomic_long_cmpxchg(&buf->consumed, consumed_old,
1022 consumed_new) != consumed_old);
1023
1024 if (consumed_old != consumed_new) {
1025 /*
1026 * Reader pushed : we are the winner of the push, we can
1027 * therefore reequilibrate reserve and commit. Atomic increment
1028 * of the commit count permits other writers to play around
1029 * with this variable before us. We keep track of
1030 * corrupted_subbuffers even in overwrite mode :
1031 * we never want to write over a non completely committed
1032 * sub-buffer : possible causes : the buffer size is too low
1033 * compared to the unordered data input, or there is a writer
1034 * that died between the reserve and the commit.
1035 */
1036 if (offsets->reserve_commit_diff) {
1037 /*
1038 * We have to alter the sub-buffer commit count.
1039 * We do not deliver the previous subbuffer, given it
1040 * was either corrupted or not consumed (overwrite
1041 * mode).
1042 */
1043 local_add(offsets->reserve_commit_diff,
1044 &buf->commit_count[
1045 SUBBUF_INDEX(offsets->begin,
1046 buf->chan)]);
1047 if (!channel->overwrite
1048 || offsets->reserve_commit_diff
1049 != channel->subbuf_size) {
1050 /*
1051 * The reserve commit diff was not subbuf_size :
1052 * it means the subbuffer was partly written to
1053 * and is therefore corrupted. If it is multiple
1054 * of subbuffer size and we are in flight
1055 * recorder mode, we are skipping over a whole
1056 * subbuffer.
1057 */
1058 local_inc(&buf->corrupted_subbuffers);
1059 }
1060 }
1061 }
1062 }
1063
1064
1065 /*
1066 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1067 *
1068 * Concurrency safe because we are the last and only thread to alter this
1069 * sub-buffer. As long as it is not delivered and read, no other thread can
1070 * alter the offset, alter the reserve_count or call the
1071 * client_buffer_end_callback on this sub-buffer.
1072 *
1073 * The only remaining threads could be the ones with pending commits. They will
1074 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1075 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1076 * corrupted sub-buffers count and push the readers across these sub-buffers.
1077 *
1078 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1079 * switches in, finding out it's corrupted. The result will be than the old
1080 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1081 * will be declared corrupted too because of the commit count adjustment.
1082 *
1083 * Note : offset_old should never be 0 here.
1084 */
1085 static inline void ltt_reserve_switch_old_subbuf(
1086 struct ust_channel *channel,
1087 struct ust_buffer *buf,
1088 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
1089 {
1090 long oldidx = SUBBUF_INDEX(offsets->old - 1, channel);
1091
1092 channel->buffer_end(buf, *tsc, offsets->old, oldidx);
1093 /* Must write buffer end before incrementing commit count */
1094 smp_wmb();
1095 offsets->commit_count =
1096 local_add_return(channel->subbuf_size
1097 - (SUBBUF_OFFSET(offsets->old - 1, channel)
1098 + 1),
1099 &buf->commit_count[oldidx]);
1100 if ((BUFFER_TRUNC(offsets->old - 1, channel)
1101 >> channel->n_subbufs_order)
1102 - ((offsets->commit_count - channel->subbuf_size)
1103 & channel->commit_count_mask) == 0)
1104 ltt_deliver(buf, oldidx, offsets->commit_count);
1105 }
1106
1107 /*
1108 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
1109 *
1110 * This code can be executed unordered : writers may already have written to the
1111 * sub-buffer before this code gets executed, caution. The commit makes sure
1112 * that this code is executed before the deliver of this sub-buffer.
1113 */
1114 static /*inline*/ void ltt_reserve_switch_new_subbuf(
1115 struct ust_channel *channel,
1116 struct ust_buffer *buf,
1117 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
1118 {
1119 long beginidx = SUBBUF_INDEX(offsets->begin, channel);
1120
1121 channel->buffer_begin(buf, *tsc, beginidx);
1122 /* Must write buffer end before incrementing commit count */
1123 smp_wmb();
1124 offsets->commit_count = local_add_return(ltt_subbuffer_header_size(),
1125 &buf->commit_count[beginidx]);
1126 /* Check if the written buffer has to be delivered */
1127 if ((BUFFER_TRUNC(offsets->begin, channel)
1128 >> channel->n_subbufs_order)
1129 - ((offsets->commit_count - channel->subbuf_size)
1130 & channel->commit_count_mask) == 0)
1131 ltt_deliver(buf, beginidx, offsets->commit_count);
1132 }
1133
1134
1135 /*
1136 * ltt_reserve_end_switch_current: finish switching current subbuffer
1137 *
1138 * Concurrency safe because we are the last and only thread to alter this
1139 * sub-buffer. As long as it is not delivered and read, no other thread can
1140 * alter the offset, alter the reserve_count or call the
1141 * client_buffer_end_callback on this sub-buffer.
1142 *
1143 * The only remaining threads could be the ones with pending commits. They will
1144 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1145 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1146 * corrupted sub-buffers count and push the readers across these sub-buffers.
1147 *
1148 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1149 * switches in, finding out it's corrupted. The result will be than the old
1150 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1151 * will be declared corrupted too because of the commit count adjustment.
1152 */
1153 static inline void ltt_reserve_end_switch_current(
1154 struct ust_channel *channel,
1155 struct ust_buffer *buf,
1156 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
1157 {
1158 long endidx = SUBBUF_INDEX(offsets->end - 1, channel);
1159
1160 channel->buffer_end(buf, *tsc, offsets->end, endidx);
1161 /* Must write buffer begin before incrementing commit count */
1162 smp_wmb();
1163 offsets->commit_count =
1164 local_add_return(channel->subbuf_size
1165 - (SUBBUF_OFFSET(offsets->end - 1, channel)
1166 + 1),
1167 &buf->commit_count[endidx]);
1168 if ((BUFFER_TRUNC(offsets->end - 1, channel)
1169 >> channel->n_subbufs_order)
1170 - ((offsets->commit_count - channel->subbuf_size)
1171 & channel->commit_count_mask) == 0)
1172 ltt_deliver(buf, endidx, offsets->commit_count);
1173 }
1174
1175 /**
1176 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
1177 * @trace: the trace structure to log to.
1178 * @ltt_channel: channel structure
1179 * @transport_data: data structure specific to ltt relay
1180 * @data_size: size of the variable length data to log.
1181 * @slot_size: pointer to total size of the slot (out)
1182 * @buf_offset : pointer to reserved buffer offset (out)
1183 * @tsc: pointer to the tsc at the slot reservation (out)
1184 * @cpu: cpuid
1185 *
1186 * Return : -ENOSPC if not enough space, else returns 0.
1187 * It will take care of sub-buffer switching.
1188 */
1189 static notrace int ltt_relay_reserve_slot(struct ltt_trace_struct *trace,
1190 struct ust_channel *channel, void **transport_data,
1191 size_t data_size, size_t *slot_size, long *buf_offset, u64 *tsc,
1192 unsigned int *rflags, int largest_align)
1193 {
1194 struct ust_buffer *buf = *transport_data = channel->buf;
1195 struct ltt_reserve_switch_offsets offsets;
1196
1197 offsets.reserve_commit_diff = 0;
1198 offsets.size = 0;
1199
1200 /*
1201 * Perform retryable operations.
1202 */
1203 if (ltt_nesting > 4) {
1204 local_inc(&buf->events_lost);
1205 return -EPERM;
1206 }
1207 do {
1208 if (ltt_relay_try_reserve(channel, buf, &offsets, data_size, tsc, rflags,
1209 largest_align))
1210 return -ENOSPC;
1211 } while (local_cmpxchg(&buf->offset, offsets.old,
1212 offsets.end) != offsets.old);
1213
1214 /*
1215 * Atomically update last_tsc. This update races against concurrent
1216 * atomic updates, but the race will always cause supplementary full TSC
1217 * events, never the opposite (missing a full TSC event when it would be
1218 * needed).
1219 */
1220 save_last_tsc(buf, *tsc);
1221
1222 /*
1223 * Push the reader if necessary
1224 */
1225 ltt_reserve_push_reader(channel, buf, &offsets);
1226
1227 /*
1228 * Switch old subbuffer if needed.
1229 */
1230 if (offsets.end_switch_old)
1231 ltt_reserve_switch_old_subbuf(channel, buf, &offsets, tsc);
1232
1233 /*
1234 * Populate new subbuffer.
1235 */
1236 if (offsets.begin_switch)
1237 ltt_reserve_switch_new_subbuf(channel, buf, &offsets, tsc);
1238
1239 if (offsets.end_switch_current)
1240 ltt_reserve_end_switch_current(channel, buf, &offsets, tsc);
1241
1242 *slot_size = offsets.size;
1243 *buf_offset = offsets.begin + offsets.before_hdr_pad;
1244 return 0;
1245 }
1246
1247 /*
1248 * Force a sub-buffer switch for a per-cpu buffer. This operation is
1249 * completely reentrant : can be called while tracing is active with
1250 * absolutely no lock held.
1251 *
1252 * Note, however, that as a local_cmpxchg is used for some atomic
1253 * operations, this function must be called from the CPU which owns the buffer
1254 * for a ACTIVE flush.
1255 */
1256 static notrace void ltt_force_switch(struct ust_buffer *buf,
1257 enum force_switch_mode mode)
1258 {
1259 struct ust_channel *channel = buf->chan;
1260 struct ltt_reserve_switch_offsets offsets;
1261 u64 tsc;
1262
1263 offsets.reserve_commit_diff = 0;
1264 offsets.size = 0;
1265
1266 /*
1267 * Perform retryable operations.
1268 */
1269 do {
1270 if (ltt_relay_try_switch(mode, channel, buf, &offsets, &tsc))
1271 return;
1272 } while (local_cmpxchg(&buf->offset, offsets.old,
1273 offsets.end) != offsets.old);
1274
1275 /*
1276 * Atomically update last_tsc. This update races against concurrent
1277 * atomic updates, but the race will always cause supplementary full TSC
1278 * events, never the opposite (missing a full TSC event when it would be
1279 * needed).
1280 */
1281 save_last_tsc(buf, tsc);
1282
1283 /*
1284 * Push the reader if necessary
1285 */
1286 if (mode == FORCE_ACTIVE)
1287 ltt_reserve_push_reader(channel, buf, &offsets);
1288
1289 /*
1290 * Switch old subbuffer if needed.
1291 */
1292 if (offsets.end_switch_old)
1293 ltt_reserve_switch_old_subbuf(channel, buf, &offsets, &tsc);
1294
1295 /*
1296 * Populate new subbuffer.
1297 */
1298 if (mode == FORCE_ACTIVE)
1299 ltt_reserve_switch_new_subbuf(channel, buf, &offsets, &tsc);
1300 }
1301
1302 static void ltt_relay_print_user_errors(struct ltt_trace_struct *trace,
1303 unsigned int chan_index, size_t data_size,
1304 struct user_dbg_data *dbg)
1305 {
1306 struct ust_channel *channel;
1307 struct ust_buffer *buf;
1308
1309 channel = &trace->channels[chan_index];
1310 buf = channel->buf;
1311
1312 printk(KERN_ERR "Error in LTT usertrace : "
1313 "buffer full : event lost in blocking "
1314 "mode. Increase LTT_RESERVE_CRITICAL.\n");
1315 printk(KERN_ERR "LTT nesting level is %u.\n", ltt_nesting);
1316 printk(KERN_ERR "LTT avail size %lu.\n",
1317 dbg->avail_size);
1318 printk(KERN_ERR "avai write : %lu, read : %lu\n",
1319 dbg->write, dbg->read);
1320
1321 dbg->write = local_read(&buf->offset);
1322 dbg->read = atomic_long_read(&buf->consumed);
1323
1324 printk(KERN_ERR "LTT cur size %lu.\n",
1325 dbg->write + LTT_RESERVE_CRITICAL + data_size
1326 - SUBBUF_TRUNC(dbg->read, channel));
1327 printk(KERN_ERR "cur write : %lu, read : %lu\n",
1328 dbg->write, dbg->read);
1329 }
1330
1331 static struct ltt_transport ust_relay_transport = {
1332 .name = "ustrelay",
1333 .ops = {
1334 .create_channel = ust_buffers_create_channel,
1335 .finish_channel = ltt_relay_finish_channel,
1336 .remove_channel = ltt_relay_remove_channel,
1337 .wakeup_channel = ltt_relay_async_wakeup_chan,
1338 // .commit_slot = ltt_relay_commit_slot,
1339 .reserve_slot = ltt_relay_reserve_slot,
1340 .user_errors = ltt_relay_print_user_errors,
1341 },
1342 };
1343
1344 /*
1345 * for flight recording. must be called after relay_commit.
1346 * This function decrements de subbuffer's lost_size each time the commit count
1347 * reaches back the reserve offset (module subbuffer size). It is useful for
1348 * crash dump.
1349 */
1350 static /* inline */ void ltt_write_commit_counter(struct ust_buffer *buf,
1351 struct ust_buffer *ltt_buf,
1352 long idx, long buf_offset, long commit_count, size_t data_size)
1353 {
1354 long offset;
1355 long commit_seq_old;
1356
1357 offset = buf_offset + data_size;
1358
1359 /*
1360 * SUBBUF_OFFSET includes commit_count_mask. We can simply
1361 * compare the offsets within the subbuffer without caring about
1362 * buffer full/empty mismatch because offset is never zero here
1363 * (subbuffer header and event headers have non-zero length).
1364 */
1365 if (unlikely(SUBBUF_OFFSET(offset - commit_count, buf->chan)))
1366 return;
1367
1368 commit_seq_old = local_read(&ltt_buf->commit_seq[idx]);
1369 while (commit_seq_old < commit_count)
1370 commit_seq_old = local_cmpxchg(&ltt_buf->commit_seq[idx],
1371 commit_seq_old, commit_count);
1372 }
1373
1374 /*
1375 * Atomic unordered slot commit. Increments the commit count in the
1376 * specified sub-buffer, and delivers it if necessary.
1377 *
1378 * Parameters:
1379 *
1380 * @ltt_channel : channel structure
1381 * @transport_data: transport-specific data
1382 * @buf_offset : offset following the event header.
1383 * @data_size : size of the event data.
1384 * @slot_size : size of the reserved slot.
1385 */
1386 /* FIXME: make this function static inline in the .h! */
1387 /*static*/ /* inline */ notrace void ltt_commit_slot(
1388 struct ust_channel *channel,
1389 void **transport_data, long buf_offset,
1390 size_t data_size, size_t slot_size)
1391 {
1392 struct ust_buffer *buf = *transport_data;
1393 long offset_end = buf_offset;
1394 long endidx = SUBBUF_INDEX(offset_end - 1, channel);
1395 long commit_count;
1396
1397 /* Must write slot data before incrementing commit count */
1398 smp_wmb();
1399 commit_count = local_add_return(slot_size,
1400 &buf->commit_count[endidx]);
1401 /* Check if all commits have been done */
1402 if ((BUFFER_TRUNC(offset_end - 1, channel)
1403 >> channel->n_subbufs_order)
1404 - ((commit_count - channel->subbuf_size)
1405 & channel->commit_count_mask) == 0)
1406 ltt_deliver(buf, endidx, commit_count);
1407 /*
1408 * Update lost_size for each commit. It's needed only for extracting
1409 * ltt buffers from vmcore, after crash.
1410 */
1411 ltt_write_commit_counter(buf, buf, endidx,
1412 buf_offset, commit_count, data_size);
1413 }
1414
1415
1416 static char initialized = 0;
1417
1418 void __attribute__((constructor)) init_ustrelay_transport(void)
1419 {
1420 if(!initialized) {
1421 ltt_transport_register(&ust_relay_transport);
1422 initialized = 1;
1423 }
1424 }
1425
1426 static void __attribute__((destructor)) ltt_relay_exit(void)
1427 {
1428 ltt_transport_unregister(&ust_relay_transport);
1429 }
This page took 0.064409 seconds and 4 git commands to generate.