update README
[lttng-ust.git] / libust / relay.c
CommitLineData
bb07823d
PMF
1/*
2 * Public API and common code for kernel->userspace relay file support.
3 *
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)
7 *
8 * Moved to kernel/relay.c by Paul Mundt, 2006.
9 * November 2006 - CPU hotplug support by Mathieu Desnoyers
10 * (mathieu.desnoyers@polymtl.ca)
11 *
12 * This file is released under the GPL.
13 */
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>
1ae7f074 25#include "kernelcompat.h"
bb07823d 26#include <sys/mman.h>
aafb1650
PMF
27#include <sys/ipc.h>
28#include <sys/shm.h>
769d0157 29//#include "list.h"
bb07823d
PMF
30#include "relay.h"
31#include "channels.h"
79d4d545 32#include <kcompat/kref.h>
bb07823d
PMF
33#include "tracer.h"
34#include "tracercore.h"
35#include "usterr.h"
36
37/* list of open channels, for cpu hotplug */
38static DEFINE_MUTEX(relay_channels_mutex);
39static LIST_HEAD(relay_channels);
40
c1dea0b3
PMF
41
42static struct dentry *ltt_create_buf_file_callback(struct rchan_buf *buf);
43
bb07823d
PMF
44/**
45 * relay_alloc_buf - allocate a channel buffer
46 * @buf: the buffer struct
47 * @size: total size of the buffer
48 */
49//ust// static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
50//ust//{
51//ust// unsigned int i, n_pages;
52//ust// struct buf_page *buf_page, *n;
53//ust//
54//ust// *size = PAGE_ALIGN(*size);
55//ust// n_pages = *size >> PAGE_SHIFT;
56//ust//
57//ust// INIT_LIST_HEAD(&buf->pages);
58//ust//
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;
69//ust// }
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);
74//ust// if (i == 0) {
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;
79//ust// }
80//ust// }
81//ust// buf->page_count = n_pages;
82//ust// return 0;
83//ust//
84//ust//depopulate:
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);
89//ust// }
90//ust// return -ENOMEM;
91//ust//}
92
93static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
94{
8cefc145
PMF
95//ust// unsigned int n_pages;
96//ust// struct buf_page *buf_page, *n;
bb07823d 97
aafb1650
PMF
98 void *ptr;
99 int result;
bb07823d
PMF
100
101 *size = PAGE_ALIGN(*size);
102
3847c3ba 103 result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700);
68ab7a5d
PMF
104 if(result == -1 && errno == EINVAL) {
105 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
106 return -1;
107 }
108 else if(result == -1) {
aafb1650 109 PERROR("shmget");
bb07823d
PMF
110 return -1;
111 }
112
3847c3ba 113 ptr = shmat(buf->shmid, NULL, 0);
aafb1650
PMF
114 if(ptr == (void *) -1) {
115 perror("shmat");
116 goto destroy_shmem;
117 }
118
119 /* Already mark the shared memory for destruction. This will occur only
120 * when all users have detached.
121 */
3847c3ba 122 result = shmctl(buf->shmid, IPC_RMID, NULL);
aafb1650
PMF
123 if(result == -1) {
124 perror("shmctl");
125 return -1;
126 }
127
128 buf->buf_data = ptr;
bb07823d
PMF
129 buf->buf_size = *size;
130
131 return 0;
aafb1650
PMF
132
133 destroy_shmem:
3847c3ba 134 result = shmctl(buf->shmid, IPC_RMID, NULL);
aafb1650
PMF
135 if(result == -1) {
136 perror("shmctl");
137 }
138
139 return -1;
bb07823d
PMF
140}
141
142/**
143 * relay_create_buf - allocate and initialize a channel buffer
144 * @chan: the relay channel
145 * @cpu: cpu the buffer belongs to
146 *
147 * Returns channel buffer if successful, %NULL otherwise.
148 */
149static struct rchan_buf *relay_create_buf(struct rchan *chan)
150{
151 int ret;
152 struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
153 if (!buf)
154 return NULL;
155
156// buf->cpu = cpu;
157 ret = relay_alloc_buf(buf, &chan->alloc_size);
158 if (ret)
159 goto free_buf;
160
161 buf->chan = chan;
162 kref_get(&buf->chan->kref);
163 return buf;
164
165free_buf:
166 kfree(buf);
167 return NULL;
168}
169
170/**
171 * relay_destroy_channel - free the channel struct
172 * @kref: target kernel reference that contains the relay channel
173 *
174 * Should only be called from kref_put().
175 */
176static void relay_destroy_channel(struct kref *kref)
177{
178 struct rchan *chan = container_of(kref, struct rchan, kref);
179 kfree(chan);
180}
181
182/**
183 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
184 * @buf: the buffer struct
185 */
186static void relay_destroy_buf(struct rchan_buf *buf)
187{
188 struct rchan *chan = buf->chan;
772030fe 189//ust// struct buf_page *buf_page;
bb07823d
PMF
190 int result;
191
192 result = munmap(buf->buf_data, buf->buf_size);
193 if(result == -1) {
194 PERROR("munmap");
195 }
196
197//ust// chan->buf[buf->cpu] = NULL;
198 kfree(buf);
199 kref_put(&chan->kref, relay_destroy_channel);
200}
201
202/**
203 * relay_remove_buf - remove a channel buffer
204 * @kref: target kernel reference that contains the relay buffer
205 *
206 * Removes the file from the fileystem, which also frees the
207 * rchan_buf_struct and the channel buffer. Should only be called from
208 * kref_put().
209 */
210static void relay_remove_buf(struct kref *kref)
211{
212 struct rchan_buf *buf = container_of(kref, struct rchan_buf, kref);
98963de4 213//ust// buf->chan->cb->remove_buf_file(buf);
bb07823d
PMF
214 relay_destroy_buf(buf);
215}
216
217/*
218 * High-level relay kernel API and associated functions.
219 */
220
221/*
222 * rchan_callback implementations defining default channel behavior. Used
223 * in place of corresponding NULL values in client callback struct.
224 */
225
226/*
227 * create_buf_file_create() default callback. Does nothing.
228 */
772030fe
PMF
229//ust// static struct dentry *create_buf_file_default_callback(const char *filename,
230//ust// struct dentry *parent,
231//ust// int mode,
232//ust// struct rchan_buf *buf)
233//ust// {
234//ust// return NULL;
235//ust// }
bb07823d 236
772030fe
PMF
237//ust// /*
238//ust// * remove_buf_file() default callback. Does nothing.
239//ust// */
240//ust// static int remove_buf_file_default_callback(struct dentry *dentry)
241//ust// {
242//ust// return -EINVAL;
243//ust// }
bb07823d
PMF
244
245/**
246 * wakeup_readers - wake up readers waiting on a channel
247 * @data: contains the channel buffer
248 *
249 * This is the timer function used to defer reader waking.
250 */
251//ust// static void wakeup_readers(unsigned long data)
252//ust// {
253//ust// struct rchan_buf *buf = (struct rchan_buf *)data;
254//ust// wake_up_interruptible(&buf->read_wait);
255//ust// }
256
257/**
258 * __relay_reset - reset a channel buffer
259 * @buf: the channel buffer
260 * @init: 1 if this is a first-time initialization
261 *
262 * See relay_reset() for description of effect.
263 */
264static void __relay_reset(struct rchan_buf *buf, unsigned int init)
265{
266 if (init) {
267//ust// init_waitqueue_head(&buf->read_wait);
268 kref_init(&buf->kref);
269//ust// setup_timer(&buf->timer, wakeup_readers, (unsigned long)buf);
270 } else
271//ust// del_timer_sync(&buf->timer);
272
273 buf->finalized = 0;
274}
275
276/*
277 * relay_open_buf - create a new relay channel buffer
278 *
279 * used by relay_open() and CPU hotplug.
280 */
281static struct rchan_buf *relay_open_buf(struct rchan *chan)
282{
283 struct rchan_buf *buf = NULL;
772030fe 284//ust// struct dentry *dentry;
bb07823d
PMF
285//ust// char *tmpname;
286
287//ust// tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
288//ust// if (!tmpname)
289//ust// goto end;
290//ust// snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
291
292 buf = relay_create_buf(chan);
293 if (!buf)
294 goto free_name;
295
296 __relay_reset(buf, 1);
297
298 /* Create file in fs */
299//ust// dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
300//ust// buf);
c1dea0b3
PMF
301
302 ltt_create_buf_file_callback(buf); // ust //
303
bb07823d
PMF
304//ust// if (!dentry)
305//ust// goto free_buf;
306//ust//
307//ust// buf->dentry = dentry;
308
309 goto free_name;
310
772030fe 311//ust//free_buf:
bb07823d
PMF
312 relay_destroy_buf(buf);
313 buf = NULL;
314free_name:
315//ust// kfree(tmpname);
772030fe 316//ust//end:
bb07823d
PMF
317 return buf;
318}
319
320/**
321 * relay_close_buf - close a channel buffer
322 * @buf: channel buffer
323 *
324 * Marks the buffer finalized and restores the default callbacks.
325 * The channel buffer and channel buffer data structure are then freed
326 * automatically when the last reference is given up.
327 */
328static void relay_close_buf(struct rchan_buf *buf)
329{
330//ust// del_timer_sync(&buf->timer);
331 kref_put(&buf->kref, relay_remove_buf);
332}
333
334//ust// static void setup_callbacks(struct rchan *chan,
335//ust// struct rchan_callbacks *cb)
336//ust// {
337//ust// if (!cb) {
338//ust// chan->cb = &default_channel_callbacks;
339//ust// return;
340//ust// }
341//ust//
342//ust// if (!cb->create_buf_file)
343//ust// cb->create_buf_file = create_buf_file_default_callback;
344//ust// if (!cb->remove_buf_file)
345//ust// cb->remove_buf_file = remove_buf_file_default_callback;
346//ust// chan->cb = cb;
347//ust// }
348
349/**
350 * relay_hotcpu_callback - CPU hotplug callback
351 * @nb: notifier block
352 * @action: hotplug action to take
353 * @hcpu: CPU number
354 *
355 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
356 */
357//ust// static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
358//ust// unsigned long action,
359//ust// void *hcpu)
360//ust// {
361//ust// unsigned int hotcpu = (unsigned long)hcpu;
362//ust// struct rchan *chan;
363//ust//
364//ust// switch (action) {
365//ust// case CPU_UP_PREPARE:
366//ust// case CPU_UP_PREPARE_FROZEN:
367//ust// mutex_lock(&relay_channels_mutex);
368//ust// list_for_each_entry(chan, &relay_channels, list) {
369//ust// if (chan->buf[hotcpu])
370//ust// continue;
371//ust// chan->buf[hotcpu] = relay_open_buf(chan, hotcpu);
372//ust// if (!chan->buf[hotcpu]) {
373//ust// printk(KERN_ERR
374//ust// "relay_hotcpu_callback: cpu %d buffer "
375//ust// "creation failed\n", hotcpu);
376//ust// mutex_unlock(&relay_channels_mutex);
377//ust// return NOTIFY_BAD;
378//ust// }
379//ust// }
380//ust// mutex_unlock(&relay_channels_mutex);
381//ust// break;
382//ust// case CPU_DEAD:
383//ust// case CPU_DEAD_FROZEN:
384//ust// /* No need to flush the cpu : will be flushed upon
385//ust// * final relay_flush() call. */
386//ust// break;
387//ust// }
388//ust// return NOTIFY_OK;
389//ust// }
390
391/**
392 * ltt_relay_open - create a new relay channel
393 * @base_filename: base name of files to create
394 * @parent: dentry of parent directory, %NULL for root directory
395 * @subbuf_size: size of sub-buffers
396 * @n_subbufs: number of sub-buffers
397 * @cb: client callback functions
398 * @private_data: user-defined data
399 *
400 * Returns channel pointer if successful, %NULL otherwise.
401 *
402 * Creates a channel buffer for each cpu using the sizes and
403 * attributes specified. The created channel buffer files
404 * will be named base_filename0...base_filenameN-1. File
405 * permissions will be %S_IRUSR.
406 */
407struct rchan *ltt_relay_open(const char *base_filename,
408 struct dentry *parent,
409 size_t subbuf_size,
410 size_t n_subbufs,
411 void *private_data)
412{
772030fe 413//ust// unsigned int i;
bb07823d
PMF
414 struct rchan *chan;
415//ust// if (!base_filename)
416//ust// return NULL;
417
418 if (!(subbuf_size && n_subbufs))
419 return NULL;
420
421 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
422 if (!chan)
423 return NULL;
424
425 chan->version = LTT_RELAY_CHANNEL_VERSION;
426 chan->n_subbufs = n_subbufs;
427 chan->subbuf_size = subbuf_size;
428 chan->subbuf_size_order = get_count_order(subbuf_size);
429 chan->alloc_size = FIX_SIZE(subbuf_size * n_subbufs);
430 chan->parent = parent;
431 chan->private_data = private_data;
432//ust// strlcpy(chan->base_filename, base_filename, NAME_MAX);
433//ust// setup_callbacks(chan, cb);
434 kref_init(&chan->kref);
435
436 mutex_lock(&relay_channels_mutex);
437//ust// for_each_online_cpu(i) {
438 chan->buf = relay_open_buf(chan);
439 if (!chan->buf)
440 goto error;
441//ust// }
442 list_add(&chan->list, &relay_channels);
443 mutex_unlock(&relay_channels_mutex);
444
445 return chan;
446
447//ust//free_bufs:
448//ust// for_each_possible_cpu(i) {
449//ust// if (!chan->buf[i])
450//ust// break;
451//ust// relay_close_buf(chan->buf[i]);
452//ust// }
453
454 error:
455 kref_put(&chan->kref, relay_destroy_channel);
456 mutex_unlock(&relay_channels_mutex);
457 return NULL;
458}
459//ust// EXPORT_SYMBOL_GPL(ltt_relay_open);
460
461/**
462 * ltt_relay_close - close the channel
463 * @chan: the channel
464 *
465 * Closes all channel buffers and frees the channel.
466 */
467void ltt_relay_close(struct rchan *chan)
468{
772030fe 469//ust// unsigned int i;
bb07823d
PMF
470
471 if (!chan)
472 return;
473
474 mutex_lock(&relay_channels_mutex);
475//ust// for_each_possible_cpu(i)
476 if (chan->buf)
477 relay_close_buf(chan->buf);
478
479 list_del(&chan->list);
480 kref_put(&chan->kref, relay_destroy_channel);
481 mutex_unlock(&relay_channels_mutex);
482}
483//ust// EXPORT_SYMBOL_GPL(ltt_relay_close);
484
485/*
486 * Start iteration at the previous element. Skip the real list head.
487 */
488//ust// struct buf_page *ltt_relay_find_prev_page(struct rchan_buf *buf,
489//ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
490//ust// {
491//ust// struct buf_page *iter;
492//ust// size_t orig_iter_off;
493//ust// unsigned int i = 0;
494//ust//
495//ust// orig_iter_off = page->offset;
496//ust// list_for_each_entry_reverse(iter, &page->list, list) {
497//ust// /*
498//ust// * Skip the real list head.
499//ust// */
500//ust// if (&iter->list == &buf->pages)
501//ust// continue;
502//ust// i++;
503//ust// if (offset >= iter->offset
504//ust// && offset < iter->offset + PAGE_SIZE) {
505//ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
506//ust// if (i > 1) {
507//ust// printk(KERN_WARNING
508//ust// "Backward random access detected in "
509//ust// "ltt_relay. Iterations %u, "
510//ust// "offset %zu, orig iter->off %zu, "
511//ust// "iter->off %zu diff_offset %zd.\n", i,
512//ust// offset, orig_iter_off, iter->offset,
513//ust// diff_offset);
514//ust// WARN_ON(1);
515//ust// }
516//ust// #endif
517//ust// return iter;
518//ust// }
519//ust// }
520//ust// WARN_ON(1);
521//ust// return NULL;
522//ust// }
523//ust// EXPORT_SYMBOL_GPL(ltt_relay_find_prev_page);
524
525/*
526 * Start iteration at the next element. Skip the real list head.
527 */
528//ust// struct buf_page *ltt_relay_find_next_page(struct rchan_buf *buf,
529//ust// struct buf_page *page, size_t offset, ssize_t diff_offset)
530//ust// {
531//ust// struct buf_page *iter;
532//ust// unsigned int i = 0;
533//ust// size_t orig_iter_off;
534//ust//
535//ust// orig_iter_off = page->offset;
536//ust// list_for_each_entry(iter, &page->list, list) {
537//ust// /*
538//ust// * Skip the real list head.
539//ust// */
540//ust// if (&iter->list == &buf->pages)
541//ust// continue;
542//ust// i++;
543//ust// if (offset >= iter->offset
544//ust// && offset < iter->offset + PAGE_SIZE) {
545//ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
546//ust// if (i > 1) {
547//ust// printk(KERN_WARNING
548//ust// "Forward random access detected in "
549//ust// "ltt_relay. Iterations %u, "
550//ust// "offset %zu, orig iter->off %zu, "
551//ust// "iter->off %zu diff_offset %zd.\n", i,
552//ust// offset, orig_iter_off, iter->offset,
553//ust// diff_offset);
554//ust// WARN_ON(1);
555//ust// }
556//ust// #endif
557//ust// return iter;
558//ust// }
559//ust// }
560//ust// WARN_ON(1);
561//ust// return NULL;
562//ust// }
563//ust// EXPORT_SYMBOL_GPL(ltt_relay_find_next_page);
564
565/**
566 * ltt_relay_write - write data to a ltt_relay buffer.
567 * @buf : buffer
568 * @offset : offset within the buffer
569 * @src : source address
570 * @len : length to write
571 * @page : cached buffer page
572 * @pagecpy : page size copied so far
573 */
574void _ltt_relay_write(struct rchan_buf *buf, size_t offset,
575 const void *src, size_t len, ssize_t cpy)
576{
577 do {
578 len -= cpy;
579 src += cpy;
580 offset += cpy;
581 /*
582 * Underlying layer should never ask for writes across
583 * subbuffers.
584 */
585 WARN_ON(offset >= buf->buf_size);
586
587 cpy = min_t(size_t, len, buf->buf_size - offset);
588 ltt_relay_do_copy(buf->buf_data + offset, src, cpy);
589 } while (unlikely(len != cpy));
590}
591//ust// EXPORT_SYMBOL_GPL(_ltt_relay_write);
592
593/**
594 * ltt_relay_read - read data from ltt_relay_buffer.
595 * @buf : buffer
596 * @offset : offset within the buffer
597 * @dest : destination address
598 * @len : length to write
599 */
600//ust// int ltt_relay_read(struct rchan_buf *buf, size_t offset,
601//ust// void *dest, size_t len)
602//ust// {
603//ust// struct buf_page *page;
604//ust// ssize_t pagecpy, orig_len;
605//ust//
606//ust// orig_len = len;
607//ust// offset &= buf->chan->alloc_size - 1;
608//ust// page = buf->rpage;
609//ust// if (unlikely(!len))
610//ust// return 0;
611//ust// for (;;) {
612//ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
613//ust// pagecpy = min_t(size_t, len, PAGE_SIZE - (offset & ~PAGE_MASK));
614//ust// memcpy(dest, page_address(page->page) + (offset & ~PAGE_MASK),
615//ust// pagecpy);
616//ust// len -= pagecpy;
617//ust// if (likely(!len))
618//ust// break;
619//ust// dest += pagecpy;
620//ust// offset += pagecpy;
621//ust// /*
622//ust// * Underlying layer should never ask for reads across
623//ust// * subbuffers.
624//ust// */
625//ust// WARN_ON(offset >= buf->chan->alloc_size);
626//ust// }
627//ust// return orig_len;
628//ust// }
629//ust// EXPORT_SYMBOL_GPL(ltt_relay_read);
630
631/**
632 * ltt_relay_read_get_page - Get a whole page to read from
633 * @buf : buffer
634 * @offset : offset within the buffer
635 */
636//ust// struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf, size_t offset)
637//ust// {
638//ust// struct buf_page *page;
639
640//ust// offset &= buf->chan->alloc_size - 1;
641//ust// page = buf->rpage;
642//ust// page = ltt_relay_cache_page(buf, &buf->rpage, page, offset);
643//ust// return page;
644//ust// }
645//ust// EXPORT_SYMBOL_GPL(ltt_relay_read_get_page);
646
647/**
648 * ltt_relay_offset_address - get address of a location within the buffer
649 * @buf : buffer
650 * @offset : offset within the buffer.
651 *
652 * Return the address where a given offset is located.
653 * Should be used to get the current subbuffer header pointer. Given we know
654 * it's never on a page boundary, it's safe to write directly to this address,
655 * as long as the write is never bigger than a page size.
656 */
657void *ltt_relay_offset_address(struct rchan_buf *buf, size_t offset)
658{
659//ust// struct buf_page *page;
660//ust// unsigned int odd;
661//ust//
662//ust// offset &= buf->chan->alloc_size - 1;
663//ust// odd = !!(offset & buf->chan->subbuf_size);
664//ust// page = buf->hpage[odd];
665//ust// if (offset < page->offset || offset >= page->offset + PAGE_SIZE)
666//ust// buf->hpage[odd] = page = buf->wpage;
667//ust// page = ltt_relay_cache_page(buf, &buf->hpage[odd], page, offset);
668//ust// return page_address(page->page) + (offset & ~PAGE_MASK);
c1dea0b3 669 return ((char *)buf->buf_data)+offset;
bb07823d
PMF
670 return NULL;
671}
672//ust// EXPORT_SYMBOL_GPL(ltt_relay_offset_address);
673
674/**
675 * relay_file_open - open file op for relay files
676 * @inode: the inode
677 * @filp: the file
678 *
679 * Increments the channel buffer refcount.
680 */
681//ust// static int relay_file_open(struct inode *inode, struct file *filp)
682//ust// {
683//ust// struct rchan_buf *buf = inode->i_private;
684//ust// kref_get(&buf->kref);
685//ust// filp->private_data = buf;
686//ust//
687//ust// return nonseekable_open(inode, filp);
688//ust// }
689
690/**
691 * relay_file_release - release file op for relay files
692 * @inode: the inode
693 * @filp: the file
694 *
695 * Decrements the channel refcount, as the filesystem is
696 * no longer using it.
697 */
698//ust// static int relay_file_release(struct inode *inode, struct file *filp)
699//ust// {
700//ust// struct rchan_buf *buf = filp->private_data;
701//ust// kref_put(&buf->kref, relay_remove_buf);
702//ust//
703//ust// return 0;
704//ust// }
705
706//ust// const struct file_operations ltt_relay_file_operations = {
707//ust// .open = relay_file_open,
708//ust// .release = relay_file_release,
709//ust// };
710//ust// EXPORT_SYMBOL_GPL(ltt_relay_file_operations);
711
712//ust// static __init int relay_init(void)
713//ust// {
714//ust// hotcpu_notifier(relay_hotcpu_callback, 5);
715//ust// return 0;
716//ust// }
717
718//ust// module_init(relay_init);
e1152c37
PMF
719/*
720 * ltt/ltt-relay.c
721 *
722 * (C) Copyright 2005-2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
723 *
724 * LTTng lockless buffer space management (reader/writer).
725 *
726 * Author:
727 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
728 *
729 * Inspired from LTT :
730 * Karim Yaghmour (karim@opersys.com)
731 * Tom Zanussi (zanussi@us.ibm.com)
732 * Bob Wisniewski (bob@watson.ibm.com)
733 * And from K42 :
734 * Bob Wisniewski (bob@watson.ibm.com)
735 *
736 * Changelog:
737 * 08/10/08, Cleanup.
738 * 19/10/05, Complete lockless mechanism.
739 * 27/05/05, Modular redesign and rewrite.
740 *
741 * Userspace reader semantic :
742 * while (poll fd != POLLHUP) {
743 * - ioctl RELAY_GET_SUBBUF_SIZE
744 * while (1) {
745 * - ioctl GET_SUBBUF
746 * - splice 1 subbuffer worth of data to a pipe
747 * - splice the data from pipe to disk/network
748 * - ioctl PUT_SUBBUF, check error value
749 * if err val < 0, previous subbuffer was corrupted.
750 * }
751 * }
752 */
753
bb07823d
PMF
754//ust// #include <linux/time.h>
755//ust// #include <linux/ltt-tracer.h>
756//ust// #include <linux/ltt-relay.h>
757//ust// #include <linux/module.h>
758//ust// #include <linux/string.h>
759//ust// #include <linux/slab.h>
760//ust// #include <linux/init.h>
761//ust// #include <linux/rcupdate.h>
762//ust// #include <linux/sched.h>
763//ust// #include <linux/bitops.h>
764//ust// #include <linux/fs.h>
765//ust// #include <linux/smp_lock.h>
766//ust// #include <linux/debugfs.h>
767//ust// #include <linux/stat.h>
768//ust// #include <linux/cpu.h>
769//ust// #include <linux/pipe_fs_i.h>
770//ust// #include <linux/splice.h>
771//ust// #include <asm/atomic.h>
772//ust// #include <asm/local.h>
e1152c37
PMF
773
774#if 0
775#define printk_dbg(fmt, args...) printk(fmt, args)
776#else
777#define printk_dbg(fmt, args...)
778#endif
779
e1152c37
PMF
780/*
781 * Last TSC comparison functions. Check if the current TSC overflows
782 * LTT_TSC_BITS bits from the last TSC read. Reads and writes last_tsc
783 * atomically.
784 */
785
786#if (BITS_PER_LONG == 32)
787static inline void save_last_tsc(struct ltt_channel_buf_struct *ltt_buf,
788 u64 tsc)
789{
790 ltt_buf->last_tsc = (unsigned long)(tsc >> LTT_TSC_BITS);
791}
792
793static inline int last_tsc_overflow(struct ltt_channel_buf_struct *ltt_buf,
794 u64 tsc)
795{
796 unsigned long tsc_shifted = (unsigned long)(tsc >> LTT_TSC_BITS);
797
798 if (unlikely((tsc_shifted - ltt_buf->last_tsc)))
799 return 1;
800 else
801 return 0;
802}
803#else
804static inline void save_last_tsc(struct ltt_channel_buf_struct *ltt_buf,
805 u64 tsc)
806{
807 ltt_buf->last_tsc = (unsigned long)tsc;
808}
809
810static inline int last_tsc_overflow(struct ltt_channel_buf_struct *ltt_buf,
811 u64 tsc)
812{
813 if (unlikely((tsc - ltt_buf->last_tsc) >> LTT_TSC_BITS))
814 return 1;
815 else
816 return 0;
817}
818#endif
819
5f54827b 820//ust// static struct file_operations ltt_file_operations;
e1152c37
PMF
821
822/*
823 * A switch is done during tracing or as a final flush after tracing (so it
824 * won't write in the new sub-buffer).
825 */
826enum force_switch_mode { FORCE_ACTIVE, FORCE_FLUSH };
827
828static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
829 struct ltt_channel_struct *ltt_chan,
830 struct rchan_buf *buf,
e1152c37
PMF
831 unsigned int n_subbufs);
832
bb07823d 833static void ltt_relay_destroy_buffer(struct ltt_channel_struct *ltt_chan);
e1152c37
PMF
834
835static void ltt_force_switch(struct rchan_buf *buf,
836 enum force_switch_mode mode);
837
838/*
839 * Trace callbacks
840 */
841static void ltt_buffer_begin_callback(struct rchan_buf *buf,
842 u64 tsc, unsigned int subbuf_idx)
843{
844 struct ltt_channel_struct *channel =
845 (struct ltt_channel_struct *)buf->chan->private_data;
846 struct ltt_subbuffer_header *header =
847 (struct ltt_subbuffer_header *)
848 ltt_relay_offset_address(buf,
849 subbuf_idx * buf->chan->subbuf_size);
850
851 header->cycle_count_begin = tsc;
852 header->lost_size = 0xFFFFFFFF; /* for debugging */
853 header->buf_size = buf->chan->subbuf_size;
854 ltt_write_trace_header(channel->trace, header);
855}
856
857/*
858 * offset is assumed to never be 0 here : never deliver a completely empty
859 * subbuffer. The lost size is between 0 and subbuf_size-1.
860 */
861static notrace void ltt_buffer_end_callback(struct rchan_buf *buf,
862 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
863{
864 struct ltt_channel_struct *channel =
865 (struct ltt_channel_struct *)buf->chan->private_data;
bb07823d 866 struct ltt_channel_buf_struct *ltt_buf = channel->buf;
e1152c37
PMF
867 struct ltt_subbuffer_header *header =
868 (struct ltt_subbuffer_header *)
869 ltt_relay_offset_address(buf,
870 subbuf_idx * buf->chan->subbuf_size);
871
872 header->lost_size = SUBBUF_OFFSET((buf->chan->subbuf_size - offset),
873 buf->chan);
874 header->cycle_count_end = tsc;
c1dea0b3
PMF
875 header->events_lost = local_read(&ltt_buf->events_lost);
876 header->subbuf_corrupt = local_read(&ltt_buf->corrupted_subbuffers);
877
e1152c37
PMF
878}
879
46ef48cd
PMF
880void (*wake_consumer)(void *, int) = NULL;
881
882void relay_set_wake_consumer(void (*wake)(void *, int))
883{
884 wake_consumer = wake;
885}
886
887void relay_wake_consumer(void *arg, int finished)
888{
889 if(wake_consumer)
890 wake_consumer(arg, finished);
891}
892
e1152c37 893static notrace void ltt_deliver(struct rchan_buf *buf, unsigned int subbuf_idx,
8431032f 894 long commit_count)
e1152c37
PMF
895{
896 struct ltt_channel_struct *channel =
897 (struct ltt_channel_struct *)buf->chan->private_data;
bb07823d 898 struct ltt_channel_buf_struct *ltt_buf = channel->buf;
3a7b90de 899 int result;
e1152c37 900
8431032f
PMF
901//ust// #ifdef CONFIG_LTT_VMCORE
902 local_set(&ltt_buf->commit_seq[subbuf_idx], commit_count);
903//ust// #endif
904
905 /* wakeup consumer */
3a7b90de
PMF
906 result = write(ltt_buf->data_ready_fd_write, "1", 1);
907 if(result == -1) {
908 PERROR("write (in ltt_relay_buffer_flush)");
909 ERR("this should never happen!");
910 }
46ef48cd 911//ust// atomic_set(&ltt_buf->wakeup_readers, 1);
e1152c37
PMF
912}
913
c1dea0b3 914static struct dentry *ltt_create_buf_file_callback(struct rchan_buf *buf)
e1152c37
PMF
915{
916 struct ltt_channel_struct *ltt_chan;
917 int err;
5f54827b 918//ust// struct dentry *dentry;
e1152c37
PMF
919
920 ltt_chan = buf->chan->private_data;
bb07823d 921 err = ltt_relay_create_buffer(ltt_chan->trace, ltt_chan, buf, buf->chan->n_subbufs);
e1152c37
PMF
922 if (err)
923 return ERR_PTR(err);
924
5f54827b
PMF
925//ust// dentry = debugfs_create_file(filename, mode, parent, buf,
926//ust// &ltt_file_operations);
927//ust// if (!dentry)
928//ust// goto error;
929//ust// return dentry;
c1dea0b3 930 return NULL; //ust//
5f54827b 931//ust//error:
bb07823d 932 ltt_relay_destroy_buffer(ltt_chan);
e1152c37
PMF
933 return NULL;
934}
935
772030fe
PMF
936//ust// static int ltt_remove_buf_file_callback(struct rchan_buf *buf)
937//ust// {
938//ust// //ust// struct rchan_buf *buf = dentry->d_inode->i_private;
939//ust// struct ltt_channel_struct *ltt_chan = buf->chan->private_data;
940//ust//
941//ust// //ust// debugfs_remove(dentry);
942//ust// ltt_relay_destroy_buffer(ltt_chan);
943//ust//
944//ust// return 0;
945//ust// }
e1152c37
PMF
946
947/*
948 * Wake writers :
949 *
950 * This must be done after the trace is removed from the RCU list so that there
951 * are no stalled writers.
952 */
bb07823d
PMF
953//ust// static void ltt_relay_wake_writers(struct ltt_channel_buf_struct *ltt_buf)
954//ust// {
955//ust//
956//ust// if (waitqueue_active(&ltt_buf->write_wait))
957//ust// wake_up_interruptible(&ltt_buf->write_wait);
958//ust// }
e1152c37
PMF
959
960/*
961 * This function should not be called from NMI interrupt context
962 */
963static notrace void ltt_buf_unfull(struct rchan_buf *buf,
964 unsigned int subbuf_idx,
965 long offset)
966{
bb07823d
PMF
967//ust// struct ltt_channel_struct *ltt_channel =
968//ust// (struct ltt_channel_struct *)buf->chan->private_data;
969//ust// struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
970//ust//
971//ust// ltt_relay_wake_writers(ltt_buf);
e1152c37
PMF
972}
973
974/**
975 * ltt_open - open file op for ltt files
976 * @inode: opened inode
977 * @file: opened file
978 *
979 * Open implementation. Makes sure only one open instance of a buffer is
980 * done at a given moment.
981 */
bb07823d
PMF
982//ust// static int ltt_open(struct inode *inode, struct file *file)
983//ust// {
984//ust// struct rchan_buf *buf = inode->i_private;
985//ust// struct ltt_channel_struct *ltt_channel =
986//ust// (struct ltt_channel_struct *)buf->chan->private_data;
987//ust// struct ltt_channel_buf_struct *ltt_buf =
988//ust// percpu_ptr(ltt_channel->buf, buf->cpu);
989//ust//
990//ust// if (!atomic_long_add_unless(&ltt_buf->active_readers, 1, 1))
991//ust// return -EBUSY;
992//ust// return ltt_relay_file_operations.open(inode, file);
993//ust// }
e1152c37
PMF
994
995/**
996 * ltt_release - release file op for ltt files
997 * @inode: opened inode
998 * @file: opened file
999 *
1000 * Release implementation.
1001 */
bb07823d
PMF
1002//ust// static int ltt_release(struct inode *inode, struct file *file)
1003//ust// {
1004//ust// struct rchan_buf *buf = inode->i_private;
1005//ust// struct ltt_channel_struct *ltt_channel =
1006//ust// (struct ltt_channel_struct *)buf->chan->private_data;
1007//ust// struct ltt_channel_buf_struct *ltt_buf =
1008//ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1009//ust// int ret;
1010//ust//
1011//ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1012//ust// atomic_long_dec(&ltt_buf->active_readers);
1013//ust// ret = ltt_relay_file_operations.release(inode, file);
1014//ust// WARN_ON(ret);
1015//ust// return ret;
1016//ust// }
e1152c37
PMF
1017
1018/**
1019 * ltt_poll - file op for ltt files
1020 * @filp: the file
1021 * @wait: poll table
1022 *
1023 * Poll implementation.
1024 */
bb07823d
PMF
1025//ust// static unsigned int ltt_poll(struct file *filp, poll_table *wait)
1026//ust// {
1027//ust// unsigned int mask = 0;
1028//ust// struct inode *inode = filp->f_dentry->d_inode;
1029//ust// struct rchan_buf *buf = inode->i_private;
1030//ust// struct ltt_channel_struct *ltt_channel =
1031//ust// (struct ltt_channel_struct *)buf->chan->private_data;
1032//ust// struct ltt_channel_buf_struct *ltt_buf =
1033//ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1034//ust//
1035//ust// if (filp->f_mode & FMODE_READ) {
1036//ust// poll_wait_set_exclusive(wait);
1037//ust// poll_wait(filp, &buf->read_wait, wait);
1038//ust//
1039//ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1040//ust// if (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1041//ust// buf->chan)
1042//ust// - SUBBUF_TRUNC(atomic_long_read(&ltt_buf->consumed),
1043//ust// buf->chan)
1044//ust// == 0) {
1045//ust// if (buf->finalized)
1046//ust// return POLLHUP;
1047//ust// else
1048//ust// return 0;
1049//ust// } else {
1050//ust// struct rchan *rchan =
1051//ust// ltt_channel->trans_channel_data;
1052//ust// if (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1053//ust// buf->chan)
1054//ust// - SUBBUF_TRUNC(atomic_long_read(
1055//ust// &ltt_buf->consumed),
1056//ust// buf->chan)
1057//ust// >= rchan->alloc_size)
1058//ust// return POLLPRI | POLLRDBAND;
1059//ust// else
1060//ust// return POLLIN | POLLRDNORM;
1061//ust// }
1062//ust// }
1063//ust// return mask;
1064//ust// }
e1152c37 1065
9c67dc50 1066int ltt_do_get_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, long *pconsumed_old)
e1152c37 1067{
bb07823d 1068 struct ltt_channel_struct *ltt_channel = (struct ltt_channel_struct *)buf->chan->private_data;
e1152c37
PMF
1069 long consumed_old, consumed_idx, commit_count, write_offset;
1070 consumed_old = atomic_long_read(&ltt_buf->consumed);
1071 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1072 commit_count = local_read(&ltt_buf->commit_count[consumed_idx]);
1073 /*
1074 * Make sure we read the commit count before reading the buffer
1075 * data and the write offset. Correct consumed offset ordering
1076 * wrt commit count is insured by the use of cmpxchg to update
1077 * the consumed offset.
1078 */
1079 smp_rmb();
1080 write_offset = local_read(&ltt_buf->offset);
1081 /*
1082 * Check that the subbuffer we are trying to consume has been
1083 * already fully committed.
1084 */
1085 if (((commit_count - buf->chan->subbuf_size)
1086 & ltt_channel->commit_count_mask)
1087 - (BUFFER_TRUNC(consumed_old, buf->chan)
1088 >> ltt_channel->n_subbufs_order)
1089 != 0) {
1090 return -EAGAIN;
1091 }
1092 /*
1093 * Check that we are not about to read the same subbuffer in
1094 * which the writer head is.
1095 */
1096 if ((SUBBUF_TRUNC(write_offset, buf->chan)
1097 - SUBBUF_TRUNC(consumed_old, buf->chan))
1098 == 0) {
1099 return -EAGAIN;
1100 }
1101
1102 *pconsumed_old = consumed_old;
1103 return 0;
1104}
1105
9c67dc50 1106int ltt_do_put_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, u32 uconsumed_old)
e1152c37
PMF
1107{
1108 long consumed_new, consumed_old;
1109
1110 consumed_old = atomic_long_read(&ltt_buf->consumed);
1111 consumed_old = consumed_old & (~0xFFFFFFFFL);
1112 consumed_old = consumed_old | uconsumed_old;
1113 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
1114
46ef48cd 1115//ust// spin_lock(&ltt_buf->full_lock);
e1152c37
PMF
1116 if (atomic_long_cmpxchg(&ltt_buf->consumed, consumed_old,
1117 consumed_new)
1118 != consumed_old) {
1119 /* We have been pushed by the writer : the last
1120 * buffer read _is_ corrupted! It can also
1121 * happen if this is a buffer we never got. */
46ef48cd 1122//ust// spin_unlock(&ltt_buf->full_lock);
e1152c37
PMF
1123 return -EIO;
1124 } else {
1125 /* tell the client that buffer is now unfull */
1126 int index;
1127 long data;
1128 index = SUBBUF_INDEX(consumed_old, buf->chan);
1129 data = BUFFER_OFFSET(consumed_old, buf->chan);
1130 ltt_buf_unfull(buf, index, data);
46ef48cd 1131//ust// spin_unlock(&ltt_buf->full_lock);
e1152c37
PMF
1132 }
1133 return 0;
1134}
1135
1136/**
1137 * ltt_ioctl - control on the debugfs file
1138 *
1139 * @inode: the inode
1140 * @filp: the file
1141 * @cmd: the command
1142 * @arg: command arg
1143 *
1144 * This ioctl implements three commands necessary for a minimal
1145 * producer/consumer implementation :
1146 * RELAY_GET_SUBBUF
1147 * Get the next sub buffer that can be read. It never blocks.
1148 * RELAY_PUT_SUBBUF
1149 * Release the currently read sub-buffer. Parameter is the last
1150 * put subbuffer (returned by GET_SUBBUF).
1151 * RELAY_GET_N_BUBBUFS
1152 * returns the number of sub buffers in the per cpu channel.
1153 * RELAY_GET_SUBBUF_SIZE
1154 * returns the size of the sub buffers.
1155 */
5f54827b
PMF
1156//ust// static int ltt_ioctl(struct inode *inode, struct file *filp,
1157//ust// unsigned int cmd, unsigned long arg)
1158//ust// {
1159//ust// struct rchan_buf *buf = inode->i_private;
1160//ust// struct ltt_channel_struct *ltt_channel =
1161//ust// (struct ltt_channel_struct *)buf->chan->private_data;
1162//ust// struct ltt_channel_buf_struct *ltt_buf =
1163//ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1164//ust// u32 __user *argp = (u32 __user *)arg;
1165//ust//
1166//ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1167//ust// switch (cmd) {
1168//ust// case RELAY_GET_SUBBUF:
1169//ust// {
1170//ust// int ret;
1171//ust// ret = ltt_do_get_subbuf(buf, ltt_buf, &consumed_old);
1172//ust// if(ret < 0)
1173//ust// return ret;
1174//ust// return put_user((u32)consumed_old, argp);
1175//ust// }
1176//ust// case RELAY_PUT_SUBBUF:
1177//ust// {
1178//ust// int ret;
1179//ust// u32 uconsumed_old;
1180//ust// ret = get_user(uconsumed_old, argp);
1181//ust// if (ret)
1182//ust// return ret; /* will return -EFAULT */
1183//ust// return ltt_do_put_subbuf(buf, ltt_buf, uconsumed_old);
1184//ust// }
1185//ust// case RELAY_GET_N_SUBBUFS:
1186//ust// return put_user((u32)buf->chan->n_subbufs, argp);
1187//ust// break;
1188//ust// case RELAY_GET_SUBBUF_SIZE:
1189//ust// return put_user((u32)buf->chan->subbuf_size, argp);
1190//ust// break;
1191//ust// default:
1192//ust// return -ENOIOCTLCMD;
1193//ust// }
1194//ust// return 0;
1195//ust// }
1196
1197//ust// #ifdef CONFIG_COMPAT
1198//ust// static long ltt_compat_ioctl(struct file *file, unsigned int cmd,
1199//ust// unsigned long arg)
1200//ust// {
1201//ust// long ret = -ENOIOCTLCMD;
1202//ust//
1203//ust// lock_kernel();
1204//ust// ret = ltt_ioctl(file->f_dentry->d_inode, file, cmd, arg);
1205//ust// unlock_kernel();
1206//ust//
1207//ust// return ret;
1208//ust// }
1209//ust// #endif
1210
1211//ust// static void ltt_relay_pipe_buf_release(struct pipe_inode_info *pipe,
1212//ust// struct pipe_buffer *pbuf)
1213//ust// {
1214//ust// }
1215//ust//
1216//ust// static struct pipe_buf_operations ltt_relay_pipe_buf_ops = {
1217//ust// .can_merge = 0,
1218//ust// .map = generic_pipe_buf_map,
1219//ust// .unmap = generic_pipe_buf_unmap,
1220//ust// .confirm = generic_pipe_buf_confirm,
1221//ust// .release = ltt_relay_pipe_buf_release,
1222//ust// .steal = generic_pipe_buf_steal,
1223//ust// .get = generic_pipe_buf_get,
1224//ust// };
1225
1226//ust// static void ltt_relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1227//ust// {
1228//ust// }
e1152c37
PMF
1229
1230/*
1231 * subbuf_splice_actor - splice up to one subbuf's worth of data
1232 */
bb07823d
PMF
1233//ust// static int subbuf_splice_actor(struct file *in,
1234//ust// loff_t *ppos,
1235//ust// struct pipe_inode_info *pipe,
1236//ust// size_t len,
1237//ust// unsigned int flags)
1238//ust// {
1239//ust// struct rchan_buf *buf = in->private_data;
1240//ust// struct ltt_channel_struct *ltt_channel =
1241//ust// (struct ltt_channel_struct *)buf->chan->private_data;
1242//ust// struct ltt_channel_buf_struct *ltt_buf =
1243//ust// percpu_ptr(ltt_channel->buf, buf->cpu);
1244//ust// unsigned int poff, subbuf_pages, nr_pages;
1245//ust// struct page *pages[PIPE_BUFFERS];
1246//ust// struct partial_page partial[PIPE_BUFFERS];
1247//ust// struct splice_pipe_desc spd = {
1248//ust// .pages = pages,
1249//ust// .nr_pages = 0,
1250//ust// .partial = partial,
1251//ust// .flags = flags,
1252//ust// .ops = &ltt_relay_pipe_buf_ops,
1253//ust// .spd_release = ltt_relay_page_release,
1254//ust// };
1255//ust// long consumed_old, consumed_idx, roffset;
1256//ust// unsigned long bytes_avail;
1257//ust//
1258//ust// /*
1259//ust// * Check that a GET_SUBBUF ioctl has been done before.
1260//ust// */
1261//ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1262//ust// consumed_old = atomic_long_read(&ltt_buf->consumed);
1263//ust// consumed_old += *ppos;
1264//ust// consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1265//ust//
1266//ust// /*
1267//ust// * Adjust read len, if longer than what is available
1268//ust// */
1269//ust// bytes_avail = SUBBUF_TRUNC(local_read(&ltt_buf->offset), buf->chan)
1270//ust// - consumed_old;
1271//ust// WARN_ON(bytes_avail > buf->chan->alloc_size);
1272//ust// len = min_t(size_t, len, bytes_avail);
1273//ust// subbuf_pages = bytes_avail >> PAGE_SHIFT;
1274//ust// nr_pages = min_t(unsigned int, subbuf_pages, PIPE_BUFFERS);
1275//ust// roffset = consumed_old & PAGE_MASK;
1276//ust// poff = consumed_old & ~PAGE_MASK;
1277//ust// printk_dbg(KERN_DEBUG "SPLICE actor len %zu pos %zd write_pos %ld\n",
1278//ust// len, (ssize_t)*ppos, local_read(&ltt_buf->offset));
1279//ust//
1280//ust// for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
1281//ust// unsigned int this_len;
1282//ust// struct buf_page *page;
1283//ust//
1284//ust// if (!len)
1285//ust// break;
1286//ust// printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
1287//ust// len, roffset);
1288//ust//
1289//ust// this_len = PAGE_SIZE - poff;
1290//ust// page = ltt_relay_read_get_page(buf, roffset);
1291//ust// spd.pages[spd.nr_pages] = page->page;
1292//ust// spd.partial[spd.nr_pages].offset = poff;
1293//ust// spd.partial[spd.nr_pages].len = this_len;
1294//ust//
1295//ust// poff = 0;
1296//ust// roffset += PAGE_SIZE;
1297//ust// len -= this_len;
1298//ust// }
1299//ust//
1300//ust// if (!spd.nr_pages)
1301//ust// return 0;
1302//ust//
1303//ust// return splice_to_pipe(pipe, &spd);
1304//ust// }
e1152c37 1305
bb07823d
PMF
1306//ust// static ssize_t ltt_relay_file_splice_read(struct file *in,
1307//ust// loff_t *ppos,
1308//ust// struct pipe_inode_info *pipe,
1309//ust// size_t len,
1310//ust// unsigned int flags)
1311//ust// {
1312//ust// ssize_t spliced;
1313//ust// int ret;
1314//ust//
1315//ust// ret = 0;
1316//ust// spliced = 0;
1317//ust//
1318//ust// printk_dbg(KERN_DEBUG "SPLICE read len %zu pos %zd\n",
1319//ust// len, (ssize_t)*ppos);
1320//ust// while (len && !spliced) {
1321//ust// ret = subbuf_splice_actor(in, ppos, pipe, len, flags);
1322//ust// printk_dbg(KERN_DEBUG "SPLICE read loop ret %d\n", ret);
1323//ust// if (ret < 0)
1324//ust// break;
1325//ust// else if (!ret) {
1326//ust// if (flags & SPLICE_F_NONBLOCK)
1327//ust// ret = -EAGAIN;
1328//ust// break;
1329//ust// }
1330//ust//
1331//ust// *ppos += ret;
1332//ust// if (ret > len)
1333//ust// len = 0;
1334//ust// else
1335//ust// len -= ret;
1336//ust// spliced += ret;
1337//ust// }
1338//ust//
1339//ust// if (spliced)
1340//ust// return spliced;
1341//ust//
1342//ust// return ret;
1343//ust// }
e1152c37
PMF
1344
1345static void ltt_relay_print_subbuffer_errors(
1346 struct ltt_channel_struct *ltt_chan,
bb07823d 1347 long cons_off)
e1152c37
PMF
1348{
1349 struct rchan *rchan = ltt_chan->trans_channel_data;
bb07823d 1350 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
e1152c37
PMF
1351 long cons_idx, commit_count, write_offset;
1352
1353 cons_idx = SUBBUF_INDEX(cons_off, rchan);
1354 commit_count = local_read(&ltt_buf->commit_count[cons_idx]);
1355 /*
1356 * No need to order commit_count and write_offset reads because we
1357 * execute after trace is stopped when there are no readers left.
1358 */
1359 write_offset = local_read(&ltt_buf->offset);
1360 printk(KERN_WARNING
1361 "LTT : unread channel %s offset is %ld "
bb07823d
PMF
1362 "and cons_off : %ld\n",
1363 ltt_chan->channel_name, write_offset, cons_off);
e1152c37
PMF
1364 /* Check each sub-buffer for non filled commit count */
1365 if (((commit_count - rchan->subbuf_size) & ltt_chan->commit_count_mask)
1366 - (BUFFER_TRUNC(cons_off, rchan) >> ltt_chan->n_subbufs_order)
1367 != 0)
1368 printk(KERN_ALERT
1369 "LTT : %s : subbuffer %lu has non filled "
1370 "commit count %lu.\n",
1371 ltt_chan->channel_name, cons_idx, commit_count);
1372 printk(KERN_ALERT "LTT : %s : commit count : %lu, subbuf size %zd\n",
1373 ltt_chan->channel_name, commit_count,
1374 rchan->subbuf_size);
1375}
1376
1377static void ltt_relay_print_errors(struct ltt_trace_struct *trace,
bb07823d 1378 struct ltt_channel_struct *ltt_chan)
e1152c37
PMF
1379{
1380 struct rchan *rchan = ltt_chan->trans_channel_data;
bb07823d 1381 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
e1152c37
PMF
1382 long cons_off;
1383
1384 for (cons_off = atomic_long_read(&ltt_buf->consumed);
1385 (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1386 rchan)
1387 - cons_off) > 0;
1388 cons_off = SUBBUF_ALIGN(cons_off, rchan))
bb07823d 1389 ltt_relay_print_subbuffer_errors(ltt_chan, cons_off);
e1152c37
PMF
1390}
1391
bb07823d 1392static void ltt_relay_print_buffer_errors(struct ltt_channel_struct *ltt_chan)
e1152c37
PMF
1393{
1394 struct ltt_trace_struct *trace = ltt_chan->trace;
bb07823d 1395 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
e1152c37
PMF
1396
1397 if (local_read(&ltt_buf->events_lost))
1398 printk(KERN_ALERT
1399 "LTT : %s : %ld events lost "
bb07823d 1400 "in %s channel.\n",
e1152c37
PMF
1401 ltt_chan->channel_name,
1402 local_read(&ltt_buf->events_lost),
bb07823d 1403 ltt_chan->channel_name);
e1152c37
PMF
1404 if (local_read(&ltt_buf->corrupted_subbuffers))
1405 printk(KERN_ALERT
1406 "LTT : %s : %ld corrupted subbuffers "
bb07823d 1407 "in %s channel.\n",
e1152c37
PMF
1408 ltt_chan->channel_name,
1409 local_read(&ltt_buf->corrupted_subbuffers),
bb07823d 1410 ltt_chan->channel_name);
e1152c37 1411
bb07823d 1412 ltt_relay_print_errors(trace, ltt_chan);
e1152c37
PMF
1413}
1414
bb07823d
PMF
1415static void ltt_relay_remove_dirs(struct ltt_trace_struct *trace)
1416{
5f54827b 1417//ust// debugfs_remove(trace->dentry.trace_root);
bb07823d 1418}
e1152c37
PMF
1419
1420static void ltt_relay_release_channel(struct kref *kref)
1421{
1422 struct ltt_channel_struct *ltt_chan = container_of(kref,
1423 struct ltt_channel_struct, kref);
bb07823d 1424 free(ltt_chan->buf);
e1152c37
PMF
1425}
1426
1427/*
1428 * Create ltt buffer.
1429 */
5f54827b
PMF
1430//ust// static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1431//ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
1432//ust// unsigned int cpu, unsigned int n_subbufs)
1433//ust// {
1434//ust// struct ltt_channel_buf_struct *ltt_buf =
1435//ust// percpu_ptr(ltt_chan->buf, cpu);
1436//ust// unsigned int j;
1437//ust//
1438//ust// ltt_buf->commit_count =
1439//ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
1440//ust// GFP_KERNEL, cpu_to_node(cpu));
1441//ust// if (!ltt_buf->commit_count)
1442//ust// return -ENOMEM;
1443//ust// kref_get(&trace->kref);
1444//ust// kref_get(&trace->ltt_transport_kref);
1445//ust// kref_get(&ltt_chan->kref);
1446//ust// local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
1447//ust// atomic_long_set(&ltt_buf->consumed, 0);
1448//ust// atomic_long_set(&ltt_buf->active_readers, 0);
1449//ust// for (j = 0; j < n_subbufs; j++)
1450//ust// local_set(&ltt_buf->commit_count[j], 0);
1451//ust// init_waitqueue_head(&ltt_buf->write_wait);
1452//ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1453//ust// spin_lock_init(&ltt_buf->full_lock);
1454//ust//
1455//ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1456//ust// /* atomic_add made on local variable on data that belongs to
1457//ust// * various CPUs : ok because tracing not started (for this cpu). */
1458//ust// local_add(ltt_subbuffer_header_size(), &ltt_buf->commit_count[0]);
1459//ust//
1460//ust// local_set(&ltt_buf->events_lost, 0);
1461//ust// local_set(&ltt_buf->corrupted_subbuffers, 0);
1462//ust//
1463//ust// return 0;
1464//ust// }
1465
e1152c37
PMF
1466static int ltt_relay_create_buffer(struct ltt_trace_struct *trace,
1467 struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
bb07823d 1468 unsigned int n_subbufs)
e1152c37 1469{
5f54827b 1470 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
e1152c37 1471 unsigned int j;
3a7b90de
PMF
1472 int fds[2];
1473 int result;
e1152c37 1474
8431032f
PMF
1475 ltt_buf->commit_count =
1476 zmalloc(sizeof(ltt_buf->commit_count) * n_subbufs);
1477 if (!ltt_buf->commit_count)
1478 return -ENOMEM;
e1152c37
PMF
1479 kref_get(&trace->kref);
1480 kref_get(&trace->ltt_transport_kref);
1481 kref_get(&ltt_chan->kref);
c1dea0b3 1482 local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
e1152c37
PMF
1483 atomic_long_set(&ltt_buf->consumed, 0);
1484 atomic_long_set(&ltt_buf->active_readers, 0);
1485 for (j = 0; j < n_subbufs; j++)
1486 local_set(&ltt_buf->commit_count[j], 0);
5f54827b 1487//ust// init_waitqueue_head(&ltt_buf->write_wait);
46ef48cd
PMF
1488//ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1489//ust// spin_lock_init(&ltt_buf->full_lock);
e1152c37
PMF
1490
1491 ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
e1152c37 1492
c1dea0b3 1493 local_add(ltt_subbuffer_header_size(), &ltt_buf->commit_count[0]);
5f54827b 1494
c1dea0b3
PMF
1495 local_set(&ltt_buf->events_lost, 0);
1496 local_set(&ltt_buf->corrupted_subbuffers, 0);
e1152c37 1497
3a7b90de
PMF
1498 result = pipe(fds);
1499 if(result == -1) {
1500 PERROR("pipe");
1501 return -1;
1502 }
1503 ltt_buf->data_ready_fd_read = fds[0];
1504 ltt_buf->data_ready_fd_write = fds[1];
46ef48cd 1505
8431032f
PMF
1506//ust// ltt_buf->commit_seq = malloc(sizeof(ltt_buf->commit_seq) * n_subbufs);
1507//ust// if(!ltt_buf->commit_seq) {
1508//ust// return -1;
1509//ust// }
1510
1511 /* FIXME: decrementally destroy on error */
1512
e1152c37
PMF
1513 return 0;
1514}
1515
bb07823d 1516static void ltt_relay_destroy_buffer(struct ltt_channel_struct *ltt_chan)
e1152c37
PMF
1517{
1518 struct ltt_trace_struct *trace = ltt_chan->trace;
bb07823d 1519 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
e1152c37
PMF
1520
1521 kref_put(&ltt_chan->trace->ltt_transport_kref,
1522 ltt_release_transport);
bb07823d 1523 ltt_relay_print_buffer_errors(ltt_chan);
8431032f
PMF
1524//ust// free(ltt_buf->commit_seq);
1525 kfree(ltt_buf->commit_count);
1526 ltt_buf->commit_count = NULL;
e1152c37
PMF
1527 kref_put(&ltt_chan->kref, ltt_relay_release_channel);
1528 kref_put(&trace->kref, ltt_release_trace);
bb07823d 1529//ust// wake_up_interruptible(&trace->kref_wq);
e1152c37
PMF
1530}
1531
8cefc145
PMF
1532static void ltt_chan_alloc_ltt_buf(struct ltt_channel_struct *ltt_chan)
1533{
1534 void *ptr;
1535 int result;
1536
1537 /* Get one page */
8431032f 1538 /* FIXME: increase size if we have a seq_commit array that overflows the page */
8cefc145
PMF
1539 size_t size = PAGE_ALIGN(1);
1540
1541 result = ltt_chan->buf_shmid = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700);
1542 if(ltt_chan->buf_shmid == -1) {
1543 PERROR("shmget");
872037bb 1544 return;
8cefc145
PMF
1545 }
1546
1547 ptr = shmat(ltt_chan->buf_shmid, NULL, 0);
1548 if(ptr == (void *) -1) {
1549 perror("shmat");
1550 goto destroy_shmem;
1551 }
1552
1553 /* Already mark the shared memory for destruction. This will occur only
1554 * when all users have detached.
1555 */
1556 result = shmctl(ltt_chan->buf_shmid, IPC_RMID, NULL);
1557 if(result == -1) {
1558 perror("shmctl");
872037bb 1559 return;
8cefc145
PMF
1560 }
1561
1562 ltt_chan->buf = ptr;
1563
872037bb 1564 return;
8cefc145
PMF
1565
1566 destroy_shmem:
1567 result = shmctl(ltt_chan->buf_shmid, IPC_RMID, NULL);
1568 if(result == -1) {
1569 perror("shmctl");
1570 }
1571
872037bb 1572 return;
8cefc145
PMF
1573}
1574
e1152c37
PMF
1575/*
1576 * Create channel.
1577 */
1578static int ltt_relay_create_channel(const char *trace_name,
1579 struct ltt_trace_struct *trace, struct dentry *dir,
1580 const char *channel_name, struct ltt_channel_struct *ltt_chan,
1581 unsigned int subbuf_size, unsigned int n_subbufs,
1582 int overwrite)
1583{
1584 char *tmpname;
1585 unsigned int tmpname_len;
1586 int err = 0;
1587
1588 tmpname = kmalloc(PATH_MAX, GFP_KERNEL);
1589 if (!tmpname)
1590 return EPERM;
1591 if (overwrite) {
1592 strncpy(tmpname, LTT_FLIGHT_PREFIX, PATH_MAX-1);
1593 strncat(tmpname, channel_name,
1594 PATH_MAX-1-sizeof(LTT_FLIGHT_PREFIX));
1595 } else {
1596 strncpy(tmpname, channel_name, PATH_MAX-1);
1597 }
1598 strncat(tmpname, "_", PATH_MAX-1-strlen(tmpname));
1599
1600 kref_init(&ltt_chan->kref);
1601
1602 ltt_chan->trace = trace;
1603 ltt_chan->buffer_begin = ltt_buffer_begin_callback;
1604 ltt_chan->buffer_end = ltt_buffer_end_callback;
1605 ltt_chan->overwrite = overwrite;
1606 ltt_chan->n_subbufs_order = get_count_order(n_subbufs);
1607 ltt_chan->commit_count_mask = (~0UL >> ltt_chan->n_subbufs_order);
bb07823d 1608//ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
8cefc145
PMF
1609
1610 ltt_chan_alloc_ltt_buf(ltt_chan);
1611
1612//ust// ltt_chan->buf = malloc(sizeof(struct ltt_channel_buf_struct));
e1152c37 1613 if (!ltt_chan->buf)
bb07823d 1614 goto alloc_error;
e1152c37
PMF
1615 ltt_chan->trans_channel_data = ltt_relay_open(tmpname,
1616 dir,
1617 subbuf_size,
1618 n_subbufs,
e1152c37
PMF
1619 ltt_chan);
1620 tmpname_len = strlen(tmpname);
1621 if (tmpname_len > 0) {
1622 /* Remove final _ for pretty printing */
1623 tmpname[tmpname_len-1] = '\0';
1624 }
1625 if (ltt_chan->trans_channel_data == NULL) {
1626 printk(KERN_ERR "LTT : Can't open %s channel for trace %s\n",
1627 tmpname, trace_name);
1628 goto relay_open_error;
1629 }
1630
1631 err = 0;
1632 goto end;
1633
1634relay_open_error:
bb07823d
PMF
1635//ust// percpu_free(ltt_chan->buf);
1636alloc_error:
e1152c37
PMF
1637 err = EPERM;
1638end:
1639 kfree(tmpname);
1640 return err;
1641}
1642
bb07823d
PMF
1643static int ltt_relay_create_dirs(struct ltt_trace_struct *new_trace)
1644{
1645//ust// new_trace->dentry.trace_root = debugfs_create_dir(new_trace->trace_name,
1646//ust// get_ltt_root());
1647//ust// if (new_trace->dentry.trace_root == NULL) {
1648//ust// printk(KERN_ERR "LTT : Trace directory name %s already taken\n",
1649//ust// new_trace->trace_name);
1650//ust// return EEXIST;
1651//ust// }
1652
1653//ust// new_trace->callbacks.create_buf_file = ltt_create_buf_file_callback;
1654//ust// new_trace->callbacks.remove_buf_file = ltt_remove_buf_file_callback;
1655
1656 return 0;
1657}
e1152c37
PMF
1658
1659/*
1660 * LTTng channel flush function.
1661 *
1662 * Must be called when no tracing is active in the channel, because of
1663 * accesses across CPUs.
1664 */
1665static notrace void ltt_relay_buffer_flush(struct rchan_buf *buf)
1666{
46ef48cd
PMF
1667 struct ltt_channel_struct *channel =
1668 (struct ltt_channel_struct *)buf->chan->private_data;
1669 struct ltt_channel_buf_struct *ltt_buf = channel->buf;
3a7b90de 1670 int result;
46ef48cd 1671
e1152c37
PMF
1672 buf->finalized = 1;
1673 ltt_force_switch(buf, FORCE_FLUSH);
46ef48cd 1674
3a7b90de
PMF
1675 result = write(ltt_buf->data_ready_fd_write, "1", 1);
1676 if(result == -1) {
1677 PERROR("write (in ltt_relay_buffer_flush)");
1678 ERR("this should never happen!");
1679 }
e1152c37
PMF
1680}
1681
1682static void ltt_relay_async_wakeup_chan(struct ltt_channel_struct *ltt_channel)
1683{
bb07823d
PMF
1684//ust// unsigned int i;
1685//ust// struct rchan *rchan = ltt_channel->trans_channel_data;
1686//ust//
1687//ust// for_each_possible_cpu(i) {
1688//ust// struct ltt_channel_buf_struct *ltt_buf =
1689//ust// percpu_ptr(ltt_channel->buf, i);
1690//ust//
1691//ust// if (atomic_read(&ltt_buf->wakeup_readers) == 1) {
1692//ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1693//ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
1694//ust// }
1695//ust// }
e1152c37
PMF
1696}
1697
bb07823d 1698static void ltt_relay_finish_buffer(struct ltt_channel_struct *ltt_channel)
e1152c37
PMF
1699{
1700 struct rchan *rchan = ltt_channel->trans_channel_data;
772030fe 1701// int result;
e1152c37 1702
bb07823d
PMF
1703 if (rchan->buf) {
1704 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
1705 ltt_relay_buffer_flush(rchan->buf);
1706//ust// ltt_relay_wake_writers(ltt_buf);
3a7b90de
PMF
1707 /* closing the pipe tells the consumer the buffer is finished */
1708
1709 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
1710 //if(result == -1) {
1711 // PERROR("write (in ltt_relay_finish_buffer)");
1712 // ERR("this should never happen!");
1713 //}
1714 close(ltt_buf->data_ready_fd_write);
e1152c37
PMF
1715 }
1716}
1717
1718
1719static void ltt_relay_finish_channel(struct ltt_channel_struct *ltt_channel)
1720{
772030fe 1721//ust// unsigned int i;
e1152c37 1722
bb07823d
PMF
1723//ust// for_each_possible_cpu(i)
1724 ltt_relay_finish_buffer(ltt_channel);
e1152c37
PMF
1725}
1726
1727static void ltt_relay_remove_channel(struct ltt_channel_struct *channel)
1728{
1729 struct rchan *rchan = channel->trans_channel_data;
1730
1731 ltt_relay_close(rchan);
1732 kref_put(&channel->kref, ltt_relay_release_channel);
1733}
1734
1735struct ltt_reserve_switch_offsets {
1736 long begin, end, old;
1737 long begin_switch, end_switch_current, end_switch_old;
1738 long commit_count, reserve_commit_diff;
1739 size_t before_hdr_pad, size;
1740};
1741
1742/*
1743 * Returns :
1744 * 0 if ok
1745 * !0 if execution must be aborted.
1746 */
1747static inline int ltt_relay_try_reserve(
1748 struct ltt_channel_struct *ltt_channel,
1749 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1750 struct rchan_buf *buf,
1751 struct ltt_reserve_switch_offsets *offsets, size_t data_size,
1752 u64 *tsc, unsigned int *rflags, int largest_align)
1753{
1754 offsets->begin = local_read(&ltt_buf->offset);
1755 offsets->old = offsets->begin;
1756 offsets->begin_switch = 0;
1757 offsets->end_switch_current = 0;
1758 offsets->end_switch_old = 0;
1759
1760 *tsc = trace_clock_read64();
1761 if (last_tsc_overflow(ltt_buf, *tsc))
1762 *rflags = LTT_RFLAG_ID_SIZE_TSC;
1763
1764 if (SUBBUF_OFFSET(offsets->begin, buf->chan) == 0) {
1765 offsets->begin_switch = 1; /* For offsets->begin */
1766 } else {
1767 offsets->size = ltt_get_header_size(ltt_channel,
1768 offsets->begin, data_size,
1769 &offsets->before_hdr_pad, *rflags);
1770 offsets->size += ltt_align(offsets->begin + offsets->size,
1771 largest_align)
1772 + data_size;
1773 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
1774 > buf->chan->subbuf_size) {
1775 offsets->end_switch_old = 1; /* For offsets->old */
1776 offsets->begin_switch = 1; /* For offsets->begin */
1777 }
1778 }
1779 if (offsets->begin_switch) {
1780 long subbuf_index;
1781
1782 if (offsets->end_switch_old)
1783 offsets->begin = SUBBUF_ALIGN(offsets->begin,
1784 buf->chan);
1785 offsets->begin = offsets->begin + ltt_subbuffer_header_size();
1786 /* Test new buffer integrity */
1787 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
1788 offsets->reserve_commit_diff =
1789 (BUFFER_TRUNC(offsets->begin, buf->chan)
1790 >> ltt_channel->n_subbufs_order)
1791 - (local_read(&ltt_buf->commit_count[subbuf_index])
1792 & ltt_channel->commit_count_mask);
1793 if (offsets->reserve_commit_diff == 0) {
1794 /* Next buffer not corrupted. */
1795 if (!ltt_channel->overwrite &&
1796 (SUBBUF_TRUNC(offsets->begin, buf->chan)
1797 - SUBBUF_TRUNC(atomic_long_read(
1798 &ltt_buf->consumed),
1799 buf->chan))
1800 >= rchan->alloc_size) {
1801 /*
1802 * We do not overwrite non consumed buffers
1803 * and we are full : event is lost.
1804 */
1805 local_inc(&ltt_buf->events_lost);
1806 return -1;
1807 } else {
1808 /*
1809 * next buffer not corrupted, we are either in
1810 * overwrite mode or the buffer is not full.
1811 * It's safe to write in this new subbuffer.
1812 */
1813 }
1814 } else {
1815 /*
1816 * Next subbuffer corrupted. Force pushing reader even
1817 * in normal mode. It's safe to write in this new
1818 * subbuffer.
1819 */
1820 }
1821 offsets->size = ltt_get_header_size(ltt_channel,
1822 offsets->begin, data_size,
1823 &offsets->before_hdr_pad, *rflags);
1824 offsets->size += ltt_align(offsets->begin + offsets->size,
1825 largest_align)
1826 + data_size;
1827 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
1828 > buf->chan->subbuf_size) {
1829 /*
1830 * Event too big for subbuffers, report error, don't
1831 * complete the sub-buffer switch.
1832 */
1833 local_inc(&ltt_buf->events_lost);
1834 return -1;
1835 } else {
1836 /*
1837 * We just made a successful buffer switch and the event
1838 * fits in the new subbuffer. Let's write.
1839 */
1840 }
1841 } else {
1842 /*
1843 * Event fits in the current buffer and we are not on a switch
1844 * boundary. It's safe to write.
1845 */
1846 }
1847 offsets->end = offsets->begin + offsets->size;
1848
1849 if ((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0) {
1850 /*
1851 * The offset_end will fall at the very beginning of the next
1852 * subbuffer.
1853 */
1854 offsets->end_switch_current = 1; /* For offsets->begin */
1855 }
1856 return 0;
1857}
1858
1859/*
1860 * Returns :
1861 * 0 if ok
1862 * !0 if execution must be aborted.
1863 */
1864static inline int ltt_relay_try_switch(
1865 enum force_switch_mode mode,
1866 struct ltt_channel_struct *ltt_channel,
1867 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
1868 struct rchan_buf *buf,
1869 struct ltt_reserve_switch_offsets *offsets,
1870 u64 *tsc)
1871{
1872 long subbuf_index;
1873
1874 offsets->begin = local_read(&ltt_buf->offset);
1875 offsets->old = offsets->begin;
1876 offsets->begin_switch = 0;
1877 offsets->end_switch_old = 0;
1878
1879 *tsc = trace_clock_read64();
1880
1881 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
1882 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
1883 offsets->end_switch_old = 1;
1884 } else {
1885 /* we do not have to switch : buffer is empty */
1886 return -1;
1887 }
1888 if (mode == FORCE_ACTIVE)
1889 offsets->begin += ltt_subbuffer_header_size();
1890 /*
1891 * Always begin_switch in FORCE_ACTIVE mode.
1892 * Test new buffer integrity
1893 */
1894 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
1895 offsets->reserve_commit_diff =
1896 (BUFFER_TRUNC(offsets->begin, buf->chan)
1897 >> ltt_channel->n_subbufs_order)
1898 - (local_read(&ltt_buf->commit_count[subbuf_index])
1899 & ltt_channel->commit_count_mask);
1900 if (offsets->reserve_commit_diff == 0) {
1901 /* Next buffer not corrupted. */
1902 if (mode == FORCE_ACTIVE
1903 && !ltt_channel->overwrite
1904 && offsets->begin - atomic_long_read(&ltt_buf->consumed)
1905 >= rchan->alloc_size) {
1906 /*
1907 * We do not overwrite non consumed buffers and we are
1908 * full : ignore switch while tracing is active.
1909 */
1910 return -1;
1911 }
1912 } else {
1913 /*
1914 * Next subbuffer corrupted. Force pushing reader even in normal
1915 * mode
1916 */
1917 }
1918 offsets->end = offsets->begin;
1919 return 0;
1920}
1921
1922static inline void ltt_reserve_push_reader(
1923 struct ltt_channel_struct *ltt_channel,
1924 struct ltt_channel_buf_struct *ltt_buf,
1925 struct rchan *rchan,
1926 struct rchan_buf *buf,
1927 struct ltt_reserve_switch_offsets *offsets)
1928{
1929 long consumed_old, consumed_new;
1930
1931 do {
1932 consumed_old = atomic_long_read(&ltt_buf->consumed);
1933 /*
1934 * If buffer is in overwrite mode, push the reader consumed
1935 * count if the write position has reached it and we are not
1936 * at the first iteration (don't push the reader farther than
1937 * the writer). This operation can be done concurrently by many
1938 * writers in the same buffer, the writer being at the farthest
1939 * write position sub-buffer index in the buffer being the one
1940 * which will win this loop.
1941 * If the buffer is not in overwrite mode, pushing the reader
1942 * only happens if a sub-buffer is corrupted.
1943 */
1944 if ((SUBBUF_TRUNC(offsets->end-1, buf->chan)
1945 - SUBBUF_TRUNC(consumed_old, buf->chan))
1946 >= rchan->alloc_size)
1947 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
1948 else {
1949 consumed_new = consumed_old;
1950 break;
1951 }
1952 } while (atomic_long_cmpxchg(&ltt_buf->consumed, consumed_old,
1953 consumed_new) != consumed_old);
1954
1955 if (consumed_old != consumed_new) {
1956 /*
1957 * Reader pushed : we are the winner of the push, we can
1958 * therefore reequilibrate reserve and commit. Atomic increment
1959 * of the commit count permits other writers to play around
1960 * with this variable before us. We keep track of
1961 * corrupted_subbuffers even in overwrite mode :
1962 * we never want to write over a non completely committed
1963 * sub-buffer : possible causes : the buffer size is too low
1964 * compared to the unordered data input, or there is a writer
1965 * that died between the reserve and the commit.
1966 */
1967 if (offsets->reserve_commit_diff) {
1968 /*
1969 * We have to alter the sub-buffer commit count.
1970 * We do not deliver the previous subbuffer, given it
1971 * was either corrupted or not consumed (overwrite
1972 * mode).
1973 */
1974 local_add(offsets->reserve_commit_diff,
1975 &ltt_buf->commit_count[
1976 SUBBUF_INDEX(offsets->begin,
1977 buf->chan)]);
1978 if (!ltt_channel->overwrite
1979 || offsets->reserve_commit_diff
1980 != rchan->subbuf_size) {
1981 /*
1982 * The reserve commit diff was not subbuf_size :
1983 * it means the subbuffer was partly written to
1984 * and is therefore corrupted. If it is multiple
1985 * of subbuffer size and we are in flight
1986 * recorder mode, we are skipping over a whole
1987 * subbuffer.
1988 */
1989 local_inc(&ltt_buf->corrupted_subbuffers);
1990 }
1991 }
1992 }
1993}
1994
1995
1996/*
1997 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1998 *
1999 * Concurrency safe because we are the last and only thread to alter this
2000 * sub-buffer. As long as it is not delivered and read, no other thread can
2001 * alter the offset, alter the reserve_count or call the
2002 * client_buffer_end_callback on this sub-buffer.
2003 *
2004 * The only remaining threads could be the ones with pending commits. They will
2005 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
2006 * We detect corrupted subbuffers with commit and reserve counts. We keep a
2007 * corrupted sub-buffers count and push the readers across these sub-buffers.
2008 *
2009 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
2010 * switches in, finding out it's corrupted. The result will be than the old
2011 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
2012 * will be declared corrupted too because of the commit count adjustment.
2013 *
2014 * Note : offset_old should never be 0 here.
2015 */
2016static inline void ltt_reserve_switch_old_subbuf(
2017 struct ltt_channel_struct *ltt_channel,
2018 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
2019 struct rchan_buf *buf,
2020 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
2021{
2022 long oldidx = SUBBUF_INDEX(offsets->old - 1, rchan);
2023
2024 ltt_channel->buffer_end(buf, *tsc, offsets->old, oldidx);
2025 /* Must write buffer end before incrementing commit count */
2026 smp_wmb();
2027 offsets->commit_count =
2028 local_add_return(rchan->subbuf_size
2029 - (SUBBUF_OFFSET(offsets->old - 1, rchan)
2030 + 1),
2031 &ltt_buf->commit_count[oldidx]);
2032 if ((BUFFER_TRUNC(offsets->old - 1, rchan)
2033 >> ltt_channel->n_subbufs_order)
2034 - ((offsets->commit_count - rchan->subbuf_size)
2035 & ltt_channel->commit_count_mask) == 0)
8431032f 2036 ltt_deliver(buf, oldidx, offsets->commit_count);
e1152c37
PMF
2037}
2038
2039/*
2040 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
2041 *
2042 * This code can be executed unordered : writers may already have written to the
2043 * sub-buffer before this code gets executed, caution. The commit makes sure
2044 * that this code is executed before the deliver of this sub-buffer.
2045 */
0b0cd937 2046static /*inline*/ void ltt_reserve_switch_new_subbuf(
e1152c37
PMF
2047 struct ltt_channel_struct *ltt_channel,
2048 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
2049 struct rchan_buf *buf,
2050 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
2051{
2052 long beginidx = SUBBUF_INDEX(offsets->begin, rchan);
2053
2054 ltt_channel->buffer_begin(buf, *tsc, beginidx);
2055 /* Must write buffer end before incrementing commit count */
2056 smp_wmb();
2057 offsets->commit_count = local_add_return(ltt_subbuffer_header_size(),
2058 &ltt_buf->commit_count[beginidx]);
2059 /* Check if the written buffer has to be delivered */
2060 if ((BUFFER_TRUNC(offsets->begin, rchan)
2061 >> ltt_channel->n_subbufs_order)
2062 - ((offsets->commit_count - rchan->subbuf_size)
2063 & ltt_channel->commit_count_mask) == 0)
8431032f 2064 ltt_deliver(buf, beginidx, offsets->commit_count);
e1152c37
PMF
2065}
2066
2067
2068/*
2069 * ltt_reserve_end_switch_current: finish switching current subbuffer
2070 *
2071 * Concurrency safe because we are the last and only thread to alter this
2072 * sub-buffer. As long as it is not delivered and read, no other thread can
2073 * alter the offset, alter the reserve_count or call the
2074 * client_buffer_end_callback on this sub-buffer.
2075 *
2076 * The only remaining threads could be the ones with pending commits. They will
2077 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
2078 * We detect corrupted subbuffers with commit and reserve counts. We keep a
2079 * corrupted sub-buffers count and push the readers across these sub-buffers.
2080 *
2081 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
2082 * switches in, finding out it's corrupted. The result will be than the old
2083 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
2084 * will be declared corrupted too because of the commit count adjustment.
2085 */
2086static inline void ltt_reserve_end_switch_current(
2087 struct ltt_channel_struct *ltt_channel,
2088 struct ltt_channel_buf_struct *ltt_buf, struct rchan *rchan,
2089 struct rchan_buf *buf,
2090 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
2091{
2092 long endidx = SUBBUF_INDEX(offsets->end - 1, rchan);
2093
2094 ltt_channel->buffer_end(buf, *tsc, offsets->end, endidx);
2095 /* Must write buffer begin before incrementing commit count */
2096 smp_wmb();
2097 offsets->commit_count =
2098 local_add_return(rchan->subbuf_size
2099 - (SUBBUF_OFFSET(offsets->end - 1, rchan)
2100 + 1),
2101 &ltt_buf->commit_count[endidx]);
2102 if ((BUFFER_TRUNC(offsets->end - 1, rchan)
2103 >> ltt_channel->n_subbufs_order)
2104 - ((offsets->commit_count - rchan->subbuf_size)
2105 & ltt_channel->commit_count_mask) == 0)
8431032f 2106 ltt_deliver(buf, endidx, offsets->commit_count);
e1152c37
PMF
2107}
2108
2109/**
2110 * ltt_relay_reserve_slot - Atomic slot reservation in a LTTng buffer.
2111 * @trace: the trace structure to log to.
2112 * @ltt_channel: channel structure
2113 * @transport_data: data structure specific to ltt relay
2114 * @data_size: size of the variable length data to log.
2115 * @slot_size: pointer to total size of the slot (out)
2116 * @buf_offset : pointer to reserved buffer offset (out)
2117 * @tsc: pointer to the tsc at the slot reservation (out)
2118 * @cpu: cpuid
2119 *
2120 * Return : -ENOSPC if not enough space, else returns 0.
2121 * It will take care of sub-buffer switching.
2122 */
2123static notrace int ltt_relay_reserve_slot(struct ltt_trace_struct *trace,
2124 struct ltt_channel_struct *ltt_channel, void **transport_data,
2125 size_t data_size, size_t *slot_size, long *buf_offset, u64 *tsc,
c1dea0b3 2126 unsigned int *rflags, int largest_align)
e1152c37
PMF
2127{
2128 struct rchan *rchan = ltt_channel->trans_channel_data;
bb07823d
PMF
2129 struct rchan_buf *buf = *transport_data = rchan->buf;
2130 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
e1152c37
PMF
2131 struct ltt_reserve_switch_offsets offsets;
2132
2133 offsets.reserve_commit_diff = 0;
2134 offsets.size = 0;
2135
2136 /*
2137 * Perform retryable operations.
2138 */
bb07823d 2139 if (ltt_nesting > 4) {
e1152c37
PMF
2140 local_inc(&ltt_buf->events_lost);
2141 return -EPERM;
2142 }
2143 do {
2144 if (ltt_relay_try_reserve(ltt_channel, ltt_buf,
2145 rchan, buf, &offsets, data_size, tsc, rflags,
2146 largest_align))
2147 return -ENOSPC;
2148 } while (local_cmpxchg(&ltt_buf->offset, offsets.old,
2149 offsets.end) != offsets.old);
2150
2151 /*
2152 * Atomically update last_tsc. This update races against concurrent
2153 * atomic updates, but the race will always cause supplementary full TSC
2154 * events, never the opposite (missing a full TSC event when it would be
2155 * needed).
2156 */
2157 save_last_tsc(ltt_buf, *tsc);
2158
2159 /*
2160 * Push the reader if necessary
2161 */
2162 ltt_reserve_push_reader(ltt_channel, ltt_buf, rchan, buf, &offsets);
2163
2164 /*
2165 * Switch old subbuffer if needed.
2166 */
2167 if (offsets.end_switch_old)
2168 ltt_reserve_switch_old_subbuf(ltt_channel, ltt_buf, rchan, buf,
2169 &offsets, tsc);
2170
2171 /*
2172 * Populate new subbuffer.
2173 */
2174 if (offsets.begin_switch)
2175 ltt_reserve_switch_new_subbuf(ltt_channel, ltt_buf, rchan,
2176 buf, &offsets, tsc);
2177
2178 if (offsets.end_switch_current)
2179 ltt_reserve_end_switch_current(ltt_channel, ltt_buf, rchan,
2180 buf, &offsets, tsc);
2181
2182 *slot_size = offsets.size;
2183 *buf_offset = offsets.begin + offsets.before_hdr_pad;
2184 return 0;
2185}
2186
2187/*
2188 * Force a sub-buffer switch for a per-cpu buffer. This operation is
2189 * completely reentrant : can be called while tracing is active with
2190 * absolutely no lock held.
2191 *
2192 * Note, however, that as a local_cmpxchg is used for some atomic
2193 * operations, this function must be called from the CPU which owns the buffer
2194 * for a ACTIVE flush.
2195 */
2196static notrace void ltt_force_switch(struct rchan_buf *buf,
2197 enum force_switch_mode mode)
2198{
2199 struct ltt_channel_struct *ltt_channel =
2200 (struct ltt_channel_struct *)buf->chan->private_data;
bb07823d 2201 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
e1152c37
PMF
2202 struct rchan *rchan = ltt_channel->trans_channel_data;
2203 struct ltt_reserve_switch_offsets offsets;
2204 u64 tsc;
2205
2206 offsets.reserve_commit_diff = 0;
2207 offsets.size = 0;
2208
2209 /*
2210 * Perform retryable operations.
2211 */
2212 do {
2213 if (ltt_relay_try_switch(mode, ltt_channel, ltt_buf,
2214 rchan, buf, &offsets, &tsc))
2215 return;
2216 } while (local_cmpxchg(&ltt_buf->offset, offsets.old,
2217 offsets.end) != offsets.old);
2218
2219 /*
2220 * Atomically update last_tsc. This update races against concurrent
2221 * atomic updates, but the race will always cause supplementary full TSC
2222 * events, never the opposite (missing a full TSC event when it would be
2223 * needed).
2224 */
2225 save_last_tsc(ltt_buf, tsc);
2226
2227 /*
2228 * Push the reader if necessary
2229 */
2230 if (mode == FORCE_ACTIVE)
2231 ltt_reserve_push_reader(ltt_channel, ltt_buf, rchan,
2232 buf, &offsets);
2233
2234 /*
2235 * Switch old subbuffer if needed.
2236 */
2237 if (offsets.end_switch_old)
2238 ltt_reserve_switch_old_subbuf(ltt_channel, ltt_buf, rchan, buf,
2239 &offsets, &tsc);
2240
2241 /*
2242 * Populate new subbuffer.
2243 */
2244 if (mode == FORCE_ACTIVE)
2245 ltt_reserve_switch_new_subbuf(ltt_channel,
2246 ltt_buf, rchan, buf, &offsets, &tsc);
2247}
2248
e1152c37
PMF
2249/*
2250 * This is called with preemption disabled when user space has requested
2251 * blocking mode. If one of the active traces has free space below a
2252 * specific threshold value, we reenable preemption and block.
2253 */
2254static int ltt_relay_user_blocking(struct ltt_trace_struct *trace,
2255 unsigned int chan_index, size_t data_size,
2256 struct user_dbg_data *dbg)
2257{
bb07823d
PMF
2258//ust// struct rchan *rchan;
2259//ust// struct ltt_channel_buf_struct *ltt_buf;
2260//ust// struct ltt_channel_struct *channel;
2261//ust// struct rchan_buf *relay_buf;
2262//ust// int cpu;
2263//ust// DECLARE_WAITQUEUE(wait, current);
2264//ust//
2265//ust// channel = &trace->channels[chan_index];
2266//ust// rchan = channel->trans_channel_data;
2267//ust// cpu = smp_processor_id();
2268//ust// relay_buf = rchan->buf[cpu];
2269//ust// ltt_buf = percpu_ptr(channel->buf, cpu);
2270//ust//
2271//ust// /*
2272//ust// * Check if data is too big for the channel : do not
2273//ust// * block for it.
2274//ust// */
2275//ust// if (LTT_RESERVE_CRITICAL + data_size > relay_buf->chan->subbuf_size)
2276//ust// return 0;
2277//ust//
2278//ust// /*
2279//ust// * If free space too low, we block. We restart from the
2280//ust// * beginning after we resume (cpu id may have changed
2281//ust// * while preemption is active).
2282//ust// */
2283//ust// spin_lock(&ltt_buf->full_lock);
2284//ust// if (!channel->overwrite) {
2285//ust// dbg->write = local_read(&ltt_buf->offset);
2286//ust// dbg->read = atomic_long_read(&ltt_buf->consumed);
2287//ust// dbg->avail_size = dbg->write + LTT_RESERVE_CRITICAL + data_size
2288//ust// - SUBBUF_TRUNC(dbg->read,
2289//ust// relay_buf->chan);
2290//ust// if (dbg->avail_size > rchan->alloc_size) {
2291//ust// __set_current_state(TASK_INTERRUPTIBLE);
2292//ust// add_wait_queue(&ltt_buf->write_wait, &wait);
2293//ust// spin_unlock(&ltt_buf->full_lock);
2294//ust// preempt_enable();
2295//ust// schedule();
2296//ust// __set_current_state(TASK_RUNNING);
2297//ust// remove_wait_queue(&ltt_buf->write_wait, &wait);
2298//ust// if (signal_pending(current))
2299//ust// return -ERESTARTSYS;
2300//ust// preempt_disable();
2301//ust// return 1;
2302//ust// }
2303//ust// }
2304//ust// spin_unlock(&ltt_buf->full_lock);
e1152c37
PMF
2305 return 0;
2306}
2307
2308static void ltt_relay_print_user_errors(struct ltt_trace_struct *trace,
2309 unsigned int chan_index, size_t data_size,
c1dea0b3 2310 struct user_dbg_data *dbg)
e1152c37
PMF
2311{
2312 struct rchan *rchan;
2313 struct ltt_channel_buf_struct *ltt_buf;
2314 struct ltt_channel_struct *channel;
2315 struct rchan_buf *relay_buf;
2316
2317 channel = &trace->channels[chan_index];
2318 rchan = channel->trans_channel_data;
bb07823d
PMF
2319 relay_buf = rchan->buf;
2320 ltt_buf = channel->buf;
e1152c37
PMF
2321
2322 printk(KERN_ERR "Error in LTT usertrace : "
2323 "buffer full : event lost in blocking "
2324 "mode. Increase LTT_RESERVE_CRITICAL.\n");
bb07823d 2325 printk(KERN_ERR "LTT nesting level is %u.\n", ltt_nesting);
e1152c37
PMF
2326 printk(KERN_ERR "LTT avail size %lu.\n",
2327 dbg->avail_size);
2328 printk(KERN_ERR "avai write : %lu, read : %lu\n",
2329 dbg->write, dbg->read);
2330
2331 dbg->write = local_read(&ltt_buf->offset);
2332 dbg->read = atomic_long_read(&ltt_buf->consumed);
2333
2334 printk(KERN_ERR "LTT cur size %lu.\n",
2335 dbg->write + LTT_RESERVE_CRITICAL + data_size
2336 - SUBBUF_TRUNC(dbg->read, relay_buf->chan));
2337 printk(KERN_ERR "cur write : %lu, read : %lu\n",
2338 dbg->write, dbg->read);
2339}
2340
5f54827b
PMF
2341//ust// static struct ltt_transport ltt_relay_transport = {
2342//ust// .name = "relay",
2343//ust// .owner = THIS_MODULE,
2344//ust// .ops = {
2345//ust// .create_dirs = ltt_relay_create_dirs,
2346//ust// .remove_dirs = ltt_relay_remove_dirs,
2347//ust// .create_channel = ltt_relay_create_channel,
2348//ust// .finish_channel = ltt_relay_finish_channel,
2349//ust// .remove_channel = ltt_relay_remove_channel,
2350//ust// .wakeup_channel = ltt_relay_async_wakeup_chan,
2351//ust// .commit_slot = ltt_relay_commit_slot,
2352//ust// .reserve_slot = ltt_relay_reserve_slot,
2353//ust// .user_blocking = ltt_relay_user_blocking,
2354//ust// .user_errors = ltt_relay_print_user_errors,
2355//ust// },
2356//ust// };
2357
2358static struct ltt_transport ust_relay_transport = {
2359 .name = "ustrelay",
bb07823d 2360//ust// .owner = THIS_MODULE,
e1152c37
PMF
2361 .ops = {
2362 .create_dirs = ltt_relay_create_dirs,
2363 .remove_dirs = ltt_relay_remove_dirs,
2364 .create_channel = ltt_relay_create_channel,
2365 .finish_channel = ltt_relay_finish_channel,
2366 .remove_channel = ltt_relay_remove_channel,
2367 .wakeup_channel = ltt_relay_async_wakeup_chan,
8431032f 2368// .commit_slot = ltt_relay_commit_slot,
e1152c37
PMF
2369 .reserve_slot = ltt_relay_reserve_slot,
2370 .user_blocking = ltt_relay_user_blocking,
2371 .user_errors = ltt_relay_print_user_errors,
2372 },
2373};
2374
5f54827b
PMF
2375//ust// static int __init ltt_relay_init(void)
2376//ust// {
2377//ust// printk(KERN_INFO "LTT : ltt-relay init\n");
2378//ust//
2379//ust// ltt_file_operations = ltt_relay_file_operations;
2380//ust// ltt_file_operations.owner = THIS_MODULE;
2381//ust// ltt_file_operations.open = ltt_open;
2382//ust// ltt_file_operations.release = ltt_release;
2383//ust// ltt_file_operations.poll = ltt_poll;
2384//ust// ltt_file_operations.splice_read = ltt_relay_file_splice_read,
2385//ust// ltt_file_operations.ioctl = ltt_ioctl;
2386//ust//#ifdef CONFIG_COMPAT
2387//ust// ltt_file_operations.compat_ioctl = ltt_compat_ioctl;
2388//ust//#endif
2389//ust//
2390//ust// ltt_transport_register(&ltt_relay_transport);
2391//ust//
2392//ust// return 0;
2393//ust// }
2394
8431032f
PMF
2395/*
2396 * for flight recording. must be called after relay_commit.
2397 * This function decrements de subbuffer's lost_size each time the commit count
2398 * reaches back the reserve offset (module subbuffer size). It is useful for
2399 * crash dump.
2400 */
2401//ust// #ifdef CONFIG_LTT_VMCORE
2402static /* inline */ void ltt_write_commit_counter(struct rchan_buf *buf,
2403 struct ltt_channel_buf_struct *ltt_buf,
2404 long idx, long buf_offset, long commit_count, size_t data_size)
2405{
2406 long offset;
2407 long commit_seq_old;
2408
2409 offset = buf_offset + data_size;
2410
2411 /*
2412 * SUBBUF_OFFSET includes commit_count_mask. We can simply
2413 * compare the offsets within the subbuffer without caring about
2414 * buffer full/empty mismatch because offset is never zero here
2415 * (subbuffer header and event headers have non-zero length).
2416 */
2417 if (unlikely(SUBBUF_OFFSET(offset - commit_count, buf->chan)))
2418 return;
2419
2420 commit_seq_old = local_read(&ltt_buf->commit_seq[idx]);
2421 while (commit_seq_old < commit_count)
2422 commit_seq_old = local_cmpxchg(&ltt_buf->commit_seq[idx],
2423 commit_seq_old, commit_count);
2424}
2425//ust// #else
2426//ust// static inline void ltt_write_commit_counter(struct rchan_buf *buf,
2427//ust// long buf_offset, size_t slot_size)
2428//ust// {
2429//ust// }
2430//ust// #endif
2431
2432/*
2433 * Atomic unordered slot commit. Increments the commit count in the
2434 * specified sub-buffer, and delivers it if necessary.
2435 *
2436 * Parameters:
2437 *
2438 * @ltt_channel : channel structure
2439 * @transport_data: transport-specific data
2440 * @buf_offset : offset following the event header.
2441 * @data_size : size of the event data.
2442 * @slot_size : size of the reserved slot.
2443 */
2444/* FIXME: make this function static inline in the .h! */
2445/*static*/ /* inline */ notrace void ltt_commit_slot(
2446 struct ltt_channel_struct *ltt_channel,
2447 void **transport_data, long buf_offset,
2448 size_t data_size, size_t slot_size)
2449{
2450 struct rchan_buf *buf = *transport_data;
2451 struct ltt_channel_buf_struct *ltt_buf = ltt_channel->buf;
2452 struct rchan *rchan = buf->chan;
2453 long offset_end = buf_offset;
2454 long endidx = SUBBUF_INDEX(offset_end - 1, rchan);
2455 long commit_count;
2456
2457 /* Must write slot data before incrementing commit count */
2458 smp_wmb();
2459 commit_count = local_add_return(slot_size,
2460 &ltt_buf->commit_count[endidx]);
2461 /* Check if all commits have been done */
2462 if ((BUFFER_TRUNC(offset_end - 1, rchan)
2463 >> ltt_channel->n_subbufs_order)
2464 - ((commit_count - rchan->subbuf_size)
2465 & ltt_channel->commit_count_mask) == 0)
2466 ltt_deliver(buf, endidx, commit_count);
2467 /*
2468 * Update lost_size for each commit. It's needed only for extracting
2469 * ltt buffers from vmcore, after crash.
2470 */
2471 ltt_write_commit_counter(buf, ltt_buf, endidx,
2472 buf_offset, commit_count, data_size);
8431032f
PMF
2473}
2474
2475
4db647c5
PMF
2476static char initialized = 0;
2477
54d6c4f2 2478void __attribute__((constructor)) init_ustrelay_transport(void)
e1152c37 2479{
4db647c5
PMF
2480 if(!initialized) {
2481 ltt_transport_register(&ust_relay_transport);
2482 initialized = 1;
2483 }
e1152c37
PMF
2484}
2485
772030fe 2486static void __attribute__((destructor)) ltt_relay_exit(void)
e1152c37 2487{
5f54827b 2488//ust// printk(KERN_INFO "LTT : ltt-relay exit\n");
e1152c37 2489
bb07823d 2490 ltt_transport_unregister(&ust_relay_transport);
e1152c37
PMF
2491}
2492
5f54827b
PMF
2493//ust// module_init(ltt_relay_init);
2494//ust// module_exit(ltt_relay_exit);
2495//ust//
2496//ust// MODULE_LICENSE("GPL");
2497//ust// MODULE_AUTHOR("Mathieu Desnoyers");
2498//ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Lockless Relay");
This page took 0.130608 seconds and 4 git commands to generate.