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>
26 #include "kernelcompat.h"
32 #include "tracercore.h"
35 /* list of open channels, for cpu hotplug */
36 static DEFINE_MUTEX(relay_channels_mutex
);
37 static LIST_HEAD(relay_channels
);
40 static struct dentry
*ltt_create_buf_file_callback(struct rchan_buf
*buf
);
43 * relay_alloc_buf - allocate a channel buffer
44 * @buf: the buffer struct
45 * @size: total size of the buffer
47 //ust// static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
49 //ust// unsigned int i, n_pages;
50 //ust// struct buf_page *buf_page, *n;
52 //ust// *size = PAGE_ALIGN(*size);
53 //ust// n_pages = *size >> PAGE_SHIFT;
55 //ust// INIT_LIST_HEAD(&buf->pages);
57 //ust// for (i = 0; i < n_pages; i++) {
58 //ust// buf_page = kmalloc_node(sizeof(*buf_page), GFP_KERNEL,
59 //ust// cpu_to_node(buf->cpu));
60 //ust// if (unlikely(!buf_page))
61 //ust// goto depopulate;
62 //ust// buf_page->page = alloc_pages_node(cpu_to_node(buf->cpu),
63 //ust// GFP_KERNEL | __GFP_ZERO, 0);
64 //ust// if (unlikely(!buf_page->page)) {
65 //ust// kfree(buf_page);
66 //ust// goto depopulate;
68 //ust// list_add_tail(&buf_page->list, &buf->pages);
69 //ust// buf_page->offset = (size_t)i << PAGE_SHIFT;
70 //ust// buf_page->buf = buf;
71 //ust// set_page_private(buf_page->page, (unsigned long)buf_page);
73 //ust// buf->wpage = buf_page;
74 //ust// buf->hpage[0] = buf_page;
75 //ust// buf->hpage[1] = buf_page;
76 //ust// buf->rpage = buf_page;
79 //ust// buf->page_count = n_pages;
83 //ust// list_for_each_entry_safe(buf_page, n, &buf->pages, list) {
84 //ust// list_del_init(&buf_page->list);
85 //ust// __free_page(buf_page->page);
86 //ust// kfree(buf_page);
88 //ust// return -ENOMEM;
91 static int relay_alloc_buf(struct rchan_buf
*buf
, size_t *size
)
94 struct buf_page
*buf_page
, *n
;
98 *size
= PAGE_ALIGN(*size
);
100 /* Maybe do read-ahead */
101 result
= mmap(NULL
, *size
, PROT_READ
| PROT_WRITE
, MAP_ANONYMOUS
| MAP_PRIVATE
, -1, 0);
102 if(result
== MAP_FAILED
) {
107 buf
->buf_data
= result
;
108 buf
->buf_size
= *size
;
114 * relay_create_buf - allocate and initialize a channel buffer
115 * @chan: the relay channel
116 * @cpu: cpu the buffer belongs to
118 * Returns channel buffer if successful, %NULL otherwise.
120 static struct rchan_buf
*relay_create_buf(struct rchan
*chan
)
123 struct rchan_buf
*buf
= kzalloc(sizeof(struct rchan_buf
), GFP_KERNEL
);
128 ret
= relay_alloc_buf(buf
, &chan
->alloc_size
);
133 kref_get(&buf
->chan
->kref
);
142 * relay_destroy_channel - free the channel struct
143 * @kref: target kernel reference that contains the relay channel
145 * Should only be called from kref_put().
147 static void relay_destroy_channel(struct kref
*kref
)
149 struct rchan
*chan
= container_of(kref
, struct rchan
, kref
);
154 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
155 * @buf: the buffer struct
157 static void relay_destroy_buf(struct rchan_buf
*buf
)
159 struct rchan
*chan
= buf
->chan
;
160 struct buf_page
*buf_page
, *n
;
163 result
= munmap(buf
->buf_data
, buf
->buf_size
);
168 //ust// chan->buf[buf->cpu] = NULL;
170 kref_put(&chan
->kref
, relay_destroy_channel
);
174 * relay_remove_buf - remove a channel buffer
175 * @kref: target kernel reference that contains the relay buffer
177 * Removes the file from the fileystem, which also frees the
178 * rchan_buf_struct and the channel buffer. Should only be called from
181 static void relay_remove_buf(struct kref
*kref
)
183 struct rchan_buf
*buf
= container_of(kref
, struct rchan_buf
, kref
);
184 //ust// buf->chan->cb->remove_buf_file(buf);
185 relay_destroy_buf(buf
);
189 * High-level relay kernel API and associated functions.
193 * rchan_callback implementations defining default channel behavior. Used
194 * in place of corresponding NULL values in client callback struct.
198 * create_buf_file_create() default callback. Does nothing.
200 static struct dentry
*create_buf_file_default_callback(const char *filename
,
201 struct dentry
*parent
,
203 struct rchan_buf
*buf
)
209 * remove_buf_file() default callback. Does nothing.
211 static int remove_buf_file_default_callback(struct dentry
*dentry
)
217 * wakeup_readers - wake up readers waiting on a channel
218 * @data: contains the channel buffer
220 * This is the timer function used to defer reader waking.
222 //ust// static void wakeup_readers(unsigned long data)
224 //ust// struct rchan_buf *buf = (struct rchan_buf *)data;
225 //ust// wake_up_interruptible(&buf->read_wait);
229 * __relay_reset - reset a channel buffer
230 * @buf: the channel buffer
231 * @init: 1 if this is a first-time initialization
233 * See relay_reset() for description of effect.
235 static void __relay_reset(struct rchan_buf
*buf
, unsigned int init
)
238 //ust// init_waitqueue_head(&buf->read_wait);
239 kref_init(&buf
->kref
);
240 //ust// setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
242 //ust// del_timer_sync(&buf->timer);
248 * relay_open_buf - create a new relay channel buffer
250 * used by relay_open() and CPU hotplug.
252 static struct rchan_buf
*relay_open_buf(struct rchan
*chan
)
254 struct rchan_buf
*buf
= NULL
;
255 struct dentry
*dentry
;
256 //ust// char *tmpname;
258 //ust// tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
259 //ust// if (!tmpname)
261 //ust// snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
263 buf
= relay_create_buf(chan
);
267 __relay_reset(buf
, 1);
269 /* Create file in fs */
270 //ust// dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
273 ltt_create_buf_file_callback(buf
); // ust //
276 //ust// goto free_buf;
278 //ust// buf->dentry = dentry;
283 relay_destroy_buf(buf
);
286 //ust// kfree(tmpname);
292 * relay_close_buf - close a channel buffer
293 * @buf: channel buffer
295 * Marks the buffer finalized and restores the default callbacks.
296 * The channel buffer and channel buffer data structure are then freed
297 * automatically when the last reference is given up.
299 static void relay_close_buf(struct rchan_buf
*buf
)
301 //ust// del_timer_sync(&buf->timer);
302 kref_put(&buf
->kref
, relay_remove_buf
);
305 //ust// static void setup_callbacks(struct rchan *chan,
306 //ust// struct rchan_callbacks *cb)
309 //ust// chan->cb = &default_channel_callbacks;
313 //ust// if (!cb->create_buf_file)
314 //ust// cb->create_buf_file = create_buf_file_default_callback;
315 //ust// if (!cb->remove_buf_file)
316 //ust// cb->remove_buf_file = remove_buf_file_default_callback;
317 //ust// chan->cb = cb;
321 * relay_hotcpu_callback - CPU hotplug callback
322 * @nb: notifier block
323 * @action: hotplug action to take
326 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
328 //ust// static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
329 //ust// unsigned long action,
332 //ust// unsigned int hotcpu = (unsigned long)hcpu;
333 //ust// struct rchan *chan;
335 //ust// switch (action) {
336 //ust// case CPU_UP_PREPARE:
337 //ust// case CPU_UP_PREPARE_FROZEN:
338 //ust// mutex_lock(&relay_channels_mutex);
339 //ust// list_for_each_entry(chan, &relay_channels, list) {
340 //ust// if (chan->buf[hotcpu])
342 //ust// chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
343 //ust// if (!chan->buf[hotcpu]) {
344 //ust// printk(KERN_ERR
345 //ust// "relay_hotcpu_callback: cpu %d buffer "
346 //ust// "creation failed\n", hotcpu);
347 //ust// mutex_unlock(&relay_channels_mutex);
348 //ust// return NOTIFY_BAD;
351 //ust// mutex_unlock(&relay_channels_mutex);
353 //ust// case CPU_DEAD:
354 //ust// case CPU_DEAD_FROZEN:
355 //ust// /* No need to flush the cpu : will be flushed upon
356 //ust// * final relay_flush() call. */
359 //ust// return NOTIFY_OK;
363 * ltt_relay_open - create a new relay channel
364 * @base_filename: base name of files to create
365 * @parent: dentry of parent directory, %NULL for root directory
366 * @subbuf_size: size of sub-buffers
367 * @n_subbufs: number of sub-buffers
368 * @cb: client callback functions
369 * @private_data: user-defined data
371 * Returns channel pointer if successful, %NULL otherwise.
373 * Creates a channel buffer for each cpu using the sizes and
374 * attributes specified. The created channel buffer files
375 * will be named base_filename0...base_filenameN-1. File
376 * permissions will be %S_IRUSR.
378 struct rchan
*ltt_relay_open(const char *base_filename
,
379 struct dentry
*parent
,
386 //ust// if (!base_filename)
389 if (!(subbuf_size
&& n_subbufs
))
392 chan
= kzalloc(sizeof(struct rchan
), GFP_KERNEL
);
396 chan
->version
= LTT_RELAY_CHANNEL_VERSION
;
397 chan
->n_subbufs
= n_subbufs
;
398 chan
->subbuf_size
= subbuf_size
;
399 chan
->subbuf_size_order
= get_count_order(subbuf_size
);
400 chan
->alloc_size
= FIX_SIZE(subbuf_size
* n_subbufs
);
401 chan
->parent
= parent
;
402 chan
->private_data
= private_data
;
403 //ust// strlcpy(chan->base_filename, base_filename, NAME_MAX);
404 //ust// setup_callbacks(chan, cb);
405 kref_init(&chan
->kref
);
407 mutex_lock(&relay_channels_mutex
);
408 //ust// for_each_online_cpu(i) {
409 chan
->buf
= relay_open_buf(chan
);
413 list_add(&chan
->list
, &relay_channels
);
414 mutex_unlock(&relay_channels_mutex
);
419 //ust// for_each_possible_cpu(i) {
420 //ust// if (!chan->buf[i])
422 //ust// relay_close_buf(chan->buf[i]);
426 kref_put(&chan
->kref
, relay_destroy_channel
);
427 mutex_unlock(&relay_channels_mutex
);
430 //ust// EXPORT_SYMBOL_GPL(ltt_relay_open);
433 * ltt_relay_close - close the channel
436 * Closes all channel buffers and frees the channel.
438 void ltt_relay_close(struct rchan
*chan
)
445 mutex_lock(&relay_channels_mutex
);
446 //ust// for_each_possible_cpu(i)
448 relay_close_buf(chan
->buf
);
450 list_del(&chan
->list
);
451 kref_put(&chan
->kref
, relay_destroy_channel
);
452 mutex_unlock(&relay_channels_mutex
);
454 //ust// EXPORT_SYMBOL_GPL(ltt_relay_close);
457 * Start iteration at the previous element. Skip the real list head.
459 //ust// struct buf_page *ltt_relay_find_prev_page(struct rchan_buf *buf,
460 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
462 //ust// struct buf_page *iter;
463 //ust// size_t orig_iter_off;
464 //ust// unsigned int i = 0;
466 //ust// orig_iter_off = page->offset;
467 //ust// list_for_each_entry_reverse(iter, &page->list, list) {
469 //ust// * Skip the real list head.
471 //ust// if (&iter->list == &buf->pages)
474 //ust// if (offset >= iter->offset
475 //ust// && offset < iter->offset + PAGE_SIZE) {
476 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
478 //ust// printk(KERN_WARNING
479 //ust// "Backward random access detected in "
480 //ust// "ltt_relay. Iterations %u, "
481 //ust// "offset %zu, orig iter->off %zu, "
482 //ust// "iter->off %zu diff_offset %zd.\n", i,
483 //ust// offset, orig_iter_off, iter->offset,
484 //ust// diff_offset);
494 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_prev_page);
497 * Start iteration at the next element. Skip the real list head.
499 //ust// struct buf_page *ltt_relay_find_next_page(struct rchan_buf *buf,
500 //ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
502 //ust// struct buf_page *iter;
503 //ust// unsigned int i = 0;
504 //ust// size_t orig_iter_off;
506 //ust// orig_iter_off = page->offset;
507 //ust// list_for_each_entry(iter, &page->list, list) {
509 //ust// * Skip the real list head.
511 //ust// if (&iter->list == &buf->pages)
514 //ust// if (offset >= iter->offset
515 //ust// && offset < iter->offset + PAGE_SIZE) {
516 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
518 //ust// printk(KERN_WARNING
519 //ust// "Forward random access detected in "
520 //ust// "ltt_relay. Iterations %u, "
521 //ust// "offset %zu, orig iter->off %zu, "
522 //ust// "iter->off %zu diff_offset %zd.\n", i,
523 //ust// offset, orig_iter_off, iter->offset,
524 //ust// diff_offset);
534 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_next_page);
537 * ltt_relay_write - write data to a ltt_relay buffer.
539 * @offset : offset within the buffer
540 * @src : source address
541 * @len : length to write
542 * @page : cached buffer page
543 * @pagecpy : page size copied so far
545 void _ltt_relay_write(struct rchan_buf
*buf
, size_t offset
,
546 const void *src
, size_t len
, ssize_t cpy
)
553 * Underlying layer should never ask for writes across
556 WARN_ON(offset
>= buf
->buf_size
);
558 cpy
= min_t(size_t, len
, buf
->buf_size
- offset
);
559 ltt_relay_do_copy(buf
->buf_data
+ offset
, src
, cpy
);
560 } while (unlikely(len
!= cpy
));
562 //ust// EXPORT_SYMBOL_GPL(_ltt_relay_write);
565 * ltt_relay_read - read data from ltt_relay_buffer.
567 * @offset : offset within the buffer
568 * @dest : destination address
569 * @len : length to write
571 //ust// int ltt_relay_read(struct rchan_buf *buf, size_t offset,
572 //ust// void *dest, size_t len)
574 //ust// struct buf_page *page;
575 //ust// ssize_t pagecpy, orig_len;
577 //ust// orig_len = len;
578 //ust// offset &= buf->chan->alloc_size - 1;
579 //ust// page = buf->rpage;
580 //ust// if (unlikely(!len))
583 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
584 //ust// pagecpy = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
585 //ust// memcpy(dest, page_address(page->page) + (offset & ~PAGE_MASK),
587 //ust// len -= pagecpy;
588 //ust// if (likely(!len))
590 //ust// dest += pagecpy;
591 //ust// offset += pagecpy;
593 //ust// * Underlying layer should never ask for reads across
594 //ust// * subbuffers.
596 //ust// WARN_ON(offset >= buf->chan->alloc_size);
598 //ust// return orig_len;
600 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read);
603 * ltt_relay_read_get_page - Get a whole page to read from
605 * @offset : offset within the buffer
607 //ust// struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf, size_t offset)
609 //ust// struct buf_page *page;
611 //ust// offset &= buf->chan->alloc_size - 1;
612 //ust// page = buf->rpage;
613 //ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
616 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read_get_page);
619 * ltt_relay_offset_address - get address of a location within the buffer
621 * @offset : offset within the buffer.
623 * Return the address where a given offset is located.
624 * Should be used to get the current subbuffer header pointer. Given we know
625 * it's never on a page boundary, it's safe to write directly to this address,
626 * as long as the write is never bigger than a page size.
628 void *ltt_relay_offset_address(struct rchan_buf
*buf
, size_t offset
)
630 //ust// struct buf_page *page;
631 //ust// unsigned int odd;
633 //ust// offset &= buf->chan->alloc_size - 1;
634 //ust// odd = !!(offset & buf->chan->subbuf_size);
635 //ust// page = buf->hpage[odd];
636 //ust// if (offset < page->offset || offset >= page->offset + PAGE_SIZE)
637 //ust// buf->hpage[odd] = page = buf->wpage;
638 //ust// page = ltt_relay_cache_page(buf, &buf->hpage[odd], page, offset);
639 //ust// return page_address(page->page) + (offset & ~PAGE_MASK);
640 return ((char *)buf
->buf_data
)+offset
;
643 //ust// EXPORT_SYMBOL_GPL(ltt_relay_offset_address);
646 * relay_file_open - open file op for relay files
650 * Increments the channel buffer refcount.
652 //ust// static int relay_file_open(struct inode *inode, struct file *filp)
654 //ust// struct rchan_buf *buf = inode->i_private;
655 //ust// kref_get(&buf->kref);
656 //ust// filp->private_data = buf;
658 //ust// return nonseekable_open(inode, filp);
662 * relay_file_release - release file op for relay files
666 * Decrements the channel refcount, as the filesystem is
667 * no longer using it.
669 //ust// static int relay_file_release(struct inode *inode, struct file *filp)
671 //ust// struct rchan_buf *buf = filp->private_data;
672 //ust// kref_put(&buf->kref, relay_remove_buf);
677 //ust// const struct file_operations ltt_relay_file_operations = {
678 //ust// .open = relay_file_open,
679 //ust// .release = relay_file_release,
681 //ust// EXPORT_SYMBOL_GPL(ltt_relay_file_operations);
683 //ust// static __init int relay_init(void)
685 //ust// hotcpu_notifier(relay_hotcpu_callback, 5);
689 //ust// module_init(relay_init);
693 * (C) Copyright 2005-2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
695 * LTTng lockless buffer space management (reader/writer).
698 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
700 * Inspired from LTT :
701 * Karim Yaghmour (karim@opersys.com)
702 * Tom Zanussi (zanussi@us.ibm.com)
703 * Bob Wisniewski (bob@watson.ibm.com)
705 * Bob Wisniewski (bob@watson.ibm.com)
709 * 19/10/05, Complete lockless mechanism.
710 * 27/05/05, Modular redesign and rewrite.
712 * Userspace reader semantic :
713 * while (poll fd != POLLHUP) {
714 * - ioctl RELAY_GET_SUBBUF_SIZE
717 * - splice 1 subbuffer worth of data to a pipe
718 * - splice the data from pipe to disk/network
719 * - ioctl PUT_SUBBUF, check error value
720 * if err val < 0, previous subbuffer was corrupted.
725 //ust// #include <linux/time.h>
726 //ust// #include <linux/ltt-tracer.h>
727 //ust// #include <linux/ltt-relay.h>
728 //ust// #include <linux/module.h>
729 //ust// #include <linux/string.h>
730 //ust// #include <linux/slab.h>
731 //ust// #include <linux/init.h>
732 //ust// #include <linux/rcupdate.h>
733 //ust// #include <linux/sched.h>
734 //ust// #include <linux/bitops.h>
735 //ust// #include <linux/fs.h>
736 //ust// #include <linux/smp_lock.h>
737 //ust// #include <linux/debugfs.h>
738 //ust// #include <linux/stat.h>
739 //ust// #include <linux/cpu.h>
740 //ust// #include <linux/pipe_fs_i.h>
741 //ust// #include <linux/splice.h>
742 //ust// #include <asm/atomic.h>
743 //ust// #include <asm/local.h>
746 #define printk_dbg(fmt, args...) printk(fmt, args)
748 #define printk_dbg(fmt, args...)
752 * Last TSC comparison functions. Check if the current TSC overflows
753 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
757 #if (BITS_PER_LONG == 32)
758 static inline void save_last_tsc(struct ltt_channel_buf_struct
*ltt_buf
,
761 ltt_buf
->last_tsc
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
764 static inline int last_tsc_overflow(struct ltt_channel_buf_struct
*ltt_buf
,
767 unsigned long tsc_shifted
= (unsigned long)(tsc
>> LTT_TSC_BITS
);
769 if (unlikely((tsc_shifted
- ltt_buf
->last_tsc
)))
775 static inline void save_last_tsc(struct ltt_channel_buf_struct
*ltt_buf
,
778 ltt_buf
->last_tsc
= (unsigned long)tsc
;
781 static inline int last_tsc_overflow(struct ltt_channel_buf_struct
*ltt_buf
,
784 if (unlikely((tsc
- ltt_buf
->last_tsc
) >> LTT_TSC_BITS
))
791 //ust// static struct file_operations ltt_file_operations;
794 * A switch is done during tracing or as a final flush after tracing (so it
795 * won't write in the new sub-buffer).
797 enum force_switch_mode
{ FORCE_ACTIVE
, FORCE_FLUSH
};
799 static int ltt_relay_create_buffer(struct ltt_trace_struct
*trace
,
800 struct ltt_channel_struct
*ltt_chan
,
801 struct rchan_buf
*buf
,
802 unsigned int n_subbufs
);
804 static void ltt_relay_destroy_buffer(struct ltt_channel_struct
*ltt_chan
);
806 static void ltt_force_switch(struct rchan_buf
*buf
,
807 enum force_switch_mode mode
);
812 static void ltt_buffer_begin_callback(struct rchan_buf
*buf
,
813 u64 tsc
, unsigned int subbuf_idx
)
815 struct ltt_channel_struct
*channel
=
816 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
817 struct ltt_subbuffer_header
*header
=
818 (struct ltt_subbuffer_header
*)
819 ltt_relay_offset_address(buf
,
820 subbuf_idx
* buf
->chan
->subbuf_size
);
822 header
->cycle_count_begin
= tsc
;
823 header
->lost_size
= 0xFFFFFFFF; /* for debugging */
824 header
->buf_size
= buf
->chan
->subbuf_size
;
825 ltt_write_trace_header(channel
->trace
, header
);
829 * offset is assumed to never be 0 here : never deliver a completely empty
830 * subbuffer. The lost size is between 0 and subbuf_size-1.
832 static notrace
void ltt_buffer_end_callback(struct rchan_buf
*buf
,
833 u64 tsc
, unsigned int offset
, unsigned int subbuf_idx
)
835 struct ltt_channel_struct
*channel
=
836 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
837 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
838 struct ltt_subbuffer_header
*header
=
839 (struct ltt_subbuffer_header
*)
840 ltt_relay_offset_address(buf
,
841 subbuf_idx
* buf
->chan
->subbuf_size
);
843 header
->lost_size
= SUBBUF_OFFSET((buf
->chan
->subbuf_size
- offset
),
845 header
->cycle_count_end
= tsc
;
846 header
->events_lost
= local_read(<t_buf
->events_lost
);
847 header
->subbuf_corrupt
= local_read(<t_buf
->corrupted_subbuffers
);
851 static notrace
void ltt_deliver(struct rchan_buf
*buf
, unsigned int subbuf_idx
,
854 struct ltt_channel_struct
*channel
=
855 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
856 struct ltt_channel_buf_struct
*ltt_buf
= channel
->buf
;
858 atomic_set(<t_buf
->wakeup_readers
, 1);
861 static struct dentry
*ltt_create_buf_file_callback(struct rchan_buf
*buf
)
863 struct ltt_channel_struct
*ltt_chan
;
865 //ust// struct dentry *dentry;
867 ltt_chan
= buf
->chan
->private_data
;
868 err
= ltt_relay_create_buffer(ltt_chan
->trace
, ltt_chan
, buf
, buf
->chan
->n_subbufs
);
872 //ust// dentry = debugfs_create_file(filename, mode, parent, buf,
873 //ust// <t_file_operations);
876 //ust// return dentry;
879 ltt_relay_destroy_buffer(ltt_chan
);
883 static int ltt_remove_buf_file_callback(struct rchan_buf
*buf
)
885 //ust// struct rchan_buf *buf = dentry->d_inode->i_private;
886 struct ltt_channel_struct
*ltt_chan
= buf
->chan
->private_data
;
888 //ust// debugfs_remove(dentry);
889 ltt_relay_destroy_buffer(ltt_chan
);
897 * This must be done after the trace is removed from the RCU list so that there
898 * are no stalled writers.
900 //ust// static void ltt_relay_wake_writers(struct ltt_channel_buf_struct *ltt_buf)
903 //ust// if (waitqueue_active(<t_buf->write_wait))
904 //ust// wake_up_interruptible(<t_buf->write_wait);
908 * This function should not be called from NMI interrupt context
910 static notrace
void ltt_buf_unfull(struct rchan_buf
*buf
,
911 unsigned int subbuf_idx
,
914 //ust// struct ltt_channel_struct *ltt_channel =
915 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
916 //ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
918 //ust// ltt_relay_wake_writers(ltt_buf);
922 * ltt_open - open file op for ltt files
923 * @inode: opened inode
926 * Open implementation. Makes sure only one open instance of a buffer is
927 * done at a given moment.
929 //ust// static int ltt_open(struct inode *inode, struct file *file)
931 //ust// struct rchan_buf *buf = inode->i_private;
932 //ust// struct ltt_channel_struct *ltt_channel =
933 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
934 //ust// struct ltt_channel_buf_struct *ltt_buf =
935 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
937 //ust// if (!atomic_long_add_unless(<t_buf->active_readers, 1, 1))
938 //ust// return -EBUSY;
939 //ust// return ltt_relay_file_operations.open(inode, file);
943 * ltt_release - release file op for ltt files
944 * @inode: opened inode
947 * Release implementation.
949 //ust// static int ltt_release(struct inode *inode, struct file *file)
951 //ust// struct rchan_buf *buf = inode->i_private;
952 //ust// struct ltt_channel_struct *ltt_channel =
953 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
954 //ust// struct ltt_channel_buf_struct *ltt_buf =
955 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
958 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
959 //ust// atomic_long_dec(<t_buf->active_readers);
960 //ust// ret = ltt_relay_file_operations.release(inode, file);
961 //ust// WARN_ON(ret);
966 * ltt_poll - file op for ltt files
970 * Poll implementation.
972 //ust// static unsigned int ltt_poll(struct file *filp, poll_table *wait)
974 //ust// unsigned int mask = 0;
975 //ust// struct inode *inode = filp->f_dentry->d_inode;
976 //ust// struct rchan_buf *buf = inode->i_private;
977 //ust// struct ltt_channel_struct *ltt_channel =
978 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
979 //ust// struct ltt_channel_buf_struct *ltt_buf =
980 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
982 //ust// if (filp->f_mode & FMODE_READ) {
983 //ust// poll_wait_set_exclusive(wait);
984 //ust// poll_wait(filp, &buf->read_wait, wait);
986 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
987 //ust// if (SUBBUF_TRUNC(local_read(<t_buf->offset),
989 //ust// - SUBBUF_TRUNC(atomic_long_read(<t_buf->consumed),
992 //ust// if (buf->finalized)
993 //ust// return POLLHUP;
997 //ust// struct rchan *rchan =
998 //ust// ltt_channel->trans_channel_data;
999 //ust// if (SUBBUF_TRUNC(local_read(<t_buf->offset),
1001 //ust// - SUBBUF_TRUNC(atomic_long_read(
1002 //ust// <t_buf->consumed),
1004 //ust// >= rchan->alloc_size)
1005 //ust// return POLLPRI | POLLRDBAND;
1007 //ust// return POLLIN | POLLRDNORM;
1010 //ust// return mask;
1013 int ltt_do_get_subbuf(struct rchan_buf
*buf
, struct ltt_channel_buf_struct
*ltt_buf
, long *pconsumed_old
)
1015 struct ltt_channel_struct
*ltt_channel
= (struct ltt_channel_struct
*)buf
->chan
->private_data
;
1016 long consumed_old
, consumed_idx
, commit_count
, write_offset
;
1017 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1018 consumed_idx
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
1019 commit_count
= local_read(<t_buf
->commit_count
[consumed_idx
]);
1021 * Make sure we read the commit count before reading the buffer
1022 * data and the write offset. Correct consumed offset ordering
1023 * wrt commit count is insured by the use of cmpxchg to update
1024 * the consumed offset.
1027 write_offset
= local_read(<t_buf
->offset
);
1029 * Check that the subbuffer we are trying to consume has been
1030 * already fully committed.
1032 if (((commit_count
- buf
->chan
->subbuf_size
)
1033 & ltt_channel
->commit_count_mask
)
1034 - (BUFFER_TRUNC(consumed_old
, buf
->chan
)
1035 >> ltt_channel
->n_subbufs_order
)
1040 * Check that we are not about to read the same subbuffer in
1041 * which the writer head is.
1043 if ((SUBBUF_TRUNC(write_offset
, buf
->chan
)
1044 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1049 *pconsumed_old
= consumed_old
;
1053 int ltt_do_put_subbuf(struct rchan_buf
*buf
, struct ltt_channel_buf_struct
*ltt_buf
, u32 uconsumed_old
)
1055 long consumed_new
, consumed_old
;
1057 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1058 consumed_old
= consumed_old
& (~0xFFFFFFFFL
);
1059 consumed_old
= consumed_old
| uconsumed_old
;
1060 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1062 spin_lock(<t_buf
->full_lock
);
1063 if (atomic_long_cmpxchg(<t_buf
->consumed
, consumed_old
,
1066 /* We have been pushed by the writer : the last
1067 * buffer read _is_ corrupted! It can also
1068 * happen if this is a buffer we never got. */
1069 spin_unlock(<t_buf
->full_lock
);
1072 /* tell the client that buffer is now unfull */
1075 index
= SUBBUF_INDEX(consumed_old
, buf
->chan
);
1076 data
= BUFFER_OFFSET(consumed_old
, buf
->chan
);
1077 ltt_buf_unfull(buf
, index
, data
);
1078 spin_unlock(<t_buf
->full_lock
);
1084 * ltt_ioctl - control on the debugfs file
1091 * This ioctl implements three commands necessary for a minimal
1092 * producer/consumer implementation :
1094 * Get the next sub buffer that can be read. It never blocks.
1096 * Release the currently read sub-buffer. Parameter is the last
1097 * put subbuffer (returned by GET_SUBBUF).
1098 * RELAY_GET_N_BUBBUFS
1099 * returns the number of sub buffers in the per cpu channel.
1100 * RELAY_GET_SUBBUF_SIZE
1101 * returns the size of the sub buffers.
1103 //ust// static int ltt_ioctl(struct inode *inode, struct file *filp,
1104 //ust// unsigned int cmd, unsigned long arg)
1106 //ust// struct rchan_buf *buf = inode->i_private;
1107 //ust// struct ltt_channel_struct *ltt_channel =
1108 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1109 //ust// struct ltt_channel_buf_struct *ltt_buf =
1110 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1111 //ust// u32 __user *argp = (u32 __user *)arg;
1113 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1114 //ust// switch (cmd) {
1115 //ust// case RELAY_GET_SUBBUF:
1118 //ust// ret = ltt_do_get_subbuf(buf, ltt_buf, &consumed_old);
1121 //ust// return put_user((u32)consumed_old, argp);
1123 //ust// case RELAY_PUT_SUBBUF:
1126 //ust// u32 uconsumed_old;
1127 //ust// ret = get_user(uconsumed_old, argp);
1129 //ust// return ret; /* will return -EFAULT */
1130 //ust// return ltt_do_put_subbuf(buf, ltt_buf, uconsumed_old);
1132 //ust// case RELAY_GET_N_SUBBUFS:
1133 //ust// return put_user((u32)buf->chan->n_subbufs, argp);
1135 //ust// case RELAY_GET_SUBBUF_SIZE:
1136 //ust// return put_user((u32)buf->chan->subbuf_size, argp);
1139 //ust// return -ENOIOCTLCMD;
1144 //ust// #ifdef CONFIG_COMPAT
1145 //ust// static long ltt_compat_ioctl(struct file *file, unsigned int cmd,
1146 //ust// unsigned long arg)
1148 //ust// long ret = -ENOIOCTLCMD;
1150 //ust// lock_kernel();
1151 //ust// ret = ltt_ioctl(file->f_dentry->d_inode, file, cmd, arg);
1152 //ust// unlock_kernel();
1158 //ust// static void ltt_relay_pipe_buf_release(struct pipe_inode_info *pipe,
1159 //ust// struct pipe_buffer *pbuf)
1163 //ust// static struct pipe_buf_operations ltt_relay_pipe_buf_ops = {
1164 //ust// .can_merge = 0,
1165 //ust// .map = generic_pipe_buf_map,
1166 //ust// .unmap = generic_pipe_buf_unmap,
1167 //ust// .confirm = generic_pipe_buf_confirm,
1168 //ust// .release = ltt_relay_pipe_buf_release,
1169 //ust// .steal = generic_pipe_buf_steal,
1170 //ust// .get = generic_pipe_buf_get,
1173 //ust// static void ltt_relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1178 * subbuf_splice_actor - splice up to one subbuf's worth of data
1180 //ust// static int subbuf_splice_actor(struct file *in,
1181 //ust// loff_t *ppos,
1182 //ust// struct pipe_inode_info *pipe,
1184 //ust// unsigned int flags)
1186 //ust// struct rchan_buf *buf = in->private_data;
1187 //ust// struct ltt_channel_struct *ltt_channel =
1188 //ust// (struct ltt_channel_struct *)buf->chan->private_data;
1189 //ust// struct ltt_channel_buf_struct *ltt_buf =
1190 //ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1191 //ust// unsigned int poff, subbuf_pages, nr_pages;
1192 //ust// struct page *pages[PIPE_BUFFERS];
1193 //ust// struct partial_page partial[PIPE_BUFFERS];
1194 //ust// struct splice_pipe_desc spd = {
1195 //ust// .pages = pages,
1196 //ust// .nr_pages = 0,
1197 //ust// .partial = partial,
1198 //ust// .flags = flags,
1199 //ust// .ops = <t_relay_pipe_buf_ops,
1200 //ust// .spd_release = ltt_relay_page_release,
1202 //ust// long consumed_old, consumed_idx, roffset;
1203 //ust// unsigned long bytes_avail;
1206 //ust// * Check that a GET_SUBBUF ioctl has been done before.
1208 //ust// WARN_ON(atomic_long_read(<t_buf->active_readers) != 1);
1209 //ust// consumed_old = atomic_long_read(<t_buf->consumed);
1210 //ust// consumed_old += *ppos;
1211 //ust// consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1214 //ust// * Adjust read len, if longer than what is available
1216 //ust// bytes_avail = SUBBUF_TRUNC(local_read(<t_buf->offset), buf->chan)
1217 //ust// - consumed_old;
1218 //ust// WARN_ON(bytes_avail > buf->chan->alloc_size);
1219 //ust// len = min_t(size_t, len, bytes_avail);
1220 //ust// subbuf_pages = bytes_avail >> PAGE_SHIFT;
1221 //ust// nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS);
1222 //ust// roffset = consumed_old & PAGE_MASK;
1223 //ust// poff = consumed_old & ~PAGE_MASK;
1224 //ust// printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n",
1225 //ust// len, (ssize_t)*ppos, local_read(<t_buf->offset));
1227 //ust// for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
1228 //ust// unsigned int this_len;
1229 //ust// struct buf_page *page;
1233 //ust// printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
1234 //ust// len, roffset);
1236 //ust// this_len = PAGE_SIZE - poff;
1237 //ust// page = ltt_relay_read_get_page(buf, roffset);
1238 //ust// spd.pages[spd.nr_pages] = page->page;
1239 //ust// spd.partial[spd.nr_pages].offset = poff;
1240 //ust// spd.partial[spd.nr_pages].len = this_len;
1243 //ust// roffset += PAGE_SIZE;
1244 //ust// len -= this_len;
1247 //ust// if (!spd.nr_pages)
1250 //ust// return splice_to_pipe(pipe, &spd);
1253 //ust// static ssize_t ltt_relay_file_splice_read(struct file *in,
1254 //ust// loff_t *ppos,
1255 //ust// struct pipe_inode_info *pipe,
1257 //ust// unsigned int flags)
1259 //ust// ssize_t spliced;
1263 //ust// spliced = 0;
1265 //ust// printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n",
1266 //ust// len, (ssize_t)*ppos);
1267 //ust// while (len && !spliced) {
1268 //ust// ret = subbuf_splice_actor(in, ppos, pipe, len, flags);
1269 //ust// printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret);
1270 //ust// if (ret < 0)
1272 //ust// else if (!ret) {
1273 //ust// if (flags & SPLICE_F_NONBLOCK)
1274 //ust// ret = -EAGAIN;
1278 //ust// *ppos += ret;
1279 //ust// if (ret > len)
1283 //ust// spliced += ret;
1286 //ust// if (spliced)
1287 //ust// return spliced;
1292 static void ltt_relay_print_subbuffer_errors(
1293 struct ltt_channel_struct
*ltt_chan
,
1296 struct rchan
*rchan
= ltt_chan
->trans_channel_data
;
1297 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1298 long cons_idx
, commit_count
, write_offset
;
1300 cons_idx
= SUBBUF_INDEX(cons_off
, rchan
);
1301 commit_count
= local_read(<t_buf
->commit_count
[cons_idx
]);
1303 * No need to order commit_count and write_offset reads because we
1304 * execute after trace is stopped when there are no readers left.
1306 write_offset
= local_read(<t_buf
->offset
);
1308 "LTT : unread channel %s offset is %ld "
1309 "and cons_off : %ld\n",
1310 ltt_chan
->channel_name
, write_offset
, cons_off
);
1311 /* Check each sub-buffer for non filled commit count */
1312 if (((commit_count
- rchan
->subbuf_size
) & ltt_chan
->commit_count_mask
)
1313 - (BUFFER_TRUNC(cons_off
, rchan
) >> ltt_chan
->n_subbufs_order
)
1316 "LTT : %s : subbuffer %lu has non filled "
1317 "commit count %lu.\n",
1318 ltt_chan
->channel_name
, cons_idx
, commit_count
);
1319 printk(KERN_ALERT
"LTT : %s : commit count : %lu, subbuf size %zd\n",
1320 ltt_chan
->channel_name
, commit_count
,
1321 rchan
->subbuf_size
);
1324 static void ltt_relay_print_errors(struct ltt_trace_struct
*trace
,
1325 struct ltt_channel_struct
*ltt_chan
)
1327 struct rchan
*rchan
= ltt_chan
->trans_channel_data
;
1328 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1331 for (cons_off
= atomic_long_read(<t_buf
->consumed
);
1332 (SUBBUF_TRUNC(local_read(<t_buf
->offset
),
1335 cons_off
= SUBBUF_ALIGN(cons_off
, rchan
))
1336 ltt_relay_print_subbuffer_errors(ltt_chan
, cons_off
);
1339 static void ltt_relay_print_buffer_errors(struct ltt_channel_struct
*ltt_chan
)
1341 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
1342 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1344 if (local_read(<t_buf
->events_lost
))
1346 "LTT : %s : %ld events lost "
1348 ltt_chan
->channel_name
,
1349 local_read(<t_buf
->events_lost
),
1350 ltt_chan
->channel_name
);
1351 if (local_read(<t_buf
->corrupted_subbuffers
))
1353 "LTT : %s : %ld corrupted subbuffers "
1355 ltt_chan
->channel_name
,
1356 local_read(<t_buf
->corrupted_subbuffers
),
1357 ltt_chan
->channel_name
);
1359 ltt_relay_print_errors(trace
, ltt_chan
);
1362 static void ltt_relay_remove_dirs(struct ltt_trace_struct
*trace
)
1364 //ust// debugfs_remove(trace->dentry.trace_root);
1367 static void ltt_relay_release_channel(struct kref
*kref
)
1369 struct ltt_channel_struct
*ltt_chan
= container_of(kref
,
1370 struct ltt_channel_struct
, kref
);
1371 free(ltt_chan
->buf
);
1375 * Create ltt buffer.
1377 //ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1378 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
1379 //ust// unsigned int cpu, unsigned int n_subbufs)
1381 //ust// struct ltt_channel_buf_struct *ltt_buf =
1382 //ust// percpu_ptr(ltt_chan->buf, cpu);
1383 //ust// unsigned int j;
1385 //ust// ltt_buf->commit_count =
1386 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
1387 //ust// GFP_KERNEL, cpu_to_node(cpu));
1388 //ust// if (!ltt_buf->commit_count)
1389 //ust// return -ENOMEM;
1390 //ust// kref_get(&trace->kref);
1391 //ust// kref_get(&trace->ltt_transport_kref);
1392 //ust// kref_get(<t_chan->kref);
1393 //ust// local_set(<t_buf->offset, ltt_subbuffer_header_size());
1394 //ust// atomic_long_set(<t_buf->consumed, 0);
1395 //ust// atomic_long_set(<t_buf->active_readers, 0);
1396 //ust// for (j = 0; j < n_subbufs; j++)
1397 //ust// local_set(<t_buf->commit_count[j], 0);
1398 //ust// init_waitqueue_head(<t_buf->write_wait);
1399 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1400 //ust// spin_lock_init(<t_buf->full_lock);
1402 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1403 //ust// /* atomic_add made on local variable on data that belongs to
1404 //ust// * various CPUs : ok because tracing not started (for this cpu). */
1405 //ust// local_add(ltt_subbuffer_header_size(), <t_buf->commit_count[0]);
1407 //ust// local_set(<t_buf->events_lost, 0);
1408 //ust// local_set(<t_buf->corrupted_subbuffers, 0);
1413 static int ltt_relay_create_buffer(struct ltt_trace_struct
*trace
,
1414 struct ltt_channel_struct
*ltt_chan
, struct rchan_buf
*buf
,
1415 unsigned int n_subbufs
)
1417 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1420 ltt_buf
->commit_count
=
1421 zmalloc(sizeof(ltt_buf
->commit_count
) * n_subbufs
);
1422 if (!ltt_buf
->commit_count
)
1424 kref_get(&trace
->kref
);
1425 kref_get(&trace
->ltt_transport_kref
);
1426 kref_get(<t_chan
->kref
);
1427 local_set(<t_buf
->offset
, ltt_subbuffer_header_size());
1428 atomic_long_set(<t_buf
->consumed
, 0);
1429 atomic_long_set(<t_buf
->active_readers
, 0);
1430 for (j
= 0; j
< n_subbufs
; j
++)
1431 local_set(<t_buf
->commit_count
[j
], 0);
1432 //ust// init_waitqueue_head(<t_buf->write_wait);
1433 atomic_set(<t_buf
->wakeup_readers
, 0);
1434 spin_lock_init(<t_buf
->full_lock
);
1436 ltt_buffer_begin_callback(buf
, trace
->start_tsc
, 0);
1438 local_add(ltt_subbuffer_header_size(), <t_buf
->commit_count
[0]);
1440 local_set(<t_buf
->events_lost
, 0);
1441 local_set(<t_buf
->corrupted_subbuffers
, 0);
1446 static void ltt_relay_destroy_buffer(struct ltt_channel_struct
*ltt_chan
)
1448 struct ltt_trace_struct
*trace
= ltt_chan
->trace
;
1449 struct ltt_channel_buf_struct
*ltt_buf
= ltt_chan
->buf
;
1451 kref_put(<t_chan
->trace
->ltt_transport_kref
,
1452 ltt_release_transport
);
1453 ltt_relay_print_buffer_errors(ltt_chan
);
1454 kfree(ltt_buf
->commit_count
);
1455 ltt_buf
->commit_count
= NULL
;
1456 kref_put(<t_chan
->kref
, ltt_relay_release_channel
);
1457 kref_put(&trace
->kref
, ltt_release_trace
);
1458 //ust// wake_up_interruptible(&trace->kref_wq);
1464 static int ltt_relay_create_channel(const char *trace_name
,
1465 struct ltt_trace_struct
*trace
, struct dentry
*dir
,
1466 const char *channel_name
, struct ltt_channel_struct
*ltt_chan
,
1467 unsigned int subbuf_size
, unsigned int n_subbufs
,
1471 unsigned int tmpname_len
;
1474 tmpname
= kmalloc(PATH_MAX
, GFP_KERNEL
);
1478 strncpy(tmpname
, LTT_FLIGHT_PREFIX
, PATH_MAX
-1);
1479 strncat(tmpname
, channel_name
,
1480 PATH_MAX
-1-sizeof(LTT_FLIGHT_PREFIX
));
1482 strncpy(tmpname
, channel_name
, PATH_MAX
-1);
1484 strncat(tmpname
, "_", PATH_MAX
-1-strlen(tmpname
));
1486 kref_init(<t_chan
->kref
);
1488 ltt_chan
->trace
= trace
;
1489 ltt_chan
->buffer_begin
= ltt_buffer_begin_callback
;
1490 ltt_chan
->buffer_end
= ltt_buffer_end_callback
;
1491 ltt_chan
->overwrite
= overwrite
;
1492 ltt_chan
->n_subbufs_order
= get_count_order(n_subbufs
);
1493 ltt_chan
->commit_count_mask
= (~0UL >> ltt_chan
->n_subbufs_order
);
1494 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
1495 ltt_chan
->buf
= malloc(sizeof(struct ltt_channel_buf_struct
));
1498 ltt_chan
->trans_channel_data
= ltt_relay_open(tmpname
,
1503 tmpname_len
= strlen(tmpname
);
1504 if (tmpname_len
> 0) {
1505 /* Remove final _ for pretty printing */
1506 tmpname
[tmpname_len
-1] = '\0';
1508 if (ltt_chan
->trans_channel_data
== NULL
) {
1509 printk(KERN_ERR
"LTT : Can't open %s channel for trace %s\n",
1510 tmpname
, trace_name
);
1511 goto relay_open_error
;
1518 //ust// percpu_free(ltt_chan->buf);
1526 static int ltt_relay_create_dirs(struct ltt_trace_struct
*new_trace
)
1528 //ust// new_trace->dentry.trace_root = debugfs_create_dir(new_trace->trace_name,
1529 //ust// get_ltt_root());
1530 //ust// if (new_trace->dentry.trace_root == NULL) {
1531 //ust// printk(KERN_ERR "LTT : Trace directory name %s already taken\n",
1532 //ust// new_trace->trace_name);
1533 //ust// return EEXIST;
1536 //ust// new_trace->callbacks.create_buf_file = ltt_create_buf_file_callback;
1537 //ust// new_trace->callbacks.remove_buf_file = ltt_remove_buf_file_callback;
1543 * LTTng channel flush function.
1545 * Must be called when no tracing is active in the channel, because of
1546 * accesses across CPUs.
1548 static notrace
void ltt_relay_buffer_flush(struct rchan_buf
*buf
)
1551 ltt_force_switch(buf
, FORCE_FLUSH
);
1554 static void ltt_relay_async_wakeup_chan(struct ltt_channel_struct
*ltt_channel
)
1556 //ust// unsigned int i;
1557 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
1559 //ust// for_each_possible_cpu(i) {
1560 //ust// struct ltt_channel_buf_struct *ltt_buf =
1561 //ust// percpu_ptr(ltt_channel->buf, i);
1563 //ust// if (atomic_read(<t_buf->wakeup_readers) == 1) {
1564 //ust// atomic_set(<t_buf->wakeup_readers, 0);
1565 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
1570 static void ltt_relay_finish_buffer(struct ltt_channel_struct
*ltt_channel
)
1572 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
1575 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
1576 ltt_relay_buffer_flush(rchan
->buf
);
1577 //ust// ltt_relay_wake_writers(ltt_buf);
1582 static void ltt_relay_finish_channel(struct ltt_channel_struct
*ltt_channel
)
1586 //ust// for_each_possible_cpu(i)
1587 ltt_relay_finish_buffer(ltt_channel
);
1590 static void ltt_relay_remove_channel(struct ltt_channel_struct
*channel
)
1592 struct rchan
*rchan
= channel
->trans_channel_data
;
1594 ltt_relay_close(rchan
);
1595 kref_put(&channel
->kref
, ltt_relay_release_channel
);
1598 struct ltt_reserve_switch_offsets
{
1599 long begin
, end
, old
;
1600 long begin_switch
, end_switch_current
, end_switch_old
;
1601 long commit_count
, reserve_commit_diff
;
1602 size_t before_hdr_pad
, size
;
1608 * !0 if execution must be aborted.
1610 static inline int ltt_relay_try_reserve(
1611 struct ltt_channel_struct
*ltt_channel
,
1612 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1613 struct rchan_buf
*buf
,
1614 struct ltt_reserve_switch_offsets
*offsets
, size_t data_size
,
1615 u64
*tsc
, unsigned int *rflags
, int largest_align
)
1617 offsets
->begin
= local_read(<t_buf
->offset
);
1618 offsets
->old
= offsets
->begin
;
1619 offsets
->begin_switch
= 0;
1620 offsets
->end_switch_current
= 0;
1621 offsets
->end_switch_old
= 0;
1623 *tsc
= trace_clock_read64();
1624 if (last_tsc_overflow(ltt_buf
, *tsc
))
1625 *rflags
= LTT_RFLAG_ID_SIZE_TSC
;
1627 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) == 0) {
1628 offsets
->begin_switch
= 1; /* For offsets->begin */
1630 offsets
->size
= ltt_get_header_size(ltt_channel
,
1631 offsets
->begin
, data_size
,
1632 &offsets
->before_hdr_pad
, *rflags
);
1633 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1636 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
1637 > buf
->chan
->subbuf_size
) {
1638 offsets
->end_switch_old
= 1; /* For offsets->old */
1639 offsets
->begin_switch
= 1; /* For offsets->begin */
1642 if (offsets
->begin_switch
) {
1645 if (offsets
->end_switch_old
)
1646 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
,
1648 offsets
->begin
= offsets
->begin
+ ltt_subbuffer_header_size();
1649 /* Test new buffer integrity */
1650 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1651 offsets
->reserve_commit_diff
=
1652 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1653 >> ltt_channel
->n_subbufs_order
)
1654 - (local_read(<t_buf
->commit_count
[subbuf_index
])
1655 & ltt_channel
->commit_count_mask
);
1656 if (offsets
->reserve_commit_diff
== 0) {
1657 /* Next buffer not corrupted. */
1658 if (!ltt_channel
->overwrite
&&
1659 (SUBBUF_TRUNC(offsets
->begin
, buf
->chan
)
1660 - SUBBUF_TRUNC(atomic_long_read(
1661 <t_buf
->consumed
),
1663 >= rchan
->alloc_size
) {
1665 * We do not overwrite non consumed buffers
1666 * and we are full : event is lost.
1668 local_inc(<t_buf
->events_lost
);
1672 * next buffer not corrupted, we are either in
1673 * overwrite mode or the buffer is not full.
1674 * It's safe to write in this new subbuffer.
1679 * Next subbuffer corrupted. Force pushing reader even
1680 * in normal mode. It's safe to write in this new
1684 offsets
->size
= ltt_get_header_size(ltt_channel
,
1685 offsets
->begin
, data_size
,
1686 &offsets
->before_hdr_pad
, *rflags
);
1687 offsets
->size
+= ltt_align(offsets
->begin
+ offsets
->size
,
1690 if ((SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) + offsets
->size
)
1691 > buf
->chan
->subbuf_size
) {
1693 * Event too big for subbuffers, report error, don't
1694 * complete the sub-buffer switch.
1696 local_inc(<t_buf
->events_lost
);
1700 * We just made a successful buffer switch and the event
1701 * fits in the new subbuffer. Let's write.
1706 * Event fits in the current buffer and we are not on a switch
1707 * boundary. It's safe to write.
1710 offsets
->end
= offsets
->begin
+ offsets
->size
;
1712 if ((SUBBUF_OFFSET(offsets
->end
, buf
->chan
)) == 0) {
1714 * The offset_end will fall at the very beginning of the next
1717 offsets
->end_switch_current
= 1; /* For offsets->begin */
1725 * !0 if execution must be aborted.
1727 static inline int ltt_relay_try_switch(
1728 enum force_switch_mode mode
,
1729 struct ltt_channel_struct
*ltt_channel
,
1730 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1731 struct rchan_buf
*buf
,
1732 struct ltt_reserve_switch_offsets
*offsets
,
1737 offsets
->begin
= local_read(<t_buf
->offset
);
1738 offsets
->old
= offsets
->begin
;
1739 offsets
->begin_switch
= 0;
1740 offsets
->end_switch_old
= 0;
1742 *tsc
= trace_clock_read64();
1744 if (SUBBUF_OFFSET(offsets
->begin
, buf
->chan
) != 0) {
1745 offsets
->begin
= SUBBUF_ALIGN(offsets
->begin
, buf
->chan
);
1746 offsets
->end_switch_old
= 1;
1748 /* we do not have to switch : buffer is empty */
1751 if (mode
== FORCE_ACTIVE
)
1752 offsets
->begin
+= ltt_subbuffer_header_size();
1754 * Always begin_switch in FORCE_ACTIVE mode.
1755 * Test new buffer integrity
1757 subbuf_index
= SUBBUF_INDEX(offsets
->begin
, buf
->chan
);
1758 offsets
->reserve_commit_diff
=
1759 (BUFFER_TRUNC(offsets
->begin
, buf
->chan
)
1760 >> ltt_channel
->n_subbufs_order
)
1761 - (local_read(<t_buf
->commit_count
[subbuf_index
])
1762 & ltt_channel
->commit_count_mask
);
1763 if (offsets
->reserve_commit_diff
== 0) {
1764 /* Next buffer not corrupted. */
1765 if (mode
== FORCE_ACTIVE
1766 && !ltt_channel
->overwrite
1767 && offsets
->begin
- atomic_long_read(<t_buf
->consumed
)
1768 >= rchan
->alloc_size
) {
1770 * We do not overwrite non consumed buffers and we are
1771 * full : ignore switch while tracing is active.
1777 * Next subbuffer corrupted. Force pushing reader even in normal
1781 offsets
->end
= offsets
->begin
;
1785 static inline void ltt_reserve_push_reader(
1786 struct ltt_channel_struct
*ltt_channel
,
1787 struct ltt_channel_buf_struct
*ltt_buf
,
1788 struct rchan
*rchan
,
1789 struct rchan_buf
*buf
,
1790 struct ltt_reserve_switch_offsets
*offsets
)
1792 long consumed_old
, consumed_new
;
1795 consumed_old
= atomic_long_read(<t_buf
->consumed
);
1797 * If buffer is in overwrite mode, push the reader consumed
1798 * count if the write position has reached it and we are not
1799 * at the first iteration (don't push the reader farther than
1800 * the writer). This operation can be done concurrently by many
1801 * writers in the same buffer, the writer being at the farthest
1802 * write position sub-buffer index in the buffer being the one
1803 * which will win this loop.
1804 * If the buffer is not in overwrite mode, pushing the reader
1805 * only happens if a sub-buffer is corrupted.
1807 if ((SUBBUF_TRUNC(offsets
->end
-1, buf
->chan
)
1808 - SUBBUF_TRUNC(consumed_old
, buf
->chan
))
1809 >= rchan
->alloc_size
)
1810 consumed_new
= SUBBUF_ALIGN(consumed_old
, buf
->chan
);
1812 consumed_new
= consumed_old
;
1815 } while (atomic_long_cmpxchg(<t_buf
->consumed
, consumed_old
,
1816 consumed_new
) != consumed_old
);
1818 if (consumed_old
!= consumed_new
) {
1820 * Reader pushed : we are the winner of the push, we can
1821 * therefore reequilibrate reserve and commit. Atomic increment
1822 * of the commit count permits other writers to play around
1823 * with this variable before us. We keep track of
1824 * corrupted_subbuffers even in overwrite mode :
1825 * we never want to write over a non completely committed
1826 * sub-buffer : possible causes : the buffer size is too low
1827 * compared to the unordered data input, or there is a writer
1828 * that died between the reserve and the commit.
1830 if (offsets
->reserve_commit_diff
) {
1832 * We have to alter the sub-buffer commit count.
1833 * We do not deliver the previous subbuffer, given it
1834 * was either corrupted or not consumed (overwrite
1837 local_add(offsets
->reserve_commit_diff
,
1838 <t_buf
->commit_count
[
1839 SUBBUF_INDEX(offsets
->begin
,
1841 if (!ltt_channel
->overwrite
1842 || offsets
->reserve_commit_diff
1843 != rchan
->subbuf_size
) {
1845 * The reserve commit diff was not subbuf_size :
1846 * it means the subbuffer was partly written to
1847 * and is therefore corrupted. If it is multiple
1848 * of subbuffer size and we are in flight
1849 * recorder mode, we are skipping over a whole
1852 local_inc(<t_buf
->corrupted_subbuffers
);
1860 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1862 * Concurrency safe because we are the last and only thread to alter this
1863 * sub-buffer. As long as it is not delivered and read, no other thread can
1864 * alter the offset, alter the reserve_count or call the
1865 * client_buffer_end_callback on this sub-buffer.
1867 * The only remaining threads could be the ones with pending commits. They will
1868 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1869 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1870 * corrupted sub-buffers count and push the readers across these sub-buffers.
1872 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1873 * switches in, finding out it's corrupted. The result will be than the old
1874 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1875 * will be declared corrupted too because of the commit count adjustment.
1877 * Note : offset_old should never be 0 here.
1879 static inline void ltt_reserve_switch_old_subbuf(
1880 struct ltt_channel_struct
*ltt_channel
,
1881 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1882 struct rchan_buf
*buf
,
1883 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
1885 long oldidx
= SUBBUF_INDEX(offsets
->old
- 1, rchan
);
1887 ltt_channel
->buffer_end(buf
, *tsc
, offsets
->old
, oldidx
);
1888 /* Must write buffer end before incrementing commit count */
1890 offsets
->commit_count
=
1891 local_add_return(rchan
->subbuf_size
1892 - (SUBBUF_OFFSET(offsets
->old
- 1, rchan
)
1894 <t_buf
->commit_count
[oldidx
]);
1895 if ((BUFFER_TRUNC(offsets
->old
- 1, rchan
)
1896 >> ltt_channel
->n_subbufs_order
)
1897 - ((offsets
->commit_count
- rchan
->subbuf_size
)
1898 & ltt_channel
->commit_count_mask
) == 0)
1899 ltt_deliver(buf
, oldidx
, NULL
);
1903 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
1905 * This code can be executed unordered : writers may already have written to the
1906 * sub-buffer before this code gets executed, caution. The commit makes sure
1907 * that this code is executed before the deliver of this sub-buffer.
1909 static inline void ltt_reserve_switch_new_subbuf(
1910 struct ltt_channel_struct
*ltt_channel
,
1911 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1912 struct rchan_buf
*buf
,
1913 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
1915 long beginidx
= SUBBUF_INDEX(offsets
->begin
, rchan
);
1917 ltt_channel
->buffer_begin(buf
, *tsc
, beginidx
);
1918 /* Must write buffer end before incrementing commit count */
1920 offsets
->commit_count
= local_add_return(ltt_subbuffer_header_size(),
1921 <t_buf
->commit_count
[beginidx
]);
1922 /* Check if the written buffer has to be delivered */
1923 if ((BUFFER_TRUNC(offsets
->begin
, rchan
)
1924 >> ltt_channel
->n_subbufs_order
)
1925 - ((offsets
->commit_count
- rchan
->subbuf_size
)
1926 & ltt_channel
->commit_count_mask
) == 0)
1927 ltt_deliver(buf
, beginidx
, NULL
);
1932 * ltt_reserve_end_switch_current: finish switching current subbuffer
1934 * Concurrency safe because we are the last and only thread to alter this
1935 * sub-buffer. As long as it is not delivered and read, no other thread can
1936 * alter the offset, alter the reserve_count or call the
1937 * client_buffer_end_callback on this sub-buffer.
1939 * The only remaining threads could be the ones with pending commits. They will
1940 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
1941 * We detect corrupted subbuffers with commit and reserve counts. We keep a
1942 * corrupted sub-buffers count and push the readers across these sub-buffers.
1944 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
1945 * switches in, finding out it's corrupted. The result will be than the old
1946 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
1947 * will be declared corrupted too because of the commit count adjustment.
1949 static inline void ltt_reserve_end_switch_current(
1950 struct ltt_channel_struct
*ltt_channel
,
1951 struct ltt_channel_buf_struct
*ltt_buf
, struct rchan
*rchan
,
1952 struct rchan_buf
*buf
,
1953 struct ltt_reserve_switch_offsets
*offsets
, u64
*tsc
)
1955 long endidx
= SUBBUF_INDEX(offsets
->end
- 1, rchan
);
1957 ltt_channel
->buffer_end(buf
, *tsc
, offsets
->end
, endidx
);
1958 /* Must write buffer begin before incrementing commit count */
1960 offsets
->commit_count
=
1961 local_add_return(rchan
->subbuf_size
1962 - (SUBBUF_OFFSET(offsets
->end
- 1, rchan
)
1964 <t_buf
->commit_count
[endidx
]);
1965 if ((BUFFER_TRUNC(offsets
->end
- 1, rchan
)
1966 >> ltt_channel
->n_subbufs_order
)
1967 - ((offsets
->commit_count
- rchan
->subbuf_size
)
1968 & ltt_channel
->commit_count_mask
) == 0)
1969 ltt_deliver(buf
, endidx
, NULL
);
1973 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
1974 * @trace: the trace structure to log to.
1975 * @ltt_channel: channel structure
1976 * @transport_data: data structure specific to ltt relay
1977 * @data_size: size of the variable length data to log.
1978 * @slot_size: pointer to total size of the slot (out)
1979 * @buf_offset : pointer to reserved buffer offset (out)
1980 * @tsc: pointer to the tsc at the slot reservation (out)
1983 * Return : -ENOSPC if not enough space, else returns 0.
1984 * It will take care of sub-buffer switching.
1986 static notrace
int ltt_relay_reserve_slot(struct ltt_trace_struct
*trace
,
1987 struct ltt_channel_struct
*ltt_channel
, void **transport_data
,
1988 size_t data_size
, size_t *slot_size
, long *buf_offset
, u64
*tsc
,
1989 unsigned int *rflags
, int largest_align
)
1991 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
1992 struct rchan_buf
*buf
= *transport_data
= rchan
->buf
;
1993 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
1994 struct ltt_reserve_switch_offsets offsets
;
1996 offsets
.reserve_commit_diff
= 0;
2000 * Perform retryable operations.
2002 if (ltt_nesting
> 4) {
2003 local_inc(<t_buf
->events_lost
);
2007 if (ltt_relay_try_reserve(ltt_channel
, ltt_buf
,
2008 rchan
, buf
, &offsets
, data_size
, tsc
, rflags
,
2011 } while (local_cmpxchg(<t_buf
->offset
, offsets
.old
,
2012 offsets
.end
) != offsets
.old
);
2015 * Atomically update last_tsc. This update races against concurrent
2016 * atomic updates, but the race will always cause supplementary full TSC
2017 * events, never the opposite (missing a full TSC event when it would be
2020 save_last_tsc(ltt_buf
, *tsc
);
2023 * Push the reader if necessary
2025 ltt_reserve_push_reader(ltt_channel
, ltt_buf
, rchan
, buf
, &offsets
);
2028 * Switch old subbuffer if needed.
2030 if (offsets
.end_switch_old
)
2031 ltt_reserve_switch_old_subbuf(ltt_channel
, ltt_buf
, rchan
, buf
,
2035 * Populate new subbuffer.
2037 if (offsets
.begin_switch
)
2038 ltt_reserve_switch_new_subbuf(ltt_channel
, ltt_buf
, rchan
,
2039 buf
, &offsets
, tsc
);
2041 if (offsets
.end_switch_current
)
2042 ltt_reserve_end_switch_current(ltt_channel
, ltt_buf
, rchan
,
2043 buf
, &offsets
, tsc
);
2045 *slot_size
= offsets
.size
;
2046 *buf_offset
= offsets
.begin
+ offsets
.before_hdr_pad
;
2051 * Force a sub-buffer switch for a per-cpu buffer. This operation is
2052 * completely reentrant : can be called while tracing is active with
2053 * absolutely no lock held.
2055 * Note, however, that as a local_cmpxchg is used for some atomic
2056 * operations, this function must be called from the CPU which owns the buffer
2057 * for a ACTIVE flush.
2059 static notrace
void ltt_force_switch(struct rchan_buf
*buf
,
2060 enum force_switch_mode mode
)
2062 struct ltt_channel_struct
*ltt_channel
=
2063 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
2064 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2065 struct rchan
*rchan
= ltt_channel
->trans_channel_data
;
2066 struct ltt_reserve_switch_offsets offsets
;
2069 offsets
.reserve_commit_diff
= 0;
2073 * Perform retryable operations.
2076 if (ltt_relay_try_switch(mode
, ltt_channel
, ltt_buf
,
2077 rchan
, buf
, &offsets
, &tsc
))
2079 } while (local_cmpxchg(<t_buf
->offset
, offsets
.old
,
2080 offsets
.end
) != offsets
.old
);
2083 * Atomically update last_tsc. This update races against concurrent
2084 * atomic updates, but the race will always cause supplementary full TSC
2085 * events, never the opposite (missing a full TSC event when it would be
2088 save_last_tsc(ltt_buf
, tsc
);
2091 * Push the reader if necessary
2093 if (mode
== FORCE_ACTIVE
)
2094 ltt_reserve_push_reader(ltt_channel
, ltt_buf
, rchan
,
2098 * Switch old subbuffer if needed.
2100 if (offsets
.end_switch_old
)
2101 ltt_reserve_switch_old_subbuf(ltt_channel
, ltt_buf
, rchan
, buf
,
2105 * Populate new subbuffer.
2107 if (mode
== FORCE_ACTIVE
)
2108 ltt_reserve_switch_new_subbuf(ltt_channel
,
2109 ltt_buf
, rchan
, buf
, &offsets
, &tsc
);
2113 * for flight recording. must be called after relay_commit.
2114 * This function decrements de subbuffer's lost_size each time the commit count
2115 * reaches back the reserve offset (module subbuffer size). It is useful for
2117 * We use slot_size - 1 to make sure we deal correctly with the case where we
2118 * fill the subbuffer completely (so the subbuf index stays in the previous
2121 #ifdef CONFIG_LTT_VMCORE
2122 static inline void ltt_write_commit_counter(struct rchan_buf
*buf
,
2123 long buf_offset
, size_t slot_size
)
2125 struct ltt_channel_struct
*ltt_channel
=
2126 (struct ltt_channel_struct
*)buf
->chan
->private_data
;
2127 struct ltt_channel_buf_struct
*ltt_buf
=
2128 percpu_ptr(ltt_channel
->buf
, buf
->cpu
);
2129 struct ltt_subbuffer_header
*header
;
2130 long offset
, subbuf_idx
, commit_count
;
2131 uint32_t lost_old
, lost_new
;
2133 subbuf_idx
= SUBBUF_INDEX(buf_offset
- 1, buf
->chan
);
2134 offset
= buf_offset
+ slot_size
;
2135 header
= (struct ltt_subbuffer_header
*)
2136 ltt_relay_offset_address(buf
,
2137 subbuf_idx
* buf
->chan
->subbuf_size
);
2139 lost_old
= header
->lost_size
;
2141 local_read(<t_buf
->commit_count
[subbuf_idx
]);
2142 /* SUBBUF_OFFSET includes commit_count_mask */
2143 if (!SUBBUF_OFFSET(offset
- commit_count
, buf
->chan
)) {
2144 lost_new
= (uint32_t)buf
->chan
->subbuf_size
2145 - SUBBUF_OFFSET(commit_count
, buf
->chan
);
2146 lost_old
= cmpxchg_local(&header
->lost_size
, lost_old
,
2148 if (lost_old
<= lost_new
)
2156 static inline void ltt_write_commit_counter(struct rchan_buf
*buf
,
2157 long buf_offset
, size_t slot_size
)
2163 * Atomic unordered slot commit. Increments the commit count in the
2164 * specified sub-buffer, and delivers it if necessary.
2168 * @ltt_channel : channel structure
2169 * @transport_data: transport-specific data
2170 * @buf_offset : offset following the event header.
2171 * @slot_size : size of the reserved slot.
2173 static notrace
void ltt_relay_commit_slot(
2174 struct ltt_channel_struct
*ltt_channel
,
2175 void **transport_data
, long buf_offset
, size_t slot_size
)
2177 struct rchan_buf
*buf
= *transport_data
;
2178 struct ltt_channel_buf_struct
*ltt_buf
= ltt_channel
->buf
;
2179 struct rchan
*rchan
= buf
->chan
;
2180 long offset_end
= buf_offset
;
2181 long endidx
= SUBBUF_INDEX(offset_end
- 1, rchan
);
2184 /* Must write slot data before incrementing commit count */
2186 commit_count
= local_add_return(slot_size
,
2187 <t_buf
->commit_count
[endidx
]);
2188 /* Check if all commits have been done */
2189 if ((BUFFER_TRUNC(offset_end
- 1, rchan
)
2190 >> ltt_channel
->n_subbufs_order
)
2191 - ((commit_count
- rchan
->subbuf_size
)
2192 & ltt_channel
->commit_count_mask
) == 0)
2193 ltt_deliver(buf
, endidx
, NULL
);
2195 * Update lost_size for each commit. It's needed only for extracting
2196 * ltt buffers from vmcore, after crash.
2198 ltt_write_commit_counter(buf
, buf_offset
, slot_size
);
2202 * This is called with preemption disabled when user space has requested
2203 * blocking mode. If one of the active traces has free space below a
2204 * specific threshold value, we reenable preemption and block.
2206 static int ltt_relay_user_blocking(struct ltt_trace_struct
*trace
,
2207 unsigned int chan_index
, size_t data_size
,
2208 struct user_dbg_data
*dbg
)
2210 //ust// struct rchan *rchan;
2211 //ust// struct ltt_channel_buf_struct *ltt_buf;
2212 //ust// struct ltt_channel_struct *channel;
2213 //ust// struct rchan_buf *relay_buf;
2215 //ust// DECLARE_WAITQUEUE(wait, current);
2217 //ust// channel = &trace->channels[chan_index];
2218 //ust// rchan = channel->trans_channel_data;
2219 //ust// cpu = smp_processor_id();
2220 //ust// relay_buf = rchan->buf[cpu];
2221 //ust// ltt_buf = percpu_ptr(channel->buf, cpu);
2224 //ust// * Check if data is too big for the channel : do not
2225 //ust// * block for it.
2227 //ust// if (LTT_RESERVE_CRITICAL + data_size > relay_buf->chan->subbuf_size)
2231 //ust// * If free space too low, we block. We restart from the
2232 //ust// * beginning after we resume (cpu id may have changed
2233 //ust// * while preemption is active).
2235 //ust// spin_lock(<t_buf->full_lock);
2236 //ust// if (!channel->overwrite) {
2237 //ust// dbg->write = local_read(<t_buf->offset);
2238 //ust// dbg->read = atomic_long_read(<t_buf->consumed);
2239 //ust// dbg->avail_size = dbg->write + LTT_RESERVE_CRITICAL + data_size
2240 //ust// - SUBBUF_TRUNC(dbg->read,
2241 //ust// relay_buf->chan);
2242 //ust// if (dbg->avail_size > rchan->alloc_size) {
2243 //ust// __set_current_state(TASK_INTERRUPTIBLE);
2244 //ust// add_wait_queue(<t_buf->write_wait, &wait);
2245 //ust// spin_unlock(<t_buf->full_lock);
2246 //ust// preempt_enable();
2248 //ust// __set_current_state(TASK_RUNNING);
2249 //ust// remove_wait_queue(<t_buf->write_wait, &wait);
2250 //ust// if (signal_pending(current))
2251 //ust// return -ERESTARTSYS;
2252 //ust// preempt_disable();
2256 //ust// spin_unlock(<t_buf->full_lock);
2260 static void ltt_relay_print_user_errors(struct ltt_trace_struct
*trace
,
2261 unsigned int chan_index
, size_t data_size
,
2262 struct user_dbg_data
*dbg
)
2264 struct rchan
*rchan
;
2265 struct ltt_channel_buf_struct
*ltt_buf
;
2266 struct ltt_channel_struct
*channel
;
2267 struct rchan_buf
*relay_buf
;
2269 channel
= &trace
->channels
[chan_index
];
2270 rchan
= channel
->trans_channel_data
;
2271 relay_buf
= rchan
->buf
;
2272 ltt_buf
= channel
->buf
;
2274 printk(KERN_ERR
"Error in LTT usertrace : "
2275 "buffer full : event lost in blocking "
2276 "mode. Increase LTT_RESERVE_CRITICAL.\n");
2277 printk(KERN_ERR
"LTT nesting level is %u.\n", ltt_nesting
);
2278 printk(KERN_ERR
"LTT avail size %lu.\n",
2280 printk(KERN_ERR
"avai write : %lu, read : %lu\n",
2281 dbg
->write
, dbg
->read
);
2283 dbg
->write
= local_read(<t_buf
->offset
);
2284 dbg
->read
= atomic_long_read(<t_buf
->consumed
);
2286 printk(KERN_ERR
"LTT cur size %lu.\n",
2287 dbg
->write
+ LTT_RESERVE_CRITICAL
+ data_size
2288 - SUBBUF_TRUNC(dbg
->read
, relay_buf
->chan
));
2289 printk(KERN_ERR
"cur write : %lu, read : %lu\n",
2290 dbg
->write
, dbg
->read
);
2293 //ust// static struct ltt_transport ltt_relay_transport = {
2294 //ust// .name = "relay",
2295 //ust// .owner = THIS_MODULE,
2297 //ust// .create_dirs = ltt_relay_create_dirs,
2298 //ust// .remove_dirs = ltt_relay_remove_dirs,
2299 //ust// .create_channel = ltt_relay_create_channel,
2300 //ust// .finish_channel = ltt_relay_finish_channel,
2301 //ust// .remove_channel = ltt_relay_remove_channel,
2302 //ust// .wakeup_channel = ltt_relay_async_wakeup_chan,
2303 //ust// .commit_slot = ltt_relay_commit_slot,
2304 //ust// .reserve_slot = ltt_relay_reserve_slot,
2305 //ust// .user_blocking = ltt_relay_user_blocking,
2306 //ust// .user_errors = ltt_relay_print_user_errors,
2310 static struct ltt_transport ust_relay_transport
= {
2312 //ust// .owner = THIS_MODULE,
2314 .create_dirs
= ltt_relay_create_dirs
,
2315 .remove_dirs
= ltt_relay_remove_dirs
,
2316 .create_channel
= ltt_relay_create_channel
,
2317 .finish_channel
= ltt_relay_finish_channel
,
2318 .remove_channel
= ltt_relay_remove_channel
,
2319 .wakeup_channel
= ltt_relay_async_wakeup_chan
,
2320 .commit_slot
= ltt_relay_commit_slot
,
2321 .reserve_slot
= ltt_relay_reserve_slot
,
2322 .user_blocking
= ltt_relay_user_blocking
,
2323 .user_errors
= ltt_relay_print_user_errors
,
2327 //ust// static int __init ltt_relay_init(void)
2329 //ust// printk(KERN_INFO "LTT : ltt-relay init\n");
2331 //ust// ltt_file_operations = ltt_relay_file_operations;
2332 //ust// ltt_file_operations.owner = THIS_MODULE;
2333 //ust// ltt_file_operations.open = ltt_open;
2334 //ust// ltt_file_operations.release = ltt_release;
2335 //ust// ltt_file_operations.poll = ltt_poll;
2336 //ust// ltt_file_operations.splice_read = ltt_relay_file_splice_read,
2337 //ust// ltt_file_operations.ioctl = ltt_ioctl;
2338 //ust//#ifdef CONFIG_COMPAT
2339 //ust// ltt_file_operations.compat_ioctl = ltt_compat_ioctl;
2342 //ust// ltt_transport_register(<t_relay_transport);
2347 static char initialized
= 0;
2349 void __attribute__((constructor
)) init_ustrelay_transport(void)
2352 ltt_transport_register(&ust_relay_transport
);
2357 static void __exit
ltt_relay_exit(void)
2359 //ust// printk(KERN_INFO "LTT : ltt-relay exit\n");
2361 ltt_transport_unregister(&ust_relay_transport
);
2364 //ust// module_init(ltt_relay_init);
2365 //ust// module_exit(ltt_relay_exit);
2367 //ust// MODULE_LICENSE("GPL");
2368 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
2369 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Lockless Relay");