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