move libkcompat inside ust
[ust.git] / libust / buffers.c
1 /*
2 * buffers.c
3 * LTTng userspace tracer buffering system
4 *
5 * Copyright (C) 2009 - Pierre-Marc Fournier (pierre-marc dot fournier at polymtl dot ca)
6 * Copyright (C) 2008 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <unistd.h>
24 #include <sys/mman.h>
25 #include <sys/ipc.h>
26 #include <sys/shm.h>
27 #include <fcntl.h>
28 #include <ust/kernelcompat.h>
29 #include <stdlib.h>
30 #include "buffers.h"
31 #include "channels.h"
32 #include "tracer.h"
33 #include "tracercore.h"
34 #include "usterr.h"
35
36 struct ltt_reserve_switch_offsets {
37 long begin, end, old;
38 long begin_switch, end_switch_current, end_switch_old;
39 size_t before_hdr_pad, size;
40 };
41
42
43 static DEFINE_MUTEX(ust_buffers_channels_mutex);
44 static LIST_HEAD(ust_buffers_channels);
45
46 static int get_n_cpus(void)
47 {
48 int result;
49 static int n_cpus = 0;
50
51 if(!n_cpus) {
52 /* On Linux, when some processors are offline
53 * _SC_NPROCESSORS_CONF counts the offline
54 * processors, whereas _SC_NPROCESSORS_ONLN
55 * does not. If we used _SC_NPROCESSORS_ONLN,
56 * getcpu() could return a value greater than
57 * this sysconf, in which case the arrays
58 * indexed by processor would overflow.
59 */
60 result = sysconf(_SC_NPROCESSORS_CONF);
61 if(result == -1) {
62 return -1;
63 }
64
65 n_cpus = result;
66 }
67
68 return n_cpus;
69 }
70
71 /* _ust_buffers_write()
72 *
73 * @buf: destination buffer
74 * @offset: offset in destination
75 * @src: source buffer
76 * @len: length of source
77 * @cpy: already copied
78 */
79
80 void _ust_buffers_write(struct ust_buffer *buf, size_t offset,
81 const void *src, size_t len, ssize_t cpy)
82 {
83 do {
84 len -= cpy;
85 src += cpy;
86 offset += cpy;
87
88 WARN_ON(offset >= buf->buf_size);
89
90 cpy = min_t(size_t, len, buf->buf_size - offset);
91 ust_buffers_do_copy(buf->buf_data + offset, src, cpy);
92 } while (unlikely(len != cpy));
93 }
94
95 static int ust_buffers_init_buffer(struct ust_trace *trace,
96 struct ust_channel *ltt_chan,
97 struct ust_buffer *buf,
98 unsigned int n_subbufs);
99
100 static int ust_buffers_alloc_buf(struct ust_buffer *buf, size_t *size)
101 {
102 void *ptr;
103 int result;
104
105 *size = PAGE_ALIGN(*size);
106
107 result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700);
108 if(result == -1 && errno == EINVAL) {
109 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
110 return -1;
111 }
112 else if(result == -1) {
113 PERROR("shmget");
114 return -1;
115 }
116
117 /* FIXME: should have matching call to shmdt */
118 ptr = shmat(buf->shmid, NULL, 0);
119 if(ptr == (void *) -1) {
120 perror("shmat");
121 goto destroy_shmem;
122 }
123
124 /* Already mark the shared memory for destruction. This will occur only
125 * when all users have detached.
126 */
127 result = shmctl(buf->shmid, IPC_RMID, NULL);
128 if(result == -1) {
129 perror("shmctl");
130 return -1;
131 }
132
133 buf->buf_data = ptr;
134 buf->buf_size = *size;
135
136 return 0;
137
138 destroy_shmem:
139 result = shmctl(buf->shmid, IPC_RMID, NULL);
140 if(result == -1) {
141 perror("shmctl");
142 }
143
144 return -1;
145 }
146
147 int ust_buffers_create_buf(struct ust_channel *channel, int cpu)
148 {
149 int result;
150 struct ust_buffer *buf = channel->buf[cpu];
151
152 buf->cpu = cpu;
153 result = ust_buffers_alloc_buf(buf, &channel->alloc_size);
154 if(result)
155 return -1;
156
157 buf->chan = channel;
158 kref_get(&channel->kref);
159 return 0;
160 }
161
162 static void ust_buffers_destroy_channel(struct kref *kref)
163 {
164 struct ust_channel *chan = container_of(kref, struct ust_channel, kref);
165 free(chan);
166 }
167
168 static void ust_buffers_destroy_buf(struct ust_buffer *buf)
169 {
170 struct ust_channel *chan = buf->chan;
171 int result;
172
173 result = munmap(buf->buf_data, buf->buf_size);
174 if(result == -1) {
175 PERROR("munmap");
176 }
177
178 //ust// chan->buf[buf->cpu] = NULL;
179 free(buf);
180 kref_put(&chan->kref, ust_buffers_destroy_channel);
181 }
182
183 /* called from kref_put */
184 static void ust_buffers_remove_buf(struct kref *kref)
185 {
186 struct ust_buffer *buf = container_of(kref, struct ust_buffer, kref);
187 ust_buffers_destroy_buf(buf);
188 }
189
190 int ust_buffers_open_buf(struct ust_channel *chan, int cpu)
191 {
192 int result;
193
194 result = ust_buffers_create_buf(chan, cpu);
195 if (result == -1)
196 return -1;
197
198 kref_init(&chan->buf[cpu]->kref);
199
200 result = ust_buffers_init_buffer(chan->trace, chan, chan->buf[cpu], chan->subbuf_cnt);
201 if(result == -1)
202 return -1;
203
204 return 0;
205
206 /* FIXME: decrementally destroy on error? */
207 }
208
209 /**
210 * ust_buffers_close_buf - close a channel buffer
211 * @buf: buffer
212 */
213 static void ust_buffers_close_buf(struct ust_buffer *buf)
214 {
215 kref_put(&buf->kref, ust_buffers_remove_buf);
216 }
217
218 int ust_buffers_channel_open(struct ust_channel *chan, size_t subbuf_size, size_t subbuf_cnt)
219 {
220 int i;
221 int result;
222
223 if(subbuf_size == 0 || subbuf_cnt == 0)
224 return -1;
225
226 /* Check that the subbuffer size is larger than a page. */
227 WARN_ON_ONCE(subbuf_size < PAGE_SIZE);
228
229 /*
230 * Make sure the number of subbuffers and subbuffer size are power of 2.
231 */
232 WARN_ON_ONCE(hweight32(subbuf_size) != 1);
233 WARN_ON(hweight32(subbuf_cnt) != 1);
234
235 chan->version = UST_CHANNEL_VERSION;
236 chan->subbuf_cnt = subbuf_cnt;
237 chan->subbuf_size = subbuf_size;
238 chan->subbuf_size_order = get_count_order(subbuf_size);
239 chan->alloc_size = subbuf_size * subbuf_cnt;
240
241 kref_init(&chan->kref);
242
243 mutex_lock(&ust_buffers_channels_mutex);
244 for(i=0; i<chan->n_cpus; i++) {
245 result = ust_buffers_open_buf(chan, i);
246 if (result == -1)
247 goto error;
248 }
249 list_add(&chan->list, &ust_buffers_channels);
250 mutex_unlock(&ust_buffers_channels_mutex);
251
252 return 0;
253
254 /* Jump directly inside the loop to close the buffers that were already
255 * opened. */
256 for(; i>=0; i--) {
257 ust_buffers_close_buf(chan->buf[i]);
258 error:
259 do {} while(0);
260 }
261
262 kref_put(&chan->kref, ust_buffers_destroy_channel);
263 mutex_unlock(&ust_buffers_channels_mutex);
264 return -1;
265 }
266
267 void ust_buffers_channel_close(struct ust_channel *chan)
268 {
269 int i;
270 if(!chan)
271 return;
272
273 mutex_lock(&ust_buffers_channels_mutex);
274 for(i=0; i<chan->n_cpus; i++) {
275 /* FIXME: if we make it here, then all buffers were necessarily allocated. Moreover, we don't
276 * initialize to NULL so we cannot use this check. Should we? */
277 //ust// if (chan->buf[i])
278 ust_buffers_close_buf(chan->buf[i]);
279 }
280
281 list_del(&chan->list);
282 kref_put(&chan->kref, ust_buffers_destroy_channel);
283 mutex_unlock(&ust_buffers_channels_mutex);
284 }
285
286 /*
287 * -------
288 */
289
290 static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan, int cpu);
291
292 static void ltt_force_switch(struct ust_buffer *buf,
293 enum force_switch_mode mode);
294
295 /*
296 * Trace callbacks
297 */
298 static void ltt_buffer_begin(struct ust_buffer *buf,
299 u64 tsc, unsigned int subbuf_idx)
300 {
301 struct ust_channel *channel = buf->chan;
302 struct ltt_subbuffer_header *header =
303 (struct ltt_subbuffer_header *)
304 ust_buffers_offset_address(buf,
305 subbuf_idx * buf->chan->subbuf_size);
306
307 header->cycle_count_begin = tsc;
308 header->data_size = 0xFFFFFFFF; /* for recognizing crashed buffers */
309 header->sb_size = 0xFFFFFFFF; /* for recognizing crashed buffers */
310 /* FIXME: add memory barrier? */
311 ltt_write_trace_header(channel->trace, header);
312 }
313
314 /*
315 * offset is assumed to never be 0 here : never deliver a completely empty
316 * subbuffer. The lost size is between 0 and subbuf_size-1.
317 */
318 static notrace void ltt_buffer_end(struct ust_buffer *buf,
319 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
320 {
321 struct ltt_subbuffer_header *header =
322 (struct ltt_subbuffer_header *)
323 ust_buffers_offset_address(buf,
324 subbuf_idx * buf->chan->subbuf_size);
325 u32 data_size = SUBBUF_OFFSET(offset - 1, buf->chan) + 1;
326
327 header->data_size = data_size;
328 header->sb_size = PAGE_ALIGN(data_size);
329 header->cycle_count_end = tsc;
330 header->events_lost = uatomic_read(&buf->events_lost);
331 header->subbuf_corrupt = uatomic_read(&buf->corrupted_subbuffers);
332 if(unlikely(header->events_lost > 0)) {
333 DBG("Some events (%d) were lost in %s_%d", header->events_lost, buf->chan->channel_name, buf->cpu);
334 }
335 }
336
337 /*
338 * This function should not be called from NMI interrupt context
339 */
340 static notrace void ltt_buf_unfull(struct ust_buffer *buf,
341 unsigned int subbuf_idx,
342 long offset)
343 {
344 }
345
346 /*
347 * Promote compiler barrier to a smp_mb().
348 * For the specific LTTng case, this IPI call should be removed if the
349 * architecture does not reorder writes. This should eventually be provided by
350 * a separate architecture-specific infrastructure.
351 */
352 //ust// static void remote_mb(void *info)
353 //ust// {
354 //ust// smp_mb();
355 //ust// }
356
357 int ust_buffers_get_subbuf(struct ust_buffer *buf, long *consumed)
358 {
359 struct ust_channel *channel = buf->chan;
360 long consumed_old, consumed_idx, commit_count, write_offset;
361 //ust// int retval;
362
363 consumed_old = uatomic_read(&buf->consumed);
364 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
365 commit_count = uatomic_read(&buf->commit_count[consumed_idx].cc_sb);
366 /*
367 * Make sure we read the commit count before reading the buffer
368 * data and the write offset. Correct consumed offset ordering
369 * wrt commit count is insured by the use of cmpxchg to update
370 * the consumed offset.
371 * smp_call_function_single can fail if the remote CPU is offline,
372 * this is OK because then there is no wmb to execute there.
373 * If our thread is executing on the same CPU as the on the buffers
374 * belongs to, we don't have to synchronize it at all. If we are
375 * migrated, the scheduler will take care of the memory barriers.
376 * Normally, smp_call_function_single() should ensure program order when
377 * executing the remote function, which implies that it surrounds the
378 * function execution with :
379 * smp_mb()
380 * send IPI
381 * csd_lock_wait
382 * recv IPI
383 * smp_mb()
384 * exec. function
385 * smp_mb()
386 * csd unlock
387 * smp_mb()
388 *
389 * However, smp_call_function_single() does not seem to clearly execute
390 * such barriers. It depends on spinlock semantic to provide the barrier
391 * before executing the IPI and, when busy-looping, csd_lock_wait only
392 * executes smp_mb() when it has to wait for the other CPU.
393 *
394 * I don't trust this code. Therefore, let's add the smp_mb() sequence
395 * required ourself, even if duplicated. It has no performance impact
396 * anyway.
397 *
398 * smp_mb() is needed because smp_rmb() and smp_wmb() only order read vs
399 * read and write vs write. They do not ensure core synchronization. We
400 * really have to ensure total order between the 3 barriers running on
401 * the 2 CPUs.
402 */
403 //ust// #ifdef LTT_NO_IPI_BARRIER
404 /*
405 * Local rmb to match the remote wmb to read the commit count before the
406 * buffer data and the write offset.
407 */
408 smp_rmb();
409 //ust// #else
410 //ust// if (raw_smp_processor_id() != buf->cpu) {
411 //ust// smp_mb(); /* Total order with IPI handler smp_mb() */
412 //ust// smp_call_function_single(buf->cpu, remote_mb, NULL, 1);
413 //ust// smp_mb(); /* Total order with IPI handler smp_mb() */
414 //ust// }
415 //ust// #endif
416
417 write_offset = uatomic_read(&buf->offset);
418 /*
419 * Check that the subbuffer we are trying to consume has been
420 * already fully committed.
421 */
422 if (((commit_count - buf->chan->subbuf_size)
423 & channel->commit_count_mask)
424 - (BUFFER_TRUNC(consumed_old, buf->chan)
425 >> channel->n_subbufs_order)
426 != 0) {
427 return -EAGAIN;
428 }
429 /*
430 * Check that we are not about to read the same subbuffer in
431 * which the writer head is.
432 */
433 if ((SUBBUF_TRUNC(write_offset, buf->chan)
434 - SUBBUF_TRUNC(consumed_old, buf->chan))
435 == 0) {
436 return -EAGAIN;
437 }
438
439 /* FIXME: is this ok to disable the reading feature? */
440 //ust// retval = update_read_sb_index(buf, consumed_idx);
441 //ust// if (retval)
442 //ust// return retval;
443
444 *consumed = consumed_old;
445
446 return 0;
447 }
448
449 int ust_buffers_put_subbuf(struct ust_buffer *buf, unsigned long uconsumed_old)
450 {
451 long consumed_new, consumed_old;
452
453 consumed_old = uatomic_read(&buf->consumed);
454 consumed_old = consumed_old & (~0xFFFFFFFFL);
455 consumed_old = consumed_old | uconsumed_old;
456 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
457
458 //ust// spin_lock(&ltt_buf->full_lock);
459 if (uatomic_cmpxchg(&buf->consumed, consumed_old,
460 consumed_new)
461 != consumed_old) {
462 /* We have been pushed by the writer : the last
463 * buffer read _is_ corrupted! It can also
464 * happen if this is a buffer we never got. */
465 //ust// spin_unlock(&ltt_buf->full_lock);
466 return -EIO;
467 } else {
468 /* tell the client that buffer is now unfull */
469 int index;
470 long data;
471 index = SUBBUF_INDEX(consumed_old, buf->chan);
472 data = BUFFER_OFFSET(consumed_old, buf->chan);
473 ltt_buf_unfull(buf, index, data);
474 //ust// spin_unlock(&ltt_buf->full_lock);
475 }
476 return 0;
477 }
478
479 static void ltt_relay_print_subbuffer_errors(
480 struct ust_channel *channel,
481 long cons_off, int cpu)
482 {
483 struct ust_buffer *ltt_buf = channel->buf[cpu];
484 long cons_idx, commit_count, commit_count_sb, write_offset;
485
486 cons_idx = SUBBUF_INDEX(cons_off, channel);
487 commit_count = uatomic_read(&ltt_buf->commit_count[cons_idx].cc);
488 commit_count_sb = uatomic_read(&ltt_buf->commit_count[cons_idx].cc_sb);
489
490 /*
491 * No need to order commit_count and write_offset reads because we
492 * execute after trace is stopped when there are no readers left.
493 */
494 write_offset = uatomic_read(&ltt_buf->offset);
495 WARN( "LTT : unread channel %s offset is %ld "
496 "and cons_off : %ld (cpu %d)\n",
497 channel->channel_name, write_offset, cons_off, cpu);
498 /* Check each sub-buffer for non filled commit count */
499 if (((commit_count - channel->subbuf_size) & channel->commit_count_mask)
500 - (BUFFER_TRUNC(cons_off, channel) >> channel->n_subbufs_order) != 0) {
501 ERR("LTT : %s : subbuffer %lu has non filled "
502 "commit count [cc, cc_sb] [%lu,%lu].\n",
503 channel->channel_name, cons_idx, commit_count, commit_count_sb);
504 }
505 ERR("LTT : %s : commit count : %lu, subbuf size %zd\n",
506 channel->channel_name, commit_count,
507 channel->subbuf_size);
508 }
509
510 static void ltt_relay_print_errors(struct ust_trace *trace,
511 struct ust_channel *channel, int cpu)
512 {
513 struct ust_buffer *ltt_buf = channel->buf[cpu];
514 long cons_off;
515
516 /*
517 * Can be called in the error path of allocation when
518 * trans_channel_data is not yet set.
519 */
520 if (!channel)
521 return;
522
523 //ust// for (cons_off = 0; cons_off < rchan->alloc_size;
524 //ust// cons_off = SUBBUF_ALIGN(cons_off, rchan))
525 //ust// ust_buffers_print_written(ltt_chan, cons_off, cpu);
526 for (cons_off = uatomic_read(&ltt_buf->consumed);
527 (SUBBUF_TRUNC(uatomic_read(&ltt_buf->offset),
528 channel)
529 - cons_off) > 0;
530 cons_off = SUBBUF_ALIGN(cons_off, channel))
531 ltt_relay_print_subbuffer_errors(channel, cons_off, cpu);
532 }
533
534 static void ltt_relay_print_buffer_errors(struct ust_channel *channel, int cpu)
535 {
536 struct ust_trace *trace = channel->trace;
537 struct ust_buffer *ltt_buf = channel->buf[cpu];
538
539 if (uatomic_read(&ltt_buf->events_lost))
540 ERR("channel %s: %ld events lost (cpu %d)",
541 channel->channel_name,
542 uatomic_read(&ltt_buf->events_lost), cpu);
543 if (uatomic_read(&ltt_buf->corrupted_subbuffers))
544 ERR("channel %s : %ld corrupted subbuffers (cpu %d)",
545 channel->channel_name,
546 uatomic_read(&ltt_buf->corrupted_subbuffers), cpu);
547
548 ltt_relay_print_errors(trace, channel, cpu);
549 }
550
551 static void ltt_relay_release_channel(struct kref *kref)
552 {
553 struct ust_channel *ltt_chan = container_of(kref,
554 struct ust_channel, kref);
555 free(ltt_chan->buf);
556 }
557
558 /*
559 * Create ltt buffer.
560 */
561 //ust// static int ltt_relay_create_buffer(struct ust_trace *trace,
562 //ust// struct ltt_channel_struct *ltt_chan, struct rchan_buf *buf,
563 //ust// unsigned int cpu, unsigned int n_subbufs)
564 //ust// {
565 //ust// struct ltt_channel_buf_struct *ltt_buf =
566 //ust// percpu_ptr(ltt_chan->buf, cpu);
567 //ust// unsigned int j;
568 //ust//
569 //ust// ltt_buf->commit_count =
570 //ust// kzalloc_node(sizeof(ltt_buf->commit_count) * n_subbufs,
571 //ust// GFP_KERNEL, cpu_to_node(cpu));
572 //ust// if (!ltt_buf->commit_count)
573 //ust// return -ENOMEM;
574 //ust// kref_get(&trace->kref);
575 //ust// kref_get(&trace->ltt_transport_kref);
576 //ust// kref_get(&ltt_chan->kref);
577 //ust// uatomic_set(&ltt_buf->offset, ltt_subbuffer_header_size());
578 //ust// uatomic_set(&ltt_buf->consumed, 0);
579 //ust// uatomic_set(&ltt_buf->active_readers, 0);
580 //ust// for (j = 0; j < n_subbufs; j++)
581 //ust// uatomic_set(&ltt_buf->commit_count[j], 0);
582 //ust// init_waitqueue_head(&ltt_buf->write_wait);
583 //ust// uatomic_set(&ltt_buf->wakeup_readers, 0);
584 //ust// spin_lock_init(&ltt_buf->full_lock);
585 //ust//
586 //ust// ltt_buffer_begin_callback(buf, trace->start_tsc, 0);
587 //ust// /* atomic_add made on local variable on data that belongs to
588 //ust// * various CPUs : ok because tracing not started (for this cpu). */
589 //ust// uatomic_add(&ltt_buf->commit_count[0], ltt_subbuffer_header_size());
590 //ust//
591 //ust// uatomic_set(&ltt_buf->events_lost, 0);
592 //ust// uatomic_set(&ltt_buf->corrupted_subbuffers, 0);
593 //ust//
594 //ust// return 0;
595 //ust// }
596
597 static int ust_buffers_init_buffer(struct ust_trace *trace,
598 struct ust_channel *ltt_chan, struct ust_buffer *buf,
599 unsigned int n_subbufs)
600 {
601 unsigned int j;
602 int fds[2];
603 int result;
604
605 buf->commit_count =
606 zmalloc(sizeof(*buf->commit_count) * n_subbufs);
607 if (!buf->commit_count)
608 return -ENOMEM;
609 kref_get(&trace->kref);
610 kref_get(&trace->ltt_transport_kref);
611 kref_get(&ltt_chan->kref);
612 uatomic_set(&buf->offset, ltt_subbuffer_header_size());
613 uatomic_set(&buf->consumed, 0);
614 uatomic_set(&buf->active_readers, 0);
615 for (j = 0; j < n_subbufs; j++) {
616 uatomic_set(&buf->commit_count[j].cc, 0);
617 uatomic_set(&buf->commit_count[j].cc_sb, 0);
618 }
619 //ust// init_waitqueue_head(&buf->write_wait);
620 //ust// uatomic_set(&buf->wakeup_readers, 0);
621 //ust// spin_lock_init(&buf->full_lock);
622
623 ltt_buffer_begin(buf, trace->start_tsc, 0);
624
625 uatomic_add(&buf->commit_count[0].cc, ltt_subbuffer_header_size());
626
627 uatomic_set(&buf->events_lost, 0);
628 uatomic_set(&buf->corrupted_subbuffers, 0);
629
630 result = pipe(fds);
631 if(result == -1) {
632 PERROR("pipe");
633 return -1;
634 }
635 buf->data_ready_fd_read = fds[0];
636 buf->data_ready_fd_write = fds[1];
637
638 /* FIXME: do we actually need this? */
639 result = fcntl(fds[0], F_SETFL, O_NONBLOCK);
640 if(result == -1) {
641 PERROR("fcntl");
642 }
643
644 //ust// buf->commit_seq = malloc(sizeof(buf->commit_seq) * n_subbufs);
645 //ust// if(!ltt_buf->commit_seq) {
646 //ust// return -1;
647 //ust// }
648 memset(buf->commit_seq, 0, sizeof(buf->commit_seq[0]) * n_subbufs);
649
650 /* FIXME: decrementally destroy on error */
651
652 return 0;
653 }
654
655 /* FIXME: use this function */
656 static void ust_buffers_destroy_buffer(struct ust_channel *ltt_chan, int cpu)
657 {
658 struct ust_trace *trace = ltt_chan->trace;
659 struct ust_buffer *ltt_buf = ltt_chan->buf[cpu];
660
661 kref_put(&ltt_chan->trace->ltt_transport_kref,
662 ltt_release_transport);
663 ltt_relay_print_buffer_errors(ltt_chan, cpu);
664 //ust// free(ltt_buf->commit_seq);
665 free(ltt_buf->commit_count);
666 ltt_buf->commit_count = NULL;
667 kref_put(&ltt_chan->kref, ltt_relay_release_channel);
668 kref_put(&trace->kref, ltt_release_trace);
669 //ust// wake_up_interruptible(&trace->kref_wq);
670 }
671
672 static int ust_buffers_alloc_channel_buf_structs(struct ust_channel *chan)
673 {
674 void *ptr;
675 int result;
676 size_t size;
677 int i;
678
679 size = PAGE_ALIGN(1);
680
681 for(i=0; i<chan->n_cpus; i++) {
682
683 result = chan->buf_struct_shmids[i] = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700);
684 if(result == -1) {
685 PERROR("shmget");
686 goto destroy_previous;
687 }
688
689 /* FIXME: should have matching call to shmdt */
690 ptr = shmat(chan->buf_struct_shmids[i], NULL, 0);
691 if(ptr == (void *) -1) {
692 perror("shmat");
693 goto destroy_shm;
694 }
695
696 /* Already mark the shared memory for destruction. This will occur only
697 * when all users have detached.
698 */
699 result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL);
700 if(result == -1) {
701 perror("shmctl");
702 goto destroy_previous;
703 }
704
705 chan->buf[i] = ptr;
706 }
707
708 return 0;
709
710 /* Jumping inside this loop occurs from within the other loop above with i as
711 * counter, so it unallocates the structures for the cpu = current_i down to
712 * zero. */
713 for(; i>=0; i--) {
714 destroy_shm:
715 result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL);
716 if(result == -1) {
717 perror("shmctl");
718 }
719
720 destroy_previous:
721 continue;
722 }
723
724 return -1;
725 }
726
727 /*
728 * Create channel.
729 */
730 static int ust_buffers_create_channel(const char *trace_name, struct ust_trace *trace,
731 const char *channel_name, struct ust_channel *ltt_chan,
732 unsigned int subbuf_size, unsigned int n_subbufs, int overwrite)
733 {
734 int result;
735
736 kref_init(&ltt_chan->kref);
737
738 ltt_chan->trace = trace;
739 ltt_chan->overwrite = overwrite;
740 ltt_chan->n_subbufs_order = get_count_order(n_subbufs);
741 ltt_chan->commit_count_mask = (~0UL >> ltt_chan->n_subbufs_order);
742 ltt_chan->n_cpus = get_n_cpus();
743 //ust// ltt_chan->buf = percpu_alloc_mask(sizeof(struct ltt_channel_buf_struct), GFP_KERNEL, cpu_possible_map);
744 ltt_chan->buf = (void *) malloc(ltt_chan->n_cpus * sizeof(void *));
745 if(ltt_chan->buf == NULL) {
746 goto error;
747 }
748 ltt_chan->buf_struct_shmids = (int *) malloc(ltt_chan->n_cpus * sizeof(int));
749 if(ltt_chan->buf_struct_shmids == NULL)
750 goto free_buf;
751
752 result = ust_buffers_alloc_channel_buf_structs(ltt_chan);
753 if(result != 0) {
754 goto free_buf_struct_shmids;
755 }
756
757 result = ust_buffers_channel_open(ltt_chan, subbuf_size, n_subbufs);
758 if (result != 0) {
759 ERR("Cannot open channel for trace %s", trace_name);
760 goto unalloc_buf_structs;
761 }
762
763 return 0;
764
765 unalloc_buf_structs:
766 /* FIXME: put a call here to unalloc the buf structs! */
767
768 free_buf_struct_shmids:
769 free(ltt_chan->buf_struct_shmids);
770
771 free_buf:
772 free(ltt_chan->buf);
773
774 error:
775 return -1;
776 }
777
778 /*
779 * LTTng channel flush function.
780 *
781 * Must be called when no tracing is active in the channel, because of
782 * accesses across CPUs.
783 */
784 static notrace void ltt_relay_buffer_flush(struct ust_buffer *buf)
785 {
786 int result;
787
788 //ust// buf->finalized = 1;
789 ltt_force_switch(buf, FORCE_FLUSH);
790
791 result = write(buf->data_ready_fd_write, "1", 1);
792 if(result == -1) {
793 PERROR("write (in ltt_relay_buffer_flush)");
794 ERR("this should never happen!");
795 }
796 }
797
798 static void ltt_relay_async_wakeup_chan(struct ust_channel *ltt_channel)
799 {
800 //ust// unsigned int i;
801 //ust// struct rchan *rchan = ltt_channel->trans_channel_data;
802 //ust//
803 //ust// for_each_possible_cpu(i) {
804 //ust// struct ltt_channel_buf_struct *ltt_buf =
805 //ust// percpu_ptr(ltt_channel->buf, i);
806 //ust//
807 //ust// if (uatomic_read(&ltt_buf->wakeup_readers) == 1) {
808 //ust// uatomic_set(&ltt_buf->wakeup_readers, 0);
809 //ust// wake_up_interruptible(&rchan->buf[i]->read_wait);
810 //ust// }
811 //ust// }
812 }
813
814 static void ltt_relay_finish_buffer(struct ust_channel *channel, unsigned int cpu)
815 {
816 // int result;
817
818 if (channel->buf[cpu]) {
819 struct ust_buffer *buf = channel->buf[cpu];
820 ltt_relay_buffer_flush(buf);
821 //ust// ltt_relay_wake_writers(ltt_buf);
822 /* closing the pipe tells the consumer the buffer is finished */
823
824 //result = write(ltt_buf->data_ready_fd_write, "D", 1);
825 //if(result == -1) {
826 // PERROR("write (in ltt_relay_finish_buffer)");
827 // ERR("this should never happen!");
828 //}
829 close(buf->data_ready_fd_write);
830 }
831 }
832
833
834 static void ltt_relay_finish_channel(struct ust_channel *channel)
835 {
836 unsigned int i;
837
838 for(i=0; i<channel->n_cpus; i++) {
839 ltt_relay_finish_buffer(channel, i);
840 }
841 }
842
843 static void ltt_relay_remove_channel(struct ust_channel *channel)
844 {
845 ust_buffers_channel_close(channel);
846 kref_put(&channel->kref, ltt_relay_release_channel);
847 }
848
849 /*
850 * ltt_reserve_switch_old_subbuf: switch old subbuffer
851 *
852 * Concurrency safe because we are the last and only thread to alter this
853 * sub-buffer. As long as it is not delivered and read, no other thread can
854 * alter the offset, alter the reserve_count or call the
855 * client_buffer_end_callback on this sub-buffer.
856 *
857 * The only remaining threads could be the ones with pending commits. They will
858 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
859 * We detect corrupted subbuffers with commit and reserve counts. We keep a
860 * corrupted sub-buffers count and push the readers across these sub-buffers.
861 *
862 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
863 * switches in, finding out it's corrupted. The result will be than the old
864 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
865 * will be declared corrupted too because of the commit count adjustment.
866 *
867 * Note : offset_old should never be 0 here.
868 */
869 static void ltt_reserve_switch_old_subbuf(
870 struct ust_channel *chan, struct ust_buffer *buf,
871 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
872 {
873 long oldidx = SUBBUF_INDEX(offsets->old - 1, chan);
874 long commit_count, padding_size;
875
876 padding_size = chan->subbuf_size
877 - (SUBBUF_OFFSET(offsets->old - 1, chan) + 1);
878 ltt_buffer_end(buf, *tsc, offsets->old, oldidx);
879
880 /*
881 * Must write slot data before incrementing commit count.
882 * This compiler barrier is upgraded into a smp_wmb() by the IPI
883 * sent by get_subbuf() when it does its smp_rmb().
884 */
885 barrier();
886 uatomic_add(&buf->commit_count[oldidx].cc, padding_size);
887 commit_count = uatomic_read(&buf->commit_count[oldidx].cc);
888 ltt_check_deliver(chan, buf, offsets->old - 1, commit_count, oldidx);
889 ltt_write_commit_counter(chan, buf, oldidx,
890 offsets->old, commit_count, padding_size);
891 }
892
893 /*
894 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
895 *
896 * This code can be executed unordered : writers may already have written to the
897 * sub-buffer before this code gets executed, caution. The commit makes sure
898 * that this code is executed before the deliver of this sub-buffer.
899 */
900 static void ltt_reserve_switch_new_subbuf(
901 struct ust_channel *chan, struct ust_buffer *buf,
902 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
903 {
904 long beginidx = SUBBUF_INDEX(offsets->begin, chan);
905 long commit_count;
906
907 ltt_buffer_begin(buf, *tsc, beginidx);
908
909 /*
910 * Must write slot data before incrementing commit count.
911 * This compiler barrier is upgraded into a smp_wmb() by the IPI
912 * sent by get_subbuf() when it does its smp_rmb().
913 */
914 barrier();
915 uatomic_add(&buf->commit_count[beginidx].cc, ltt_subbuffer_header_size());
916 commit_count = uatomic_read(&buf->commit_count[beginidx].cc);
917 /* Check if the written buffer has to be delivered */
918 ltt_check_deliver(chan, buf, offsets->begin, commit_count, beginidx);
919 ltt_write_commit_counter(chan, buf, beginidx,
920 offsets->begin, commit_count, ltt_subbuffer_header_size());
921 }
922
923 /*
924 * ltt_reserve_end_switch_current: finish switching current subbuffer
925 *
926 * Concurrency safe because we are the last and only thread to alter this
927 * sub-buffer. As long as it is not delivered and read, no other thread can
928 * alter the offset, alter the reserve_count or call the
929 * client_buffer_end_callback on this sub-buffer.
930 *
931 * The only remaining threads could be the ones with pending commits. They will
932 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
933 * We detect corrupted subbuffers with commit and reserve counts. We keep a
934 * corrupted sub-buffers count and push the readers across these sub-buffers.
935 *
936 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
937 * switches in, finding out it's corrupted. The result will be than the old
938 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
939 * will be declared corrupted too because of the commit count adjustment.
940 */
941 static void ltt_reserve_end_switch_current(
942 struct ust_channel *chan,
943 struct ust_buffer *buf,
944 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
945 {
946 long endidx = SUBBUF_INDEX(offsets->end - 1, chan);
947 long commit_count, padding_size;
948
949 padding_size = chan->subbuf_size
950 - (SUBBUF_OFFSET(offsets->end - 1, chan) + 1);
951
952 ltt_buffer_end(buf, *tsc, offsets->end, endidx);
953
954 /*
955 * Must write slot data before incrementing commit count.
956 * This compiler barrier is upgraded into a smp_wmb() by the IPI
957 * sent by get_subbuf() when it does its smp_rmb().
958 */
959 barrier();
960 uatomic_add(&buf->commit_count[endidx].cc, padding_size);
961 commit_count = uatomic_read(&buf->commit_count[endidx].cc);
962 ltt_check_deliver(chan, buf,
963 offsets->end - 1, commit_count, endidx);
964 ltt_write_commit_counter(chan, buf, endidx,
965 offsets->end, commit_count, padding_size);
966 }
967
968 /*
969 * Returns :
970 * 0 if ok
971 * !0 if execution must be aborted.
972 */
973 static int ltt_relay_try_switch_slow(
974 enum force_switch_mode mode,
975 struct ust_channel *chan,
976 struct ust_buffer *buf,
977 struct ltt_reserve_switch_offsets *offsets,
978 u64 *tsc)
979 {
980 long subbuf_index;
981 long reserve_commit_diff;
982
983 offsets->begin = uatomic_read(&buf->offset);
984 offsets->old = offsets->begin;
985 offsets->begin_switch = 0;
986 offsets->end_switch_old = 0;
987
988 *tsc = trace_clock_read64();
989
990 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
991 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
992 offsets->end_switch_old = 1;
993 } else {
994 /* we do not have to switch : buffer is empty */
995 return -1;
996 }
997 if (mode == FORCE_ACTIVE)
998 offsets->begin += ltt_subbuffer_header_size();
999 /*
1000 * Always begin_switch in FORCE_ACTIVE mode.
1001 * Test new buffer integrity
1002 */
1003 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
1004 reserve_commit_diff =
1005 (BUFFER_TRUNC(offsets->begin, buf->chan)
1006 >> chan->n_subbufs_order)
1007 - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb)
1008 & chan->commit_count_mask);
1009 if (reserve_commit_diff == 0) {
1010 /* Next buffer not corrupted. */
1011 if (mode == FORCE_ACTIVE
1012 && !chan->overwrite
1013 && offsets->begin - uatomic_read(&buf->consumed)
1014 >= chan->alloc_size) {
1015 /*
1016 * We do not overwrite non consumed buffers and we are
1017 * full : ignore switch while tracing is active.
1018 */
1019 return -1;
1020 }
1021 } else {
1022 /*
1023 * Next subbuffer corrupted. Force pushing reader even in normal
1024 * mode
1025 */
1026 }
1027 offsets->end = offsets->begin;
1028 return 0;
1029 }
1030
1031 /*
1032 * Force a sub-buffer switch for a per-cpu buffer. This operation is
1033 * completely reentrant : can be called while tracing is active with
1034 * absolutely no lock held.
1035 */
1036 void ltt_force_switch_lockless_slow(struct ust_buffer *buf,
1037 enum force_switch_mode mode)
1038 {
1039 struct ust_channel *chan = buf->chan;
1040 struct ltt_reserve_switch_offsets offsets;
1041 u64 tsc;
1042
1043 offsets.size = 0;
1044
1045 DBG("Switching (forced) %s_%d", chan->channel_name, buf->cpu);
1046 /*
1047 * Perform retryable operations.
1048 */
1049 do {
1050 if (ltt_relay_try_switch_slow(mode, chan, buf,
1051 &offsets, &tsc))
1052 return;
1053 } while (uatomic_cmpxchg(&buf->offset, offsets.old,
1054 offsets.end) != offsets.old);
1055
1056 /*
1057 * Atomically update last_tsc. This update races against concurrent
1058 * atomic updates, but the race will always cause supplementary full TSC
1059 * events, never the opposite (missing a full TSC event when it would be
1060 * needed).
1061 */
1062 save_last_tsc(buf, tsc);
1063
1064 /*
1065 * Push the reader if necessary
1066 */
1067 if (mode == FORCE_ACTIVE) {
1068 ltt_reserve_push_reader(chan, buf, offsets.end - 1);
1069 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan));
1070 }
1071
1072 /*
1073 * Switch old subbuffer if needed.
1074 */
1075 if (offsets.end_switch_old) {
1076 //ust// ltt_clear_noref_flag(rchan, buf, SUBBUF_INDEX(offsets.old - 1, rchan));
1077 ltt_reserve_switch_old_subbuf(chan, buf, &offsets, &tsc);
1078 }
1079
1080 /*
1081 * Populate new subbuffer.
1082 */
1083 if (mode == FORCE_ACTIVE)
1084 ltt_reserve_switch_new_subbuf(chan, buf, &offsets, &tsc);
1085 }
1086
1087 /*
1088 * Returns :
1089 * 0 if ok
1090 * !0 if execution must be aborted.
1091 */
1092 static int ltt_relay_try_reserve_slow(struct ust_channel *chan, struct ust_buffer *buf,
1093 struct ltt_reserve_switch_offsets *offsets, size_t data_size,
1094 u64 *tsc, unsigned int *rflags, int largest_align)
1095 {
1096 long reserve_commit_diff;
1097
1098 offsets->begin = uatomic_read(&buf->offset);
1099 offsets->old = offsets->begin;
1100 offsets->begin_switch = 0;
1101 offsets->end_switch_current = 0;
1102 offsets->end_switch_old = 0;
1103
1104 *tsc = trace_clock_read64();
1105 if (last_tsc_overflow(buf, *tsc))
1106 *rflags = LTT_RFLAG_ID_SIZE_TSC;
1107
1108 if (unlikely(SUBBUF_OFFSET(offsets->begin, buf->chan) == 0)) {
1109 offsets->begin_switch = 1; /* For offsets->begin */
1110 } else {
1111 offsets->size = ust_get_header_size(chan,
1112 offsets->begin, data_size,
1113 &offsets->before_hdr_pad, *rflags);
1114 offsets->size += ltt_align(offsets->begin + offsets->size,
1115 largest_align)
1116 + data_size;
1117 if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) +
1118 offsets->size) > buf->chan->subbuf_size)) {
1119 offsets->end_switch_old = 1; /* For offsets->old */
1120 offsets->begin_switch = 1; /* For offsets->begin */
1121 }
1122 }
1123 if (unlikely(offsets->begin_switch)) {
1124 long subbuf_index;
1125
1126 /*
1127 * We are typically not filling the previous buffer completely.
1128 */
1129 if (likely(offsets->end_switch_old))
1130 offsets->begin = SUBBUF_ALIGN(offsets->begin,
1131 buf->chan);
1132 offsets->begin = offsets->begin + ltt_subbuffer_header_size();
1133 /* Test new buffer integrity */
1134 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
1135 reserve_commit_diff =
1136 (BUFFER_TRUNC(offsets->begin, buf->chan)
1137 >> chan->n_subbufs_order)
1138 - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb)
1139 & chan->commit_count_mask);
1140 if (likely(reserve_commit_diff == 0)) {
1141 /* Next buffer not corrupted. */
1142 if (unlikely(!chan->overwrite &&
1143 (SUBBUF_TRUNC(offsets->begin, buf->chan)
1144 - SUBBUF_TRUNC(uatomic_read(
1145 &buf->consumed),
1146 buf->chan))
1147 >= chan->alloc_size)) {
1148 /*
1149 * We do not overwrite non consumed buffers
1150 * and we are full : event is lost.
1151 */
1152 uatomic_inc(&buf->events_lost);
1153 return -1;
1154 } else {
1155 /*
1156 * next buffer not corrupted, we are either in
1157 * overwrite mode or the buffer is not full.
1158 * It's safe to write in this new subbuffer.
1159 */
1160 }
1161 } else {
1162 /*
1163 * Next subbuffer corrupted. Drop event in normal and
1164 * overwrite mode. Caused by either a writer OOPS or
1165 * too many nested writes over a reserve/commit pair.
1166 */
1167 uatomic_inc(&buf->events_lost);
1168 return -1;
1169 }
1170 offsets->size = ust_get_header_size(chan,
1171 offsets->begin, data_size,
1172 &offsets->before_hdr_pad, *rflags);
1173 offsets->size += ltt_align(offsets->begin + offsets->size,
1174 largest_align)
1175 + data_size;
1176 if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan)
1177 + offsets->size) > buf->chan->subbuf_size)) {
1178 /*
1179 * Event too big for subbuffers, report error, don't
1180 * complete the sub-buffer switch.
1181 */
1182 uatomic_inc(&buf->events_lost);
1183 return -1;
1184 } else {
1185 /*
1186 * We just made a successful buffer switch and the event
1187 * fits in the new subbuffer. Let's write.
1188 */
1189 }
1190 } else {
1191 /*
1192 * Event fits in the current buffer and we are not on a switch
1193 * boundary. It's safe to write.
1194 */
1195 }
1196 offsets->end = offsets->begin + offsets->size;
1197
1198 if (unlikely((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0)) {
1199 /*
1200 * The offset_end will fall at the very beginning of the next
1201 * subbuffer.
1202 */
1203 offsets->end_switch_current = 1; /* For offsets->begin */
1204 }
1205 return 0;
1206 }
1207
1208 /**
1209 * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer.
1210 * @trace: the trace structure to log to.
1211 * @ltt_channel: channel structure
1212 * @transport_data: data structure specific to ltt relay
1213 * @data_size: size of the variable length data to log.
1214 * @slot_size: pointer to total size of the slot (out)
1215 * @buf_offset : pointer to reserved buffer offset (out)
1216 * @tsc: pointer to the tsc at the slot reservation (out)
1217 * @cpu: cpuid
1218 *
1219 * Return : -ENOSPC if not enough space, else returns 0.
1220 * It will take care of sub-buffer switching.
1221 */
1222 int ltt_reserve_slot_lockless_slow(struct ust_trace *trace,
1223 struct ust_channel *chan, void **transport_data,
1224 size_t data_size, size_t *slot_size, long *buf_offset, u64 *tsc,
1225 unsigned int *rflags, int largest_align, int cpu)
1226 {
1227 struct ust_buffer *buf = chan->buf[cpu];
1228 struct ltt_reserve_switch_offsets offsets;
1229
1230 offsets.size = 0;
1231
1232 do {
1233 if (unlikely(ltt_relay_try_reserve_slow(chan, buf, &offsets,
1234 data_size, tsc, rflags, largest_align)))
1235 return -ENOSPC;
1236 } while (unlikely(uatomic_cmpxchg(&buf->offset, offsets.old,
1237 offsets.end) != offsets.old));
1238
1239 /*
1240 * Atomically update last_tsc. This update races against concurrent
1241 * atomic updates, but the race will always cause supplementary full TSC
1242 * events, never the opposite (missing a full TSC event when it would be
1243 * needed).
1244 */
1245 save_last_tsc(buf, *tsc);
1246
1247 /*
1248 * Push the reader if necessary
1249 */
1250 ltt_reserve_push_reader(chan, buf, offsets.end - 1);
1251
1252 /*
1253 * Clear noref flag for this subbuffer.
1254 */
1255 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.end - 1, chan));
1256
1257 /*
1258 * Switch old subbuffer if needed.
1259 */
1260 if (unlikely(offsets.end_switch_old)) {
1261 //ust// ltt_clear_noref_flag(chan, buf, SUBBUF_INDEX(offsets.old - 1, chan));
1262 ltt_reserve_switch_old_subbuf(chan, buf, &offsets, tsc);
1263 DBG("Switching %s_%d", chan->channel_name, cpu);
1264 }
1265
1266 /*
1267 * Populate new subbuffer.
1268 */
1269 if (unlikely(offsets.begin_switch))
1270 ltt_reserve_switch_new_subbuf(chan, buf, &offsets, tsc);
1271
1272 if (unlikely(offsets.end_switch_current))
1273 ltt_reserve_end_switch_current(chan, buf, &offsets, tsc);
1274
1275 *slot_size = offsets.size;
1276 *buf_offset = offsets.begin + offsets.before_hdr_pad;
1277 return 0;
1278 }
1279
1280 static struct ltt_transport ust_relay_transport = {
1281 .name = "ustrelay",
1282 .ops = {
1283 .create_channel = ust_buffers_create_channel,
1284 .finish_channel = ltt_relay_finish_channel,
1285 .remove_channel = ltt_relay_remove_channel,
1286 .wakeup_channel = ltt_relay_async_wakeup_chan,
1287 },
1288 };
1289
1290 static char initialized = 0;
1291
1292 void __attribute__((constructor)) init_ustrelay_transport(void)
1293 {
1294 if(!initialized) {
1295 ltt_transport_register(&ust_relay_transport);
1296 initialized = 1;
1297 }
1298 }
1299
1300 static void __attribute__((destructor)) ust_buffers_exit(void)
1301 {
1302 ltt_transport_unregister(&ust_relay_transport);
1303 }
1304
1305 size_t ltt_write_event_header_slow(struct ust_trace *trace,
1306 struct ust_channel *channel,
1307 struct ust_buffer *buf, long buf_offset,
1308 u16 eID, u32 event_size,
1309 u64 tsc, unsigned int rflags)
1310 {
1311 struct ltt_event_header header;
1312 u16 small_size;
1313
1314 switch (rflags) {
1315 case LTT_RFLAG_ID_SIZE_TSC:
1316 header.id_time = 29 << LTT_TSC_BITS;
1317 break;
1318 case LTT_RFLAG_ID_SIZE:
1319 header.id_time = 30 << LTT_TSC_BITS;
1320 break;
1321 case LTT_RFLAG_ID:
1322 header.id_time = 31 << LTT_TSC_BITS;
1323 break;
1324 }
1325
1326 header.id_time |= (u32)tsc & LTT_TSC_MASK;
1327 ust_buffers_write(buf, buf_offset, &header, sizeof(header));
1328 buf_offset += sizeof(header);
1329
1330 switch (rflags) {
1331 case LTT_RFLAG_ID_SIZE_TSC:
1332 small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE);
1333 ust_buffers_write(buf, buf_offset,
1334 &eID, sizeof(u16));
1335 buf_offset += sizeof(u16);
1336 ust_buffers_write(buf, buf_offset,
1337 &small_size, sizeof(u16));
1338 buf_offset += sizeof(u16);
1339 if (small_size == LTT_MAX_SMALL_SIZE) {
1340 ust_buffers_write(buf, buf_offset,
1341 &event_size, sizeof(u32));
1342 buf_offset += sizeof(u32);
1343 }
1344 buf_offset += ltt_align(buf_offset, sizeof(u64));
1345 ust_buffers_write(buf, buf_offset,
1346 &tsc, sizeof(u64));
1347 buf_offset += sizeof(u64);
1348 break;
1349 case LTT_RFLAG_ID_SIZE:
1350 small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE);
1351 ust_buffers_write(buf, buf_offset,
1352 &eID, sizeof(u16));
1353 buf_offset += sizeof(u16);
1354 ust_buffers_write(buf, buf_offset,
1355 &small_size, sizeof(u16));
1356 buf_offset += sizeof(u16);
1357 if (small_size == LTT_MAX_SMALL_SIZE) {
1358 ust_buffers_write(buf, buf_offset,
1359 &event_size, sizeof(u32));
1360 buf_offset += sizeof(u32);
1361 }
1362 break;
1363 case LTT_RFLAG_ID:
1364 ust_buffers_write(buf, buf_offset,
1365 &eID, sizeof(u16));
1366 buf_offset += sizeof(u16);
1367 break;
1368 }
1369
1370 return buf_offset;
1371 }
This page took 0.055741 seconds and 4 git commands to generate.