ust: cleanups and functionality
[lttng-ust.git] / libtracing / relay.c
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>
25 #include <sys/mman.h>
26 #include "kernelcompat.h"
27 #include "list.h"
28 #include "relay.h"
29 #include "channels.h"
30 #include "kref.h"
31 #include "tracer.h"
32 #include "tracercore.h"
33 #include "usterr.h"
34
35 /* list of open channels, for cpu hotplug */
36 static DEFINE_MUTEX(relay_channels_mutex);
37 static LIST_HEAD(relay_channels);
38
39
40 static struct dentry *ltt_create_buf_file_callback(struct rchan_buf *buf);
41
42 /**
43 * relay_alloc_buf - allocate a channel buffer
44 * @buf: the buffer struct
45 * @size: total size of the buffer
46 */
47 //ust// static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
48 //ust//{
49 //ust// unsigned int i, n_pages;
50 //ust// struct buf_page *buf_page, *n;
51 //ust//
52 //ust// *size = PAGE_ALIGN(*size);
53 //ust// n_pages = *size >> PAGE_SHIFT;
54 //ust//
55 //ust// INIT_LIST_HEAD(&buf->pages);
56 //ust//
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;
67 //ust// }
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);
72 //ust// if (i == 0) {
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;
77 //ust// }
78 //ust// }
79 //ust// buf->page_count = n_pages;
80 //ust// return 0;
81 //ust//
82 //ust//depopulate:
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);
87 //ust// }
88 //ust// return -ENOMEM;
89 //ust//}
90
91 static int relay_alloc_buf(struct rchan_buf *buf, size_t *size)
92 {
93 unsigned int n_pages;
94 struct buf_page *buf_page, *n;
95
96 void *result;
97
98 *size = PAGE_ALIGN(*size);
99
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) {
103 PERROR("mmap");
104 return -1;
105 }
106
107 buf->buf_data = result;
108 buf->buf_size = *size;
109
110 return 0;
111 }
112
113 /**
114 * relay_create_buf - allocate and initialize a channel buffer
115 * @chan: the relay channel
116 * @cpu: cpu the buffer belongs to
117 *
118 * Returns channel buffer if successful, %NULL otherwise.
119 */
120 static struct rchan_buf *relay_create_buf(struct rchan *chan)
121 {
122 int ret;
123 struct rchan_buf *buf = kzalloc(sizeof(struct rchan_buf), GFP_KERNEL);
124 if (!buf)
125 return NULL;
126
127 // buf->cpu = cpu;
128 ret = relay_alloc_buf(buf, &chan->alloc_size);
129 if (ret)
130 goto free_buf;
131
132 buf->chan = chan;
133 kref_get(&buf->chan->kref);
134 return buf;
135
136 free_buf:
137 kfree(buf);
138 return NULL;
139 }
140
141 /**
142 * relay_destroy_channel - free the channel struct
143 * @kref: target kernel reference that contains the relay channel
144 *
145 * Should only be called from kref_put().
146 */
147 static void relay_destroy_channel(struct kref *kref)
148 {
149 struct rchan *chan = container_of(kref, struct rchan, kref);
150 kfree(chan);
151 }
152
153 /**
154 * relay_destroy_buf - destroy an rchan_buf struct and associated buffer
155 * @buf: the buffer struct
156 */
157 static void relay_destroy_buf(struct rchan_buf *buf)
158 {
159 struct rchan *chan = buf->chan;
160 struct buf_page *buf_page, *n;
161 int result;
162
163 result = munmap(buf->buf_data, buf->buf_size);
164 if(result == -1) {
165 PERROR("munmap");
166 }
167
168 //ust// chan->buf[buf->cpu] = NULL;
169 kfree(buf);
170 kref_put(&chan->kref, relay_destroy_channel);
171 }
172
173 /**
174 * relay_remove_buf - remove a channel buffer
175 * @kref: target kernel reference that contains the relay buffer
176 *
177 * Removes the file from the fileystem, which also frees the
178 * rchan_buf_struct and the channel buffer. Should only be called from
179 * kref_put().
180 */
181 static void relay_remove_buf(struct kref *kref)
182 {
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);
186 }
187
188 /*
189 * High-level relay kernel API and associated functions.
190 */
191
192 /*
193 * rchan_callback implementations defining default channel behavior. Used
194 * in place of corresponding NULL values in client callback struct.
195 */
196
197 /*
198 * create_buf_file_create() default callback. Does nothing.
199 */
200 static struct dentry *create_buf_file_default_callback(const char *filename,
201 struct dentry *parent,
202 int mode,
203 struct rchan_buf *buf)
204 {
205 return NULL;
206 }
207
208 /*
209 * remove_buf_file() default callback. Does nothing.
210 */
211 static int remove_buf_file_default_callback(struct dentry *dentry)
212 {
213 return -EINVAL;
214 }
215
216 /**
217 * wakeup_readers - wake up readers waiting on a channel
218 * @data: contains the channel buffer
219 *
220 * This is the timer function used to defer reader waking.
221 */
222 //ust// static void wakeup_readers(unsigned long data)
223 //ust// {
224 //ust// struct rchan_buf *buf = (struct rchan_buf *)data;
225 //ust// wake_up_interruptible(&buf->read_wait);
226 //ust// }
227
228 /**
229 * __relay_reset - reset a channel buffer
230 * @buf: the channel buffer
231 * @init: 1 if this is a first-time initialization
232 *
233 * See relay_reset() for description of effect.
234 */
235 static void __relay_reset(struct rchan_buf *buf, unsigned int init)
236 {
237 if (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);
241 } else
242 //ust// del_timer_sync(&buf->timer);
243
244 buf->finalized = 0;
245 }
246
247 /*
248 * relay_open_buf - create a new relay channel buffer
249 *
250 * used by relay_open() and CPU hotplug.
251 */
252 static struct rchan_buf *relay_open_buf(struct rchan *chan)
253 {
254 struct rchan_buf *buf = NULL;
255 struct dentry *dentry;
256 //ust// char *tmpname;
257
258 //ust// tmpname = kzalloc(NAME_MAX + 1, GFP_KERNEL);
259 //ust// if (!tmpname)
260 //ust// goto end;
261 //ust// snprintf(tmpname, NAME_MAX, "%s%d", chan->base_filename, cpu);
262
263 buf = relay_create_buf(chan);
264 if (!buf)
265 goto free_name;
266
267 __relay_reset(buf, 1);
268
269 /* Create file in fs */
270 //ust// dentry = chan->cb->create_buf_file(tmpname, chan->parent, S_IRUSR,
271 //ust// buf);
272
273 ltt_create_buf_file_callback(buf); // ust //
274
275 //ust// if (!dentry)
276 //ust// goto free_buf;
277 //ust//
278 //ust// buf->dentry = dentry;
279
280 goto free_name;
281
282 free_buf:
283 relay_destroy_buf(buf);
284 buf = NULL;
285 free_name:
286 //ust// kfree(tmpname);
287 end:
288 return buf;
289 }
290
291 /**
292 * relay_close_buf - close a channel buffer
293 * @buf: channel buffer
294 *
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.
298 */
299 static void relay_close_buf(struct rchan_buf *buf)
300 {
301 //ust// del_timer_sync(&buf->timer);
302 kref_put(&buf->kref, relay_remove_buf);
303 }
304
305 //ust// static void setup_callbacks(struct rchan *chan,
306 //ust// struct rchan_callbacks *cb)
307 //ust// {
308 //ust// if (!cb) {
309 //ust// chan->cb = &default_channel_callbacks;
310 //ust// return;
311 //ust// }
312 //ust//
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;
318 //ust// }
319
320 /**
321 * relay_hotcpu_callback - CPU hotplug callback
322 * @nb: notifier block
323 * @action: hotplug action to take
324 * @hcpu: CPU number
325 *
326 * Returns the success/failure of the operation. (%NOTIFY_OK, %NOTIFY_BAD)
327 */
328 //ust// static int __cpuinit relay_hotcpu_callback(struct notifier_block *nb,
329 //ust// unsigned long action,
330 //ust// void *hcpu)
331 //ust// {
332 //ust// unsigned int hotcpu = (unsigned long)hcpu;
333 //ust// struct rchan *chan;
334 //ust//
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])
341 //ust// continue;
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;
349 //ust// }
350 //ust// }
351 //ust// mutex_unlock(&relay_channels_mutex);
352 //ust// break;
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. */
357 //ust// break;
358 //ust// }
359 //ust// return NOTIFY_OK;
360 //ust// }
361
362 /**
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
370 *
371 * Returns channel pointer if successful, %NULL otherwise.
372 *
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.
377 */
378 struct rchan *ltt_relay_open(const char *base_filename,
379 struct dentry *parent,
380 size_t subbuf_size,
381 size_t n_subbufs,
382 void *private_data)
383 {
384 unsigned int i;
385 struct rchan *chan;
386 //ust// if (!base_filename)
387 //ust// return NULL;
388
389 if (!(subbuf_size && n_subbufs))
390 return NULL;
391
392 chan = kzalloc(sizeof(struct rchan), GFP_KERNEL);
393 if (!chan)
394 return NULL;
395
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);
406
407 mutex_lock(&relay_channels_mutex);
408 //ust// for_each_online_cpu(i) {
409 chan->buf = relay_open_buf(chan);
410 if (!chan->buf)
411 goto error;
412 //ust// }
413 list_add(&chan->list, &relay_channels);
414 mutex_unlock(&relay_channels_mutex);
415
416 return chan;
417
418 //ust//free_bufs:
419 //ust// for_each_possible_cpu(i) {
420 //ust// if (!chan->buf[i])
421 //ust// break;
422 //ust// relay_close_buf(chan->buf[i]);
423 //ust// }
424
425 error:
426 kref_put(&chan->kref, relay_destroy_channel);
427 mutex_unlock(&relay_channels_mutex);
428 return NULL;
429 }
430 //ust// EXPORT_SYMBOL_GPL(ltt_relay_open);
431
432 /**
433 * ltt_relay_close - close the channel
434 * @chan: the channel
435 *
436 * Closes all channel buffers and frees the channel.
437 */
438 void ltt_relay_close(struct rchan *chan)
439 {
440 unsigned int i;
441
442 if (!chan)
443 return;
444
445 mutex_lock(&relay_channels_mutex);
446 //ust// for_each_possible_cpu(i)
447 if (chan->buf)
448 relay_close_buf(chan->buf);
449
450 list_del(&chan->list);
451 kref_put(&chan->kref, relay_destroy_channel);
452 mutex_unlock(&relay_channels_mutex);
453 }
454 //ust// EXPORT_SYMBOL_GPL(ltt_relay_close);
455
456 /*
457 * Start iteration at the previous element. Skip the real list head.
458 */
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)
461 //ust// {
462 //ust// struct buf_page *iter;
463 //ust// size_t orig_iter_off;
464 //ust// unsigned int i = 0;
465 //ust//
466 //ust// orig_iter_off = page->offset;
467 //ust// list_for_each_entry_reverse(iter, &page->list, list) {
468 //ust// /*
469 //ust// * Skip the real list head.
470 //ust// */
471 //ust// if (&iter->list == &buf->pages)
472 //ust// continue;
473 //ust// i++;
474 //ust// if (offset >= iter->offset
475 //ust// && offset < iter->offset + PAGE_SIZE) {
476 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
477 //ust// if (i > 1) {
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);
485 //ust// WARN_ON(1);
486 //ust// }
487 //ust// #endif
488 //ust// return iter;
489 //ust// }
490 //ust// }
491 //ust// WARN_ON(1);
492 //ust// return NULL;
493 //ust// }
494 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_prev_page);
495
496 /*
497 * Start iteration at the next element. Skip the real list head.
498 */
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)
501 //ust// {
502 //ust// struct buf_page *iter;
503 //ust// unsigned int i = 0;
504 //ust// size_t orig_iter_off;
505 //ust//
506 //ust// orig_iter_off = page->offset;
507 //ust// list_for_each_entry(iter, &page->list, list) {
508 //ust// /*
509 //ust// * Skip the real list head.
510 //ust// */
511 //ust// if (&iter->list == &buf->pages)
512 //ust// continue;
513 //ust// i++;
514 //ust// if (offset >= iter->offset
515 //ust// && offset < iter->offset + PAGE_SIZE) {
516 //ust// #ifdef CONFIG_LTT_RELAY_CHECK_RANDOM_ACCESS
517 //ust// if (i > 1) {
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);
525 //ust// WARN_ON(1);
526 //ust// }
527 //ust// #endif
528 //ust// return iter;
529 //ust// }
530 //ust// }
531 //ust// WARN_ON(1);
532 //ust// return NULL;
533 //ust// }
534 //ust// EXPORT_SYMBOL_GPL(ltt_relay_find_next_page);
535
536 /**
537 * ltt_relay_write - write data to a ltt_relay buffer.
538 * @buf : 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
544 */
545 void _ltt_relay_write(struct rchan_buf *buf, size_t offset,
546 const void *src, size_t len, ssize_t cpy)
547 {
548 do {
549 len -= cpy;
550 src += cpy;
551 offset += cpy;
552 /*
553 * Underlying layer should never ask for writes across
554 * subbuffers.
555 */
556 WARN_ON(offset >= buf->buf_size);
557
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));
561 }
562 //ust// EXPORT_SYMBOL_GPL(_ltt_relay_write);
563
564 /**
565 * ltt_relay_read - read data from ltt_relay_buffer.
566 * @buf : buffer
567 * @offset : offset within the buffer
568 * @dest : destination address
569 * @len : length to write
570 */
571 //ust// int ltt_relay_read(struct rchan_buf *buf, size_t offset,
572 //ust// void *dest, size_t len)
573 //ust// {
574 //ust// struct buf_page *page;
575 //ust// ssize_t pagecpy, orig_len;
576 //ust//
577 //ust// orig_len = len;
578 //ust// offset &= buf->chan->alloc_size - 1;
579 //ust// page = buf->rpage;
580 //ust// if (unlikely(!len))
581 //ust// return 0;
582 //ust// for (;;) {
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),
586 //ust// pagecpy);
587 //ust// len -= pagecpy;
588 //ust// if (likely(!len))
589 //ust// break;
590 //ust// dest += pagecpy;
591 //ust// offset += pagecpy;
592 //ust// /*
593 //ust// * Underlying layer should never ask for reads across
594 //ust// * subbuffers.
595 //ust// */
596 //ust// WARN_ON(offset >= buf->chan->alloc_size);
597 //ust// }
598 //ust// return orig_len;
599 //ust// }
600 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read);
601
602 /**
603 * ltt_relay_read_get_page - Get a whole page to read from
604 * @buf : buffer
605 * @offset : offset within the buffer
606 */
607 //ust// struct buf_page *ltt_relay_read_get_page(struct rchan_buf *buf, size_t offset)
608 //ust// {
609 //ust// struct buf_page *page;
610
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);
614 //ust// return page;
615 //ust// }
616 //ust// EXPORT_SYMBOL_GPL(ltt_relay_read_get_page);
617
618 /**
619 * ltt_relay_offset_address - get address of a location within the buffer
620 * @buf : buffer
621 * @offset : offset within the buffer.
622 *
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.
627 */
628 void *ltt_relay_offset_address(struct rchan_buf *buf, size_t offset)
629 {
630 //ust// struct buf_page *page;
631 //ust// unsigned int odd;
632 //ust//
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;
641 return NULL;
642 }
643 //ust// EXPORT_SYMBOL_GPL(ltt_relay_offset_address);
644
645 /**
646 * relay_file_open - open file op for relay files
647 * @inode: the inode
648 * @filp: the file
649 *
650 * Increments the channel buffer refcount.
651 */
652 //ust// static int relay_file_open(struct inode *inode, struct file *filp)
653 //ust// {
654 //ust// struct rchan_buf *buf = inode->i_private;
655 //ust// kref_get(&buf->kref);
656 //ust// filp->private_data = buf;
657 //ust//
658 //ust// return nonseekable_open(inode, filp);
659 //ust// }
660
661 /**
662 * relay_file_release - release file op for relay files
663 * @inode: the inode
664 * @filp: the file
665 *
666 * Decrements the channel refcount, as the filesystem is
667 * no longer using it.
668 */
669 //ust// static int relay_file_release(struct inode *inode, struct file *filp)
670 //ust// {
671 //ust// struct rchan_buf *buf = filp->private_data;
672 //ust// kref_put(&buf->kref, relay_remove_buf);
673 //ust//
674 //ust// return 0;
675 //ust// }
676
677 //ust// const struct file_operations ltt_relay_file_operations = {
678 //ust// .open = relay_file_open,
679 //ust// .release = relay_file_release,
680 //ust// };
681 //ust// EXPORT_SYMBOL_GPL(ltt_relay_file_operations);
682
683 //ust// static __init int relay_init(void)
684 //ust// {
685 //ust// hotcpu_notifier(relay_hotcpu_callback, 5);
686 //ust// return 0;
687 //ust// }
688
689 //ust// module_init(relay_init);
690 /*
691 * ltt/ltt-relay.c
692 *
693 * (C) Copyright 2005-2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
694 *
695 * LTTng lockless buffer space management (reader/writer).
696 *
697 * Author:
698 * Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
699 *
700 * Inspired from LTT :
701 * Karim Yaghmour (karim@opersys.com)
702 * Tom Zanussi (zanussi@us.ibm.com)
703 * Bob Wisniewski (bob@watson.ibm.com)
704 * And from K42 :
705 * Bob Wisniewski (bob@watson.ibm.com)
706 *
707 * Changelog:
708 * 08/10/08, Cleanup.
709 * 19/10/05, Complete lockless mechanism.
710 * 27/05/05, Modular redesign and rewrite.
711 *
712 * Userspace reader semantic :
713 * while (poll fd != POLLHUP) {
714 * - ioctl RELAY_GET_SUBBUF_SIZE
715 * while (1) {
716 * - ioctl GET_SUBBUF
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.
721 * }
722 * }
723 */
724
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>
744
745 #if 0
746 #define printk_dbg(fmt, args...) printk(fmt, args)
747 #else
748 #define printk_dbg(fmt, args...)
749 #endif
750
751 /*
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
754 * atomically.
755 */
756
757 #if (BITS_PER_LONG == 32)
758 static inline void save_last_tsc(struct ltt_channel_buf_struct *ltt_buf,
759 u64 tsc)
760 {
761 ltt_buf->last_tsc = (unsigned long)(tsc >> LTT_TSC_BITS);
762 }
763
764 static inline int last_tsc_overflow(struct ltt_channel_buf_struct *ltt_buf,
765 u64 tsc)
766 {
767 unsigned long tsc_shifted = (unsigned long)(tsc >> LTT_TSC_BITS);
768
769 if (unlikely((tsc_shifted - ltt_buf->last_tsc)))
770 return 1;
771 else
772 return 0;
773 }
774 #else
775 static inline void save_last_tsc(struct ltt_channel_buf_struct *ltt_buf,
776 u64 tsc)
777 {
778 ltt_buf->last_tsc = (unsigned long)tsc;
779 }
780
781 static inline int last_tsc_overflow(struct ltt_channel_buf_struct *ltt_buf,
782 u64 tsc)
783 {
784 if (unlikely((tsc - ltt_buf->last_tsc) >> LTT_TSC_BITS))
785 return 1;
786 else
787 return 0;
788 }
789 #endif
790
791 //ust// static struct file_operations ltt_file_operations;
792
793 /*
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).
796 */
797 enum force_switch_mode { FORCE_ACTIVE, FORCE_FLUSH };
798
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);
803
804 static void ltt_relay_destroy_buffer(struct ltt_channel_struct *ltt_chan);
805
806 static void ltt_force_switch(struct rchan_buf *buf,
807 enum force_switch_mode mode);
808
809 /*
810 * Trace callbacks
811 */
812 static void ltt_buffer_begin_callback(struct rchan_buf *buf,
813 u64 tsc, unsigned int subbuf_idx)
814 {
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);
821
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);
826 }
827
828 /*
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.
831 */
832 static notrace void ltt_buffer_end_callback(struct rchan_buf *buf,
833 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
834 {
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);
842
843 header->lost_size = SUBBUF_OFFSET((buf->chan->subbuf_size - offset),
844 buf->chan);
845 header->cycle_count_end = tsc;
846 header->events_lost = local_read(&ltt_buf->events_lost);
847 header->subbuf_corrupt = local_read(&ltt_buf->corrupted_subbuffers);
848
849 }
850
851 static notrace void ltt_deliver(struct rchan_buf *buf, unsigned int subbuf_idx,
852 void *subbuf)
853 {
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;
857
858 atomic_set(&ltt_buf->wakeup_readers, 1);
859 }
860
861 static struct dentry *ltt_create_buf_file_callback(struct rchan_buf *buf)
862 {
863 struct ltt_channel_struct *ltt_chan;
864 int err;
865 //ust// struct dentry *dentry;
866
867 ltt_chan = buf->chan->private_data;
868 err = ltt_relay_create_buffer(ltt_chan->trace, ltt_chan, buf, buf->chan->n_subbufs);
869 if (err)
870 return ERR_PTR(err);
871
872 //ust// dentry = debugfs_create_file(filename, mode, parent, buf,
873 //ust// &ltt_file_operations);
874 //ust// if (!dentry)
875 //ust// goto error;
876 //ust// return dentry;
877 return NULL; //ust//
878 //ust//error:
879 ltt_relay_destroy_buffer(ltt_chan);
880 return NULL;
881 }
882
883 static int ltt_remove_buf_file_callback(struct rchan_buf *buf)
884 {
885 //ust// struct rchan_buf *buf = dentry->d_inode->i_private;
886 struct ltt_channel_struct *ltt_chan = buf->chan->private_data;
887
888 //ust// debugfs_remove(dentry);
889 ltt_relay_destroy_buffer(ltt_chan);
890
891 return 0;
892 }
893
894 /*
895 * Wake writers :
896 *
897 * This must be done after the trace is removed from the RCU list so that there
898 * are no stalled writers.
899 */
900 //ust// static void ltt_relay_wake_writers(struct ltt_channel_buf_struct *ltt_buf)
901 //ust// {
902 //ust//
903 //ust// if (waitqueue_active(&ltt_buf->write_wait))
904 //ust// wake_up_interruptible(&ltt_buf->write_wait);
905 //ust// }
906
907 /*
908 * This function should not be called from NMI interrupt context
909 */
910 static notrace void ltt_buf_unfull(struct rchan_buf *buf,
911 unsigned int subbuf_idx,
912 long offset)
913 {
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;
917 //ust//
918 //ust// ltt_relay_wake_writers(ltt_buf);
919 }
920
921 /**
922 * ltt_open - open file op for ltt files
923 * @inode: opened inode
924 * @file: opened file
925 *
926 * Open implementation. Makes sure only one open instance of a buffer is
927 * done at a given moment.
928 */
929 //ust// static int ltt_open(struct inode *inode, struct file *file)
930 //ust// {
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);
936 //ust//
937 //ust// if (!atomic_long_add_unless(&ltt_buf->active_readers, 1, 1))
938 //ust// return -EBUSY;
939 //ust// return ltt_relay_file_operations.open(inode, file);
940 //ust// }
941
942 /**
943 * ltt_release - release file op for ltt files
944 * @inode: opened inode
945 * @file: opened file
946 *
947 * Release implementation.
948 */
949 //ust// static int ltt_release(struct inode *inode, struct file *file)
950 //ust// {
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);
956 //ust// int ret;
957 //ust//
958 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
959 //ust// atomic_long_dec(&ltt_buf->active_readers);
960 //ust// ret = ltt_relay_file_operations.release(inode, file);
961 //ust// WARN_ON(ret);
962 //ust// return ret;
963 //ust// }
964
965 /**
966 * ltt_poll - file op for ltt files
967 * @filp: the file
968 * @wait: poll table
969 *
970 * Poll implementation.
971 */
972 //ust// static unsigned int ltt_poll(struct file *filp, poll_table *wait)
973 //ust// {
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);
981 //ust//
982 //ust// if (filp->f_mode & FMODE_READ) {
983 //ust// poll_wait_set_exclusive(wait);
984 //ust// poll_wait(filp, &buf->read_wait, wait);
985 //ust//
986 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
987 //ust// if (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
988 //ust// buf->chan)
989 //ust// - SUBBUF_TRUNC(atomic_long_read(&ltt_buf->consumed),
990 //ust// buf->chan)
991 //ust// == 0) {
992 //ust// if (buf->finalized)
993 //ust// return POLLHUP;
994 //ust// else
995 //ust// return 0;
996 //ust// } else {
997 //ust// struct rchan *rchan =
998 //ust// ltt_channel->trans_channel_data;
999 //ust// if (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1000 //ust// buf->chan)
1001 //ust// - SUBBUF_TRUNC(atomic_long_read(
1002 //ust// &ltt_buf->consumed),
1003 //ust// buf->chan)
1004 //ust// >= rchan->alloc_size)
1005 //ust// return POLLPRI | POLLRDBAND;
1006 //ust// else
1007 //ust// return POLLIN | POLLRDNORM;
1008 //ust// }
1009 //ust// }
1010 //ust// return mask;
1011 //ust// }
1012
1013 int ltt_do_get_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, long *pconsumed_old)
1014 {
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(&ltt_buf->consumed);
1018 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1019 commit_count = local_read(&ltt_buf->commit_count[consumed_idx]);
1020 /*
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.
1025 */
1026 smp_rmb();
1027 write_offset = local_read(&ltt_buf->offset);
1028 /*
1029 * Check that the subbuffer we are trying to consume has been
1030 * already fully committed.
1031 */
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)
1036 != 0) {
1037 return -EAGAIN;
1038 }
1039 /*
1040 * Check that we are not about to read the same subbuffer in
1041 * which the writer head is.
1042 */
1043 if ((SUBBUF_TRUNC(write_offset, buf->chan)
1044 - SUBBUF_TRUNC(consumed_old, buf->chan))
1045 == 0) {
1046 return -EAGAIN;
1047 }
1048
1049 *pconsumed_old = consumed_old;
1050 return 0;
1051 }
1052
1053 int ltt_do_put_subbuf(struct rchan_buf *buf, struct ltt_channel_buf_struct *ltt_buf, u32 uconsumed_old)
1054 {
1055 long consumed_new, consumed_old;
1056
1057 consumed_old = atomic_long_read(&ltt_buf->consumed);
1058 consumed_old = consumed_old & (~0xFFFFFFFFL);
1059 consumed_old = consumed_old | uconsumed_old;
1060 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
1061
1062 spin_lock(&ltt_buf->full_lock);
1063 if (atomic_long_cmpxchg(&ltt_buf->consumed, consumed_old,
1064 consumed_new)
1065 != 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(&ltt_buf->full_lock);
1070 return -EIO;
1071 } else {
1072 /* tell the client that buffer is now unfull */
1073 int index;
1074 long data;
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(&ltt_buf->full_lock);
1079 }
1080 return 0;
1081 }
1082
1083 /**
1084 * ltt_ioctl - control on the debugfs file
1085 *
1086 * @inode: the inode
1087 * @filp: the file
1088 * @cmd: the command
1089 * @arg: command arg
1090 *
1091 * This ioctl implements three commands necessary for a minimal
1092 * producer/consumer implementation :
1093 * RELAY_GET_SUBBUF
1094 * Get the next sub buffer that can be read. It never blocks.
1095 * RELAY_PUT_SUBBUF
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.
1102 */
1103 //ust// static int ltt_ioctl(struct inode *inode, struct file *filp,
1104 //ust// unsigned int cmd, unsigned long arg)
1105 //ust// {
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;
1112 //ust//
1113 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1114 //ust// switch (cmd) {
1115 //ust// case RELAY_GET_SUBBUF:
1116 //ust// {
1117 //ust// int ret;
1118 //ust// ret = ltt_do_get_subbuf(buf, ltt_buf, &consumed_old);
1119 //ust// if(ret < 0)
1120 //ust// return ret;
1121 //ust// return put_user((u32)consumed_old, argp);
1122 //ust// }
1123 //ust// case RELAY_PUT_SUBBUF:
1124 //ust// {
1125 //ust// int ret;
1126 //ust// u32 uconsumed_old;
1127 //ust// ret = get_user(uconsumed_old, argp);
1128 //ust// if (ret)
1129 //ust// return ret; /* will return -EFAULT */
1130 //ust// return ltt_do_put_subbuf(buf, ltt_buf, uconsumed_old);
1131 //ust// }
1132 //ust// case RELAY_GET_N_SUBBUFS:
1133 //ust// return put_user((u32)buf->chan->n_subbufs, argp);
1134 //ust// break;
1135 //ust// case RELAY_GET_SUBBUF_SIZE:
1136 //ust// return put_user((u32)buf->chan->subbuf_size, argp);
1137 //ust// break;
1138 //ust// default:
1139 //ust// return -ENOIOCTLCMD;
1140 //ust// }
1141 //ust// return 0;
1142 //ust// }
1143
1144 //ust// #ifdef CONFIG_COMPAT
1145 //ust// static long ltt_compat_ioctl(struct file *file, unsigned int cmd,
1146 //ust// unsigned long arg)
1147 //ust// {
1148 //ust// long ret = -ENOIOCTLCMD;
1149 //ust//
1150 //ust// lock_kernel();
1151 //ust// ret = ltt_ioctl(file->f_dentry->d_inode, file, cmd, arg);
1152 //ust// unlock_kernel();
1153 //ust//
1154 //ust// return ret;
1155 //ust// }
1156 //ust// #endif
1157
1158 //ust// static void ltt_relay_pipe_buf_release(struct pipe_inode_info *pipe,
1159 //ust// struct pipe_buffer *pbuf)
1160 //ust// {
1161 //ust// }
1162 //ust//
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,
1171 //ust// };
1172
1173 //ust// static void ltt_relay_page_release(struct splice_pipe_desc *spd, unsigned int i)
1174 //ust// {
1175 //ust// }
1176
1177 /*
1178 * subbuf_splice_actor - splice up to one subbuf's worth of data
1179 */
1180 //ust// static int subbuf_splice_actor(struct file *in,
1181 //ust// loff_t *ppos,
1182 //ust// struct pipe_inode_info *pipe,
1183 //ust// size_t len,
1184 //ust// unsigned int flags)
1185 //ust// {
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 = &ltt_relay_pipe_buf_ops,
1200 //ust// .spd_release = ltt_relay_page_release,
1201 //ust// };
1202 //ust// long consumed_old, consumed_idx, roffset;
1203 //ust// unsigned long bytes_avail;
1204 //ust//
1205 //ust// /*
1206 //ust// * Check that a GET_SUBBUF ioctl has been done before.
1207 //ust// */
1208 //ust// WARN_ON(atomic_long_read(&ltt_buf->active_readers) != 1);
1209 //ust// consumed_old = atomic_long_read(&ltt_buf->consumed);
1210 //ust// consumed_old += *ppos;
1211 //ust// consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
1212 //ust//
1213 //ust// /*
1214 //ust// * Adjust read len, if longer than what is available
1215 //ust// */
1216 //ust// bytes_avail = SUBBUF_TRUNC(local_read(&ltt_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(&ltt_buf->offset));
1226 //ust//
1227 //ust// for (; spd.nr_pages < nr_pages; spd.nr_pages++) {
1228 //ust// unsigned int this_len;
1229 //ust// struct buf_page *page;
1230 //ust//
1231 //ust// if (!len)
1232 //ust// break;
1233 //ust// printk_dbg(KERN_DEBUG "SPLICE actor loop len %zu roffset %ld\n",
1234 //ust// len, roffset);
1235 //ust//
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;
1241 //ust//
1242 //ust// poff = 0;
1243 //ust// roffset += PAGE_SIZE;
1244 //ust// len -= this_len;
1245 //ust// }
1246 //ust//
1247 //ust// if (!spd.nr_pages)
1248 //ust// return 0;
1249 //ust//
1250 //ust// return splice_to_pipe(pipe, &spd);
1251 //ust// }
1252
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,
1256 //ust// size_t len,
1257 //ust// unsigned int flags)
1258 //ust// {
1259 //ust// ssize_t spliced;
1260 //ust// int ret;
1261 //ust//
1262 //ust// ret = 0;
1263 //ust// spliced = 0;
1264 //ust//
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)
1271 //ust// break;
1272 //ust// else if (!ret) {
1273 //ust// if (flags & SPLICE_F_NONBLOCK)
1274 //ust// ret = -EAGAIN;
1275 //ust// break;
1276 //ust// }
1277 //ust//
1278 //ust// *ppos += ret;
1279 //ust// if (ret > len)
1280 //ust// len = 0;
1281 //ust// else
1282 //ust// len -= ret;
1283 //ust// spliced += ret;
1284 //ust// }
1285 //ust//
1286 //ust// if (spliced)
1287 //ust// return spliced;
1288 //ust//
1289 //ust// return ret;
1290 //ust// }
1291
1292 static void ltt_relay_print_subbuffer_errors(
1293 struct ltt_channel_struct *ltt_chan,
1294 long cons_off)
1295 {
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;
1299
1300 cons_idx = SUBBUF_INDEX(cons_off, rchan);
1301 commit_count = local_read(&ltt_buf->commit_count[cons_idx]);
1302 /*
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.
1305 */
1306 write_offset = local_read(&ltt_buf->offset);
1307 printk(KERN_WARNING
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)
1314 != 0)
1315 printk(KERN_ALERT
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);
1322 }
1323
1324 static void ltt_relay_print_errors(struct ltt_trace_struct *trace,
1325 struct ltt_channel_struct *ltt_chan)
1326 {
1327 struct rchan *rchan = ltt_chan->trans_channel_data;
1328 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1329 long cons_off;
1330
1331 for (cons_off = atomic_long_read(&ltt_buf->consumed);
1332 (SUBBUF_TRUNC(local_read(&ltt_buf->offset),
1333 rchan)
1334 - cons_off) > 0;
1335 cons_off = SUBBUF_ALIGN(cons_off, rchan))
1336 ltt_relay_print_subbuffer_errors(ltt_chan, cons_off);
1337 }
1338
1339 static void ltt_relay_print_buffer_errors(struct ltt_channel_struct *ltt_chan)
1340 {
1341 struct ltt_trace_struct *trace = ltt_chan->trace;
1342 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1343
1344 if (local_read(&ltt_buf->events_lost))
1345 printk(KERN_ALERT
1346 "LTT : %s : %ld events lost "
1347 "in %s channel.\n",
1348 ltt_chan->channel_name,
1349 local_read(&ltt_buf->events_lost),
1350 ltt_chan->channel_name);
1351 if (local_read(&ltt_buf->corrupted_subbuffers))
1352 printk(KERN_ALERT
1353 "LTT : %s : %ld corrupted subbuffers "
1354 "in %s channel.\n",
1355 ltt_chan->channel_name,
1356 local_read(&ltt_buf->corrupted_subbuffers),
1357 ltt_chan->channel_name);
1358
1359 ltt_relay_print_errors(trace, ltt_chan);
1360 }
1361
1362 static void ltt_relay_remove_dirs(struct ltt_trace_struct *trace)
1363 {
1364 //ust// debugfs_remove(trace->dentry.trace_root);
1365 }
1366
1367 static void ltt_relay_release_channel(struct kref *kref)
1368 {
1369 struct ltt_channel_struct *ltt_chan = container_of(kref,
1370 struct ltt_channel_struct, kref);
1371 free(ltt_chan->buf);
1372 }
1373
1374 /*
1375 * Create ltt buffer.
1376 */
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)
1380 //ust// {
1381 //ust// struct ltt_channel_buf_struct *ltt_buf =
1382 //ust// percpu_ptr(ltt_chan->buf, cpu);
1383 //ust// unsigned int j;
1384 //ust//
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(&ltt_chan->kref);
1393 //ust// local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
1394 //ust// atomic_long_set(&ltt_buf->consumed, 0);
1395 //ust// atomic_long_set(&ltt_buf->active_readers, 0);
1396 //ust// for (j = 0; j < n_subbufs; j++)
1397 //ust// local_set(&ltt_buf->commit_count[j], 0);
1398 //ust// init_waitqueue_head(&ltt_buf->write_wait);
1399 //ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1400 //ust// spin_lock_init(&ltt_buf->full_lock);
1401 //ust//
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(), &ltt_buf->commit_count[0]);
1406 //ust//
1407 //ust// local_set(&ltt_buf->events_lost, 0);
1408 //ust// local_set(&ltt_buf->corrupted_subbuffers, 0);
1409 //ust//
1410 //ust// return 0;
1411 //ust// }
1412
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)
1416 {
1417 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1418 unsigned int j;
1419
1420 ltt_buf->commit_count =
1421 zmalloc(sizeof(ltt_buf->commit_count) * n_subbufs);
1422 if (!ltt_buf->commit_count)
1423 return -ENOMEM;
1424 kref_get(&trace->kref);
1425 kref_get(&trace->ltt_transport_kref);
1426 kref_get(&ltt_chan->kref);
1427 local_set(&ltt_buf->offset, ltt_subbuffer_header_size());
1428 atomic_long_set(&ltt_buf->consumed, 0);
1429 atomic_long_set(&ltt_buf->active_readers, 0);
1430 for (j = 0; j < n_subbufs; j++)
1431 local_set(&ltt_buf->commit_count[j], 0);
1432 //ust// init_waitqueue_head(&ltt_buf->write_wait);
1433 atomic_set(&ltt_buf->wakeup_readers, 0);
1434 spin_lock_init(&ltt_buf->full_lock);
1435
1436 ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
1437
1438 local_add(ltt_subbuffer_header_size(), &ltt_buf->commit_count[0]);
1439
1440 local_set(&ltt_buf->events_lost, 0);
1441 local_set(&ltt_buf->corrupted_subbuffers, 0);
1442
1443 return 0;
1444 }
1445
1446 static void ltt_relay_destroy_buffer(struct ltt_channel_struct *ltt_chan)
1447 {
1448 struct ltt_trace_struct *trace = ltt_chan->trace;
1449 struct ltt_channel_buf_struct *ltt_buf = ltt_chan->buf;
1450
1451 kref_put(&ltt_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(&ltt_chan->kref, ltt_relay_release_channel);
1457 kref_put(&trace->kref, ltt_release_trace);
1458 //ust// wake_up_interruptible(&trace->kref_wq);
1459 }
1460
1461 /*
1462 * Create channel.
1463 */
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,
1468 int overwrite)
1469 {
1470 char *tmpname;
1471 unsigned int tmpname_len;
1472 int err = 0;
1473
1474 tmpname = kmalloc(PATH_MAX, GFP_KERNEL);
1475 if (!tmpname)
1476 return EPERM;
1477 if (overwrite) {
1478 strncpy(tmpname, LTT_FLIGHT_PREFIX, PATH_MAX-1);
1479 strncat(tmpname, channel_name,
1480 PATH_MAX-1-sizeof(LTT_FLIGHT_PREFIX));
1481 } else {
1482 strncpy(tmpname, channel_name, PATH_MAX-1);
1483 }
1484 strncat(tmpname, "_", PATH_MAX-1-strlen(tmpname));
1485
1486 kref_init(&ltt_chan->kref);
1487
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));
1496 if (!ltt_chan->buf)
1497 goto alloc_error;
1498 ltt_chan->trans_channel_data = ltt_relay_open(tmpname,
1499 dir,
1500 subbuf_size,
1501 n_subbufs,
1502 ltt_chan);
1503 tmpname_len = strlen(tmpname);
1504 if (tmpname_len > 0) {
1505 /* Remove final _ for pretty printing */
1506 tmpname[tmpname_len-1] = '\0';
1507 }
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;
1512 }
1513
1514 err = 0;
1515 goto end;
1516
1517 relay_open_error:
1518 //ust// percpu_free(ltt_chan->buf);
1519 alloc_error:
1520 err = EPERM;
1521 end:
1522 kfree(tmpname);
1523 return err;
1524 }
1525
1526 static int ltt_relay_create_dirs(struct ltt_trace_struct *new_trace)
1527 {
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;
1534 //ust// }
1535
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;
1538
1539 return 0;
1540 }
1541
1542 /*
1543 * LTTng channel flush function.
1544 *
1545 * Must be called when no tracing is active in the channel, because of
1546 * accesses across CPUs.
1547 */
1548 static notrace void ltt_relay_buffer_flush(struct rchan_buf *buf)
1549 {
1550 buf->finalized = 1;
1551 ltt_force_switch(buf, FORCE_FLUSH);
1552 }
1553
1554 static void ltt_relay_async_wakeup_chan(struct ltt_channel_struct *ltt_channel)
1555 {
1556 //ust// unsigned int i;
1557 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
1558 //ust//
1559 //ust// for_each_possible_cpu(i) {
1560 //ust// struct ltt_channel_buf_struct *ltt_buf =
1561 //ust// percpu_ptr(ltt_channel->buf, i);
1562 //ust//
1563 //ust// if (atomic_read(&ltt_buf->wakeup_readers) == 1) {
1564 //ust// atomic_set(&ltt_buf->wakeup_readers, 0);
1565 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
1566 //ust// }
1567 //ust// }
1568 }
1569
1570 static void ltt_relay_finish_buffer(struct ltt_channel_struct *ltt_channel)
1571 {
1572 struct rchan *rchan = ltt_channel->trans_channel_data;
1573
1574 if (rchan->buf) {
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);
1578 }
1579 }
1580
1581
1582 static void ltt_relay_finish_channel(struct ltt_channel_struct *ltt_channel)
1583 {
1584 unsigned int i;
1585
1586 //ust// for_each_possible_cpu(i)
1587 ltt_relay_finish_buffer(ltt_channel);
1588 }
1589
1590 static void ltt_relay_remove_channel(struct ltt_channel_struct *channel)
1591 {
1592 struct rchan *rchan = channel->trans_channel_data;
1593
1594 ltt_relay_close(rchan);
1595 kref_put(&channel->kref, ltt_relay_release_channel);
1596 }
1597
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;
1603 };
1604
1605 /*
1606 * Returns :
1607 * 0 if ok
1608 * !0 if execution must be aborted.
1609 */
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)
1616 {
1617 offsets->begin = local_read(&ltt_buf->offset);
1618 offsets->old = offsets->begin;
1619 offsets->begin_switch = 0;
1620 offsets->end_switch_current = 0;
1621 offsets->end_switch_old = 0;
1622
1623 *tsc = trace_clock_read64();
1624 if (last_tsc_overflow(ltt_buf, *tsc))
1625 *rflags = LTT_RFLAG_ID_SIZE_TSC;
1626
1627 if (SUBBUF_OFFSET(offsets->begin, buf->chan) == 0) {
1628 offsets->begin_switch = 1; /* For offsets->begin */
1629 } else {
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,
1634 largest_align)
1635 + data_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 */
1640 }
1641 }
1642 if (offsets->begin_switch) {
1643 long subbuf_index;
1644
1645 if (offsets->end_switch_old)
1646 offsets->begin = SUBBUF_ALIGN(offsets->begin,
1647 buf->chan);
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(&ltt_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 &ltt_buf->consumed),
1662 buf->chan))
1663 >= rchan->alloc_size) {
1664 /*
1665 * We do not overwrite non consumed buffers
1666 * and we are full : event is lost.
1667 */
1668 local_inc(&ltt_buf->events_lost);
1669 return -1;
1670 } else {
1671 /*
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.
1675 */
1676 }
1677 } else {
1678 /*
1679 * Next subbuffer corrupted. Force pushing reader even
1680 * in normal mode. It's safe to write in this new
1681 * subbuffer.
1682 */
1683 }
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,
1688 largest_align)
1689 + data_size;
1690 if ((SUBBUF_OFFSET(offsets->begin, buf->chan) + offsets->size)
1691 > buf->chan->subbuf_size) {
1692 /*
1693 * Event too big for subbuffers, report error, don't
1694 * complete the sub-buffer switch.
1695 */
1696 local_inc(&ltt_buf->events_lost);
1697 return -1;
1698 } else {
1699 /*
1700 * We just made a successful buffer switch and the event
1701 * fits in the new subbuffer. Let's write.
1702 */
1703 }
1704 } else {
1705 /*
1706 * Event fits in the current buffer and we are not on a switch
1707 * boundary. It's safe to write.
1708 */
1709 }
1710 offsets->end = offsets->begin + offsets->size;
1711
1712 if ((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0) {
1713 /*
1714 * The offset_end will fall at the very beginning of the next
1715 * subbuffer.
1716 */
1717 offsets->end_switch_current = 1; /* For offsets->begin */
1718 }
1719 return 0;
1720 }
1721
1722 /*
1723 * Returns :
1724 * 0 if ok
1725 * !0 if execution must be aborted.
1726 */
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,
1733 u64 *tsc)
1734 {
1735 long subbuf_index;
1736
1737 offsets->begin = local_read(&ltt_buf->offset);
1738 offsets->old = offsets->begin;
1739 offsets->begin_switch = 0;
1740 offsets->end_switch_old = 0;
1741
1742 *tsc = trace_clock_read64();
1743
1744 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
1745 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
1746 offsets->end_switch_old = 1;
1747 } else {
1748 /* we do not have to switch : buffer is empty */
1749 return -1;
1750 }
1751 if (mode == FORCE_ACTIVE)
1752 offsets->begin += ltt_subbuffer_header_size();
1753 /*
1754 * Always begin_switch in FORCE_ACTIVE mode.
1755 * Test new buffer integrity
1756 */
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(&ltt_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(&ltt_buf->consumed)
1768 >= rchan->alloc_size) {
1769 /*
1770 * We do not overwrite non consumed buffers and we are
1771 * full : ignore switch while tracing is active.
1772 */
1773 return -1;
1774 }
1775 } else {
1776 /*
1777 * Next subbuffer corrupted. Force pushing reader even in normal
1778 * mode
1779 */
1780 }
1781 offsets->end = offsets->begin;
1782 return 0;
1783 }
1784
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)
1791 {
1792 long consumed_old, consumed_new;
1793
1794 do {
1795 consumed_old = atomic_long_read(&ltt_buf->consumed);
1796 /*
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.
1806 */
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);
1811 else {
1812 consumed_new = consumed_old;
1813 break;
1814 }
1815 } while (atomic_long_cmpxchg(&ltt_buf->consumed, consumed_old,
1816 consumed_new) != consumed_old);
1817
1818 if (consumed_old != consumed_new) {
1819 /*
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.
1829 */
1830 if (offsets->reserve_commit_diff) {
1831 /*
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
1835 * mode).
1836 */
1837 local_add(offsets->reserve_commit_diff,
1838 &ltt_buf->commit_count[
1839 SUBBUF_INDEX(offsets->begin,
1840 buf->chan)]);
1841 if (!ltt_channel->overwrite
1842 || offsets->reserve_commit_diff
1843 != rchan->subbuf_size) {
1844 /*
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
1850 * subbuffer.
1851 */
1852 local_inc(&ltt_buf->corrupted_subbuffers);
1853 }
1854 }
1855 }
1856 }
1857
1858
1859 /*
1860 * ltt_reserve_switch_old_subbuf: switch old subbuffer
1861 *
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.
1866 *
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.
1871 *
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.
1876 *
1877 * Note : offset_old should never be 0 here.
1878 */
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)
1884 {
1885 long oldidx = SUBBUF_INDEX(offsets->old - 1, rchan);
1886
1887 ltt_channel->buffer_end(buf, *tsc, offsets->old, oldidx);
1888 /* Must write buffer end before incrementing commit count */
1889 smp_wmb();
1890 offsets->commit_count =
1891 local_add_return(rchan->subbuf_size
1892 - (SUBBUF_OFFSET(offsets->old - 1, rchan)
1893 + 1),
1894 &ltt_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);
1900 }
1901
1902 /*
1903 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
1904 *
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.
1908 */
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)
1914 {
1915 long beginidx = SUBBUF_INDEX(offsets->begin, rchan);
1916
1917 ltt_channel->buffer_begin(buf, *tsc, beginidx);
1918 /* Must write buffer end before incrementing commit count */
1919 smp_wmb();
1920 offsets->commit_count = local_add_return(ltt_subbuffer_header_size(),
1921 &ltt_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);
1928 }
1929
1930
1931 /*
1932 * ltt_reserve_end_switch_current: finish switching current subbuffer
1933 *
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.
1938 *
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.
1943 *
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.
1948 */
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)
1954 {
1955 long endidx = SUBBUF_INDEX(offsets->end - 1, rchan);
1956
1957 ltt_channel->buffer_end(buf, *tsc, offsets->end, endidx);
1958 /* Must write buffer begin before incrementing commit count */
1959 smp_wmb();
1960 offsets->commit_count =
1961 local_add_return(rchan->subbuf_size
1962 - (SUBBUF_OFFSET(offsets->end - 1, rchan)
1963 + 1),
1964 &ltt_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);
1970 }
1971
1972 /**
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)
1981 * @cpu: cpuid
1982 *
1983 * Return : -ENOSPC if not enough space, else returns 0.
1984 * It will take care of sub-buffer switching.
1985 */
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)
1990 {
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;
1995
1996 offsets.reserve_commit_diff = 0;
1997 offsets.size = 0;
1998
1999 /*
2000 * Perform retryable operations.
2001 */
2002 if (ltt_nesting > 4) {
2003 local_inc(&ltt_buf->events_lost);
2004 return -EPERM;
2005 }
2006 do {
2007 if (ltt_relay_try_reserve(ltt_channel, ltt_buf,
2008 rchan, buf, &offsets, data_size, tsc, rflags,
2009 largest_align))
2010 return -ENOSPC;
2011 } while (local_cmpxchg(&ltt_buf->offset, offsets.old,
2012 offsets.end) != offsets.old);
2013
2014 /*
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
2018 * needed).
2019 */
2020 save_last_tsc(ltt_buf, *tsc);
2021
2022 /*
2023 * Push the reader if necessary
2024 */
2025 ltt_reserve_push_reader(ltt_channel, ltt_buf, rchan, buf, &offsets);
2026
2027 /*
2028 * Switch old subbuffer if needed.
2029 */
2030 if (offsets.end_switch_old)
2031 ltt_reserve_switch_old_subbuf(ltt_channel, ltt_buf, rchan, buf,
2032 &offsets, tsc);
2033
2034 /*
2035 * Populate new subbuffer.
2036 */
2037 if (offsets.begin_switch)
2038 ltt_reserve_switch_new_subbuf(ltt_channel, ltt_buf, rchan,
2039 buf, &offsets, tsc);
2040
2041 if (offsets.end_switch_current)
2042 ltt_reserve_end_switch_current(ltt_channel, ltt_buf, rchan,
2043 buf, &offsets, tsc);
2044
2045 *slot_size = offsets.size;
2046 *buf_offset = offsets.begin + offsets.before_hdr_pad;
2047 return 0;
2048 }
2049
2050 /*
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.
2054 *
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.
2058 */
2059 static notrace void ltt_force_switch(struct rchan_buf *buf,
2060 enum force_switch_mode mode)
2061 {
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;
2067 u64 tsc;
2068
2069 offsets.reserve_commit_diff = 0;
2070 offsets.size = 0;
2071
2072 /*
2073 * Perform retryable operations.
2074 */
2075 do {
2076 if (ltt_relay_try_switch(mode, ltt_channel, ltt_buf,
2077 rchan, buf, &offsets, &tsc))
2078 return;
2079 } while (local_cmpxchg(&ltt_buf->offset, offsets.old,
2080 offsets.end) != offsets.old);
2081
2082 /*
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
2086 * needed).
2087 */
2088 save_last_tsc(ltt_buf, tsc);
2089
2090 /*
2091 * Push the reader if necessary
2092 */
2093 if (mode == FORCE_ACTIVE)
2094 ltt_reserve_push_reader(ltt_channel, ltt_buf, rchan,
2095 buf, &offsets);
2096
2097 /*
2098 * Switch old subbuffer if needed.
2099 */
2100 if (offsets.end_switch_old)
2101 ltt_reserve_switch_old_subbuf(ltt_channel, ltt_buf, rchan, buf,
2102 &offsets, &tsc);
2103
2104 /*
2105 * Populate new subbuffer.
2106 */
2107 if (mode == FORCE_ACTIVE)
2108 ltt_reserve_switch_new_subbuf(ltt_channel,
2109 ltt_buf, rchan, buf, &offsets, &tsc);
2110 }
2111
2112 /*
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
2116 * crash dump.
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
2119 * subbuffer).
2120 */
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)
2124 {
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;
2132
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);
2138 for (;;) {
2139 lost_old = header->lost_size;
2140 commit_count =
2141 local_read(&ltt_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,
2147 lost_new);
2148 if (lost_old <= lost_new)
2149 break;
2150 } else {
2151 break;
2152 }
2153 }
2154 }
2155 #else
2156 static inline void ltt_write_commit_counter(struct rchan_buf *buf,
2157 long buf_offset, size_t slot_size)
2158 {
2159 }
2160 #endif
2161
2162 /*
2163 * Atomic unordered slot commit. Increments the commit count in the
2164 * specified sub-buffer, and delivers it if necessary.
2165 *
2166 * Parameters:
2167 *
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.
2172 */
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)
2176 {
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);
2182 long commit_count;
2183
2184 /* Must write slot data before incrementing commit count */
2185 smp_wmb();
2186 commit_count = local_add_return(slot_size,
2187 &ltt_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);
2194 /*
2195 * Update lost_size for each commit. It's needed only for extracting
2196 * ltt buffers from vmcore, after crash.
2197 */
2198 ltt_write_commit_counter(buf, buf_offset, slot_size);
2199 }
2200
2201 /*
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.
2205 */
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)
2209 {
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;
2214 //ust// int cpu;
2215 //ust// DECLARE_WAITQUEUE(wait, current);
2216 //ust//
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);
2222 //ust//
2223 //ust// /*
2224 //ust// * Check if data is too big for the channel : do not
2225 //ust// * block for it.
2226 //ust// */
2227 //ust// if (LTT_RESERVE_CRITICAL + data_size > relay_buf->chan->subbuf_size)
2228 //ust// return 0;
2229 //ust//
2230 //ust// /*
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).
2234 //ust// */
2235 //ust// spin_lock(&ltt_buf->full_lock);
2236 //ust// if (!channel->overwrite) {
2237 //ust// dbg->write = local_read(&ltt_buf->offset);
2238 //ust// dbg->read = atomic_long_read(&ltt_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(&ltt_buf->write_wait, &wait);
2245 //ust// spin_unlock(&ltt_buf->full_lock);
2246 //ust// preempt_enable();
2247 //ust// schedule();
2248 //ust// __set_current_state(TASK_RUNNING);
2249 //ust// remove_wait_queue(&ltt_buf->write_wait, &wait);
2250 //ust// if (signal_pending(current))
2251 //ust// return -ERESTARTSYS;
2252 //ust// preempt_disable();
2253 //ust// return 1;
2254 //ust// }
2255 //ust// }
2256 //ust// spin_unlock(&ltt_buf->full_lock);
2257 return 0;
2258 }
2259
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)
2263 {
2264 struct rchan *rchan;
2265 struct ltt_channel_buf_struct *ltt_buf;
2266 struct ltt_channel_struct *channel;
2267 struct rchan_buf *relay_buf;
2268
2269 channel = &trace->channels[chan_index];
2270 rchan = channel->trans_channel_data;
2271 relay_buf = rchan->buf;
2272 ltt_buf = channel->buf;
2273
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",
2279 dbg->avail_size);
2280 printk(KERN_ERR "avai write : %lu, read : %lu\n",
2281 dbg->write, dbg->read);
2282
2283 dbg->write = local_read(&ltt_buf->offset);
2284 dbg->read = atomic_long_read(&ltt_buf->consumed);
2285
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);
2291 }
2292
2293 //ust// static struct ltt_transport ltt_relay_transport = {
2294 //ust// .name = "relay",
2295 //ust// .owner = THIS_MODULE,
2296 //ust// .ops = {
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,
2307 //ust// },
2308 //ust// };
2309
2310 static struct ltt_transport ust_relay_transport = {
2311 .name = "ustrelay",
2312 //ust// .owner = THIS_MODULE,
2313 .ops = {
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,
2324 },
2325 };
2326
2327 //ust// static int __init ltt_relay_init(void)
2328 //ust// {
2329 //ust// printk(KERN_INFO "LTT : ltt-relay init\n");
2330 //ust//
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;
2340 //ust//#endif
2341 //ust//
2342 //ust// ltt_transport_register(&ltt_relay_transport);
2343 //ust//
2344 //ust// return 0;
2345 //ust// }
2346
2347 static char initialized = 0;
2348
2349 void __attribute__((constructor)) init_ustrelay_transport(void)
2350 {
2351 if(!initialized) {
2352 ltt_transport_register(&ust_relay_transport);
2353 initialized = 1;
2354 }
2355 }
2356
2357 static void __exit ltt_relay_exit(void)
2358 {
2359 //ust// printk(KERN_INFO "LTT : ltt-relay exit\n");
2360
2361 ltt_transport_unregister(&ust_relay_transport);
2362 }
2363
2364 //ust// module_init(ltt_relay_init);
2365 //ust// module_exit(ltt_relay_exit);
2366 //ust//
2367 //ust// MODULE_LICENSE("GPL");
2368 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
2369 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Lockless Relay");
This page took 0.100943 seconds and 4 git commands to generate.