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