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"
34 #include "tracercore.h"
37 /* list of open channels, for cpu hotplug */
38 static DEFINE_MUTEX(relay_channels_mutex
);
39 static LIST_HEAD(relay_channels
);
42 static struct dentry
*ltt_create_buf_file_callback(struct rchan_buf
*buf
);
45 * relay_alloc_buf - allocate a channel buffer
46 * @buf: the buffer struct
47 * @size: total size of the buffer
49 //ust// static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
51 //ust// unsigned int i, n_pages;
52 //ust// struct buf_page *buf_page, *n;
54 //ust// *size = PAGE_ALIGN(*size);
55 //ust// n_pages = *size >> PAGE_SHIFT;
57 //ust// INIT_LIST_HEAD(&buf->pages);
59 //ust// for (i = 0; i < n_pages; i++) {
60 //ust// buf_page = kmalloc_node(sizeof(*buf_page), GFP_KERNEL,
61 //ust// cpu_to_node(buf->cpu));
62 //ust// if (unlikely(!buf_page))
63 //ust// goto depopulate;
64 //ust// buf_page->page = alloc_pages_node(cpu_to_node(buf->cpu),
65 //ust// GFP_KERNEL | __GFP_ZERO, 0);
66 //ust// if (unlikely(!buf_page->page)) {
67 //ust// kfree(buf_page);
68 //ust// goto depopulate;
70 //ust// list_add_tail(&buf_page->list, &buf->pages);
71 //ust// buf_page->offset = (size_t)i << PAGE_SHIFT;
72 //ust// buf_page->buf = buf;
73 //ust// set_page_private(buf_page->page, (unsigned long)buf_page);
75 //ust// buf->wpage = buf_page;
76 //ust// buf->hpage[0] = buf_page;
77 //ust// buf->hpage[1] = buf_page;
78 //ust// buf->rpage = buf_page;
81 //ust// buf->page_count = n_pages;
85 //ust// list_for_each_entry_safe(buf_page, n, &buf->pages, list) {
86 //ust// list_del_init(&buf_page->list);
87 //ust// __free_page(buf_page->page);
88 //ust// kfree(buf_page);
90 //ust// return -ENOMEM;
93 static int relay_alloc_buf(struct rchan_buf
*buf
, size_t *size
)
95 //ust// unsigned int n_pages;
96 //ust// struct buf_page *buf_page, *n;
101 *size
= PAGE_ALIGN(*size
);
103 result
= buf
->shmid
= shmget(getpid(), *size
, IPC_CREAT
| IPC_EXCL
| 0700);
104 if(buf
->shmid
== -1) {
109 ptr
= shmat(buf
->shmid
, NULL
, 0);
110 if(ptr
== (void *) -1) {
115 /* Already mark the shared memory for destruction. This will occur only
116 * when all users have detached.
118 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
125 buf
->buf_size
= *size
;
130 result
= shmctl(buf
->shmid
, IPC_RMID
, NULL
);
139 * relay_create_buf - allocate and initialize a channel buffer
140 * @chan: the relay channel
141 * @cpu: cpu the buffer belongs to
143 * Returns channel buffer if successful, %NULL otherwise.
145 static struct rchan_buf
*relay_create_buf(struct rchan
*chan
)
148 struct rchan_buf
*buf
= kzalloc(sizeof(struct rchan_buf
), GFP_KERNEL
);
153 ret
= relay_alloc_buf(buf
, &chan
->alloc_size
);
158 kref_get(&buf
->chan
->kref
);
167 * relay_destroy_channel - free the channel struct
168 * @kref: target kernel reference that contains the relay channel
170 * Should only be called from kref_put().
172 static void relay_destroy_channel(struct kref
*kref
)
174 struct rchan
*chan
= container_of(kref
, struct rchan
, kref
);
179 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
180 * @buf: the buffer struct
182 static void relay_destroy_buf(struct rchan_buf
*buf
)
184 struct rchan
*chan
= buf
->chan
;
185 struct buf_page
*buf_page
, *n
;
188 result
= munmap(buf
->buf_data
, buf
->buf_size
);
193 //ust// chan->buf[buf->cpu] = NULL;
195 kref_put(&chan
->kref
, relay_destroy_channel
);
199 * relay_remove_buf - remove a channel buffer
200 * @kref: target kernel reference that contains the relay buffer
202 * Removes the file from the fileystem, which also frees the
203 * rchan_buf_struct and the channel buffer. Should only be called from
206 static void relay_remove_buf(struct kref
*kref
)
208 struct rchan_buf
*buf
= container_of(kref
, struct rchan_buf
, kref
);
209 //ust// buf->chan->cb->remove_buf_file(buf);
210 relay_destroy_buf(buf
);
214 * High-level relay kernel API and associated functions.
218 * rchan_callback implementations defining default channel behavior. Used
219 * in place of corresponding NULL values in client callback struct.
223 * create_buf_file_create() default callback. Does nothing.
225 static struct dentry
*create_buf_file_default_callback(const char *filename
,
226 struct dentry
*parent
,
228 struct rchan_buf
*buf
)
234 * remove_buf_file() default callback. Does nothing.
236 static int remove_buf_file_default_callback(struct dentry
*dentry
)
242 * wakeup_readers - wake up readers waiting on a channel
243 * @data: contains the channel buffer
245 * This is the timer function used to defer reader waking.
247 //ust// static void wakeup_readers(unsigned long data)
249 //ust// struct rchan_buf *buf = (struct rchan_buf *)data;
250 //ust// wake_up_interruptible(&buf->read_wait);
254 * __relay_reset - reset a channel buffer
255 * @buf: the channel buffer
256 * @init: 1 if this is a first-time initialization
258 * See relay_reset() for description of effect.
260 static void __relay_reset(struct rchan_buf
*buf
, unsigned int init
)
263 //ust// init_waitqueue_head(&buf->read_wait);
264 kref_init(&buf
->kref
);
265 //ust// setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
267 //ust// del_timer_sync(&buf->timer);
273 * relay_open_buf - create a new relay channel buffer
275 * used by relay_open() and CPU hotplug.
277 static struct rchan_buf
*relay_open_buf(struct rchan
*chan
)
279 struct rchan_buf
*buf
= NULL
;
280 struct dentry
*dentry
;
281 //ust// char *tmpname;
283 //ust// tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
284 //ust// if (!tmpname)
286 //ust// snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
288 buf
= relay_create_buf(chan
);
292 __relay_reset(buf
, 1);
294 /* Create file in fs */
295 //ust// dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
298 ltt_create_buf_file_callback(buf
); // ust //
301 //ust// goto free_buf;
303 //ust// buf->dentry = dentry;
308 relay_destroy_buf(buf
);
311 //ust// kfree(tmpname);
317 * relay_close_buf - close a channel buffer
318 * @buf: channel buffer
320 * Marks the buffer finalized and restores the default callbacks.
321 * The channel buffer and channel buffer data structure are then freed
322 * automatically when the last reference is given up.
324 static void relay_close_buf(struct rchan_buf
*buf
)
326 //ust// del_timer_sync(&buf->timer);
327 kref_put(&buf
->kref
, relay_remove_buf
);
330 //ust// static void setup_callbacks(struct rchan *chan,
331 //ust// struct rchan_callbacks *cb)
334 //ust// chan->cb = &default_channel_callbacks;
338 //ust// if (!cb->create_buf_file)
339 //ust// cb->create_buf_file = create_buf_file_default_callback;
340 //ust// if (!cb->remove_buf_file)
341 //ust// cb->remove_buf_file = remove_buf_file_default_callback;
342 //ust// chan->cb = cb;
346 * relay_hotcpu_callback - CPU hotplug callback
347 * @nb: notifier block
348 * @action: hotplug action to take
351 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
353 //ust// static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
354 //ust// unsigned long action,
357 //ust// unsigned int hotcpu = (unsigned long)hcpu;
358 //ust// struct rchan *chan;
360 //ust// switch (action) {
361 //ust// case CPU_UP_PREPARE:
362 //ust// case CPU_UP_PREPARE_FROZEN:
363 //ust// mutex_lock(&relay_channels_mutex);
364 //ust// list_for_each_entry(chan, &relay_channels, list) {
365 //ust// if (chan->buf[hotcpu])
367 //ust// chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
368 //ust// if (!chan->buf[hotcpu]) {
369 //ust// printk(KERN_ERR
370 //ust// "relay_hotcpu_callback: cpu %d buffer "
371 //ust// "creation failed\n", hotcpu);
372 //ust// mutex_unlock(&relay_channels_mutex);
373 //ust// return NOTIFY_BAD;
376 //ust// mutex_unlock(&relay_channels_mutex);
378 //ust// case CPU_DEAD:
379 //ust// case CPU_DEAD_FROZEN:
380 //ust// /* No need to flush the cpu : will be flushed upon
381 //ust// * final relay_flush() call. */
384 //ust// return NOTIFY_OK;
388 * ltt_relay_open - create a new relay channel
389 * @base_filename: base name of files to create
390 * @parent: dentry of parent directory, %NULL for root directory
391 * @subbuf_size: size of sub-buffers
392 * @n_subbufs: number of sub-buffers
393 * @cb: client callback functions
394 * @private_data: user-defined data
396 * Returns channel pointer if successful, %NULL otherwise.
398 * Creates a channel buffer for each cpu using the sizes and
399 * attributes specified. The created channel buffer files
400 * will be named base_filename0...base_filenameN-1. File
401 * permissions will be %S_IRUSR.
403 struct rchan
*ltt_relay_open(const char *base_filename
,
404 struct dentry
*parent
,
411 //ust// if (!base_filename)
414 if (!(subbuf_size
&& n_subbufs
))
417 chan
= kzalloc(sizeof(struct rchan
), GFP_KERNEL
);
421 chan
->version
= LTT_RELAY_CHANNEL_VERSION
;
422 chan
->n_subbufs
= n_subbufs
;
423 chan
->subbuf_size
= subbuf_size
;
424 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
425 chan
->alloc_size
= FIX_SIZE(subbuf_size
* n_subbufs
);
426 chan
->parent
= parent
;
427 chan
->private_data
= private_data
;
428 //ust// strlcpy(chan->base_filename, base_filename, NAME_MAX);
429 //ust// setup_callbacks(chan, cb);
430 kref_init(&chan
->kref
);
432 mutex_lock(&relay_channels_mutex
);
433 //ust// for_each_online_cpu(i) {
434 chan
->buf
= relay_open_buf(chan
);
438 list_add(&chan
->list
, &relay_channels
);
439 mutex_unlock(&relay_channels_mutex
);
444 //ust// for_each_possible_cpu(i) {
445 //ust// if (!chan->buf[i])
447 //ust// relay_close_buf(chan->buf[i]);
451 kref_put(&chan
->kref
, relay_destroy_channel
);
452 mutex_unlock(&relay_channels_mutex
);
455 //ust// EXPORT_SYMBOL_GPL(ltt_relay_open);
458 * ltt_relay_close - close the channel
461 * Closes all channel buffers and frees the channel.
463 void ltt_relay_close(struct rchan
*chan
)
470 mutex_lock(&relay_channels_mutex
);
471 //ust// for_each_possible_cpu(i)
473 relay_close_buf(chan
->buf
);
475 list_del(&chan
->list
);
476 kref_put(&chan
->kref
, relay_destroy_channel
);
477 mutex_unlock(&relay_channels_mutex
);
479 //ust// EXPORT_SYMBOL_GPL(ltt_relay_close);
482 * Start iteration at the previous element. Skip the real list head.
484 //ust// struct buf_page *ltt_relay_find_prev_page(struct rchan_buf *buf,
485 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
487 //ust// struct buf_page *iter;
488 //ust// size_t orig_iter_off;
489 //ust// unsigned int i = 0;
491 //ust// orig_iter_off = page->offset;
492 //ust// list_for_each_entry_reverse(iter, &page->list, list) {
494 //ust// * Skip the real list head.
496 //ust// if (&iter->list == &buf->pages)
499 //ust// if (offset >= iter->offset
500 //ust// && offset < iter->offset + PAGE_SIZE) {
501 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
503 //ust// printk(KERN_WARNING
504 //ust// "Backward random access detected in "
505 //ust// "ltt_relay. Iterations %u, "
506 //ust// "offset %zu, orig iter->off %zu, "
507 //ust// "iter->off %zu diff_offset %zd.\n", i,
508 //ust// offset, orig_iter_off, iter->offset,
509 //ust// diff_offset);
519 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_prev_page);
522 * Start iteration at the next element. Skip the real list head.
524 //ust// struct buf_page *ltt_relay_find_next_page(struct rchan_buf *buf,
525 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
527 //ust// struct buf_page *iter;
528 //ust// unsigned int i = 0;
529 //ust// size_t orig_iter_off;
531 //ust// orig_iter_off = page->offset;
532 //ust// list_for_each_entry(iter, &page->list, list) {
534 //ust// * Skip the real list head.
536 //ust// if (&iter->list == &buf->pages)
539 //ust// if (offset >= iter->offset
540 //ust// && offset < iter->offset + PAGE_SIZE) {
541 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
543 //ust// printk(KERN_WARNING
544 //ust// "Forward random access detected in "
545 //ust// "ltt_relay. Iterations %u, "
546 //ust// "offset %zu, orig iter->off %zu, "
547 //ust// "iter->off %zu diff_offset %zd.\n", i,
548 //ust// offset, orig_iter_off, iter->offset,
549 //ust// diff_offset);
559 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_next_page);
562 * ltt_relay_write - write data to a ltt_relay buffer.
564 * @offset : offset within the buffer
565 * @src : source address
566 * @len : length to write
567 * @page : cached buffer page
568 * @pagecpy : page size copied so far
570 void _ltt_relay_write(struct rchan_buf
*buf
, size_t offset
,
571 const void *src
, size_t len
, ssize_t cpy
)
578 * Underlying layer should never ask for writes across
581 WARN_ON(offset
>= buf
->buf_size
);
583 cpy
= min_t(size_t, len
, buf
->buf_size
- offset
);
584 ltt_relay_do_copy(buf
->buf_data
+ offset
, src
, cpy
);
585 } while (unlikely(len
!= cpy
));
587 //ust// EXPORT_SYMBOL_GPL(_ltt_relay_write);
590 * ltt_relay_read - read data from ltt_relay_buffer.
592 * @offset : offset within the buffer
593 * @dest : destination address
594 * @len : length to write
596 //ust// int ltt_relay_read(struct rchan_buf *buf, size_t offset,
597 //ust// void *dest, size_t len)
599 //ust// struct buf_page *page;
600 //ust// ssize_t pagecpy, orig_len;
602 //ust// orig_len = len;
603 //ust// offset &= buf->chan->alloc_size - 1;
604 //ust// page = buf->rpage;
605 //ust// if (unlikely(!len))
608 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
609 //ust// pagecpy = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
610 //ust// memcpy(dest, page_address(page->page) + (offset & ~PAGE_MASK),
612 //ust// len -= pagecpy;
613 //ust// if (likely(!len))
615 //ust// dest += pagecpy;
616 //ust// offset += pagecpy;
618 //ust// * Underlying layer should never ask for reads across
619 //ust// * subbuffers.
621 //ust// WARN_ON(offset >= buf->chan->alloc_size);
623 //ust// return orig_len;
625 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read);
628 * ltt_relay_read_get_page - Get a whole page to read from
630 * @offset : offset within the buffer
632 //ust// struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf, size_t offset)
634 //ust// struct buf_page *page;
636 //ust// offset &= buf->chan->alloc_size - 1;
637 //ust// page = buf->rpage;
638 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
641 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read_get_page);
644 * ltt_relay_offset_address - get address of a location within the buffer
646 * @offset : offset within the buffer.
648 * Return the address where a given offset is located.
649 * Should be used to get the current subbuffer header pointer. Given we know
650 * it's never on a page boundary, it's safe to write directly to this address,
651 * as long as the write is never bigger than a page size.
653 void *ltt_relay_offset_address(struct rchan_buf
*buf
, size_t offset
)
655 //ust// struct buf_page *page;
656 //ust// unsigned int odd;
658 //ust// offset &= buf->chan->alloc_size - 1;
659 //ust// odd = !!(offset & buf->chan->subbuf_size);
660 //ust// page = buf->hpage[odd];
661 //ust// if (offset < page->offset || offset >= page->offset + PAGE_SIZE)
662 //ust// buf->hpage[odd] = page = buf->wpage;
663 //ust// page = ltt_relay_cache_page(buf, &buf->hpage[odd], page, offset);
664 //ust// return page_address(page->page) + (offset & ~PAGE_MASK);
665 return ((char *)buf
->buf_data
)+offset
;
668 //ust// EXPORT_SYMBOL_GPL(ltt_relay_offset_address);
671 * relay_file_open - open file op for relay files
675 * Increments the channel buffer refcount.
677 //ust// static int relay_file_open(struct inode *inode, struct file *filp)
679 //ust// struct rchan_buf *buf = inode->i_private;
680 //ust// kref_get(&buf->kref);
681 //ust// filp->private_data = buf;
683 //ust// return nonseekable_open(inode, filp);
687 * relay_file_release - release file op for relay files
691 * Decrements the channel refcount, as the filesystem is
692 * no longer using it.
694 //ust// static int relay_file_release(struct inode *inode, struct file *filp)
696 //ust// struct rchan_buf *buf = filp->private_data;
697 //ust// kref_put(&buf->kref, relay_remove_buf);
702 //ust// const struct file_operations ltt_relay_file_operations = {
703 //ust// .open = relay_file_open,
704 //ust// .release = relay_file_release,
706 //ust// EXPORT_SYMBOL_GPL(ltt_relay_file_operations);
708 //ust// static __init int relay_init(void)
710 //ust// hotcpu_notifier(relay_hotcpu_callback, 5);
714 //ust// module_init(relay_init);
718 * (C) Copyright 2005-2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
720 * LTTng lockless buffer space management (reader/writer).
723 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
725 * Inspired from LTT :
726 * Karim Yaghmour (karim@opersys.com)
727 * Tom Zanussi (zanussi@us.ibm.com)
728 * Bob Wisniewski (bob@watson.ibm.com)
730 * Bob Wisniewski (bob@watson.ibm.com)
734 * 19/10/05, Complete lockless mechanism.
735 * 27/05/05, Modular redesign and rewrite.
737 * Userspace reader semantic :
738 * while (poll fd != POLLHUP) {
739 * - ioctl RELAY_GET_SUBBUF_SIZE
742 * - splice 1 subbuffer worth of data to a pipe
743 * - splice the data from pipe to disk/network
744 * - ioctl PUT_SUBBUF, check error value
745 * if err val < 0, previous subbuffer was corrupted.
750 //ust// #include <linux/time.h>
751 //ust// #include <linux/ltt-tracer.h>
752 //ust// #include <linux/ltt-relay.h>
753 //ust// #include <linux/module.h>
754 //ust// #include <linux/string.h>
755 //ust// #include <linux/slab.h>
756 //ust// #include <linux/init.h>
757 //ust// #include <linux/rcupdate.h>
758 //ust// #include <linux/sched.h>
759 //ust// #include <linux/bitops.h>
760 //ust// #include <linux/fs.h>
761 //ust// #include <linux/smp_lock.h>
762 //ust// #include <linux/debugfs.h>
763 //ust// #include <linux/stat.h>
764 //ust// #include <linux/cpu.h>
765 //ust// #include <linux/pipe_fs_i.h>
766 //ust// #include <linux/splice.h>
767 //ust// #include <asm/atomic.h>
768 //ust// #include <asm/local.h>
771 #define printk_dbg(fmt, args...) printk(fmt, args)
773 #define printk_dbg(fmt, args...)
777 * Last TSC comparison functions. Check if the current TSC overflows
778 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
782 #if (BITS_PER_LONG == 32)
783 static inline void save_last_tsc(struct ltt_channel_buf_struct
*ltt_buf
,
786 ltt_buf
->last_tsc
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
789 static inline int last_tsc_overflow(struct ltt_channel_buf_struct
*ltt_buf
,
792 unsigned long tsc_shifted
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
794 if (unlikely((tsc_shifted
- ltt_buf
->last_tsc
)))
800 static inline void save_last_tsc(struct ltt_channel_buf_struct
*ltt_buf
,
803 ltt_buf
->last_tsc
= (unsigned long)tsc
;
806 static inline int last_tsc_overflow(struct ltt_channel_buf_struct
*ltt_buf
,
809 if (unlikely((tsc
- ltt_buf
->last_tsc
) >> LTT_TSC_BITS
))
816 //ust// static struct file_operations ltt_file_operations;
819 * A switch is done during tracing or as a final flush after tracing (so it
820 * won't write in the new sub-buffer).
822 enum force_switch_mode
{ FORCE_ACTIVE
, FORCE_FLUSH
};
824 static int ltt_relay_create_buffer(struct ltt_trace_struct
*trace
,
825 struct ltt_channel_struct
*ltt_chan
,
826 struct rchan_buf
*buf
,
827 unsigned int n_subbufs
);
829 static void ltt_relay_destroy_buffer(struct ltt_channel_struct
*ltt_chan
);
831 static void ltt_force_switch(struct rchan_buf
*buf
,
832 enum force_switch_mode mode
);
837 static void ltt_buffer_begin_callback(struct rchan_buf
*buf
,
838 u64 tsc
, unsigned int subbuf_idx
)
840 struct ltt_channel_struct
*channel
=
841 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
842 struct ltt_subbuffer_header
*header
=
843 (struct ltt_subbuffer_header
*)
844 ltt_relay_offset_address(buf
,
845 subbuf_idx
* buf
->chan
->subbuf_size
);
847 header
->cycle_count_begin
= tsc
;
848 header
->lost_size
= 0xFFFFFFFF; /* for debugging */
849 header
->buf_size
= buf
->chan
->subbuf_size
;
850 ltt_write_trace_header(channel
->trace
, header
);
854 * offset is assumed to never be 0 here : never deliver a completely empty
855 * subbuffer. The lost size is between 0 and subbuf_size-1.
857 static notrace
void ltt_buffer_end_callback(struct rchan_buf
*buf
,
858 u64 tsc
, unsigned int offset
, unsigned int subbuf_idx
)
860 struct ltt_channel_struct
*channel
=
861 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
862 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
863 struct ltt_subbuffer_header
*header
=
864 (struct ltt_subbuffer_header
*)
865 ltt_relay_offset_address(buf
,
866 subbuf_idx
* buf
->chan
->subbuf_size
);
868 header
->lost_size
= SUBBUF_OFFSET((buf
->chan
->subbuf_size
- offset
),
870 header
->cycle_count_end
= tsc
;
871 header
->events_lost
= local_read(<t_buf
->events_lost
);
872 header
->subbuf_corrupt
= local_read(<t_buf
->corrupted_subbuffers
);
876 void (*wake_consumer
)(void *, int) = NULL
;
878 void relay_set_wake_consumer(void (*wake
)(void *, int))
880 wake_consumer
= wake
;
883 void relay_wake_consumer(void *arg
, int finished
)
886 wake_consumer(arg
, finished
);
889 static notrace
void ltt_deliver(struct rchan_buf
*buf
, unsigned int subbuf_idx
,
892 struct ltt_channel_struct
*channel
=
893 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
894 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
897 result
= write(ltt_buf
->data_ready_fd_write
, "1", 1);
899 PERROR("write (in ltt_relay_buffer_flush)");
900 ERR("this should never happen!");
902 //ust// atomic_set(<t_buf->wakeup_readers, 1);
905 static struct dentry
*ltt_create_buf_file_callback(struct rchan_buf
*buf
)
907 struct ltt_channel_struct
*ltt_chan
;
909 //ust// struct dentry *dentry;
911 ltt_chan
= buf
->chan
->private_data
;
912 err
= ltt_relay_create_buffer(ltt_chan
->trace
, ltt_chan
, buf
, buf
->chan
->n_subbufs
);
916 //ust// dentry = debugfs_create_file(filename, mode, parent, buf,
917 //ust// <t_file_operations);
920 //ust// return dentry;
923 ltt_relay_destroy_buffer(ltt_chan
);
927 static int ltt_remove_buf_file_callback(struct rchan_buf
*buf
)
929 //ust// struct rchan_buf *buf = dentry->d_inode->i_private;
930 struct ltt_channel_struct
*ltt_chan
= buf
->chan
->private_data
;
932 //ust// debugfs_remove(dentry);
933 ltt_relay_destroy_buffer(ltt_chan
);
941 * This must be done after the trace is removed from the RCU list so that there
942 * are no stalled writers.
944 //ust// static void ltt_relay_wake_writers(struct ltt_channel_buf_struct *ltt_buf)
947 //ust// if (waitqueue_active(<t_buf->write_wait))
948 //ust// wake_up_interruptible(<t_buf->write_wait);
952 * This function should not be called from NMI interrupt context
954 static notrace
void ltt_buf_unfull(struct rchan_buf
*buf
,
955 unsigned int subbuf_idx
,
958 //ust// struct ltt_channel_struct *ltt_channel =
959 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
960 //ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
962 //ust// ltt_relay_wake_writers(ltt_buf);
966 * ltt_open - open file op for ltt files
967 * @inode: opened inode
970 * Open implementation. Makes sure only one open instance of a buffer is
971 * done at a given moment.
973 //ust// static int ltt_open(struct inode *inode, struct file *file)
975 //ust// struct rchan_buf *buf = inode->i_private;
976 //ust// struct ltt_channel_struct *ltt_channel =
977 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
978 //ust// struct ltt_channel_buf_struct *ltt_buf =
979 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
981 //ust// if (!atomic_long_add_unless(<t_buf->active_readers, 1, 1))
982 //ust// return -EBUSY;
983 //ust// return ltt_relay_file_operations.open(inode, file);
987 * ltt_release - release file op for ltt files
988 * @inode: opened inode
991 * Release implementation.
993 //ust// static int ltt_release(struct inode *inode, struct file *file)
995 //ust// struct rchan_buf *buf = inode->i_private;
996 //ust// struct ltt_channel_struct *ltt_channel =
997 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
998 //ust// struct ltt_channel_buf_struct *ltt_buf =
999 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1002 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1003 //ust// atomic_long_dec(<t_buf->active_readers);
1004 //ust// ret = ltt_relay_file_operations.release(inode, file);
1005 //ust// WARN_ON(ret);
1010 * ltt_poll - file op for ltt files
1014 * Poll implementation.
1016 //ust// static unsigned int ltt_poll(struct file *filp, poll_table *wait)
1018 //ust// unsigned int mask = 0;
1019 //ust// struct inode *inode = filp->f_dentry->d_inode;
1020 //ust// struct rchan_buf *buf = inode->i_private;
1021 //ust// struct ltt_channel_struct *ltt_channel =
1022 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1023 //ust// struct ltt_channel_buf_struct *ltt_buf =
1024 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1026 //ust// if (filp->f_mode & FMODE_READ) {
1027 //ust// poll_wait_set_exclusive(wait);
1028 //ust// poll_wait(filp, &buf->read_wait, wait);
1030 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1031 //ust// if (SUBBUF_TRUNC(local_read(<t_buf->offset),
1033 //ust// - SUBBUF_TRUNC(atomic_long_read(<t_buf->consumed),
1036 //ust// if (buf->finalized)
1037 //ust// return POLLHUP;
1041 //ust// struct rchan *rchan =
1042 //ust// ltt_channel->trans_channel_data;
1043 //ust// if (SUBBUF_TRUNC(local_read(<t_buf->offset),
1045 //ust// - SUBBUF_TRUNC(atomic_long_read(
1046 //ust// <t_buf->consumed),
1048 //ust// >= rchan->alloc_size)
1049 //ust// return POLLPRI | POLLRDBAND;
1051 //ust// return POLLIN | POLLRDNORM;
1054 //ust// return mask;
1057 int ltt_do_get_subbuf(struct rchan_buf
*buf
, struct ltt_channel_buf_struct
*ltt_buf
, long *pconsumed_old
)
1059 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)buf
->chan
->private_data
;
1060 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
1061 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1062 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
1063 commit_count
= local_read(<t_buf
->commit_count
[consumed_idx
]);
1065 * Make sure we read the commit count before reading the buffer
1066 * data and the write offset. Correct consumed offset ordering
1067 * wrt commit count is insured by the use of cmpxchg to update
1068 * the consumed offset.
1071 write_offset
= local_read(<t_buf
->offset
);
1073 * Check that the subbuffer we are trying to consume has been
1074 * already fully committed.
1076 if (((commit_count
- buf
->chan
->subbuf_size
)
1077 & ltt_channel
->commit_count_mask
)
1078 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
1079 >> ltt_channel
->n_subbufs_order
)
1084 * Check that we are not about to read the same subbuffer in
1085 * which the writer head is.
1087 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
1088 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1093 *pconsumed_old
= consumed_old
;
1097 int ltt_do_put_subbuf(struct rchan_buf
*buf
, struct ltt_channel_buf_struct
*ltt_buf
, u32 uconsumed_old
)
1099 long consumed_new
, consumed_old
;
1101 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1102 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
1103 consumed_old
= consumed_old
| uconsumed_old
;
1104 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1106 //ust// spin_lock(<t_buf->full_lock);
1107 if (atomic_long_cmpxchg(<t_buf
->consumed
, consumed_old
,
1110 /* We have been pushed by the writer : the last
1111 * buffer read _is_ corrupted! It can also
1112 * happen if this is a buffer we never got. */
1113 //ust// spin_unlock(<t_buf->full_lock);
1116 /* tell the client that buffer is now unfull */
1119 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
1120 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
1121 ltt_buf_unfull(buf
, index
, data
);
1122 //ust// spin_unlock(<t_buf->full_lock);
1128 * ltt_ioctl - control on the debugfs file
1135 * This ioctl implements three commands necessary for a minimal
1136 * producer/consumer implementation :
1138 * Get the next sub buffer that can be read. It never blocks.
1140 * Release the currently read sub-buffer. Parameter is the last
1141 * put subbuffer (returned by GET_SUBBUF).
1142 * RELAY_GET_N_BUBBUFS
1143 * returns the number of sub buffers in the per cpu channel.
1144 * RELAY_GET_SUBBUF_SIZE
1145 * returns the size of the sub buffers.
1147 //ust// static int ltt_ioctl(struct inode *inode, struct file *filp,
1148 //ust// unsigned int cmd, unsigned long arg)
1150 //ust// struct rchan_buf *buf = inode->i_private;
1151 //ust// struct ltt_channel_struct *ltt_channel =
1152 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1153 //ust// struct ltt_channel_buf_struct *ltt_buf =
1154 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1155 //ust// u32 __user *argp = (u32 __user *)arg;
1157 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1158 //ust// switch (cmd) {
1159 //ust// case RELAY_GET_SUBBUF:
1162 //ust// ret = ltt_do_get_subbuf(buf, ltt_buf, &consumed_old);
1165 //ust// return put_user((u32)consumed_old, argp);
1167 //ust// case RELAY_PUT_SUBBUF:
1170 //ust// u32 uconsumed_old;
1171 //ust// ret = get_user(uconsumed_old, argp);
1173 //ust// return ret; /* will return -EFAULT */
1174 //ust// return ltt_do_put_subbuf(buf, ltt_buf, uconsumed_old);
1176 //ust// case RELAY_GET_N_SUBBUFS:
1177 //ust// return put_user((u32)buf->chan->n_subbufs, argp);
1179 //ust// case RELAY_GET_SUBBUF_SIZE:
1180 //ust// return put_user((u32)buf->chan->subbuf_size, argp);
1183 //ust// return -ENOIOCTLCMD;
1188 //ust// #ifdef CONFIG_COMPAT
1189 //ust// static long ltt_compat_ioctl(struct file *file, unsigned int cmd,
1190 //ust// unsigned long arg)
1192 //ust// long ret = -ENOIOCTLCMD;
1194 //ust// lock_kernel();
1195 //ust// ret = ltt_ioctl(file->f_dentry->d_inode, file, cmd, arg);
1196 //ust// unlock_kernel();
1202 //ust// static void ltt_relay_pipe_buf_release(struct pipe_inode_info *pipe,
1203 //ust// struct pipe_buffer *pbuf)
1207 //ust// static struct pipe_buf_operations ltt_relay_pipe_buf_ops = {
1208 //ust// .can_merge = 0,
1209 //ust// .map = generic_pipe_buf_map,
1210 //ust// .unmap = generic_pipe_buf_unmap,
1211 //ust// .confirm = generic_pipe_buf_confirm,
1212 //ust// .release = ltt_relay_pipe_buf_release,
1213 //ust// .steal = generic_pipe_buf_steal,
1214 //ust// .get = generic_pipe_buf_get,
1217 //ust// static void ltt_relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1222 * subbuf_splice_actor - splice up to one subbuf's worth of data
1224 //ust// static int subbuf_splice_actor(struct file *in,
1225 //ust// loff_t *ppos,
1226 //ust// struct pipe_inode_info *pipe,
1228 //ust// unsigned int flags)
1230 //ust// struct rchan_buf *buf = in->private_data;
1231 //ust// struct ltt_channel_struct *ltt_channel =
1232 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1233 //ust// struct ltt_channel_buf_struct *ltt_buf =
1234 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1235 //ust// unsigned int poff, subbuf_pages, nr_pages;
1236 //ust// struct page *pages[PIPE_BUFFERS];
1237 //ust// struct partial_page partial[PIPE_BUFFERS];
1238 //ust// struct splice_pipe_desc spd = {
1239 //ust// .pages = pages,
1240 //ust// .nr_pages = 0,
1241 //ust// .partial = partial,
1242 //ust// .flags = flags,
1243 //ust// .ops = <t_relay_pipe_buf_ops,
1244 //ust// .spd_release = ltt_relay_page_release,
1246 //ust// long consumed_old, consumed_idx, roffset;
1247 //ust// unsigned long bytes_avail;
1250 //ust// * Check that a GET_SUBBUF ioctl has been done before.
1252 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1253 //ust// consumed_old = atomic_long_read(<t_buf->consumed);
1254 //ust// consumed_old += *ppos;
1255 //ust// consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1258 //ust// * Adjust read len, if longer than what is available
1260 //ust// bytes_avail = SUBBUF_TRUNC(local_read(<t_buf->offset), buf->chan)
1261 //ust// - consumed_old;
1262 //ust// WARN_ON(bytes_avail > buf->chan->alloc_size);
1263 //ust// len = min_t(size_t, len, bytes_avail);
1264 //ust// subbuf_pages = bytes_avail >> PAGE_SHIFT;
1265 //ust// nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS);
1266 //ust// roffset = consumed_old & PAGE_MASK;
1267 //ust// poff = consumed_old & ~PAGE_MASK;
1268 //ust// printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n",
1269 //ust// len, (ssize_t)*ppos, local_read(<t_buf->offset));
1271 //ust// for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
1272 //ust// unsigned int this_len;
1273 //ust// struct buf_page *page;
1277 //ust// printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
1278 //ust// len, roffset);
1280 //ust// this_len = PAGE_SIZE - poff;
1281 //ust// page = ltt_relay_read_get_page(buf, roffset);
1282 //ust// spd.pages[spd.nr_pages] = page->page;
1283 //ust// spd.partial[spd.nr_pages].offset = poff;
1284 //ust// spd.partial[spd.nr_pages].len = this_len;
1287 //ust// roffset += PAGE_SIZE;
1288 //ust// len -= this_len;
1291 //ust// if (!spd.nr_pages)
1294 //ust// return splice_to_pipe(pipe, &spd);
1297 //ust// static ssize_t ltt_relay_file_splice_read(struct file *in,
1298 //ust// loff_t *ppos,
1299 //ust// struct pipe_inode_info *pipe,
1301 //ust// unsigned int flags)
1303 //ust// ssize_t spliced;
1307 //ust// spliced = 0;
1309 //ust// printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n",
1310 //ust// len, (ssize_t)*ppos);
1311 //ust// while (len && !spliced) {
1312 //ust// ret = subbuf_splice_actor(in, ppos, pipe, len, flags);
1313 //ust// printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret);
1314 //ust// if (ret < 0)
1316 //ust// else if (!ret) {
1317 //ust// if (flags & SPLICE_F_NONBLOCK)
1318 //ust// ret = -EAGAIN;
1322 //ust// *ppos += ret;
1323 //ust// if (ret > len)
1327 //ust// spliced += ret;
1330 //ust// if (spliced)
1331 //ust// return spliced;
1336 static void ltt_relay_print_subbuffer_errors(
1337 struct ltt_channel_struct
*ltt_chan
,
1340 struct rchan
*rchan
= ltt_chan
->trans_channel_data
;
1341 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1342 long cons_idx
, commit_count
, write_offset
;
1344 cons_idx
= SUBBUF_INDEX(cons_off
, rchan
);
1345 commit_count
= local_read(<t_buf
->commit_count
[cons_idx
]);
1347 * No need to order commit_count and write_offset reads because we
1348 * execute after trace is stopped when there are no readers left.
1350 write_offset
= local_read(<t_buf
->offset
);
1352 "LTT : unread channel %s offset is %ld "
1353 "and cons_off : %ld\n",
1354 ltt_chan
->channel_name
, write_offset
, cons_off
);
1355 /* Check each sub-buffer for non filled commit count */
1356 if (((commit_count
- rchan
->subbuf_size
) & ltt_chan
->commit_count_mask
)
1357 - (BUFFER_TRUNC(cons_off
, rchan
) >> ltt_chan
->n_subbufs_order
)
1360 "LTT : %s : subbuffer %lu has non filled "
1361 "commit count %lu.\n",
1362 ltt_chan
->channel_name
, cons_idx
, commit_count
);
1363 printk(KERN_ALERT
"LTT : %s : commit count : %lu, subbuf size %zd\n",
1364 ltt_chan
->channel_name
, commit_count
,
1365 rchan
->subbuf_size
);
1368 static void ltt_relay_print_errors(struct ltt_trace_struct
*trace
,
1369 struct ltt_channel_struct
*ltt_chan
)
1371 struct rchan
*rchan
= ltt_chan
->trans_channel_data
;
1372 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1375 for (cons_off
= atomic_long_read(<t_buf
->consumed
);
1376 (SUBBUF_TRUNC(local_read(<t_buf
->offset
),
1379 cons_off
= SUBBUF_ALIGN(cons_off
, rchan
))
1380 ltt_relay_print_subbuffer_errors(ltt_chan
, cons_off
);
1383 static void ltt_relay_print_buffer_errors(struct ltt_channel_struct
*ltt_chan
)
1385 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
1386 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1388 if (local_read(<t_buf
->events_lost
))
1390 "LTT : %s : %ld events lost "
1392 ltt_chan
->channel_name
,
1393 local_read(<t_buf
->events_lost
),
1394 ltt_chan
->channel_name
);
1395 if (local_read(<t_buf
->corrupted_subbuffers
))
1397 "LTT : %s : %ld corrupted subbuffers "
1399 ltt_chan
->channel_name
,
1400 local_read(<t_buf
->corrupted_subbuffers
),
1401 ltt_chan
->channel_name
);
1403 ltt_relay_print_errors(trace
, ltt_chan
);
1406 static void ltt_relay_remove_dirs(struct ltt_trace_struct
*trace
)
1408 //ust// debugfs_remove(trace->dentry.trace_root);
1411 static void ltt_relay_release_channel(struct kref
*kref
)
1413 struct ltt_channel_struct
*ltt_chan
= container_of(kref
,
1414 struct ltt_channel_struct
, kref
);
1415 free(ltt_chan
->buf
);
1419 * Create ltt buffer.
1421 //ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1422 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
1423 //ust// unsigned int cpu, unsigned int n_subbufs)
1425 //ust// struct ltt_channel_buf_struct *ltt_buf =
1426 //ust// percpu_ptr(ltt_chan->buf, cpu);
1427 //ust// unsigned int j;
1429 //ust// ltt_buf->commit_count =
1430 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
1431 //ust// GFP_KERNEL, cpu_to_node(cpu));
1432 //ust// if (!ltt_buf->commit_count)
1433 //ust// return -ENOMEM;
1434 //ust// kref_get(&trace->kref);
1435 //ust// kref_get(&trace->ltt_transport_kref);
1436 //ust// kref_get(<t_chan->kref);
1437 //ust// local_set(<t_buf->offset, ltt_subbuffer_header_size());
1438 //ust// atomic_long_set(<t_buf->consumed, 0);
1439 //ust// atomic_long_set(<t_buf->active_readers, 0);
1440 //ust// for (j = 0; j < n_subbufs; j++)
1441 //ust// local_set(<t_buf->commit_count[j], 0);
1442 //ust// init_waitqueue_head(<t_buf->write_wait);
1443 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1444 //ust// spin_lock_init(<t_buf->full_lock);
1446 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1447 //ust// /* atomic_add made on local variable on data that belongs to
1448 //ust// * various CPUs : ok because tracing not started (for this cpu). */
1449 //ust// local_add(ltt_subbuffer_header_size(), <t_buf->commit_count[0]);
1451 //ust// local_set(<t_buf->events_lost, 0);
1452 //ust// local_set(<t_buf->corrupted_subbuffers, 0);
1457 static int ltt_relay_create_buffer(struct ltt_trace_struct
*trace
,
1458 struct ltt_channel_struct
*ltt_chan
, struct rchan_buf
*buf
,
1459 unsigned int n_subbufs
)
1461 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1466 //ust// ltt_buf->commit_count =
1467 //ust// zmalloc(sizeof(ltt_buf->commit_count) * n_subbufs);
1468 //ust// if (!ltt_buf->commit_count)
1469 //ust// return -ENOMEM;
1470 kref_get(&trace
->kref
);
1471 kref_get(&trace
->ltt_transport_kref
);
1472 kref_get(<t_chan
->kref
);
1473 local_set(<t_buf
->offset
, ltt_subbuffer_header_size());
1474 atomic_long_set(<t_buf
->consumed
, 0);
1475 atomic_long_set(<t_buf
->active_readers
, 0);
1476 for (j
= 0; j
< n_subbufs
; j
++)
1477 local_set(<t_buf
->commit_count
[j
], 0);
1478 //ust// init_waitqueue_head(<t_buf->write_wait);
1479 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1480 //ust// spin_lock_init(<t_buf->full_lock);
1482 ltt_buffer_begin_callback(buf
, trace
->start_tsc
, 0);
1484 local_add(ltt_subbuffer_header_size(), <t_buf
->commit_count
[0]);
1486 local_set(<t_buf
->events_lost
, 0);
1487 local_set(<t_buf
->corrupted_subbuffers
, 0);
1494 ltt_buf
->data_ready_fd_read
= fds
[0];
1495 ltt_buf
->data_ready_fd_write
= fds
[1];
1500 static void ltt_relay_destroy_buffer(struct ltt_channel_struct
*ltt_chan
)
1502 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
1503 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1505 kref_put(<t_chan
->trace
->ltt_transport_kref
,
1506 ltt_release_transport
);
1507 ltt_relay_print_buffer_errors(ltt_chan
);
1508 //ust// kfree(ltt_buf->commit_count);
1509 //ust// ltt_buf->commit_count = NULL;
1510 kref_put(<t_chan
->kref
, ltt_relay_release_channel
);
1511 kref_put(&trace
->kref
, ltt_release_trace
);
1512 //ust// wake_up_interruptible(&trace->kref_wq);
1515 static void ltt_chan_alloc_ltt_buf(struct ltt_channel_struct
*ltt_chan
)
1521 /* FIXME: increase size if we have a commit_count array that overflows the page */
1522 size_t size
= PAGE_ALIGN(1);
1524 result
= ltt_chan
->buf_shmid
= shmget(getpid(), size
, IPC_CREAT
| IPC_EXCL
| 0700);
1525 if(ltt_chan
->buf_shmid
== -1) {
1530 ptr
= shmat(ltt_chan
->buf_shmid
, NULL
, 0);
1531 if(ptr
== (void *) -1) {
1536 /* Already mark the shared memory for destruction. This will occur only
1537 * when all users have detached.
1539 result
= shmctl(ltt_chan
->buf_shmid
, IPC_RMID
, NULL
);
1545 ltt_chan
->buf
= ptr
;
1550 result
= shmctl(ltt_chan
->buf_shmid
, IPC_RMID
, NULL
);
1561 static int ltt_relay_create_channel(const char *trace_name
,
1562 struct ltt_trace_struct
*trace
, struct dentry
*dir
,
1563 const char *channel_name
, struct ltt_channel_struct
*ltt_chan
,
1564 unsigned int subbuf_size
, unsigned int n_subbufs
,
1568 unsigned int tmpname_len
;
1571 tmpname
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1575 strncpy(tmpname
, LTT_FLIGHT_PREFIX
, PATH_MAX
-1);
1576 strncat(tmpname
, channel_name
,
1577 PATH_MAX
-1-sizeof(LTT_FLIGHT_PREFIX
));
1579 strncpy(tmpname
, channel_name
, PATH_MAX
-1);
1581 strncat(tmpname
, "_", PATH_MAX
-1-strlen(tmpname
));
1583 kref_init(<t_chan
->kref
);
1585 ltt_chan
->trace
= trace
;
1586 ltt_chan
->buffer_begin
= ltt_buffer_begin_callback
;
1587 ltt_chan
->buffer_end
= ltt_buffer_end_callback
;
1588 ltt_chan
->overwrite
= overwrite
;
1589 ltt_chan
->n_subbufs_order
= get_count_order(n_subbufs
);
1590 ltt_chan
->commit_count_mask
= (~0UL >> ltt_chan
->n_subbufs_order
);
1591 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
1593 ltt_chan_alloc_ltt_buf(ltt_chan
);
1595 //ust// ltt_chan->buf = malloc(sizeof(struct ltt_channel_buf_struct));
1598 ltt_chan
->trans_channel_data
= ltt_relay_open(tmpname
,
1603 tmpname_len
= strlen(tmpname
);
1604 if (tmpname_len
> 0) {
1605 /* Remove final _ for pretty printing */
1606 tmpname
[tmpname_len
-1] = '\0';
1608 if (ltt_chan
->trans_channel_data
== NULL
) {
1609 printk(KERN_ERR
"LTT : Can't open %s channel for trace %s\n",
1610 tmpname
, trace_name
);
1611 goto relay_open_error
;
1618 //ust// percpu_free(ltt_chan->buf);
1626 static int ltt_relay_create_dirs(struct ltt_trace_struct
*new_trace
)
1628 //ust// new_trace->dentry.trace_root = debugfs_create_dir(new_trace->trace_name,
1629 //ust// get_ltt_root());
1630 //ust// if (new_trace->dentry.trace_root == NULL) {
1631 //ust// printk(KERN_ERR "LTT : Trace directory name %s already taken\n",
1632 //ust// new_trace->trace_name);
1633 //ust// return EEXIST;
1636 //ust// new_trace->callbacks.create_buf_file = ltt_create_buf_file_callback;
1637 //ust// new_trace->callbacks.remove_buf_file = ltt_remove_buf_file_callback;
1643 * LTTng channel flush function.
1645 * Must be called when no tracing is active in the channel, because of
1646 * accesses across CPUs.
1648 static notrace
void ltt_relay_buffer_flush(struct rchan_buf
*buf
)
1650 struct ltt_channel_struct
*channel
=
1651 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
1652 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
1656 ltt_force_switch(buf
, FORCE_FLUSH
);
1658 result
= write(ltt_buf
->data_ready_fd_write
, "1", 1);
1660 PERROR("write (in ltt_relay_buffer_flush)");
1661 ERR("this should never happen!");
1665 static void ltt_relay_async_wakeup_chan(struct ltt_channel_struct
*ltt_channel
)
1667 //ust// unsigned int i;
1668 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
1670 //ust// for_each_possible_cpu(i) {
1671 //ust// struct ltt_channel_buf_struct *ltt_buf =
1672 //ust// percpu_ptr(ltt_channel->buf, i);
1674 //ust// if (atomic_read(<t_buf->wakeup_readers) == 1) {
1675 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1676 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
1681 static void ltt_relay_finish_buffer(struct ltt_channel_struct
*ltt_channel
)
1683 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
1687 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
1688 ltt_relay_buffer_flush(rchan
->buf
);
1689 //ust// ltt_relay_wake_writers(ltt_buf);
1690 /* closing the pipe tells the consumer the buffer is finished */
1692 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
1693 //if(result == -1) {
1694 // PERROR("write (in ltt_relay_finish_buffer)");
1695 // ERR("this should never happen!");
1697 close(ltt_buf
->data_ready_fd_write
);
1702 static void ltt_relay_finish_channel(struct ltt_channel_struct
*ltt_channel
)
1706 //ust// for_each_possible_cpu(i)
1707 ltt_relay_finish_buffer(ltt_channel
);
1710 static void ltt_relay_remove_channel(struct ltt_channel_struct
*channel
)
1712 struct rchan
*rchan
= channel
->trans_channel_data
;
1714 ltt_relay_close(rchan
);
1715 kref_put(&channel
->kref
, ltt_relay_release_channel
);
1718 struct ltt_reserve_switch_offsets
{
1719 long begin
, end
, old
;
1720 long begin_switch
, end_switch_current
, end_switch_old
;
1721 long commit_count
, reserve_commit_diff
;
1722 size_t before_hdr_pad
, size
;
1728 * !0 if execution must be aborted.
1730 static inline int ltt_relay_try_reserve(
1731 struct ltt_channel_struct
*ltt_channel
,
1732 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1733 struct rchan_buf
*buf
,
1734 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
1735 u64
*tsc
, unsigned int *rflags
, int largest_align
)
1737 offsets
->begin
= local_read(<t_buf
->offset
);
1738 offsets
->old
= offsets
->begin
;
1739 offsets
->begin_switch
= 0;
1740 offsets
->end_switch_current
= 0;
1741 offsets
->end_switch_old
= 0;
1743 *tsc
= trace_clock_read64();
1744 if (last_tsc_overflow(ltt_buf
, *tsc
))
1745 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
1747 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0) {
1748 offsets
->begin_switch
= 1; /* For offsets->begin */
1750 offsets
->size
= ltt_get_header_size(ltt_channel
,
1751 offsets
->begin
, data_size
,
1752 &offsets
->before_hdr_pad
, *rflags
);
1753 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1756 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
1757 > buf
->chan
->subbuf_size
) {
1758 offsets
->end_switch_old
= 1; /* For offsets->old */
1759 offsets
->begin_switch
= 1; /* For offsets->begin */
1762 if (offsets
->begin_switch
) {
1765 if (offsets
->end_switch_old
)
1766 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
1768 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
1769 /* Test new buffer integrity */
1770 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1771 offsets
->reserve_commit_diff
=
1772 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1773 >> ltt_channel
->n_subbufs_order
)
1774 - (local_read(<t_buf
->commit_count
[subbuf_index
])
1775 & ltt_channel
->commit_count_mask
);
1776 if (offsets
->reserve_commit_diff
== 0) {
1777 /* Next buffer not corrupted. */
1778 if (!ltt_channel
->overwrite
&&
1779 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
1780 - SUBBUF_TRUNC(atomic_long_read(
1781 <t_buf
->consumed
),
1783 >= rchan
->alloc_size
) {
1785 * We do not overwrite non consumed buffers
1786 * and we are full : event is lost.
1788 local_inc(<t_buf
->events_lost
);
1792 * next buffer not corrupted, we are either in
1793 * overwrite mode or the buffer is not full.
1794 * It's safe to write in this new subbuffer.
1799 * Next subbuffer corrupted. Force pushing reader even
1800 * in normal mode. It's safe to write in this new
1804 offsets
->size
= ltt_get_header_size(ltt_channel
,
1805 offsets
->begin
, data_size
,
1806 &offsets
->before_hdr_pad
, *rflags
);
1807 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1810 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
1811 > buf
->chan
->subbuf_size
) {
1813 * Event too big for subbuffers, report error, don't
1814 * complete the sub-buffer switch.
1816 local_inc(<t_buf
->events_lost
);
1820 * We just made a successful buffer switch and the event
1821 * fits in the new subbuffer. Let's write.
1826 * Event fits in the current buffer and we are not on a switch
1827 * boundary. It's safe to write.
1830 offsets
->end
= offsets
->begin
+ offsets
->size
;
1832 if ((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0) {
1834 * The offset_end will fall at the very beginning of the next
1837 offsets
->end_switch_current
= 1; /* For offsets->begin */
1845 * !0 if execution must be aborted.
1847 static inline int ltt_relay_try_switch(
1848 enum force_switch_mode mode
,
1849 struct ltt_channel_struct
*ltt_channel
,
1850 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1851 struct rchan_buf
*buf
,
1852 struct ltt_reserve_switch_offsets
*offsets
,
1857 offsets
->begin
= local_read(<t_buf
->offset
);
1858 offsets
->old
= offsets
->begin
;
1859 offsets
->begin_switch
= 0;
1860 offsets
->end_switch_old
= 0;
1862 *tsc
= trace_clock_read64();
1864 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
1865 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
1866 offsets
->end_switch_old
= 1;
1868 /* we do not have to switch : buffer is empty */
1871 if (mode
== FORCE_ACTIVE
)
1872 offsets
->begin
+= ltt_subbuffer_header_size();
1874 * Always begin_switch in FORCE_ACTIVE mode.
1875 * Test new buffer integrity
1877 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1878 offsets
->reserve_commit_diff
=
1879 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1880 >> ltt_channel
->n_subbufs_order
)
1881 - (local_read(<t_buf
->commit_count
[subbuf_index
])
1882 & ltt_channel
->commit_count_mask
);
1883 if (offsets
->reserve_commit_diff
== 0) {
1884 /* Next buffer not corrupted. */
1885 if (mode
== FORCE_ACTIVE
1886 && !ltt_channel
->overwrite
1887 && offsets
->begin
- atomic_long_read(<t_buf
->consumed
)
1888 >= rchan
->alloc_size
) {
1890 * We do not overwrite non consumed buffers and we are
1891 * full : ignore switch while tracing is active.
1897 * Next subbuffer corrupted. Force pushing reader even in normal
1901 offsets
->end
= offsets
->begin
;
1905 static inline void ltt_reserve_push_reader(
1906 struct ltt_channel_struct
*ltt_channel
,
1907 struct ltt_channel_buf_struct
*ltt_buf
,
1908 struct rchan
*rchan
,
1909 struct rchan_buf
*buf
,
1910 struct ltt_reserve_switch_offsets
*offsets
)
1912 long consumed_old
, consumed_new
;
1915 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1917 * If buffer is in overwrite mode, push the reader consumed
1918 * count if the write position has reached it and we are not
1919 * at the first iteration (don't push the reader farther than
1920 * the writer). This operation can be done concurrently by many
1921 * writers in the same buffer, the writer being at the farthest
1922 * write position sub-buffer index in the buffer being the one
1923 * which will win this loop.
1924 * If the buffer is not in overwrite mode, pushing the reader
1925 * only happens if a sub-buffer is corrupted.
1927 if ((SUBBUF_TRUNC(offsets
->end
-1, buf
->chan
)
1928 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1929 >= rchan
->alloc_size
)
1930 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1932 consumed_new
= consumed_old
;
1935 } while (atomic_long_cmpxchg(<t_buf
->consumed
, consumed_old
,
1936 consumed_new
) != consumed_old
);
1938 if (consumed_old
!= consumed_new
) {
1940 * Reader pushed : we are the winner of the push, we can
1941 * therefore reequilibrate reserve and commit. Atomic increment
1942 * of the commit count permits other writers to play around
1943 * with this variable before us. We keep track of
1944 * corrupted_subbuffers even in overwrite mode :
1945 * we never want to write over a non completely committed
1946 * sub-buffer : possible causes : the buffer size is too low
1947 * compared to the unordered data input, or there is a writer
1948 * that died between the reserve and the commit.
1950 if (offsets
->reserve_commit_diff
) {
1952 * We have to alter the sub-buffer commit count.
1953 * We do not deliver the previous subbuffer, given it
1954 * was either corrupted or not consumed (overwrite
1957 local_add(offsets
->reserve_commit_diff
,
1958 <t_buf
->commit_count
[
1959 SUBBUF_INDEX(offsets
->begin
,
1961 if (!ltt_channel
->overwrite
1962 || offsets
->reserve_commit_diff
1963 != rchan
->subbuf_size
) {
1965 * The reserve commit diff was not subbuf_size :
1966 * it means the subbuffer was partly written to
1967 * and is therefore corrupted. If it is multiple
1968 * of subbuffer size and we are in flight
1969 * recorder mode, we are skipping over a whole
1972 local_inc(<t_buf
->corrupted_subbuffers
);
1980 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1982 * Concurrency safe because we are the last and only thread to alter this
1983 * sub-buffer. As long as it is not delivered and read, no other thread can
1984 * alter the offset, alter the reserve_count or call the
1985 * client_buffer_end_callback on this sub-buffer.
1987 * The only remaining threads could be the ones with pending commits. They will
1988 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1989 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1990 * corrupted sub-buffers count and push the readers across these sub-buffers.
1992 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1993 * switches in, finding out it's corrupted. The result will be than the old
1994 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1995 * will be declared corrupted too because of the commit count adjustment.
1997 * Note : offset_old should never be 0 here.
1999 static inline void ltt_reserve_switch_old_subbuf(
2000 struct ltt_channel_struct
*ltt_channel
,
2001 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
2002 struct rchan_buf
*buf
,
2003 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
2005 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, rchan
);
2007 ltt_channel
->buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
2008 /* Must write buffer end before incrementing commit count */
2010 offsets
->commit_count
=
2011 local_add_return(rchan
->subbuf_size
2012 - (SUBBUF_OFFSET(offsets
->old
- 1, rchan
)
2014 <t_buf
->commit_count
[oldidx
]);
2015 if ((BUFFER_TRUNC(offsets
->old
- 1, rchan
)
2016 >> ltt_channel
->n_subbufs_order
)
2017 - ((offsets
->commit_count
- rchan
->subbuf_size
)
2018 & ltt_channel
->commit_count_mask
) == 0)
2019 ltt_deliver(buf
, oldidx
, NULL
);
2023 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
2025 * This code can be executed unordered : writers may already have written to the
2026 * sub-buffer before this code gets executed, caution. The commit makes sure
2027 * that this code is executed before the deliver of this sub-buffer.
2029 static /*inline*/ void ltt_reserve_switch_new_subbuf(
2030 struct ltt_channel_struct
*ltt_channel
,
2031 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
2032 struct rchan_buf
*buf
,
2033 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
2035 long beginidx
= SUBBUF_INDEX(offsets
->begin
, rchan
);
2037 ltt_channel
->buffer_begin(buf
, *tsc
, beginidx
);
2038 /* Must write buffer end before incrementing commit count */
2040 offsets
->commit_count
= local_add_return(ltt_subbuffer_header_size(),
2041 <t_buf
->commit_count
[beginidx
]);
2042 /* Check if the written buffer has to be delivered */
2043 if ((BUFFER_TRUNC(offsets
->begin
, rchan
)
2044 >> ltt_channel
->n_subbufs_order
)
2045 - ((offsets
->commit_count
- rchan
->subbuf_size
)
2046 & ltt_channel
->commit_count_mask
) == 0)
2047 ltt_deliver(buf
, beginidx
, NULL
);
2052 * ltt_reserve_end_switch_current: finish switching current subbuffer
2054 * Concurrency safe because we are the last and only thread to alter this
2055 * sub-buffer. As long as it is not delivered and read, no other thread can
2056 * alter the offset, alter the reserve_count or call the
2057 * client_buffer_end_callback on this sub-buffer.
2059 * The only remaining threads could be the ones with pending commits. They will
2060 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
2061 * We detect corrupted subbuffers with commit and reserve counts. We keep a
2062 * corrupted sub-buffers count and push the readers across these sub-buffers.
2064 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
2065 * switches in, finding out it's corrupted. The result will be than the old
2066 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
2067 * will be declared corrupted too because of the commit count adjustment.
2069 static inline void ltt_reserve_end_switch_current(
2070 struct ltt_channel_struct
*ltt_channel
,
2071 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
2072 struct rchan_buf
*buf
,
2073 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
2075 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, rchan
);
2077 ltt_channel
->buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
2078 /* Must write buffer begin before incrementing commit count */
2080 offsets
->commit_count
=
2081 local_add_return(rchan
->subbuf_size
2082 - (SUBBUF_OFFSET(offsets
->end
- 1, rchan
)
2084 <t_buf
->commit_count
[endidx
]);
2085 if ((BUFFER_TRUNC(offsets
->end
- 1, rchan
)
2086 >> ltt_channel
->n_subbufs_order
)
2087 - ((offsets
->commit_count
- rchan
->subbuf_size
)
2088 & ltt_channel
->commit_count_mask
) == 0)
2089 ltt_deliver(buf
, endidx
, NULL
);
2093 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
2094 * @trace: the trace structure to log to.
2095 * @ltt_channel: channel structure
2096 * @transport_data: data structure specific to ltt relay
2097 * @data_size: size of the variable length data to log.
2098 * @slot_size: pointer to total size of the slot (out)
2099 * @buf_offset : pointer to reserved buffer offset (out)
2100 * @tsc: pointer to the tsc at the slot reservation (out)
2103 * Return : -ENOSPC if not enough space, else returns 0.
2104 * It will take care of sub-buffer switching.
2106 static notrace
int ltt_relay_reserve_slot(struct ltt_trace_struct
*trace
,
2107 struct ltt_channel_struct
*ltt_channel
, void **transport_data
,
2108 size_t data_size
, size_t *slot_size
, long *buf_offset
, u64
*tsc
,
2109 unsigned int *rflags
, int largest_align
)
2111 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
2112 struct rchan_buf
*buf
= *transport_data
= rchan
->buf
;
2113 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2114 struct ltt_reserve_switch_offsets offsets
;
2116 offsets
.reserve_commit_diff
= 0;
2120 * Perform retryable operations.
2122 if (ltt_nesting
> 4) {
2123 local_inc(<t_buf
->events_lost
);
2127 if (ltt_relay_try_reserve(ltt_channel
, ltt_buf
,
2128 rchan
, buf
, &offsets
, data_size
, tsc
, rflags
,
2131 } while (local_cmpxchg(<t_buf
->offset
, offsets
.old
,
2132 offsets
.end
) != offsets
.old
);
2135 * Atomically update last_tsc. This update races against concurrent
2136 * atomic updates, but the race will always cause supplementary full TSC
2137 * events, never the opposite (missing a full TSC event when it would be
2140 save_last_tsc(ltt_buf
, *tsc
);
2143 * Push the reader if necessary
2145 ltt_reserve_push_reader(ltt_channel
, ltt_buf
, rchan
, buf
, &offsets
);
2148 * Switch old subbuffer if needed.
2150 if (offsets
.end_switch_old
)
2151 ltt_reserve_switch_old_subbuf(ltt_channel
, ltt_buf
, rchan
, buf
,
2155 * Populate new subbuffer.
2157 if (offsets
.begin_switch
)
2158 ltt_reserve_switch_new_subbuf(ltt_channel
, ltt_buf
, rchan
,
2159 buf
, &offsets
, tsc
);
2161 if (offsets
.end_switch_current
)
2162 ltt_reserve_end_switch_current(ltt_channel
, ltt_buf
, rchan
,
2163 buf
, &offsets
, tsc
);
2165 *slot_size
= offsets
.size
;
2166 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
2171 * Force a sub-buffer switch for a per-cpu buffer. This operation is
2172 * completely reentrant : can be called while tracing is active with
2173 * absolutely no lock held.
2175 * Note, however, that as a local_cmpxchg is used for some atomic
2176 * operations, this function must be called from the CPU which owns the buffer
2177 * for a ACTIVE flush.
2179 static notrace
void ltt_force_switch(struct rchan_buf
*buf
,
2180 enum force_switch_mode mode
)
2182 struct ltt_channel_struct
*ltt_channel
=
2183 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
2184 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2185 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
2186 struct ltt_reserve_switch_offsets offsets
;
2189 offsets
.reserve_commit_diff
= 0;
2193 * Perform retryable operations.
2196 if (ltt_relay_try_switch(mode
, ltt_channel
, ltt_buf
,
2197 rchan
, buf
, &offsets
, &tsc
))
2199 } while (local_cmpxchg(<t_buf
->offset
, offsets
.old
,
2200 offsets
.end
) != offsets
.old
);
2203 * Atomically update last_tsc. This update races against concurrent
2204 * atomic updates, but the race will always cause supplementary full TSC
2205 * events, never the opposite (missing a full TSC event when it would be
2208 save_last_tsc(ltt_buf
, tsc
);
2211 * Push the reader if necessary
2213 if (mode
== FORCE_ACTIVE
)
2214 ltt_reserve_push_reader(ltt_channel
, ltt_buf
, rchan
,
2218 * Switch old subbuffer if needed.
2220 if (offsets
.end_switch_old
)
2221 ltt_reserve_switch_old_subbuf(ltt_channel
, ltt_buf
, rchan
, buf
,
2225 * Populate new subbuffer.
2227 if (mode
== FORCE_ACTIVE
)
2228 ltt_reserve_switch_new_subbuf(ltt_channel
,
2229 ltt_buf
, rchan
, buf
, &offsets
, &tsc
);
2233 * for flight recording. must be called after relay_commit.
2234 * This function decrements de subbuffer's lost_size each time the commit count
2235 * reaches back the reserve offset (module subbuffer size). It is useful for
2237 * We use slot_size - 1 to make sure we deal correctly with the case where we
2238 * fill the subbuffer completely (so the subbuf index stays in the previous
2241 //ust// #ifdef CONFIG_LTT_VMCORE
2242 static /*inline*/ void ltt_write_commit_counter(struct rchan_buf
*buf
,
2243 long buf_offset
, size_t slot_size
)
2245 struct ltt_channel_struct
*ltt_channel
=
2246 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
2247 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2248 struct ltt_subbuffer_header
*header
;
2249 long offset
, subbuf_idx
, commit_count
;
2250 uint32_t lost_old
, lost_new
;
2252 subbuf_idx
= SUBBUF_INDEX(buf_offset
- 1, buf
->chan
);
2253 offset
= buf_offset
+ slot_size
;
2254 header
= (struct ltt_subbuffer_header
*)
2255 ltt_relay_offset_address(buf
,
2256 subbuf_idx
* buf
->chan
->subbuf_size
);
2258 lost_old
= header
->lost_size
;
2260 local_read(<t_buf
->commit_count
[subbuf_idx
]);
2261 /* SUBBUF_OFFSET includes commit_count_mask */
2262 if (!SUBBUF_OFFSET(offset
- commit_count
, buf
->chan
)) {
2263 lost_new
= (uint32_t)buf
->chan
->subbuf_size
2264 - SUBBUF_OFFSET(commit_count
, buf
->chan
);
2265 lost_old
= cmpxchg_local(&header
->lost_size
, lost_old
,
2267 if (lost_old
<= lost_new
)
2275 //ust// static inline void ltt_write_commit_counter(struct rchan_buf *buf,
2276 //ust// long buf_offset, size_t slot_size)
2282 * Atomic unordered slot commit. Increments the commit count in the
2283 * specified sub-buffer, and delivers it if necessary.
2287 * @ltt_channel : channel structure
2288 * @transport_data: transport-specific data
2289 * @buf_offset : offset following the event header.
2290 * @slot_size : size of the reserved slot.
2292 static notrace
void ltt_relay_commit_slot(
2293 struct ltt_channel_struct
*ltt_channel
,
2294 void **transport_data
, long buf_offset
, size_t slot_size
)
2296 struct rchan_buf
*buf
= *transport_data
;
2297 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2298 struct rchan
*rchan
= buf
->chan
;
2299 long offset_end
= buf_offset
;
2300 long endidx
= SUBBUF_INDEX(offset_end
- 1, rchan
);
2303 /* Must write slot data before incrementing commit count */
2305 commit_count
= local_add_return(slot_size
,
2306 <t_buf
->commit_count
[endidx
]);
2307 /* Check if all commits have been done */
2308 if ((BUFFER_TRUNC(offset_end
- 1, rchan
)
2309 >> ltt_channel
->n_subbufs_order
)
2310 - ((commit_count
- rchan
->subbuf_size
)
2311 & ltt_channel
->commit_count_mask
) == 0)
2312 ltt_deliver(buf
, endidx
, NULL
);
2314 * Update lost_size for each commit. It's needed only for extracting
2315 * ltt buffers from vmcore, after crash.
2317 ltt_write_commit_counter(buf
, buf_offset
, slot_size
);
2319 DBG("commited slot. now commit count is %ld", commit_count
);
2323 * This is called with preemption disabled when user space has requested
2324 * blocking mode. If one of the active traces has free space below a
2325 * specific threshold value, we reenable preemption and block.
2327 static int ltt_relay_user_blocking(struct ltt_trace_struct
*trace
,
2328 unsigned int chan_index
, size_t data_size
,
2329 struct user_dbg_data
*dbg
)
2331 //ust// struct rchan *rchan;
2332 //ust// struct ltt_channel_buf_struct *ltt_buf;
2333 //ust// struct ltt_channel_struct *channel;
2334 //ust// struct rchan_buf *relay_buf;
2336 //ust// DECLARE_WAITQUEUE(wait, current);
2338 //ust// channel = &trace->channels[chan_index];
2339 //ust// rchan = channel->trans_channel_data;
2340 //ust// cpu = smp_processor_id();
2341 //ust// relay_buf = rchan->buf[cpu];
2342 //ust// ltt_buf = percpu_ptr(channel->buf, cpu);
2345 //ust// * Check if data is too big for the channel : do not
2346 //ust// * block for it.
2348 //ust// if (LTT_RESERVE_CRITICAL + data_size > relay_buf->chan->subbuf_size)
2352 //ust// * If free space too low, we block. We restart from the
2353 //ust// * beginning after we resume (cpu id may have changed
2354 //ust// * while preemption is active).
2356 //ust// spin_lock(<t_buf->full_lock);
2357 //ust// if (!channel->overwrite) {
2358 //ust// dbg->write = local_read(<t_buf->offset);
2359 //ust// dbg->read = atomic_long_read(<t_buf->consumed);
2360 //ust// dbg->avail_size = dbg->write + LTT_RESERVE_CRITICAL + data_size
2361 //ust// - SUBBUF_TRUNC(dbg->read,
2362 //ust// relay_buf->chan);
2363 //ust// if (dbg->avail_size > rchan->alloc_size) {
2364 //ust// __set_current_state(TASK_INTERRUPTIBLE);
2365 //ust// add_wait_queue(<t_buf->write_wait, &wait);
2366 //ust// spin_unlock(<t_buf->full_lock);
2367 //ust// preempt_enable();
2369 //ust// __set_current_state(TASK_RUNNING);
2370 //ust// remove_wait_queue(<t_buf->write_wait, &wait);
2371 //ust// if (signal_pending(current))
2372 //ust// return -ERESTARTSYS;
2373 //ust// preempt_disable();
2377 //ust// spin_unlock(<t_buf->full_lock);
2381 static void ltt_relay_print_user_errors(struct ltt_trace_struct
*trace
,
2382 unsigned int chan_index
, size_t data_size
,
2383 struct user_dbg_data
*dbg
)
2385 struct rchan
*rchan
;
2386 struct ltt_channel_buf_struct
*ltt_buf
;
2387 struct ltt_channel_struct
*channel
;
2388 struct rchan_buf
*relay_buf
;
2390 channel
= &trace
->channels
[chan_index
];
2391 rchan
= channel
->trans_channel_data
;
2392 relay_buf
= rchan
->buf
;
2393 ltt_buf
= channel
->buf
;
2395 printk(KERN_ERR
"Error in LTT usertrace : "
2396 "buffer full : event lost in blocking "
2397 "mode. Increase LTT_RESERVE_CRITICAL.\n");
2398 printk(KERN_ERR
"LTT nesting level is %u.\n", ltt_nesting
);
2399 printk(KERN_ERR
"LTT avail size %lu.\n",
2401 printk(KERN_ERR
"avai write : %lu, read : %lu\n",
2402 dbg
->write
, dbg
->read
);
2404 dbg
->write
= local_read(<t_buf
->offset
);
2405 dbg
->read
= atomic_long_read(<t_buf
->consumed
);
2407 printk(KERN_ERR
"LTT cur size %lu.\n",
2408 dbg
->write
+ LTT_RESERVE_CRITICAL
+ data_size
2409 - SUBBUF_TRUNC(dbg
->read
, relay_buf
->chan
));
2410 printk(KERN_ERR
"cur write : %lu, read : %lu\n",
2411 dbg
->write
, dbg
->read
);
2414 //ust// static struct ltt_transport ltt_relay_transport = {
2415 //ust// .name = "relay",
2416 //ust// .owner = THIS_MODULE,
2418 //ust// .create_dirs = ltt_relay_create_dirs,
2419 //ust// .remove_dirs = ltt_relay_remove_dirs,
2420 //ust// .create_channel = ltt_relay_create_channel,
2421 //ust// .finish_channel = ltt_relay_finish_channel,
2422 //ust// .remove_channel = ltt_relay_remove_channel,
2423 //ust// .wakeup_channel = ltt_relay_async_wakeup_chan,
2424 //ust// .commit_slot = ltt_relay_commit_slot,
2425 //ust// .reserve_slot = ltt_relay_reserve_slot,
2426 //ust// .user_blocking = ltt_relay_user_blocking,
2427 //ust// .user_errors = ltt_relay_print_user_errors,
2431 static struct ltt_transport ust_relay_transport
= {
2433 //ust// .owner = THIS_MODULE,
2435 .create_dirs
= ltt_relay_create_dirs
,
2436 .remove_dirs
= ltt_relay_remove_dirs
,
2437 .create_channel
= ltt_relay_create_channel
,
2438 .finish_channel
= ltt_relay_finish_channel
,
2439 .remove_channel
= ltt_relay_remove_channel
,
2440 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
2441 .commit_slot
= ltt_relay_commit_slot
,
2442 .reserve_slot
= ltt_relay_reserve_slot
,
2443 .user_blocking
= ltt_relay_user_blocking
,
2444 .user_errors
= ltt_relay_print_user_errors
,
2448 //ust// static int __init ltt_relay_init(void)
2450 //ust// printk(KERN_INFO "LTT : ltt-relay init\n");
2452 //ust// ltt_file_operations = ltt_relay_file_operations;
2453 //ust// ltt_file_operations.owner = THIS_MODULE;
2454 //ust// ltt_file_operations.open = ltt_open;
2455 //ust// ltt_file_operations.release = ltt_release;
2456 //ust// ltt_file_operations.poll = ltt_poll;
2457 //ust// ltt_file_operations.splice_read = ltt_relay_file_splice_read,
2458 //ust// ltt_file_operations.ioctl = ltt_ioctl;
2459 //ust//#ifdef CONFIG_COMPAT
2460 //ust// ltt_file_operations.compat_ioctl = ltt_compat_ioctl;
2463 //ust// ltt_transport_register(<t_relay_transport);
2468 static char initialized
= 0;
2470 void __attribute__((constructor
)) init_ustrelay_transport(void)
2473 ltt_transport_register(&ust_relay_transport
);
2478 static void __exit
ltt_relay_exit(void)
2480 //ust// printk(KERN_INFO "LTT : ltt-relay exit\n");
2482 ltt_transport_unregister(&ust_relay_transport
);
2485 //ust// module_init(ltt_relay_init);
2486 //ust// module_exit(ltt_relay_exit);
2488 //ust// MODULE_LICENSE("GPL");
2489 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
2490 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Lockless Relay");