2 * Public API and common code for kernel->userspace relay file support.
4 * Copyright (C) 2002-2005 - Tom Zanussi (zanussi@us.ibm.com), IBM Corp
5 * Copyright (C) 1999-2005 - Karim Yaghmour (karim@opersys.com)
6 * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
8 * Moved to kernel/relay.c by Paul Mundt, 2006.
9 * November 2006 - CPU hotplug support by Mathieu Desnoyers
10 * (mathieu.desnoyers@polymtl.ca)
12 * This file is released under the GPL.
14 //ust// #include <linux/errno.h>
15 //ust// #include <linux/stddef.h>
16 //ust// #include <linux/slab.h>
17 //ust// #include <linux/module.h>
18 //ust// #include <linux/string.h>
19 //ust// #include <linux/ltt-relay.h>
20 //ust// #include <linux/vmalloc.h>
21 //ust// #include <linux/mm.h>
22 //ust// #include <linux/cpu.h>
23 //ust// #include <linux/splice.h>
24 //ust// #include <linux/bitops.h>
25 #include "kernelcompat.h"
33 #include <kcompat/kref.h>
35 #include "tracercore.h"
38 /* list of open channels, for cpu hotplug */
39 static DEFINE_MUTEX(relay_channels_mutex
);
40 static LIST_HEAD(relay_channels
);
43 static struct dentry
*ltt_create_buf_file_callback(struct rchan_buf
*buf
);
46 * relay_alloc_buf - allocate a channel buffer
47 * @buf: the buffer struct
48 * @size: total size of the buffer
50 //ust// static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
52 //ust// unsigned int i, n_pages;
53 //ust// struct buf_page *buf_page, *n;
55 //ust// *size = PAGE_ALIGN(*size);
56 //ust// n_pages = *size >> PAGE_SHIFT;
58 //ust// INIT_LIST_HEAD(&buf->pages);
60 //ust// for (i = 0; i < n_pages; i++) {
61 //ust// buf_page = kmalloc_node(sizeof(*buf_page), GFP_KERNEL,
62 //ust// cpu_to_node(buf->cpu));
63 //ust// if (unlikely(!buf_page))
64 //ust// goto depopulate;
65 //ust// buf_page->page = alloc_pages_node(cpu_to_node(buf->cpu),
66 //ust// GFP_KERNEL | __GFP_ZERO, 0);
67 //ust// if (unlikely(!buf_page->page)) {
68 //ust// kfree(buf_page);
69 //ust// goto depopulate;
71 //ust// list_add_tail(&buf_page->list, &buf->pages);
72 //ust// buf_page->offset = (size_t)i << PAGE_SHIFT;
73 //ust// buf_page->buf = buf;
74 //ust// set_page_private(buf_page->page, (unsigned long)buf_page);
76 //ust// buf->wpage = buf_page;
77 //ust// buf->hpage[0] = buf_page;
78 //ust// buf->hpage[1] = buf_page;
79 //ust// buf->rpage = buf_page;
82 //ust// buf->page_count = n_pages;
86 //ust// list_for_each_entry_safe(buf_page, n, &buf->pages, list) {
87 //ust// list_del_init(&buf_page->list);
88 //ust// __free_page(buf_page->page);
89 //ust// kfree(buf_page);
91 //ust// return -ENOMEM;
94 static int relay_alloc_buf(struct rchan_buf
*buf
, size_t *size
)
96 //ust// unsigned int n_pages;
97 //ust// struct buf_page *buf_page, *n;
102 *size
= PAGE_ALIGN(*size
);
104 result
= buf
->shmid
= shmget(getpid(), *size
, IPC_CREAT
| IPC_EXCL
| 0700);
105 if(result
== -1 && errno
== EINVAL
) {
106 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
109 else if(result
== -1) {
114 ptr
= shmat(buf
->shmid
, NULL
, 0);
115 if(ptr
== (void *) -1) {
120 /* Already mark the shared memory for destruction. This will occur only
121 * when all users have detached.
123 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
130 buf
->buf_size
= *size
;
135 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
144 * relay_create_buf - allocate and initialize a channel buffer
145 * @chan: the relay channel
146 * @cpu: cpu the buffer belongs to
148 * Returns channel buffer if successful, %NULL otherwise.
150 static struct rchan_buf
*relay_create_buf(struct rchan
*chan
)
153 struct rchan_buf
*buf
= kzalloc(sizeof(struct rchan_buf
), GFP_KERNEL
);
158 ret
= relay_alloc_buf(buf
, &chan
->alloc_size
);
163 kref_get(&buf
->chan
->kref
);
172 * relay_destroy_channel - free the channel struct
173 * @kref: target kernel reference that contains the relay channel
175 * Should only be called from kref_put().
177 static void relay_destroy_channel(struct kref
*kref
)
179 struct rchan
*chan
= container_of(kref
, struct rchan
, kref
);
184 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
185 * @buf: the buffer struct
187 static void relay_destroy_buf(struct rchan_buf
*buf
)
189 struct rchan
*chan
= buf
->chan
;
190 //ust// struct buf_page *buf_page;
193 result
= munmap(buf
->buf_data
, buf
->buf_size
);
198 //ust// chan->buf[buf->cpu] = NULL;
200 kref_put(&chan
->kref
, relay_destroy_channel
);
204 * relay_remove_buf - remove a channel buffer
205 * @kref: target kernel reference that contains the relay buffer
207 * Removes the file from the fileystem, which also frees the
208 * rchan_buf_struct and the channel buffer. Should only be called from
211 static void relay_remove_buf(struct kref
*kref
)
213 struct rchan_buf
*buf
= container_of(kref
, struct rchan_buf
, kref
);
214 //ust// buf->chan->cb->remove_buf_file(buf);
215 relay_destroy_buf(buf
);
219 * High-level relay kernel API and associated functions.
223 * rchan_callback implementations defining default channel behavior. Used
224 * in place of corresponding NULL values in client callback struct.
228 * create_buf_file_create() default callback. Does nothing.
230 //ust// static struct dentry *create_buf_file_default_callback(const char *filename,
231 //ust// struct dentry *parent,
233 //ust// struct rchan_buf *buf)
239 //ust// * remove_buf_file() default callback. Does nothing.
241 //ust// static int remove_buf_file_default_callback(struct dentry *dentry)
243 //ust// return -EINVAL;
247 * wakeup_readers - wake up readers waiting on a channel
248 * @data: contains the channel buffer
250 * This is the timer function used to defer reader waking.
252 //ust// static void wakeup_readers(unsigned long data)
254 //ust// struct rchan_buf *buf = (struct rchan_buf *)data;
255 //ust// wake_up_interruptible(&buf->read_wait);
259 * __relay_reset - reset a channel buffer
260 * @buf: the channel buffer
261 * @init: 1 if this is a first-time initialization
263 * See relay_reset() for description of effect.
265 static void __relay_reset(struct rchan_buf
*buf
, unsigned int init
)
268 //ust// init_waitqueue_head(&buf->read_wait);
269 kref_init(&buf
->kref
);
270 //ust// setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
272 //ust// del_timer_sync(&buf->timer);
278 * relay_open_buf - create a new relay channel buffer
280 * used by relay_open() and CPU hotplug.
282 static struct rchan_buf
*relay_open_buf(struct rchan
*chan
)
284 struct rchan_buf
*buf
= NULL
;
285 //ust// struct dentry *dentry;
286 //ust// char *tmpname;
288 //ust// tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
289 //ust// if (!tmpname)
291 //ust// snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
293 buf
= relay_create_buf(chan
);
297 __relay_reset(buf
, 1);
299 /* Create file in fs */
300 //ust// dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
303 ltt_create_buf_file_callback(buf
); // ust //
306 //ust// goto free_buf;
308 //ust// buf->dentry = dentry;
313 relay_destroy_buf(buf
);
316 //ust// kfree(tmpname);
322 * relay_close_buf - close a channel buffer
323 * @buf: channel buffer
325 * Marks the buffer finalized and restores the default callbacks.
326 * The channel buffer and channel buffer data structure are then freed
327 * automatically when the last reference is given up.
329 static void relay_close_buf(struct rchan_buf
*buf
)
331 //ust// del_timer_sync(&buf->timer);
332 kref_put(&buf
->kref
, relay_remove_buf
);
335 //ust// static void setup_callbacks(struct rchan *chan,
336 //ust// struct rchan_callbacks *cb)
339 //ust// chan->cb = &default_channel_callbacks;
343 //ust// if (!cb->create_buf_file)
344 //ust// cb->create_buf_file = create_buf_file_default_callback;
345 //ust// if (!cb->remove_buf_file)
346 //ust// cb->remove_buf_file = remove_buf_file_default_callback;
347 //ust// chan->cb = cb;
351 * relay_hotcpu_callback - CPU hotplug callback
352 * @nb: notifier block
353 * @action: hotplug action to take
356 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
358 //ust// static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
359 //ust// unsigned long action,
362 //ust// unsigned int hotcpu = (unsigned long)hcpu;
363 //ust// struct rchan *chan;
365 //ust// switch (action) {
366 //ust// case CPU_UP_PREPARE:
367 //ust// case CPU_UP_PREPARE_FROZEN:
368 //ust// mutex_lock(&relay_channels_mutex);
369 //ust// list_for_each_entry(chan, &relay_channels, list) {
370 //ust// if (chan->buf[hotcpu])
372 //ust// chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
373 //ust// if (!chan->buf[hotcpu]) {
374 //ust// printk(KERN_ERR
375 //ust// "relay_hotcpu_callback: cpu %d buffer "
376 //ust// "creation failed\n", hotcpu);
377 //ust// mutex_unlock(&relay_channels_mutex);
378 //ust// return NOTIFY_BAD;
381 //ust// mutex_unlock(&relay_channels_mutex);
383 //ust// case CPU_DEAD:
384 //ust// case CPU_DEAD_FROZEN:
385 //ust// /* No need to flush the cpu : will be flushed upon
386 //ust// * final relay_flush() call. */
389 //ust// return NOTIFY_OK;
393 * ltt_relay_open - create a new relay channel
394 * @base_filename: base name of files to create
395 * @parent: dentry of parent directory, %NULL for root directory
396 * @subbuf_size: size of sub-buffers
397 * @n_subbufs: number of sub-buffers
398 * @cb: client callback functions
399 * @private_data: user-defined data
401 * Returns channel pointer if successful, %NULL otherwise.
403 * Creates a channel buffer for each cpu using the sizes and
404 * attributes specified. The created channel buffer files
405 * will be named base_filename0...base_filenameN-1. File
406 * permissions will be %S_IRUSR.
408 struct rchan
*ltt_relay_open(const char *base_filename
,
409 struct dentry
*parent
,
414 //ust// unsigned int i;
416 //ust// if (!base_filename)
419 if (!(subbuf_size
&& n_subbufs
))
422 chan
= kzalloc(sizeof(struct rchan
), GFP_KERNEL
);
426 chan
->version
= LTT_RELAY_CHANNEL_VERSION
;
427 chan
->n_subbufs
= n_subbufs
;
428 chan
->subbuf_size
= subbuf_size
;
429 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
430 chan
->alloc_size
= FIX_SIZE(subbuf_size
* n_subbufs
);
431 chan
->parent
= parent
;
432 chan
->private_data
= private_data
;
433 //ust// strlcpy(chan->base_filename, base_filename, NAME_MAX);
434 //ust// setup_callbacks(chan, cb);
435 kref_init(&chan
->kref
);
437 mutex_lock(&relay_channels_mutex
);
438 //ust// for_each_online_cpu(i) {
439 chan
->buf
= relay_open_buf(chan
);
443 list_add(&chan
->list
, &relay_channels
);
444 mutex_unlock(&relay_channels_mutex
);
449 //ust// for_each_possible_cpu(i) {
450 //ust// if (!chan->buf[i])
452 //ust// relay_close_buf(chan->buf[i]);
456 kref_put(&chan
->kref
, relay_destroy_channel
);
457 mutex_unlock(&relay_channels_mutex
);
460 //ust// EXPORT_SYMBOL_GPL(ltt_relay_open);
463 * ltt_relay_close - close the channel
466 * Closes all channel buffers and frees the channel.
468 void ltt_relay_close(struct rchan
*chan
)
470 //ust// unsigned int i;
475 mutex_lock(&relay_channels_mutex
);
476 //ust// for_each_possible_cpu(i)
478 relay_close_buf(chan
->buf
);
480 list_del(&chan
->list
);
481 kref_put(&chan
->kref
, relay_destroy_channel
);
482 mutex_unlock(&relay_channels_mutex
);
484 //ust// EXPORT_SYMBOL_GPL(ltt_relay_close);
487 * Start iteration at the previous element. Skip the real list head.
489 //ust// struct buf_page *ltt_relay_find_prev_page(struct rchan_buf *buf,
490 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
492 //ust// struct buf_page *iter;
493 //ust// size_t orig_iter_off;
494 //ust// unsigned int i = 0;
496 //ust// orig_iter_off = page->offset;
497 //ust// list_for_each_entry_reverse(iter, &page->list, list) {
499 //ust// * Skip the real list head.
501 //ust// if (&iter->list == &buf->pages)
504 //ust// if (offset >= iter->offset
505 //ust// && offset < iter->offset + PAGE_SIZE) {
506 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
508 //ust// printk(KERN_WARNING
509 //ust// "Backward random access detected in "
510 //ust// "ltt_relay. Iterations %u, "
511 //ust// "offset %zu, orig iter->off %zu, "
512 //ust// "iter->off %zu diff_offset %zd.\n", i,
513 //ust// offset, orig_iter_off, iter->offset,
514 //ust// diff_offset);
524 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_prev_page);
527 * Start iteration at the next element. Skip the real list head.
529 //ust// struct buf_page *ltt_relay_find_next_page(struct rchan_buf *buf,
530 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
532 //ust// struct buf_page *iter;
533 //ust// unsigned int i = 0;
534 //ust// size_t orig_iter_off;
536 //ust// orig_iter_off = page->offset;
537 //ust// list_for_each_entry(iter, &page->list, list) {
539 //ust// * Skip the real list head.
541 //ust// if (&iter->list == &buf->pages)
544 //ust// if (offset >= iter->offset
545 //ust// && offset < iter->offset + PAGE_SIZE) {
546 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
548 //ust// printk(KERN_WARNING
549 //ust// "Forward random access detected in "
550 //ust// "ltt_relay. Iterations %u, "
551 //ust// "offset %zu, orig iter->off %zu, "
552 //ust// "iter->off %zu diff_offset %zd.\n", i,
553 //ust// offset, orig_iter_off, iter->offset,
554 //ust// diff_offset);
564 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_next_page);
567 * ltt_relay_write - write data to a ltt_relay buffer.
569 * @offset : offset within the buffer
570 * @src : source address
571 * @len : length to write
572 * @page : cached buffer page
573 * @pagecpy : page size copied so far
575 void _ltt_relay_write(struct rchan_buf
*buf
, size_t offset
,
576 const void *src
, size_t len
, ssize_t cpy
)
583 * Underlying layer should never ask for writes across
586 WARN_ON(offset
>= buf
->buf_size
);
588 cpy
= min_t(size_t, len
, buf
->buf_size
- offset
);
589 ltt_relay_do_copy(buf
->buf_data
+ offset
, src
, cpy
);
590 } while (unlikely(len
!= cpy
));
592 //ust// EXPORT_SYMBOL_GPL(_ltt_relay_write);
595 * ltt_relay_read - read data from ltt_relay_buffer.
597 * @offset : offset within the buffer
598 * @dest : destination address
599 * @len : length to write
601 //ust// int ltt_relay_read(struct rchan_buf *buf, size_t offset,
602 //ust// void *dest, size_t len)
604 //ust// struct buf_page *page;
605 //ust// ssize_t pagecpy, orig_len;
607 //ust// orig_len = len;
608 //ust// offset &= buf->chan->alloc_size - 1;
609 //ust// page = buf->rpage;
610 //ust// if (unlikely(!len))
613 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
614 //ust// pagecpy = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
615 //ust// memcpy(dest, page_address(page->page) + (offset & ~PAGE_MASK),
617 //ust// len -= pagecpy;
618 //ust// if (likely(!len))
620 //ust// dest += pagecpy;
621 //ust// offset += pagecpy;
623 //ust// * Underlying layer should never ask for reads across
624 //ust// * subbuffers.
626 //ust// WARN_ON(offset >= buf->chan->alloc_size);
628 //ust// return orig_len;
630 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read);
633 * ltt_relay_read_get_page - Get a whole page to read from
635 * @offset : offset within the buffer
637 //ust// struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf, size_t offset)
639 //ust// struct buf_page *page;
641 //ust// offset &= buf->chan->alloc_size - 1;
642 //ust// page = buf->rpage;
643 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
646 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read_get_page);
649 * ltt_relay_offset_address - get address of a location within the buffer
651 * @offset : offset within the buffer.
653 * Return the address where a given offset is located.
654 * Should be used to get the current subbuffer header pointer. Given we know
655 * it's never on a page boundary, it's safe to write directly to this address,
656 * as long as the write is never bigger than a page size.
658 void *ltt_relay_offset_address(struct rchan_buf
*buf
, size_t offset
)
660 //ust// struct buf_page *page;
661 //ust// unsigned int odd;
663 //ust// offset &= buf->chan->alloc_size - 1;
664 //ust// odd = !!(offset & buf->chan->subbuf_size);
665 //ust// page = buf->hpage[odd];
666 //ust// if (offset < page->offset || offset >= page->offset + PAGE_SIZE)
667 //ust// buf->hpage[odd] = page = buf->wpage;
668 //ust// page = ltt_relay_cache_page(buf, &buf->hpage[odd], page, offset);
669 //ust// return page_address(page->page) + (offset & ~PAGE_MASK);
670 return ((char *)buf
->buf_data
)+offset
;
673 //ust// EXPORT_SYMBOL_GPL(ltt_relay_offset_address);
676 * relay_file_open - open file op for relay files
680 * Increments the channel buffer refcount.
682 //ust// static int relay_file_open(struct inode *inode, struct file *filp)
684 //ust// struct rchan_buf *buf = inode->i_private;
685 //ust// kref_get(&buf->kref);
686 //ust// filp->private_data = buf;
688 //ust// return nonseekable_open(inode, filp);
692 * relay_file_release - release file op for relay files
696 * Decrements the channel refcount, as the filesystem is
697 * no longer using it.
699 //ust// static int relay_file_release(struct inode *inode, struct file *filp)
701 //ust// struct rchan_buf *buf = filp->private_data;
702 //ust// kref_put(&buf->kref, relay_remove_buf);
707 //ust// const struct file_operations ltt_relay_file_operations = {
708 //ust// .open = relay_file_open,
709 //ust// .release = relay_file_release,
711 //ust// EXPORT_SYMBOL_GPL(ltt_relay_file_operations);
713 //ust// static __init int relay_init(void)
715 //ust// hotcpu_notifier(relay_hotcpu_callback, 5);
719 //ust// module_init(relay_init);
723 * (C) Copyright 2005-2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
725 * LTTng lockless buffer space management (reader/writer).
728 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
730 * Inspired from LTT :
731 * Karim Yaghmour (karim@opersys.com)
732 * Tom Zanussi (zanussi@us.ibm.com)
733 * Bob Wisniewski (bob@watson.ibm.com)
735 * Bob Wisniewski (bob@watson.ibm.com)
739 * 19/10/05, Complete lockless mechanism.
740 * 27/05/05, Modular redesign and rewrite.
742 * Userspace reader semantic :
743 * while (poll fd != POLLHUP) {
744 * - ioctl RELAY_GET_SUBBUF_SIZE
747 * - splice 1 subbuffer worth of data to a pipe
748 * - splice the data from pipe to disk/network
749 * - ioctl PUT_SUBBUF, check error value
750 * if err val < 0, previous subbuffer was corrupted.
755 //ust// #include <linux/time.h>
756 //ust// #include <linux/ltt-tracer.h>
757 //ust// #include <linux/ltt-relay.h>
758 //ust// #include <linux/module.h>
759 //ust// #include <linux/string.h>
760 //ust// #include <linux/slab.h>
761 //ust// #include <linux/init.h>
762 //ust// #include <linux/rcupdate.h>
763 //ust// #include <linux/sched.h>
764 //ust// #include <linux/bitops.h>
765 //ust// #include <linux/fs.h>
766 //ust// #include <linux/smp_lock.h>
767 //ust// #include <linux/debugfs.h>
768 //ust// #include <linux/stat.h>
769 //ust// #include <linux/cpu.h>
770 //ust// #include <linux/pipe_fs_i.h>
771 //ust// #include <linux/splice.h>
772 //ust// #include <asm/atomic.h>
773 //ust// #include <asm/local.h>
776 #define printk_dbg(fmt, args...) printk(fmt, args)
778 #define printk_dbg(fmt, args...)
782 * Last TSC comparison functions. Check if the current TSC overflows
783 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
787 #if (BITS_PER_LONG == 32)
788 static inline void save_last_tsc(struct ltt_channel_buf_struct
*ltt_buf
,
791 ltt_buf
->last_tsc
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
794 static inline int last_tsc_overflow(struct ltt_channel_buf_struct
*ltt_buf
,
797 unsigned long tsc_shifted
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
799 if (unlikely((tsc_shifted
- ltt_buf
->last_tsc
)))
805 static inline void save_last_tsc(struct ltt_channel_buf_struct
*ltt_buf
,
808 ltt_buf
->last_tsc
= (unsigned long)tsc
;
811 static inline int last_tsc_overflow(struct ltt_channel_buf_struct
*ltt_buf
,
814 if (unlikely((tsc
- ltt_buf
->last_tsc
) >> LTT_TSC_BITS
))
821 //ust// static struct file_operations ltt_file_operations;
824 * A switch is done during tracing or as a final flush after tracing (so it
825 * won't write in the new sub-buffer).
827 enum force_switch_mode
{ FORCE_ACTIVE
, FORCE_FLUSH
};
829 static int ltt_relay_create_buffer(struct ltt_trace_struct
*trace
,
830 struct ltt_channel_struct
*ltt_chan
,
831 struct rchan_buf
*buf
,
832 unsigned int n_subbufs
);
834 static void ltt_relay_destroy_buffer(struct ltt_channel_struct
*ltt_chan
);
836 static void ltt_force_switch(struct rchan_buf
*buf
,
837 enum force_switch_mode mode
);
842 static void ltt_buffer_begin_callback(struct rchan_buf
*buf
,
843 u64 tsc
, unsigned int subbuf_idx
)
845 struct ltt_channel_struct
*channel
=
846 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
847 struct ltt_subbuffer_header
*header
=
848 (struct ltt_subbuffer_header
*)
849 ltt_relay_offset_address(buf
,
850 subbuf_idx
* buf
->chan
->subbuf_size
);
852 header
->cycle_count_begin
= tsc
;
853 header
->lost_size
= 0xFFFFFFFF; /* for debugging */
854 header
->buf_size
= buf
->chan
->subbuf_size
;
855 ltt_write_trace_header(channel
->trace
, header
);
859 * offset is assumed to never be 0 here : never deliver a completely empty
860 * subbuffer. The lost size is between 0 and subbuf_size-1.
862 static notrace
void ltt_buffer_end_callback(struct rchan_buf
*buf
,
863 u64 tsc
, unsigned int offset
, unsigned int subbuf_idx
)
865 struct ltt_channel_struct
*channel
=
866 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
867 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
868 struct ltt_subbuffer_header
*header
=
869 (struct ltt_subbuffer_header
*)
870 ltt_relay_offset_address(buf
,
871 subbuf_idx
* buf
->chan
->subbuf_size
);
873 header
->lost_size
= SUBBUF_OFFSET((buf
->chan
->subbuf_size
- offset
),
875 header
->cycle_count_end
= tsc
;
876 header
->events_lost
= local_read(<t_buf
->events_lost
);
877 header
->subbuf_corrupt
= local_read(<t_buf
->corrupted_subbuffers
);
881 void (*wake_consumer
)(void *, int) = NULL
;
883 void relay_set_wake_consumer(void (*wake
)(void *, int))
885 wake_consumer
= wake
;
888 void relay_wake_consumer(void *arg
, int finished
)
891 wake_consumer(arg
, finished
);
894 static notrace
void ltt_deliver(struct rchan_buf
*buf
, unsigned int subbuf_idx
,
897 struct ltt_channel_struct
*channel
=
898 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
899 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
902 //ust// #ifdef CONFIG_LTT_VMCORE
903 local_set(<t_buf
->commit_seq
[subbuf_idx
], commit_count
);
906 /* wakeup consumer */
907 result
= write(ltt_buf
->data_ready_fd_write
, "1", 1);
909 PERROR("write (in ltt_relay_buffer_flush)");
910 ERR("this should never happen!");
912 //ust// atomic_set(<t_buf->wakeup_readers, 1);
915 static struct dentry
*ltt_create_buf_file_callback(struct rchan_buf
*buf
)
917 struct ltt_channel_struct
*ltt_chan
;
919 //ust// struct dentry *dentry;
921 ltt_chan
= buf
->chan
->private_data
;
922 err
= ltt_relay_create_buffer(ltt_chan
->trace
, ltt_chan
, buf
, buf
->chan
->n_subbufs
);
926 //ust// dentry = debugfs_create_file(filename, mode, parent, buf,
927 //ust// <t_file_operations);
930 //ust// return dentry;
933 ltt_relay_destroy_buffer(ltt_chan
);
937 //ust// static int ltt_remove_buf_file_callback(struct rchan_buf *buf)
939 //ust// //ust// struct rchan_buf *buf = dentry->d_inode->i_private;
940 //ust// struct ltt_channel_struct *ltt_chan = buf->chan->private_data;
942 //ust// //ust// debugfs_remove(dentry);
943 //ust// ltt_relay_destroy_buffer(ltt_chan);
951 * This must be done after the trace is removed from the RCU list so that there
952 * are no stalled writers.
954 //ust// static void ltt_relay_wake_writers(struct ltt_channel_buf_struct *ltt_buf)
957 //ust// if (waitqueue_active(<t_buf->write_wait))
958 //ust// wake_up_interruptible(<t_buf->write_wait);
962 * This function should not be called from NMI interrupt context
964 static notrace
void ltt_buf_unfull(struct rchan_buf
*buf
,
965 unsigned int subbuf_idx
,
968 //ust// struct ltt_channel_struct *ltt_channel =
969 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
970 //ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
972 //ust// ltt_relay_wake_writers(ltt_buf);
976 * ltt_open - open file op for ltt files
977 * @inode: opened inode
980 * Open implementation. Makes sure only one open instance of a buffer is
981 * done at a given moment.
983 //ust// static int ltt_open(struct inode *inode, struct file *file)
985 //ust// struct rchan_buf *buf = inode->i_private;
986 //ust// struct ltt_channel_struct *ltt_channel =
987 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
988 //ust// struct ltt_channel_buf_struct *ltt_buf =
989 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
991 //ust// if (!atomic_long_add_unless(<t_buf->active_readers, 1, 1))
992 //ust// return -EBUSY;
993 //ust// return ltt_relay_file_operations.open(inode, file);
997 * ltt_release - release file op for ltt files
998 * @inode: opened inode
1001 * Release implementation.
1003 //ust// static int ltt_release(struct inode *inode, struct file *file)
1005 //ust// struct rchan_buf *buf = inode->i_private;
1006 //ust// struct ltt_channel_struct *ltt_channel =
1007 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1008 //ust// struct ltt_channel_buf_struct *ltt_buf =
1009 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1012 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1013 //ust// atomic_long_dec(<t_buf->active_readers);
1014 //ust// ret = ltt_relay_file_operations.release(inode, file);
1015 //ust// WARN_ON(ret);
1020 * ltt_poll - file op for ltt files
1024 * Poll implementation.
1026 //ust// static unsigned int ltt_poll(struct file *filp, poll_table *wait)
1028 //ust// unsigned int mask = 0;
1029 //ust// struct inode *inode = filp->f_dentry->d_inode;
1030 //ust// struct rchan_buf *buf = inode->i_private;
1031 //ust// struct ltt_channel_struct *ltt_channel =
1032 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1033 //ust// struct ltt_channel_buf_struct *ltt_buf =
1034 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1036 //ust// if (filp->f_mode & FMODE_READ) {
1037 //ust// poll_wait_set_exclusive(wait);
1038 //ust// poll_wait(filp, &buf->read_wait, wait);
1040 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1041 //ust// if (SUBBUF_TRUNC(local_read(<t_buf->offset),
1043 //ust// - SUBBUF_TRUNC(atomic_long_read(<t_buf->consumed),
1046 //ust// if (buf->finalized)
1047 //ust// return POLLHUP;
1051 //ust// struct rchan *rchan =
1052 //ust// ltt_channel->trans_channel_data;
1053 //ust// if (SUBBUF_TRUNC(local_read(<t_buf->offset),
1055 //ust// - SUBBUF_TRUNC(atomic_long_read(
1056 //ust// <t_buf->consumed),
1058 //ust// >= rchan->alloc_size)
1059 //ust// return POLLPRI | POLLRDBAND;
1061 //ust// return POLLIN | POLLRDNORM;
1064 //ust// return mask;
1067 int ltt_do_get_subbuf(struct rchan_buf
*buf
, struct ltt_channel_buf_struct
*ltt_buf
, long *pconsumed_old
)
1069 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)buf
->chan
->private_data
;
1070 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
1071 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1072 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
1073 commit_count
= local_read(<t_buf
->commit_count
[consumed_idx
]);
1075 * Make sure we read the commit count before reading the buffer
1076 * data and the write offset. Correct consumed offset ordering
1077 * wrt commit count is insured by the use of cmpxchg to update
1078 * the consumed offset.
1081 write_offset
= local_read(<t_buf
->offset
);
1083 * Check that the subbuffer we are trying to consume has been
1084 * already fully committed.
1086 if (((commit_count
- buf
->chan
->subbuf_size
)
1087 & ltt_channel
->commit_count_mask
)
1088 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
1089 >> ltt_channel
->n_subbufs_order
)
1094 * Check that we are not about to read the same subbuffer in
1095 * which the writer head is.
1097 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
1098 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1103 *pconsumed_old
= consumed_old
;
1107 int ltt_do_put_subbuf(struct rchan_buf
*buf
, struct ltt_channel_buf_struct
*ltt_buf
, u32 uconsumed_old
)
1109 long consumed_new
, consumed_old
;
1111 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1112 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
1113 consumed_old
= consumed_old
| uconsumed_old
;
1114 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1116 //ust// spin_lock(<t_buf->full_lock);
1117 if (atomic_long_cmpxchg(<t_buf
->consumed
, consumed_old
,
1120 /* We have been pushed by the writer : the last
1121 * buffer read _is_ corrupted! It can also
1122 * happen if this is a buffer we never got. */
1123 //ust// spin_unlock(<t_buf->full_lock);
1126 /* tell the client that buffer is now unfull */
1129 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
1130 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
1131 ltt_buf_unfull(buf
, index
, data
);
1132 //ust// spin_unlock(<t_buf->full_lock);
1138 * ltt_ioctl - control on the debugfs file
1145 * This ioctl implements three commands necessary for a minimal
1146 * producer/consumer implementation :
1148 * Get the next sub buffer that can be read. It never blocks.
1150 * Release the currently read sub-buffer. Parameter is the last
1151 * put subbuffer (returned by GET_SUBBUF).
1152 * RELAY_GET_N_BUBBUFS
1153 * returns the number of sub buffers in the per cpu channel.
1154 * RELAY_GET_SUBBUF_SIZE
1155 * returns the size of the sub buffers.
1157 //ust// static int ltt_ioctl(struct inode *inode, struct file *filp,
1158 //ust// unsigned int cmd, unsigned long arg)
1160 //ust// struct rchan_buf *buf = inode->i_private;
1161 //ust// struct ltt_channel_struct *ltt_channel =
1162 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1163 //ust// struct ltt_channel_buf_struct *ltt_buf =
1164 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1165 //ust// u32 __user *argp = (u32 __user *)arg;
1167 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1168 //ust// switch (cmd) {
1169 //ust// case RELAY_GET_SUBBUF:
1172 //ust// ret = ltt_do_get_subbuf(buf, ltt_buf, &consumed_old);
1175 //ust// return put_user((u32)consumed_old, argp);
1177 //ust// case RELAY_PUT_SUBBUF:
1180 //ust// u32 uconsumed_old;
1181 //ust// ret = get_user(uconsumed_old, argp);
1183 //ust// return ret; /* will return -EFAULT */
1184 //ust// return ltt_do_put_subbuf(buf, ltt_buf, uconsumed_old);
1186 //ust// case RELAY_GET_N_SUBBUFS:
1187 //ust// return put_user((u32)buf->chan->n_subbufs, argp);
1189 //ust// case RELAY_GET_SUBBUF_SIZE:
1190 //ust// return put_user((u32)buf->chan->subbuf_size, argp);
1193 //ust// return -ENOIOCTLCMD;
1198 //ust// #ifdef CONFIG_COMPAT
1199 //ust// static long ltt_compat_ioctl(struct file *file, unsigned int cmd,
1200 //ust// unsigned long arg)
1202 //ust// long ret = -ENOIOCTLCMD;
1204 //ust// lock_kernel();
1205 //ust// ret = ltt_ioctl(file->f_dentry->d_inode, file, cmd, arg);
1206 //ust// unlock_kernel();
1212 //ust// static void ltt_relay_pipe_buf_release(struct pipe_inode_info *pipe,
1213 //ust// struct pipe_buffer *pbuf)
1217 //ust// static struct pipe_buf_operations ltt_relay_pipe_buf_ops = {
1218 //ust// .can_merge = 0,
1219 //ust// .map = generic_pipe_buf_map,
1220 //ust// .unmap = generic_pipe_buf_unmap,
1221 //ust// .confirm = generic_pipe_buf_confirm,
1222 //ust// .release = ltt_relay_pipe_buf_release,
1223 //ust// .steal = generic_pipe_buf_steal,
1224 //ust// .get = generic_pipe_buf_get,
1227 //ust// static void ltt_relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1232 * subbuf_splice_actor - splice up to one subbuf's worth of data
1234 //ust// static int subbuf_splice_actor(struct file *in,
1235 //ust// loff_t *ppos,
1236 //ust// struct pipe_inode_info *pipe,
1238 //ust// unsigned int flags)
1240 //ust// struct rchan_buf *buf = in->private_data;
1241 //ust// struct ltt_channel_struct *ltt_channel =
1242 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1243 //ust// struct ltt_channel_buf_struct *ltt_buf =
1244 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1245 //ust// unsigned int poff, subbuf_pages, nr_pages;
1246 //ust// struct page *pages[PIPE_BUFFERS];
1247 //ust// struct partial_page partial[PIPE_BUFFERS];
1248 //ust// struct splice_pipe_desc spd = {
1249 //ust// .pages = pages,
1250 //ust// .nr_pages = 0,
1251 //ust// .partial = partial,
1252 //ust// .flags = flags,
1253 //ust// .ops = <t_relay_pipe_buf_ops,
1254 //ust// .spd_release = ltt_relay_page_release,
1256 //ust// long consumed_old, consumed_idx, roffset;
1257 //ust// unsigned long bytes_avail;
1260 //ust// * Check that a GET_SUBBUF ioctl has been done before.
1262 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1263 //ust// consumed_old = atomic_long_read(<t_buf->consumed);
1264 //ust// consumed_old += *ppos;
1265 //ust// consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1268 //ust// * Adjust read len, if longer than what is available
1270 //ust// bytes_avail = SUBBUF_TRUNC(local_read(<t_buf->offset), buf->chan)
1271 //ust// - consumed_old;
1272 //ust// WARN_ON(bytes_avail > buf->chan->alloc_size);
1273 //ust// len = min_t(size_t, len, bytes_avail);
1274 //ust// subbuf_pages = bytes_avail >> PAGE_SHIFT;
1275 //ust// nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS);
1276 //ust// roffset = consumed_old & PAGE_MASK;
1277 //ust// poff = consumed_old & ~PAGE_MASK;
1278 //ust// printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n",
1279 //ust// len, (ssize_t)*ppos, local_read(<t_buf->offset));
1281 //ust// for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
1282 //ust// unsigned int this_len;
1283 //ust// struct buf_page *page;
1287 //ust// printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
1288 //ust// len, roffset);
1290 //ust// this_len = PAGE_SIZE - poff;
1291 //ust// page = ltt_relay_read_get_page(buf, roffset);
1292 //ust// spd.pages[spd.nr_pages] = page->page;
1293 //ust// spd.partial[spd.nr_pages].offset = poff;
1294 //ust// spd.partial[spd.nr_pages].len = this_len;
1297 //ust// roffset += PAGE_SIZE;
1298 //ust// len -= this_len;
1301 //ust// if (!spd.nr_pages)
1304 //ust// return splice_to_pipe(pipe, &spd);
1307 //ust// static ssize_t ltt_relay_file_splice_read(struct file *in,
1308 //ust// loff_t *ppos,
1309 //ust// struct pipe_inode_info *pipe,
1311 //ust// unsigned int flags)
1313 //ust// ssize_t spliced;
1317 //ust// spliced = 0;
1319 //ust// printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n",
1320 //ust// len, (ssize_t)*ppos);
1321 //ust// while (len && !spliced) {
1322 //ust// ret = subbuf_splice_actor(in, ppos, pipe, len, flags);
1323 //ust// printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret);
1324 //ust// if (ret < 0)
1326 //ust// else if (!ret) {
1327 //ust// if (flags & SPLICE_F_NONBLOCK)
1328 //ust// ret = -EAGAIN;
1332 //ust// *ppos += ret;
1333 //ust// if (ret > len)
1337 //ust// spliced += ret;
1340 //ust// if (spliced)
1341 //ust// return spliced;
1346 static void ltt_relay_print_subbuffer_errors(
1347 struct ltt_channel_struct
*ltt_chan
,
1350 struct rchan
*rchan
= ltt_chan
->trans_channel_data
;
1351 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1352 long cons_idx
, commit_count
, write_offset
;
1354 cons_idx
= SUBBUF_INDEX(cons_off
, rchan
);
1355 commit_count
= local_read(<t_buf
->commit_count
[cons_idx
]);
1357 * No need to order commit_count and write_offset reads because we
1358 * execute after trace is stopped when there are no readers left.
1360 write_offset
= local_read(<t_buf
->offset
);
1362 "LTT : unread channel %s offset is %ld "
1363 "and cons_off : %ld\n",
1364 ltt_chan
->channel_name
, write_offset
, cons_off
);
1365 /* Check each sub-buffer for non filled commit count */
1366 if (((commit_count
- rchan
->subbuf_size
) & ltt_chan
->commit_count_mask
)
1367 - (BUFFER_TRUNC(cons_off
, rchan
) >> ltt_chan
->n_subbufs_order
)
1370 "LTT : %s : subbuffer %lu has non filled "
1371 "commit count %lu.\n",
1372 ltt_chan
->channel_name
, cons_idx
, commit_count
);
1373 printk(KERN_ALERT
"LTT : %s : commit count : %lu, subbuf size %zd\n",
1374 ltt_chan
->channel_name
, commit_count
,
1375 rchan
->subbuf_size
);
1378 static void ltt_relay_print_errors(struct ltt_trace_struct
*trace
,
1379 struct ltt_channel_struct
*ltt_chan
)
1381 struct rchan
*rchan
= ltt_chan
->trans_channel_data
;
1382 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1385 for (cons_off
= atomic_long_read(<t_buf
->consumed
);
1386 (SUBBUF_TRUNC(local_read(<t_buf
->offset
),
1389 cons_off
= SUBBUF_ALIGN(cons_off
, rchan
))
1390 ltt_relay_print_subbuffer_errors(ltt_chan
, cons_off
);
1393 static void ltt_relay_print_buffer_errors(struct ltt_channel_struct
*ltt_chan
)
1395 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
1396 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1398 if (local_read(<t_buf
->events_lost
))
1400 "LTT : %s : %ld events lost "
1402 ltt_chan
->channel_name
,
1403 local_read(<t_buf
->events_lost
),
1404 ltt_chan
->channel_name
);
1405 if (local_read(<t_buf
->corrupted_subbuffers
))
1407 "LTT : %s : %ld corrupted subbuffers "
1409 ltt_chan
->channel_name
,
1410 local_read(<t_buf
->corrupted_subbuffers
),
1411 ltt_chan
->channel_name
);
1413 ltt_relay_print_errors(trace
, ltt_chan
);
1416 static void ltt_relay_remove_dirs(struct ltt_trace_struct
*trace
)
1418 //ust// debugfs_remove(trace->dentry.trace_root);
1421 static void ltt_relay_release_channel(struct kref
*kref
)
1423 struct ltt_channel_struct
*ltt_chan
= container_of(kref
,
1424 struct ltt_channel_struct
, kref
);
1425 free(ltt_chan
->buf
);
1429 * Create ltt buffer.
1431 //ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1432 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
1433 //ust// unsigned int cpu, unsigned int n_subbufs)
1435 //ust// struct ltt_channel_buf_struct *ltt_buf =
1436 //ust// percpu_ptr(ltt_chan->buf, cpu);
1437 //ust// unsigned int j;
1439 //ust// ltt_buf->commit_count =
1440 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
1441 //ust// GFP_KERNEL, cpu_to_node(cpu));
1442 //ust// if (!ltt_buf->commit_count)
1443 //ust// return -ENOMEM;
1444 //ust// kref_get(&trace->kref);
1445 //ust// kref_get(&trace->ltt_transport_kref);
1446 //ust// kref_get(<t_chan->kref);
1447 //ust// local_set(<t_buf->offset, ltt_subbuffer_header_size());
1448 //ust// atomic_long_set(<t_buf->consumed, 0);
1449 //ust// atomic_long_set(<t_buf->active_readers, 0);
1450 //ust// for (j = 0; j < n_subbufs; j++)
1451 //ust// local_set(<t_buf->commit_count[j], 0);
1452 //ust// init_waitqueue_head(<t_buf->write_wait);
1453 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1454 //ust// spin_lock_init(<t_buf->full_lock);
1456 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1457 //ust// /* atomic_add made on local variable on data that belongs to
1458 //ust// * various CPUs : ok because tracing not started (for this cpu). */
1459 //ust// local_add(ltt_subbuffer_header_size(), <t_buf->commit_count[0]);
1461 //ust// local_set(<t_buf->events_lost, 0);
1462 //ust// local_set(<t_buf->corrupted_subbuffers, 0);
1467 static int ltt_relay_create_buffer(struct ltt_trace_struct
*trace
,
1468 struct ltt_channel_struct
*ltt_chan
, struct rchan_buf
*buf
,
1469 unsigned int n_subbufs
)
1471 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1476 ltt_buf
->commit_count
=
1477 zmalloc(sizeof(ltt_buf
->commit_count
) * n_subbufs
);
1478 if (!ltt_buf
->commit_count
)
1480 kref_get(&trace
->kref
);
1481 kref_get(&trace
->ltt_transport_kref
);
1482 kref_get(<t_chan
->kref
);
1483 local_set(<t_buf
->offset
, ltt_subbuffer_header_size());
1484 atomic_long_set(<t_buf
->consumed
, 0);
1485 atomic_long_set(<t_buf
->active_readers
, 0);
1486 for (j
= 0; j
< n_subbufs
; j
++)
1487 local_set(<t_buf
->commit_count
[j
], 0);
1488 //ust// init_waitqueue_head(<t_buf->write_wait);
1489 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1490 //ust// spin_lock_init(<t_buf->full_lock);
1492 ltt_buffer_begin_callback(buf
, trace
->start_tsc
, 0);
1494 local_add(ltt_subbuffer_header_size(), <t_buf
->commit_count
[0]);
1496 local_set(<t_buf
->events_lost
, 0);
1497 local_set(<t_buf
->corrupted_subbuffers
, 0);
1504 ltt_buf
->data_ready_fd_read
= fds
[0];
1505 ltt_buf
->data_ready_fd_write
= fds
[1];
1507 /* FIXME: do we actually need this? */
1508 result
= fcntl(fds
[0], F_SETFL
, O_NONBLOCK
);
1513 //ust// ltt_buf->commit_seq = malloc(sizeof(ltt_buf->commit_seq) * n_subbufs);
1514 //ust// if(!ltt_buf->commit_seq) {
1518 /* FIXME: decrementally destroy on error */
1523 static void ltt_relay_destroy_buffer(struct ltt_channel_struct
*ltt_chan
)
1525 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
1526 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1528 kref_put(<t_chan
->trace
->ltt_transport_kref
,
1529 ltt_release_transport
);
1530 ltt_relay_print_buffer_errors(ltt_chan
);
1531 //ust// free(ltt_buf->commit_seq);
1532 kfree(ltt_buf
->commit_count
);
1533 ltt_buf
->commit_count
= NULL
;
1534 kref_put(<t_chan
->kref
, ltt_relay_release_channel
);
1535 kref_put(&trace
->kref
, ltt_release_trace
);
1536 //ust// wake_up_interruptible(&trace->kref_wq);
1539 static void ltt_chan_alloc_ltt_buf(struct ltt_channel_struct
*ltt_chan
)
1545 /* FIXME: increase size if we have a seq_commit array that overflows the page */
1546 size_t size
= PAGE_ALIGN(1);
1548 result
= ltt_chan
->buf_shmid
= shmget(getpid(), size
, IPC_CREAT
| IPC_EXCL
| 0700);
1549 if(ltt_chan
->buf_shmid
== -1) {
1554 ptr
= shmat(ltt_chan
->buf_shmid
, NULL
, 0);
1555 if(ptr
== (void *) -1) {
1560 /* Already mark the shared memory for destruction. This will occur only
1561 * when all users have detached.
1563 result
= shmctl(ltt_chan
->buf_shmid
, IPC_RMID
, NULL
);
1569 ltt_chan
->buf
= ptr
;
1574 result
= shmctl(ltt_chan
->buf_shmid
, IPC_RMID
, NULL
);
1585 static int ltt_relay_create_channel(const char *trace_name
,
1586 struct ltt_trace_struct
*trace
, struct dentry
*dir
,
1587 const char *channel_name
, struct ltt_channel_struct
*ltt_chan
,
1588 unsigned int subbuf_size
, unsigned int n_subbufs
,
1592 unsigned int tmpname_len
;
1595 tmpname
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1599 strncpy(tmpname
, LTT_FLIGHT_PREFIX
, PATH_MAX
-1);
1600 strncat(tmpname
, channel_name
,
1601 PATH_MAX
-1-sizeof(LTT_FLIGHT_PREFIX
));
1603 strncpy(tmpname
, channel_name
, PATH_MAX
-1);
1605 strncat(tmpname
, "_", PATH_MAX
-1-strlen(tmpname
));
1607 kref_init(<t_chan
->kref
);
1609 ltt_chan
->trace
= trace
;
1610 ltt_chan
->buffer_begin
= ltt_buffer_begin_callback
;
1611 ltt_chan
->buffer_end
= ltt_buffer_end_callback
;
1612 ltt_chan
->overwrite
= overwrite
;
1613 ltt_chan
->n_subbufs_order
= get_count_order(n_subbufs
);
1614 ltt_chan
->commit_count_mask
= (~0UL >> ltt_chan
->n_subbufs_order
);
1615 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
1617 ltt_chan_alloc_ltt_buf(ltt_chan
);
1619 //ust// ltt_chan->buf = malloc(sizeof(struct ltt_channel_buf_struct));
1622 ltt_chan
->trans_channel_data
= ltt_relay_open(tmpname
,
1627 tmpname_len
= strlen(tmpname
);
1628 if (tmpname_len
> 0) {
1629 /* Remove final _ for pretty printing */
1630 tmpname
[tmpname_len
-1] = '\0';
1632 if (ltt_chan
->trans_channel_data
== NULL
) {
1633 printk(KERN_ERR
"LTT : Can't open %s channel for trace %s\n",
1634 tmpname
, trace_name
);
1635 goto relay_open_error
;
1642 //ust// percpu_free(ltt_chan->buf);
1650 static int ltt_relay_create_dirs(struct ltt_trace_struct
*new_trace
)
1652 //ust// new_trace->dentry.trace_root = debugfs_create_dir(new_trace->trace_name,
1653 //ust// get_ltt_root());
1654 //ust// if (new_trace->dentry.trace_root == NULL) {
1655 //ust// printk(KERN_ERR "LTT : Trace directory name %s already taken\n",
1656 //ust// new_trace->trace_name);
1657 //ust// return EEXIST;
1660 //ust// new_trace->callbacks.create_buf_file = ltt_create_buf_file_callback;
1661 //ust// new_trace->callbacks.remove_buf_file = ltt_remove_buf_file_callback;
1667 * LTTng channel flush function.
1669 * Must be called when no tracing is active in the channel, because of
1670 * accesses across CPUs.
1672 static notrace
void ltt_relay_buffer_flush(struct rchan_buf
*buf
)
1674 struct ltt_channel_struct
*channel
=
1675 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
1676 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
1680 ltt_force_switch(buf
, FORCE_FLUSH
);
1682 result
= write(ltt_buf
->data_ready_fd_write
, "1", 1);
1684 PERROR("write (in ltt_relay_buffer_flush)");
1685 ERR("this should never happen!");
1689 static void ltt_relay_async_wakeup_chan(struct ltt_channel_struct
*ltt_channel
)
1691 //ust// unsigned int i;
1692 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
1694 //ust// for_each_possible_cpu(i) {
1695 //ust// struct ltt_channel_buf_struct *ltt_buf =
1696 //ust// percpu_ptr(ltt_channel->buf, i);
1698 //ust// if (atomic_read(<t_buf->wakeup_readers) == 1) {
1699 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1700 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
1705 static void ltt_relay_finish_buffer(struct ltt_channel_struct
*ltt_channel
)
1707 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
1711 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
1712 ltt_relay_buffer_flush(rchan
->buf
);
1713 //ust// ltt_relay_wake_writers(ltt_buf);
1714 /* closing the pipe tells the consumer the buffer is finished */
1716 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
1717 //if(result == -1) {
1718 // PERROR("write (in ltt_relay_finish_buffer)");
1719 // ERR("this should never happen!");
1721 close(ltt_buf
->data_ready_fd_write
);
1726 static void ltt_relay_finish_channel(struct ltt_channel_struct
*ltt_channel
)
1728 //ust// unsigned int i;
1730 //ust// for_each_possible_cpu(i)
1731 ltt_relay_finish_buffer(ltt_channel
);
1734 static void ltt_relay_remove_channel(struct ltt_channel_struct
*channel
)
1736 struct rchan
*rchan
= channel
->trans_channel_data
;
1738 ltt_relay_close(rchan
);
1739 kref_put(&channel
->kref
, ltt_relay_release_channel
);
1742 struct ltt_reserve_switch_offsets
{
1743 long begin
, end
, old
;
1744 long begin_switch
, end_switch_current
, end_switch_old
;
1745 long commit_count
, reserve_commit_diff
;
1746 size_t before_hdr_pad
, size
;
1752 * !0 if execution must be aborted.
1754 static inline int ltt_relay_try_reserve(
1755 struct ltt_channel_struct
*ltt_channel
,
1756 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1757 struct rchan_buf
*buf
,
1758 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
1759 u64
*tsc
, unsigned int *rflags
, int largest_align
)
1761 offsets
->begin
= local_read(<t_buf
->offset
);
1762 offsets
->old
= offsets
->begin
;
1763 offsets
->begin_switch
= 0;
1764 offsets
->end_switch_current
= 0;
1765 offsets
->end_switch_old
= 0;
1767 *tsc
= trace_clock_read64();
1768 if (last_tsc_overflow(ltt_buf
, *tsc
))
1769 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
1771 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0) {
1772 offsets
->begin_switch
= 1; /* For offsets->begin */
1774 offsets
->size
= ltt_get_header_size(ltt_channel
,
1775 offsets
->begin
, data_size
,
1776 &offsets
->before_hdr_pad
, *rflags
);
1777 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1780 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
1781 > buf
->chan
->subbuf_size
) {
1782 offsets
->end_switch_old
= 1; /* For offsets->old */
1783 offsets
->begin_switch
= 1; /* For offsets->begin */
1786 if (offsets
->begin_switch
) {
1789 if (offsets
->end_switch_old
)
1790 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
1792 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
1793 /* Test new buffer integrity */
1794 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1795 offsets
->reserve_commit_diff
=
1796 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1797 >> ltt_channel
->n_subbufs_order
)
1798 - (local_read(<t_buf
->commit_count
[subbuf_index
])
1799 & ltt_channel
->commit_count_mask
);
1800 if (offsets
->reserve_commit_diff
== 0) {
1803 consumed
= atomic_long_read(<t_buf
->consumed
);
1805 /* Next buffer not corrupted. */
1806 if (!ltt_channel
->overwrite
&&
1807 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
1808 - SUBBUF_TRUNC(consumed
, buf
->chan
))
1809 >= rchan
->alloc_size
) {
1811 long consumed_idx
= SUBBUF_INDEX(consumed
, buf
->chan
);
1812 long commit_count
= local_read(<t_buf
->commit_count
[consumed_idx
]);
1813 if(((commit_count
- buf
->chan
->subbuf_size
) & ltt_channel
->commit_count_mask
) - (BUFFER_TRUNC(consumed
, buf
->chan
) >> ltt_channel
->n_subbufs_order
) != 0) {
1814 WARN("Event dropped. Caused by non-committed event.");
1817 WARN("Event dropped. Caused by non-consumed buffer.");
1820 * We do not overwrite non consumed buffers
1821 * and we are full : event is lost.
1823 local_inc(<t_buf
->events_lost
);
1827 * next buffer not corrupted, we are either in
1828 * overwrite mode or the buffer is not full.
1829 * It's safe to write in this new subbuffer.
1834 * Next subbuffer corrupted. Force pushing reader even
1835 * in normal mode. It's safe to write in this new
1839 offsets
->size
= ltt_get_header_size(ltt_channel
,
1840 offsets
->begin
, data_size
,
1841 &offsets
->before_hdr_pad
, *rflags
);
1842 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1845 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
1846 > buf
->chan
->subbuf_size
) {
1848 * Event too big for subbuffers, report error, don't
1849 * complete the sub-buffer switch.
1851 local_inc(<t_buf
->events_lost
);
1855 * We just made a successful buffer switch and the event
1856 * fits in the new subbuffer. Let's write.
1861 * Event fits in the current buffer and we are not on a switch
1862 * boundary. It's safe to write.
1865 offsets
->end
= offsets
->begin
+ offsets
->size
;
1867 if ((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0) {
1869 * The offset_end will fall at the very beginning of the next
1872 offsets
->end_switch_current
= 1; /* For offsets->begin */
1880 * !0 if execution must be aborted.
1882 static inline int ltt_relay_try_switch(
1883 enum force_switch_mode mode
,
1884 struct ltt_channel_struct
*ltt_channel
,
1885 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1886 struct rchan_buf
*buf
,
1887 struct ltt_reserve_switch_offsets
*offsets
,
1892 offsets
->begin
= local_read(<t_buf
->offset
);
1893 offsets
->old
= offsets
->begin
;
1894 offsets
->begin_switch
= 0;
1895 offsets
->end_switch_old
= 0;
1897 *tsc
= trace_clock_read64();
1899 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
1900 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
1901 offsets
->end_switch_old
= 1;
1903 /* we do not have to switch : buffer is empty */
1906 if (mode
== FORCE_ACTIVE
)
1907 offsets
->begin
+= ltt_subbuffer_header_size();
1909 * Always begin_switch in FORCE_ACTIVE mode.
1910 * Test new buffer integrity
1912 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1913 offsets
->reserve_commit_diff
=
1914 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1915 >> ltt_channel
->n_subbufs_order
)
1916 - (local_read(<t_buf
->commit_count
[subbuf_index
])
1917 & ltt_channel
->commit_count_mask
);
1918 if (offsets
->reserve_commit_diff
== 0) {
1919 /* Next buffer not corrupted. */
1920 if (mode
== FORCE_ACTIVE
1921 && !ltt_channel
->overwrite
1922 && offsets
->begin
- atomic_long_read(<t_buf
->consumed
)
1923 >= rchan
->alloc_size
) {
1925 * We do not overwrite non consumed buffers and we are
1926 * full : ignore switch while tracing is active.
1932 * Next subbuffer corrupted. Force pushing reader even in normal
1936 offsets
->end
= offsets
->begin
;
1940 static inline void ltt_reserve_push_reader(
1941 struct ltt_channel_struct
*ltt_channel
,
1942 struct ltt_channel_buf_struct
*ltt_buf
,
1943 struct rchan
*rchan
,
1944 struct rchan_buf
*buf
,
1945 struct ltt_reserve_switch_offsets
*offsets
)
1947 long consumed_old
, consumed_new
;
1950 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1952 * If buffer is in overwrite mode, push the reader consumed
1953 * count if the write position has reached it and we are not
1954 * at the first iteration (don't push the reader farther than
1955 * the writer). This operation can be done concurrently by many
1956 * writers in the same buffer, the writer being at the farthest
1957 * write position sub-buffer index in the buffer being the one
1958 * which will win this loop.
1959 * If the buffer is not in overwrite mode, pushing the reader
1960 * only happens if a sub-buffer is corrupted.
1962 if ((SUBBUF_TRUNC(offsets
->end
-1, buf
->chan
)
1963 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1964 >= rchan
->alloc_size
)
1965 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1967 consumed_new
= consumed_old
;
1970 } while (atomic_long_cmpxchg(<t_buf
->consumed
, consumed_old
,
1971 consumed_new
) != consumed_old
);
1973 if (consumed_old
!= consumed_new
) {
1975 * Reader pushed : we are the winner of the push, we can
1976 * therefore reequilibrate reserve and commit. Atomic increment
1977 * of the commit count permits other writers to play around
1978 * with this variable before us. We keep track of
1979 * corrupted_subbuffers even in overwrite mode :
1980 * we never want to write over a non completely committed
1981 * sub-buffer : possible causes : the buffer size is too low
1982 * compared to the unordered data input, or there is a writer
1983 * that died between the reserve and the commit.
1985 if (offsets
->reserve_commit_diff
) {
1987 * We have to alter the sub-buffer commit count.
1988 * We do not deliver the previous subbuffer, given it
1989 * was either corrupted or not consumed (overwrite
1992 local_add(offsets
->reserve_commit_diff
,
1993 <t_buf
->commit_count
[
1994 SUBBUF_INDEX(offsets
->begin
,
1996 if (!ltt_channel
->overwrite
1997 || offsets
->reserve_commit_diff
1998 != rchan
->subbuf_size
) {
2000 * The reserve commit diff was not subbuf_size :
2001 * it means the subbuffer was partly written to
2002 * and is therefore corrupted. If it is multiple
2003 * of subbuffer size and we are in flight
2004 * recorder mode, we are skipping over a whole
2007 local_inc(<t_buf
->corrupted_subbuffers
);
2015 * ltt_reserve_switch_old_subbuf: switch old subbuffer
2017 * Concurrency safe because we are the last and only thread to alter this
2018 * sub-buffer. As long as it is not delivered and read, no other thread can
2019 * alter the offset, alter the reserve_count or call the
2020 * client_buffer_end_callback on this sub-buffer.
2022 * The only remaining threads could be the ones with pending commits. They will
2023 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
2024 * We detect corrupted subbuffers with commit and reserve counts. We keep a
2025 * corrupted sub-buffers count and push the readers across these sub-buffers.
2027 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
2028 * switches in, finding out it's corrupted. The result will be than the old
2029 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
2030 * will be declared corrupted too because of the commit count adjustment.
2032 * Note : offset_old should never be 0 here.
2034 static inline void ltt_reserve_switch_old_subbuf(
2035 struct ltt_channel_struct
*ltt_channel
,
2036 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
2037 struct rchan_buf
*buf
,
2038 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
2040 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, rchan
);
2042 ltt_channel
->buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
2043 /* Must write buffer end before incrementing commit count */
2045 offsets
->commit_count
=
2046 local_add_return(rchan
->subbuf_size
2047 - (SUBBUF_OFFSET(offsets
->old
- 1, rchan
)
2049 <t_buf
->commit_count
[oldidx
]);
2050 if ((BUFFER_TRUNC(offsets
->old
- 1, rchan
)
2051 >> ltt_channel
->n_subbufs_order
)
2052 - ((offsets
->commit_count
- rchan
->subbuf_size
)
2053 & ltt_channel
->commit_count_mask
) == 0)
2054 ltt_deliver(buf
, oldidx
, offsets
->commit_count
);
2058 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
2060 * This code can be executed unordered : writers may already have written to the
2061 * sub-buffer before this code gets executed, caution. The commit makes sure
2062 * that this code is executed before the deliver of this sub-buffer.
2064 static /*inline*/ void ltt_reserve_switch_new_subbuf(
2065 struct ltt_channel_struct
*ltt_channel
,
2066 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
2067 struct rchan_buf
*buf
,
2068 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
2070 long beginidx
= SUBBUF_INDEX(offsets
->begin
, rchan
);
2072 ltt_channel
->buffer_begin(buf
, *tsc
, beginidx
);
2073 /* Must write buffer end before incrementing commit count */
2075 offsets
->commit_count
= local_add_return(ltt_subbuffer_header_size(),
2076 <t_buf
->commit_count
[beginidx
]);
2077 /* Check if the written buffer has to be delivered */
2078 if ((BUFFER_TRUNC(offsets
->begin
, rchan
)
2079 >> ltt_channel
->n_subbufs_order
)
2080 - ((offsets
->commit_count
- rchan
->subbuf_size
)
2081 & ltt_channel
->commit_count_mask
) == 0)
2082 ltt_deliver(buf
, beginidx
, offsets
->commit_count
);
2087 * ltt_reserve_end_switch_current: finish switching current subbuffer
2089 * Concurrency safe because we are the last and only thread to alter this
2090 * sub-buffer. As long as it is not delivered and read, no other thread can
2091 * alter the offset, alter the reserve_count or call the
2092 * client_buffer_end_callback on this sub-buffer.
2094 * The only remaining threads could be the ones with pending commits. They will
2095 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
2096 * We detect corrupted subbuffers with commit and reserve counts. We keep a
2097 * corrupted sub-buffers count and push the readers across these sub-buffers.
2099 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
2100 * switches in, finding out it's corrupted. The result will be than the old
2101 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
2102 * will be declared corrupted too because of the commit count adjustment.
2104 static inline void ltt_reserve_end_switch_current(
2105 struct ltt_channel_struct
*ltt_channel
,
2106 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
2107 struct rchan_buf
*buf
,
2108 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
2110 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, rchan
);
2112 ltt_channel
->buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
2113 /* Must write buffer begin before incrementing commit count */
2115 offsets
->commit_count
=
2116 local_add_return(rchan
->subbuf_size
2117 - (SUBBUF_OFFSET(offsets
->end
- 1, rchan
)
2119 <t_buf
->commit_count
[endidx
]);
2120 if ((BUFFER_TRUNC(offsets
->end
- 1, rchan
)
2121 >> ltt_channel
->n_subbufs_order
)
2122 - ((offsets
->commit_count
- rchan
->subbuf_size
)
2123 & ltt_channel
->commit_count_mask
) == 0)
2124 ltt_deliver(buf
, endidx
, offsets
->commit_count
);
2128 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
2129 * @trace: the trace structure to log to.
2130 * @ltt_channel: channel structure
2131 * @transport_data: data structure specific to ltt relay
2132 * @data_size: size of the variable length data to log.
2133 * @slot_size: pointer to total size of the slot (out)
2134 * @buf_offset : pointer to reserved buffer offset (out)
2135 * @tsc: pointer to the tsc at the slot reservation (out)
2138 * Return : -ENOSPC if not enough space, else returns 0.
2139 * It will take care of sub-buffer switching.
2141 static notrace
int ltt_relay_reserve_slot(struct ltt_trace_struct
*trace
,
2142 struct ltt_channel_struct
*ltt_channel
, void **transport_data
,
2143 size_t data_size
, size_t *slot_size
, long *buf_offset
, u64
*tsc
,
2144 unsigned int *rflags
, int largest_align
)
2146 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
2147 struct rchan_buf
*buf
= *transport_data
= rchan
->buf
;
2148 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2149 struct ltt_reserve_switch_offsets offsets
;
2151 offsets
.reserve_commit_diff
= 0;
2155 * Perform retryable operations.
2157 if (ltt_nesting
> 4) {
2158 local_inc(<t_buf
->events_lost
);
2162 if (ltt_relay_try_reserve(ltt_channel
, ltt_buf
,
2163 rchan
, buf
, &offsets
, data_size
, tsc
, rflags
,
2166 } while (local_cmpxchg(<t_buf
->offset
, offsets
.old
,
2167 offsets
.end
) != offsets
.old
);
2170 * Atomically update last_tsc. This update races against concurrent
2171 * atomic updates, but the race will always cause supplementary full TSC
2172 * events, never the opposite (missing a full TSC event when it would be
2175 save_last_tsc(ltt_buf
, *tsc
);
2178 * Push the reader if necessary
2180 ltt_reserve_push_reader(ltt_channel
, ltt_buf
, rchan
, buf
, &offsets
);
2183 * Switch old subbuffer if needed.
2185 if (offsets
.end_switch_old
)
2186 ltt_reserve_switch_old_subbuf(ltt_channel
, ltt_buf
, rchan
, buf
,
2190 * Populate new subbuffer.
2192 if (offsets
.begin_switch
)
2193 ltt_reserve_switch_new_subbuf(ltt_channel
, ltt_buf
, rchan
,
2194 buf
, &offsets
, tsc
);
2196 if (offsets
.end_switch_current
)
2197 ltt_reserve_end_switch_current(ltt_channel
, ltt_buf
, rchan
,
2198 buf
, &offsets
, tsc
);
2200 *slot_size
= offsets
.size
;
2201 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
2206 * Force a sub-buffer switch for a per-cpu buffer. This operation is
2207 * completely reentrant : can be called while tracing is active with
2208 * absolutely no lock held.
2210 * Note, however, that as a local_cmpxchg is used for some atomic
2211 * operations, this function must be called from the CPU which owns the buffer
2212 * for a ACTIVE flush.
2214 static notrace
void ltt_force_switch(struct rchan_buf
*buf
,
2215 enum force_switch_mode mode
)
2217 struct ltt_channel_struct
*ltt_channel
=
2218 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
2219 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2220 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
2221 struct ltt_reserve_switch_offsets offsets
;
2224 offsets
.reserve_commit_diff
= 0;
2228 * Perform retryable operations.
2231 if (ltt_relay_try_switch(mode
, ltt_channel
, ltt_buf
,
2232 rchan
, buf
, &offsets
, &tsc
))
2234 } while (local_cmpxchg(<t_buf
->offset
, offsets
.old
,
2235 offsets
.end
) != offsets
.old
);
2238 * Atomically update last_tsc. This update races against concurrent
2239 * atomic updates, but the race will always cause supplementary full TSC
2240 * events, never the opposite (missing a full TSC event when it would be
2243 save_last_tsc(ltt_buf
, tsc
);
2246 * Push the reader if necessary
2248 if (mode
== FORCE_ACTIVE
)
2249 ltt_reserve_push_reader(ltt_channel
, ltt_buf
, rchan
,
2253 * Switch old subbuffer if needed.
2255 if (offsets
.end_switch_old
)
2256 ltt_reserve_switch_old_subbuf(ltt_channel
, ltt_buf
, rchan
, buf
,
2260 * Populate new subbuffer.
2262 if (mode
== FORCE_ACTIVE
)
2263 ltt_reserve_switch_new_subbuf(ltt_channel
,
2264 ltt_buf
, rchan
, buf
, &offsets
, &tsc
);
2268 * This is called with preemption disabled when user space has requested
2269 * blocking mode. If one of the active traces has free space below a
2270 * specific threshold value, we reenable preemption and block.
2272 static int ltt_relay_user_blocking(struct ltt_trace_struct
*trace
,
2273 unsigned int chan_index
, size_t data_size
,
2274 struct user_dbg_data
*dbg
)
2276 //ust// struct rchan *rchan;
2277 //ust// struct ltt_channel_buf_struct *ltt_buf;
2278 //ust// struct ltt_channel_struct *channel;
2279 //ust// struct rchan_buf *relay_buf;
2281 //ust// DECLARE_WAITQUEUE(wait, current);
2283 //ust// channel = &trace->channels[chan_index];
2284 //ust// rchan = channel->trans_channel_data;
2285 //ust// cpu = smp_processor_id();
2286 //ust// relay_buf = rchan->buf[cpu];
2287 //ust// ltt_buf = percpu_ptr(channel->buf, cpu);
2290 //ust// * Check if data is too big for the channel : do not
2291 //ust// * block for it.
2293 //ust// if (LTT_RESERVE_CRITICAL + data_size > relay_buf->chan->subbuf_size)
2297 //ust// * If free space too low, we block. We restart from the
2298 //ust// * beginning after we resume (cpu id may have changed
2299 //ust// * while preemption is active).
2301 //ust// spin_lock(<t_buf->full_lock);
2302 //ust// if (!channel->overwrite) {
2303 //ust// dbg->write = local_read(<t_buf->offset);
2304 //ust// dbg->read = atomic_long_read(<t_buf->consumed);
2305 //ust// dbg->avail_size = dbg->write + LTT_RESERVE_CRITICAL + data_size
2306 //ust// - SUBBUF_TRUNC(dbg->read,
2307 //ust// relay_buf->chan);
2308 //ust// if (dbg->avail_size > rchan->alloc_size) {
2309 //ust// __set_current_state(TASK_INTERRUPTIBLE);
2310 //ust// add_wait_queue(<t_buf->write_wait, &wait);
2311 //ust// spin_unlock(<t_buf->full_lock);
2312 //ust// preempt_enable();
2314 //ust// __set_current_state(TASK_RUNNING);
2315 //ust// remove_wait_queue(<t_buf->write_wait, &wait);
2316 //ust// if (signal_pending(current))
2317 //ust// return -ERESTARTSYS;
2318 //ust// preempt_disable();
2322 //ust// spin_unlock(<t_buf->full_lock);
2326 static void ltt_relay_print_user_errors(struct ltt_trace_struct
*trace
,
2327 unsigned int chan_index
, size_t data_size
,
2328 struct user_dbg_data
*dbg
)
2330 struct rchan
*rchan
;
2331 struct ltt_channel_buf_struct
*ltt_buf
;
2332 struct ltt_channel_struct
*channel
;
2333 struct rchan_buf
*relay_buf
;
2335 channel
= &trace
->channels
[chan_index
];
2336 rchan
= channel
->trans_channel_data
;
2337 relay_buf
= rchan
->buf
;
2338 ltt_buf
= channel
->buf
;
2340 printk(KERN_ERR
"Error in LTT usertrace : "
2341 "buffer full : event lost in blocking "
2342 "mode. Increase LTT_RESERVE_CRITICAL.\n");
2343 printk(KERN_ERR
"LTT nesting level is %u.\n", ltt_nesting
);
2344 printk(KERN_ERR
"LTT avail size %lu.\n",
2346 printk(KERN_ERR
"avai write : %lu, read : %lu\n",
2347 dbg
->write
, dbg
->read
);
2349 dbg
->write
= local_read(<t_buf
->offset
);
2350 dbg
->read
= atomic_long_read(<t_buf
->consumed
);
2352 printk(KERN_ERR
"LTT cur size %lu.\n",
2353 dbg
->write
+ LTT_RESERVE_CRITICAL
+ data_size
2354 - SUBBUF_TRUNC(dbg
->read
, relay_buf
->chan
));
2355 printk(KERN_ERR
"cur write : %lu, read : %lu\n",
2356 dbg
->write
, dbg
->read
);
2359 //ust// static struct ltt_transport ltt_relay_transport = {
2360 //ust// .name = "relay",
2361 //ust// .owner = THIS_MODULE,
2363 //ust// .create_dirs = ltt_relay_create_dirs,
2364 //ust// .remove_dirs = ltt_relay_remove_dirs,
2365 //ust// .create_channel = ltt_relay_create_channel,
2366 //ust// .finish_channel = ltt_relay_finish_channel,
2367 //ust// .remove_channel = ltt_relay_remove_channel,
2368 //ust// .wakeup_channel = ltt_relay_async_wakeup_chan,
2369 //ust// .commit_slot = ltt_relay_commit_slot,
2370 //ust// .reserve_slot = ltt_relay_reserve_slot,
2371 //ust// .user_blocking = ltt_relay_user_blocking,
2372 //ust// .user_errors = ltt_relay_print_user_errors,
2376 static struct ltt_transport ust_relay_transport
= {
2378 //ust// .owner = THIS_MODULE,
2380 .create_dirs
= ltt_relay_create_dirs
,
2381 .remove_dirs
= ltt_relay_remove_dirs
,
2382 .create_channel
= ltt_relay_create_channel
,
2383 .finish_channel
= ltt_relay_finish_channel
,
2384 .remove_channel
= ltt_relay_remove_channel
,
2385 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
2386 // .commit_slot = ltt_relay_commit_slot,
2387 .reserve_slot
= ltt_relay_reserve_slot
,
2388 .user_blocking
= ltt_relay_user_blocking
,
2389 .user_errors
= ltt_relay_print_user_errors
,
2393 //ust// static int __init ltt_relay_init(void)
2395 //ust// printk(KERN_INFO "LTT : ltt-relay init\n");
2397 //ust// ltt_file_operations = ltt_relay_file_operations;
2398 //ust// ltt_file_operations.owner = THIS_MODULE;
2399 //ust// ltt_file_operations.open = ltt_open;
2400 //ust// ltt_file_operations.release = ltt_release;
2401 //ust// ltt_file_operations.poll = ltt_poll;
2402 //ust// ltt_file_operations.splice_read = ltt_relay_file_splice_read,
2403 //ust// ltt_file_operations.ioctl = ltt_ioctl;
2404 //ust//#ifdef CONFIG_COMPAT
2405 //ust// ltt_file_operations.compat_ioctl = ltt_compat_ioctl;
2408 //ust// ltt_transport_register(<t_relay_transport);
2414 * for flight recording. must be called after relay_commit.
2415 * This function decrements de subbuffer's lost_size each time the commit count
2416 * reaches back the reserve offset (module subbuffer size). It is useful for
2419 //ust// #ifdef CONFIG_LTT_VMCORE
2420 static /* inline */ void ltt_write_commit_counter(struct rchan_buf
*buf
,
2421 struct ltt_channel_buf_struct
*ltt_buf
,
2422 long idx
, long buf_offset
, long commit_count
, size_t data_size
)
2425 long commit_seq_old
;
2427 offset
= buf_offset
+ data_size
;
2430 * SUBBUF_OFFSET includes commit_count_mask. We can simply
2431 * compare the offsets within the subbuffer without caring about
2432 * buffer full/empty mismatch because offset is never zero here
2433 * (subbuffer header and event headers have non-zero length).
2435 if (unlikely(SUBBUF_OFFSET(offset
- commit_count
, buf
->chan
)))
2438 commit_seq_old
= local_read(<t_buf
->commit_seq
[idx
]);
2439 while (commit_seq_old
< commit_count
)
2440 commit_seq_old
= local_cmpxchg(<t_buf
->commit_seq
[idx
],
2441 commit_seq_old
, commit_count
);
2444 //ust// static inline void ltt_write_commit_counter(struct rchan_buf *buf,
2445 //ust// long buf_offset, size_t slot_size)
2451 * Atomic unordered slot commit. Increments the commit count in the
2452 * specified sub-buffer, and delivers it if necessary.
2456 * @ltt_channel : channel structure
2457 * @transport_data: transport-specific data
2458 * @buf_offset : offset following the event header.
2459 * @data_size : size of the event data.
2460 * @slot_size : size of the reserved slot.
2462 /* FIXME: make this function static inline in the .h! */
2463 /*static*/ /* inline */ notrace
void ltt_commit_slot(
2464 struct ltt_channel_struct
*ltt_channel
,
2465 void **transport_data
, long buf_offset
,
2466 size_t data_size
, size_t slot_size
)
2468 struct rchan_buf
*buf
= *transport_data
;
2469 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2470 struct rchan
*rchan
= buf
->chan
;
2471 long offset_end
= buf_offset
;
2472 long endidx
= SUBBUF_INDEX(offset_end
- 1, rchan
);
2475 /* Must write slot data before incrementing commit count */
2477 commit_count
= local_add_return(slot_size
,
2478 <t_buf
->commit_count
[endidx
]);
2479 /* Check if all commits have been done */
2480 if ((BUFFER_TRUNC(offset_end
- 1, rchan
)
2481 >> ltt_channel
->n_subbufs_order
)
2482 - ((commit_count
- rchan
->subbuf_size
)
2483 & ltt_channel
->commit_count_mask
) == 0)
2484 ltt_deliver(buf
, endidx
, commit_count
);
2486 * Update lost_size for each commit. It's needed only for extracting
2487 * ltt buffers from vmcore, after crash.
2489 ltt_write_commit_counter(buf
, ltt_buf
, endidx
,
2490 buf_offset
, commit_count
, data_size
);
2494 static char initialized
= 0;
2496 void __attribute__((constructor
)) init_ustrelay_transport(void)
2499 ltt_transport_register(&ust_relay_transport
);
2504 static void __attribute__((destructor
)) ltt_relay_exit(void)
2506 //ust// printk(KERN_INFO "LTT : ltt-relay exit\n");
2508 ltt_transport_unregister(&ust_relay_transport
);
2511 //ust// module_init(ltt_relay_init);
2512 //ust// module_exit(ltt_relay_exit);
2514 //ust// MODULE_LICENSE("GPL");
2515 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
2516 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Lockless Relay");