Add UST_DEBUG env. var. support
[lttng-ust.git] / libust / buffers.c
CommitLineData
b5b073e2
PMF
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)
d4d511d9 6 * Copyright (C) 2008-2011 - Mathieu Desnoyers (mathieu.desnoyers@polymtl.ca)
b5b073e2
PMF
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
4fbf9cd1
MD
10 * License as published by the Free Software Foundation; version 2.1 of
11 * the License.
b5b073e2
PMF
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
d4d511d9
MD
23/*
24 * Note: this code does not support the ref/noref flag and reader-owned
283f1158
MD
25 * subbuffer scheme. Therefore, flight recorder mode uses a mechanism
26 * where the reader can read corrupted data (and detect this), thus
27 * returning -EIO.
d4d511d9
MD
28 */
29
204141ee 30#include <unistd.h>
b5b073e2
PMF
31#include <sys/mman.h>
32#include <sys/ipc.h>
33#include <sys/shm.h>
34#include <fcntl.h>
909bc43f 35#include <stdlib.h>
518d7abb
PMF
36
37#include <ust/clock.h>
38
b5b073e2
PMF
39#include "buffers.h"
40#include "channels.h"
41#include "tracer.h"
42#include "tracercore.h"
30ffe279 43#include "usterr_signal_safe.h"
b5b073e2 44
b73a4c47
PMF
45struct ltt_reserve_switch_offsets {
46 long begin, end, old;
47 long begin_switch, end_switch_current, end_switch_old;
48 size_t before_hdr_pad, size;
49};
50
51
b5b073e2 52static DEFINE_MUTEX(ust_buffers_channels_mutex);
0222e121 53static CDS_LIST_HEAD(ust_buffers_channels);
b5b073e2 54
d4d511d9
MD
55static void ltt_force_switch(struct ust_buffer *buf,
56 enum force_switch_mode mode);
57
bb3132c8
MD
58/**
59 * _ust_buffers_strncpy_fixup - Fix an incomplete string in a ltt_relay buffer.
60 * @buf : buffer
61 * @offset : offset within the buffer
62 * @len : length to write
63 * @copied: string actually copied
64 * @terminated: does string end with \0
b73a4c47 65 *
bb3132c8 66 * Fills string with "X" if incomplete.
b73a4c47 67 */
bb3132c8
MD
68void _ust_buffers_strncpy_fixup(struct ust_buffer *buf, size_t offset,
69 size_t len, size_t copied, int terminated)
b73a4c47 70{
bb3132c8
MD
71 size_t buf_offset, cpy;
72
73 if (copied == len) {
74 /*
75 * Deal with non-terminated string.
76 */
77 assert(!terminated);
78 offset += copied - 1;
79 buf_offset = BUFFER_OFFSET(offset, buf->chan);
80 /*
81 * Underlying layer should never ask for writes across
82 * subbuffers.
83 */
84 assert(buf_offset
85 < buf->chan->subbuf_size*buf->chan->subbuf_cnt);
86 ust_buffers_do_memset(buf->buf_data + buf_offset, '\0', 1);
87 return;
88 }
89
90 /*
91 * Deal with incomplete string.
92 * Overwrite string's \0 with X too.
93 */
94 cpy = copied - 1;
95 assert(terminated);
96 len -= cpy;
97 offset += cpy;
98 buf_offset = BUFFER_OFFSET(offset, buf->chan);
99
100 /*
101 * Underlying layer should never ask for writes across subbuffers.
102 */
103 assert(buf_offset
104 < buf->chan->subbuf_size*buf->chan->subbuf_cnt);
b73a4c47 105
bb3132c8
MD
106 ust_buffers_do_memset(buf->buf_data + buf_offset,
107 'X', len);
b73a4c47 108
bb3132c8
MD
109 /*
110 * Overwrite last 'X' with '\0'.
111 */
112 offset += len - 1;
113 buf_offset = BUFFER_OFFSET(offset, buf->chan);
114 /*
115 * Underlying layer should never ask for writes across subbuffers.
116 */
117 assert(buf_offset
118 < buf->chan->subbuf_size*buf->chan->subbuf_cnt);
119 ust_buffers_do_memset(buf->buf_data + buf_offset, '\0', 1);
b73a4c47
PMF
120}
121
ef15e552
NC
122static void ltt_buffer_begin(struct ust_buffer *buf,
123 u64 tsc, unsigned int subbuf_idx)
124{
125 struct ust_channel *channel = buf->chan;
126 struct ltt_subbuffer_header *header =
127 (struct ltt_subbuffer_header *)
128 ust_buffers_offset_address(buf,
129 subbuf_idx * buf->chan->subbuf_size);
b5b073e2 130
ef15e552
NC
131 header->cycle_count_begin = tsc;
132 header->data_size = 0xFFFFFFFF; /* for recognizing crashed buffers */
133 header->sb_size = 0xFFFFFFFF; /* for recognizing crashed buffers */
c74e3560
MD
134 /*
135 * No memory barrier needed to order data_data/sb_size vs commit count
136 * update, because commit count update contains a compiler barrier that
137 * ensures the order of the writes are OK from a program POV. It only
138 * matters for crash dump recovery which is not executed concurrently,
139 * so memory write order does not matter.
140 */
ef15e552
NC
141 ltt_write_trace_header(channel->trace, header);
142}
143
144static int map_buf_data(struct ust_buffer *buf, size_t *size)
b5b073e2
PMF
145{
146 void *ptr;
147 int result;
148
149 *size = PAGE_ALIGN(*size);
150
151 result = buf->shmid = shmget(getpid(), *size, IPC_CREAT | IPC_EXCL | 0700);
ef15e552 152 if (result < 0 && errno == EINVAL) {
b5b073e2
PMF
153 ERR("shmget() returned EINVAL; maybe /proc/sys/kernel/shmmax should be increased.");
154 return -1;
ef15e552 155 } else if (result < 0) {
b5b073e2
PMF
156 PERROR("shmget");
157 return -1;
158 }
159
160 ptr = shmat(buf->shmid, NULL, 0);
ef15e552 161 if (ptr == (void *) -1) {
b5b073e2
PMF
162 perror("shmat");
163 goto destroy_shmem;
164 }
165
166 /* Already mark the shared memory for destruction. This will occur only
167 * when all users have detached.
168 */
169 result = shmctl(buf->shmid, IPC_RMID, NULL);
170 if(result == -1) {
171 perror("shmctl");
172 return -1;
173 }
174
175 buf->buf_data = ptr;
176 buf->buf_size = *size;
177
178 return 0;
179
ef15e552 180destroy_shmem:
b5b073e2
PMF
181 result = shmctl(buf->shmid, IPC_RMID, NULL);
182 if(result == -1) {
183 perror("shmctl");
184 }
185
186 return -1;
187}
188
ef15e552 189static int open_buf(struct ust_channel *chan, int cpu)
b5b073e2 190{
ef15e552
NC
191 int result, fds[2];
192 unsigned int j;
193 struct ust_trace *trace = chan->trace;
194 struct ust_buffer *buf = chan->buf[cpu];
195 unsigned int n_subbufs = chan->subbuf_cnt;
b5b073e2 196
ef15e552
NC
197
198 result = map_buf_data(buf, &chan->alloc_size);
199 if (result < 0)
204141ee 200 return -1;
b5b073e2 201
ef15e552
NC
202 buf->commit_count =
203 zmalloc(sizeof(*buf->commit_count) * n_subbufs);
204 if (!buf->commit_count)
205 goto unmap_buf;
b5b073e2 206
ef15e552
NC
207 result = pipe(fds);
208 if (result < 0) {
209 PERROR("pipe");
210 goto free_commit_count;
211 }
212 buf->data_ready_fd_read = fds[0];
213 buf->data_ready_fd_write = fds[1];
b5b073e2 214
ef15e552
NC
215 buf->cpu = cpu;
216 buf->chan = chan;
b5b073e2 217
ef15e552
NC
218 uatomic_set(&buf->offset, ltt_subbuffer_header_size());
219 uatomic_set(&buf->consumed, 0);
220 uatomic_set(&buf->active_readers, 0);
221 for (j = 0; j < n_subbufs; j++) {
222 uatomic_set(&buf->commit_count[j].cc, 0);
223 uatomic_set(&buf->commit_count[j].cc_sb, 0);
b5b073e2
PMF
224 }
225
ef15e552 226 ltt_buffer_begin(buf, trace->start_tsc, 0);
b5b073e2 227
ef15e552
NC
228 uatomic_add(&buf->commit_count[0].cc, ltt_subbuffer_header_size());
229
230 uatomic_set(&buf->events_lost, 0);
231 uatomic_set(&buf->corrupted_subbuffers, 0);
232
233 memset(buf->commit_seq, 0, sizeof(buf->commit_seq[0]) * n_subbufs);
234
235 return 0;
236
237free_commit_count:
238 free(buf->commit_count);
239
240unmap_buf:
241 if (shmdt(buf->buf_data) < 0) {
242 PERROR("shmdt failed");
243 }
244
245 return -1;
b5b073e2
PMF
246}
247
ef15e552 248static void close_buf(struct ust_buffer *buf)
b5b073e2 249{
204141ee 250 int result;
b5b073e2 251
ef15e552
NC
252 result = shmdt(buf->buf_data);
253 if (result < 0) {
254 PERROR("shmdt");
255 }
b5b073e2 256
ef15e552
NC
257 result = close(buf->data_ready_fd_read);
258 if (result < 0) {
259 PERROR("close");
260 }
b5b073e2 261
ef15e552
NC
262 result = close(buf->data_ready_fd_write);
263 if (result < 0 && errno != EBADF) {
264 PERROR("close");
265 }
b5b073e2
PMF
266}
267
b5b073e2 268
ef15e552
NC
269static int open_channel(struct ust_channel *chan, size_t subbuf_size,
270 size_t subbuf_cnt)
b5b073e2 271{
204141ee
PMF
272 int i;
273 int result;
274
b5b073e2
PMF
275 if(subbuf_size == 0 || subbuf_cnt == 0)
276 return -1;
277
b73a4c47
PMF
278 /* Check that the subbuffer size is larger than a page. */
279 WARN_ON_ONCE(subbuf_size < PAGE_SIZE);
280
281 /*
282 * Make sure the number of subbuffers and subbuffer size are power of 2.
283 */
284 WARN_ON_ONCE(hweight32(subbuf_size) != 1);
285 WARN_ON(hweight32(subbuf_cnt) != 1);
286
b5b073e2
PMF
287 chan->version = UST_CHANNEL_VERSION;
288 chan->subbuf_cnt = subbuf_cnt;
289 chan->subbuf_size = subbuf_size;
290 chan->subbuf_size_order = get_count_order(subbuf_size);
b73a4c47 291 chan->alloc_size = subbuf_size * subbuf_cnt;
204141ee 292
f7b16408 293 pthread_mutex_lock(&ust_buffers_channels_mutex);
ef15e552
NC
294 for (i=0; i < chan->n_cpus; i++) {
295 result = open_buf(chan, i);
204141ee
PMF
296 if (result == -1)
297 goto error;
298 }
0222e121 299 cds_list_add(&chan->list, &ust_buffers_channels);
f7b16408 300 pthread_mutex_unlock(&ust_buffers_channels_mutex);
b5b073e2
PMF
301
302 return 0;
303
b2ccc231 304 /* Error handling */
204141ee 305error:
b2ccc231
MD
306 for(i--; i >= 0; i--)
307 close_buf(chan->buf[i]);
204141ee 308
f7b16408 309 pthread_mutex_unlock(&ust_buffers_channels_mutex);
b5b073e2
PMF
310 return -1;
311}
312
ef15e552 313static void close_channel(struct ust_channel *chan)
b5b073e2 314{
204141ee
PMF
315 int i;
316 if(!chan)
b5b073e2
PMF
317 return;
318
f7b16408 319 pthread_mutex_lock(&ust_buffers_channels_mutex);
d4d511d9
MD
320 /*
321 * checking for chan->buf[i] being NULL or not is useless in
322 * practice because we allocate buffers for all possible cpus.
323 * However, should we decide to change this and only allocate
324 * for online cpus, this check becomes useful.
325 */
326 for (i=0; i<chan->n_cpus; i++) {
327 if (chan->buf[i])
ef15e552 328 close_buf(chan->buf[i]);
204141ee 329 }
b5b073e2 330
0222e121 331 cds_list_del(&chan->list);
ef15e552 332
f7b16408 333 pthread_mutex_unlock(&ust_buffers_channels_mutex);
b5b073e2
PMF
334}
335
b5b073e2
PMF
336/*
337 * offset is assumed to never be 0 here : never deliver a completely empty
338 * subbuffer. The lost size is between 0 and subbuf_size-1.
339 */
b73a4c47 340static notrace void ltt_buffer_end(struct ust_buffer *buf,
b5b073e2
PMF
341 u64 tsc, unsigned int offset, unsigned int subbuf_idx)
342{
343 struct ltt_subbuffer_header *header =
344 (struct ltt_subbuffer_header *)
b73a4c47 345 ust_buffers_offset_address(buf,
b5b073e2 346 subbuf_idx * buf->chan->subbuf_size);
8c36d1ee 347 u32 data_size = SUBBUF_OFFSET(offset - 1, buf->chan) + 1;
b5b073e2 348
8c36d1ee 349 header->sb_size = PAGE_ALIGN(data_size);
b5b073e2 350 header->cycle_count_end = tsc;
b102c2b0
PMF
351 header->events_lost = uatomic_read(&buf->events_lost);
352 header->subbuf_corrupt = uatomic_read(&buf->corrupted_subbuffers);
719569e4
PMF
353 if(unlikely(header->events_lost > 0)) {
354 DBG("Some events (%d) were lost in %s_%d", header->events_lost, buf->chan->channel_name, buf->cpu);
355 }
c74e3560
MD
356 /*
357 * Makes sure data_size write happens after write of the rest of the
358 * buffer end data, because data_size is used to identify a completely
359 * written subbuffer in a crash dump.
360 */
361 cmm_barrier();
362 header->data_size = data_size;
b5b073e2
PMF
363}
364
365/*
366 * This function should not be called from NMI interrupt context
367 */
368static notrace void ltt_buf_unfull(struct ust_buffer *buf,
369 unsigned int subbuf_idx,
370 long offset)
371{
b5b073e2
PMF
372}
373
b73a4c47 374/*
0222e121 375 * Promote compiler cmm_barrier to a smp_mb().
b73a4c47
PMF
376 * For the specific LTTng case, this IPI call should be removed if the
377 * architecture does not reorder writes. This should eventually be provided by
378 * a separate architecture-specific infrastructure.
379 */
e17571a5
PMF
380//ust// static void remote_mb(void *info)
381//ust// {
382//ust// smp_mb();
383//ust// }
b73a4c47
PMF
384
385int ust_buffers_get_subbuf(struct ust_buffer *buf, long *consumed)
b5b073e2
PMF
386{
387 struct ust_channel *channel = buf->chan;
388 long consumed_old, consumed_idx, commit_count, write_offset;
b73a4c47
PMF
389//ust// int retval;
390
b102c2b0 391 consumed_old = uatomic_read(&buf->consumed);
b5b073e2 392 consumed_idx = SUBBUF_INDEX(consumed_old, buf->chan);
b102c2b0 393 commit_count = uatomic_read(&buf->commit_count[consumed_idx].cc_sb);
b5b073e2
PMF
394 /*
395 * Make sure we read the commit count before reading the buffer
396 * data and the write offset. Correct consumed offset ordering
397 * wrt commit count is insured by the use of cmpxchg to update
398 * the consumed offset.
b73a4c47 399 */
d4d511d9 400
b73a4c47
PMF
401 /*
402 * Local rmb to match the remote wmb to read the commit count before the
403 * buffer data and the write offset.
b5b073e2 404 */
0222e121 405 cmm_smp_rmb();
b73a4c47 406
b102c2b0 407 write_offset = uatomic_read(&buf->offset);
b5b073e2
PMF
408 /*
409 * Check that the subbuffer we are trying to consume has been
410 * already fully committed.
411 */
412 if (((commit_count - buf->chan->subbuf_size)
413 & channel->commit_count_mask)
414 - (BUFFER_TRUNC(consumed_old, buf->chan)
415 >> channel->n_subbufs_order)
416 != 0) {
417 return -EAGAIN;
418 }
419 /*
420 * Check that we are not about to read the same subbuffer in
421 * which the writer head is.
422 */
423 if ((SUBBUF_TRUNC(write_offset, buf->chan)
424 - SUBBUF_TRUNC(consumed_old, buf->chan))
425 == 0) {
426 return -EAGAIN;
427 }
b73a4c47
PMF
428 *consumed = consumed_old;
429
b5b073e2
PMF
430 return 0;
431}
432
b73a4c47 433int ust_buffers_put_subbuf(struct ust_buffer *buf, unsigned long uconsumed_old)
b5b073e2
PMF
434{
435 long consumed_new, consumed_old;
436
b102c2b0 437 consumed_old = uatomic_read(&buf->consumed);
b5b073e2
PMF
438 consumed_old = consumed_old & (~0xFFFFFFFFL);
439 consumed_old = consumed_old | uconsumed_old;
440 consumed_new = SUBBUF_ALIGN(consumed_old, buf->chan);
441
b102c2b0 442 if (uatomic_cmpxchg(&buf->consumed, consumed_old,
b5b073e2
PMF
443 consumed_new)
444 != consumed_old) {
445 /* We have been pushed by the writer : the last
446 * buffer read _is_ corrupted! It can also
447 * happen if this is a buffer we never got. */
b5b073e2
PMF
448 return -EIO;
449 } else {
450 /* tell the client that buffer is now unfull */
451 int index;
452 long data;
453 index = SUBBUF_INDEX(consumed_old, buf->chan);
454 data = BUFFER_OFFSET(consumed_old, buf->chan);
455 ltt_buf_unfull(buf, index, data);
b5b073e2
PMF
456 }
457 return 0;
458}
459
ef15e552 460static int map_buf_structs(struct ust_channel *chan)
b5b073e2
PMF
461{
462 void *ptr;
463 int result;
204141ee
PMF
464 size_t size;
465 int i;
b5b073e2 466
204141ee 467 size = PAGE_ALIGN(1);
b5b073e2 468
204141ee 469 for(i=0; i<chan->n_cpus; i++) {
b5b073e2 470
204141ee
PMF
471 result = chan->buf_struct_shmids[i] = shmget(getpid(), size, IPC_CREAT | IPC_EXCL | 0700);
472 if(result == -1) {
473 PERROR("shmget");
474 goto destroy_previous;
475 }
b5b073e2 476
204141ee
PMF
477 ptr = shmat(chan->buf_struct_shmids[i], NULL, 0);
478 if(ptr == (void *) -1) {
479 perror("shmat");
480 goto destroy_shm;
481 }
482
483 /* Already mark the shared memory for destruction. This will occur only
484 * when all users have detached.
485 */
486 result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL);
487 if(result == -1) {
488 perror("shmctl");
489 goto destroy_previous;
490 }
491
492 chan->buf[i] = ptr;
b5b073e2
PMF
493 }
494
204141ee 495 return 0;
b5b073e2 496
204141ee
PMF
497 /* Jumping inside this loop occurs from within the other loop above with i as
498 * counter, so it unallocates the structures for the cpu = current_i down to
499 * zero. */
500 for(; i>=0; i--) {
501 destroy_shm:
502 result = shmctl(chan->buf_struct_shmids[i], IPC_RMID, NULL);
503 if(result == -1) {
504 perror("shmctl");
505 }
b5b073e2 506
204141ee
PMF
507 destroy_previous:
508 continue;
b5b073e2
PMF
509 }
510
204141ee 511 return -1;
b5b073e2
PMF
512}
513
ef15e552
NC
514static int unmap_buf_structs(struct ust_channel *chan)
515{
516 int i;
517
518 for (i=0; i < chan->n_cpus; i++) {
519 if (shmdt(chan->buf[i]) < 0) {
520 PERROR("shmdt");
521 }
522 }
fbae86d6 523 return 0;
ef15e552
NC
524}
525
b5b073e2
PMF
526/*
527 * Create channel.
528 */
ef15e552
NC
529static int create_channel(const char *trace_name, struct ust_trace *trace,
530 const char *channel_name, struct ust_channel *chan,
b5b073e2
PMF
531 unsigned int subbuf_size, unsigned int n_subbufs, int overwrite)
532{
ef15e552 533 int i, result;
b5b073e2 534
ef15e552
NC
535 chan->trace = trace;
536 chan->overwrite = overwrite;
537 chan->n_subbufs_order = get_count_order(n_subbufs);
538 chan->commit_count_mask = (~0UL >> chan->n_subbufs_order);
539 chan->n_cpus = get_n_cpus();
b5b073e2 540
ef15e552
NC
541 /* These mappings should ideall be per-cpu, if somebody can do that
542 * from userspace, that would be cool!
543 */
544 chan->buf = (void *) zmalloc(chan->n_cpus * sizeof(void *));
545 if(chan->buf == NULL) {
204141ee
PMF
546 goto error;
547 }
ef15e552
NC
548 chan->buf_struct_shmids = (int *) zmalloc(chan->n_cpus * sizeof(int));
549 if(chan->buf_struct_shmids == NULL)
204141ee 550 goto free_buf;
b5b073e2 551
ef15e552 552 result = map_buf_structs(chan);
204141ee
PMF
553 if(result != 0) {
554 goto free_buf_struct_shmids;
555 }
b5b073e2 556
ef15e552 557 result = open_channel(chan, subbuf_size, n_subbufs);
204141ee 558 if (result != 0) {
c1f20530 559 ERR("Cannot open channel for trace %s", trace_name);
ef15e552 560 goto unmap_buf_structs;
b5b073e2
PMF
561 }
562
204141ee
PMF
563 return 0;
564
ef15e552
NC
565unmap_buf_structs:
566 for (i=0; i < chan->n_cpus; i++) {
567 if (shmdt(chan->buf[i]) < 0) {
568 PERROR("shmdt bufstruct");
569 }
570 }
204141ee
PMF
571
572free_buf_struct_shmids:
ef15e552 573 free(chan->buf_struct_shmids);
b5b073e2 574
204141ee 575free_buf:
ef15e552 576 free(chan->buf);
204141ee
PMF
577
578error:
579 return -1;
b5b073e2
PMF
580}
581
ef15e552
NC
582
583static void remove_channel(struct ust_channel *chan)
584{
585 close_channel(chan);
586
587 unmap_buf_structs(chan);
588
589 free(chan->buf_struct_shmids);
590
591 free(chan->buf);
ef15e552
NC
592}
593
b5b073e2
PMF
594static void ltt_relay_async_wakeup_chan(struct ust_channel *ltt_channel)
595{
b5b073e2
PMF
596}
597
204141ee 598static void ltt_relay_finish_buffer(struct ust_channel *channel, unsigned int cpu)
b5b073e2 599{
204141ee
PMF
600 if (channel->buf[cpu]) {
601 struct ust_buffer *buf = channel->buf[cpu];
97c10252 602 ltt_force_switch(buf, FORCE_FLUSH);
ef15e552 603
b5b073e2 604 /* closing the pipe tells the consumer the buffer is finished */
b5b073e2
PMF
605 close(buf->data_ready_fd_write);
606 }
607}
608
609
ef15e552 610static void finish_channel(struct ust_channel *channel)
b5b073e2 611{
204141ee 612 unsigned int i;
b5b073e2 613
d4d511d9 614 for (i=0; i<channel->n_cpus; i++) {
204141ee
PMF
615 ltt_relay_finish_buffer(channel, i);
616 }
b5b073e2
PMF
617}
618
b5b073e2 619
b5b073e2 620/*
b73a4c47
PMF
621 * ltt_reserve_switch_old_subbuf: switch old subbuffer
622 *
623 * Concurrency safe because we are the last and only thread to alter this
624 * sub-buffer. As long as it is not delivered and read, no other thread can
625 * alter the offset, alter the reserve_count or call the
626 * client_buffer_end_callback on this sub-buffer.
627 *
628 * The only remaining threads could be the ones with pending commits. They will
629 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
630 * We detect corrupted subbuffers with commit and reserve counts. We keep a
631 * corrupted sub-buffers count and push the readers across these sub-buffers.
632 *
633 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
634 * switches in, finding out it's corrupted. The result will be than the old
635 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
636 * will be declared corrupted too because of the commit count adjustment.
637 *
638 * Note : offset_old should never be 0 here.
b5b073e2 639 */
b73a4c47
PMF
640static void ltt_reserve_switch_old_subbuf(
641 struct ust_channel *chan, struct ust_buffer *buf,
642 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
b5b073e2 643{
b73a4c47
PMF
644 long oldidx = SUBBUF_INDEX(offsets->old - 1, chan);
645 long commit_count, padding_size;
b5b073e2 646
b73a4c47
PMF
647 padding_size = chan->subbuf_size
648 - (SUBBUF_OFFSET(offsets->old - 1, chan) + 1);
649 ltt_buffer_end(buf, *tsc, offsets->old, oldidx);
b5b073e2 650
b73a4c47
PMF
651 /*
652 * Must write slot data before incrementing commit count.
ef15e552 653 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
0222e121 654 * sent by get_subbuf() when it does its cmm_smp_rmb().
b73a4c47 655 */
0222e121 656 cmm_smp_wmb();
b102c2b0
PMF
657 uatomic_add(&buf->commit_count[oldidx].cc, padding_size);
658 commit_count = uatomic_read(&buf->commit_count[oldidx].cc);
b73a4c47 659 ltt_check_deliver(chan, buf, offsets->old - 1, commit_count, oldidx);
1e8c9e7b 660 ltt_write_commit_counter(chan, buf, oldidx,
b73a4c47
PMF
661 offsets->old, commit_count, padding_size);
662}
b5b073e2 663
b73a4c47
PMF
664/*
665 * ltt_reserve_switch_new_subbuf: Populate new subbuffer.
666 *
667 * This code can be executed unordered : writers may already have written to the
668 * sub-buffer before this code gets executed, caution. The commit makes sure
669 * that this code is executed before the deliver of this sub-buffer.
670 */
671static void ltt_reserve_switch_new_subbuf(
672 struct ust_channel *chan, struct ust_buffer *buf,
673 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
674{
675 long beginidx = SUBBUF_INDEX(offsets->begin, chan);
676 long commit_count;
b5b073e2 677
b73a4c47 678 ltt_buffer_begin(buf, *tsc, beginidx);
b5b073e2 679
b73a4c47
PMF
680 /*
681 * Must write slot data before incrementing commit count.
ef15e552 682 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
0222e121 683 * sent by get_subbuf() when it does its cmm_smp_rmb().
b73a4c47 684 */
0222e121 685 cmm_smp_wmb();
b102c2b0
PMF
686 uatomic_add(&buf->commit_count[beginidx].cc, ltt_subbuffer_header_size());
687 commit_count = uatomic_read(&buf->commit_count[beginidx].cc);
b73a4c47
PMF
688 /* Check if the written buffer has to be delivered */
689 ltt_check_deliver(chan, buf, offsets->begin, commit_count, beginidx);
1e8c9e7b 690 ltt_write_commit_counter(chan, buf, beginidx,
b73a4c47
PMF
691 offsets->begin, commit_count, ltt_subbuffer_header_size());
692}
b5b073e2 693
b73a4c47
PMF
694/*
695 * ltt_reserve_end_switch_current: finish switching current subbuffer
696 *
697 * Concurrency safe because we are the last and only thread to alter this
698 * sub-buffer. As long as it is not delivered and read, no other thread can
699 * alter the offset, alter the reserve_count or call the
700 * client_buffer_end_callback on this sub-buffer.
701 *
702 * The only remaining threads could be the ones with pending commits. They will
703 * have to do the deliver themselves. Not concurrency safe in overwrite mode.
704 * We detect corrupted subbuffers with commit and reserve counts. We keep a
705 * corrupted sub-buffers count and push the readers across these sub-buffers.
706 *
707 * Not concurrency safe if a writer is stalled in a subbuffer and another writer
708 * switches in, finding out it's corrupted. The result will be than the old
709 * (uncommited) subbuffer will be declared corrupted, and that the new subbuffer
710 * will be declared corrupted too because of the commit count adjustment.
711 */
712static void ltt_reserve_end_switch_current(
713 struct ust_channel *chan,
714 struct ust_buffer *buf,
715 struct ltt_reserve_switch_offsets *offsets, u64 *tsc)
716{
717 long endidx = SUBBUF_INDEX(offsets->end - 1, chan);
718 long commit_count, padding_size;
719
720 padding_size = chan->subbuf_size
721 - (SUBBUF_OFFSET(offsets->end - 1, chan) + 1);
722
723 ltt_buffer_end(buf, *tsc, offsets->end, endidx);
724
725 /*
726 * Must write slot data before incrementing commit count.
ef15e552 727 * This compiler barrier is upgraded into a cmm_smp_wmb() by the IPI
0222e121 728 * sent by get_subbuf() when it does its cmm_smp_rmb().
b73a4c47 729 */
0222e121 730 cmm_smp_wmb();
b102c2b0
PMF
731 uatomic_add(&buf->commit_count[endidx].cc, padding_size);
732 commit_count = uatomic_read(&buf->commit_count[endidx].cc);
b73a4c47
PMF
733 ltt_check_deliver(chan, buf,
734 offsets->end - 1, commit_count, endidx);
1e8c9e7b 735 ltt_write_commit_counter(chan, buf, endidx,
b73a4c47 736 offsets->end, commit_count, padding_size);
b5b073e2
PMF
737}
738
739/*
740 * Returns :
741 * 0 if ok
742 * !0 if execution must be aborted.
743 */
b73a4c47 744static int ltt_relay_try_switch_slow(
b5b073e2 745 enum force_switch_mode mode,
b73a4c47 746 struct ust_channel *chan,
b5b073e2
PMF
747 struct ust_buffer *buf,
748 struct ltt_reserve_switch_offsets *offsets,
749 u64 *tsc)
750{
751 long subbuf_index;
b73a4c47 752 long reserve_commit_diff;
b5b073e2 753
b102c2b0 754 offsets->begin = uatomic_read(&buf->offset);
b5b073e2
PMF
755 offsets->old = offsets->begin;
756 offsets->begin_switch = 0;
757 offsets->end_switch_old = 0;
758
759 *tsc = trace_clock_read64();
760
761 if (SUBBUF_OFFSET(offsets->begin, buf->chan) != 0) {
762 offsets->begin = SUBBUF_ALIGN(offsets->begin, buf->chan);
763 offsets->end_switch_old = 1;
764 } else {
765 /* we do not have to switch : buffer is empty */
766 return -1;
767 }
768 if (mode == FORCE_ACTIVE)
769 offsets->begin += ltt_subbuffer_header_size();
770 /*
771 * Always begin_switch in FORCE_ACTIVE mode.
772 * Test new buffer integrity
773 */
774 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
b73a4c47 775 reserve_commit_diff =
b5b073e2 776 (BUFFER_TRUNC(offsets->begin, buf->chan)
b73a4c47 777 >> chan->n_subbufs_order)
b102c2b0 778 - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb)
b73a4c47
PMF
779 & chan->commit_count_mask);
780 if (reserve_commit_diff == 0) {
b5b073e2
PMF
781 /* Next buffer not corrupted. */
782 if (mode == FORCE_ACTIVE
b73a4c47 783 && !chan->overwrite
b102c2b0 784 && offsets->begin - uatomic_read(&buf->consumed)
b73a4c47 785 >= chan->alloc_size) {
b5b073e2
PMF
786 /*
787 * We do not overwrite non consumed buffers and we are
788 * full : ignore switch while tracing is active.
789 */
790 return -1;
791 }
792 } else {
793 /*
794 * Next subbuffer corrupted. Force pushing reader even in normal
795 * mode
796 */
797 }
798 offsets->end = offsets->begin;
799 return 0;
800}
801
b5b073e2 802/*
b73a4c47
PMF
803 * Force a sub-buffer switch for a per-cpu buffer. This operation is
804 * completely reentrant : can be called while tracing is active with
805 * absolutely no lock held.
b5b073e2 806 */
b73a4c47
PMF
807void ltt_force_switch_lockless_slow(struct ust_buffer *buf,
808 enum force_switch_mode mode)
b5b073e2 809{
b73a4c47 810 struct ust_channel *chan = buf->chan;
b5b073e2 811 struct ltt_reserve_switch_offsets offsets;
b73a4c47 812 u64 tsc;
b5b073e2 813
b5b073e2
PMF
814 offsets.size = 0;
815
10dd3941 816 DBG("Switching (forced) %s_%d", chan->channel_name, buf->cpu);
b5b073e2
PMF
817 /*
818 * Perform retryable operations.
819 */
b5b073e2 820 do {
b73a4c47
PMF
821 if (ltt_relay_try_switch_slow(mode, chan, buf,
822 &offsets, &tsc))
823 return;
b102c2b0 824 } while (uatomic_cmpxchg(&buf->offset, offsets.old,
b5b073e2
PMF
825 offsets.end) != offsets.old);
826
827 /*
828 * Atomically update last_tsc. This update races against concurrent
829 * atomic updates, but the race will always cause supplementary full TSC
830 * events, never the opposite (missing a full TSC event when it would be
831 * needed).
832 */
b73a4c47 833 save_last_tsc(buf, tsc);
b5b073e2
PMF
834
835 /*
836 * Push the reader if necessary
837 */
b73a4c47
PMF
838 if (mode == FORCE_ACTIVE) {
839 ltt_reserve_push_reader(chan, buf, offsets.end - 1);
b73a4c47 840 }
b5b073e2
PMF
841
842 /*
843 * Switch old subbuffer if needed.
844 */
b73a4c47 845 if (offsets.end_switch_old) {
b73a4c47
PMF
846 ltt_reserve_switch_old_subbuf(chan, buf, &offsets, &tsc);
847 }
b5b073e2
PMF
848
849 /*
850 * Populate new subbuffer.
851 */
b73a4c47
PMF
852 if (mode == FORCE_ACTIVE)
853 ltt_reserve_switch_new_subbuf(chan, buf, &offsets, &tsc);
854}
b5b073e2 855
b73a4c47
PMF
856/*
857 * Returns :
858 * 0 if ok
859 * !0 if execution must be aborted.
860 */
861static int ltt_relay_try_reserve_slow(struct ust_channel *chan, struct ust_buffer *buf,
862 struct ltt_reserve_switch_offsets *offsets, size_t data_size,
863 u64 *tsc, unsigned int *rflags, int largest_align)
864{
865 long reserve_commit_diff;
b5b073e2 866
b102c2b0 867 offsets->begin = uatomic_read(&buf->offset);
b73a4c47
PMF
868 offsets->old = offsets->begin;
869 offsets->begin_switch = 0;
870 offsets->end_switch_current = 0;
871 offsets->end_switch_old = 0;
872
873 *tsc = trace_clock_read64();
874 if (last_tsc_overflow(buf, *tsc))
875 *rflags = LTT_RFLAG_ID_SIZE_TSC;
876
877 if (unlikely(SUBBUF_OFFSET(offsets->begin, buf->chan) == 0)) {
878 offsets->begin_switch = 1; /* For offsets->begin */
879 } else {
880 offsets->size = ust_get_header_size(chan,
881 offsets->begin, data_size,
882 &offsets->before_hdr_pad, *rflags);
883 offsets->size += ltt_align(offsets->begin + offsets->size,
884 largest_align)
885 + data_size;
886 if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan) +
887 offsets->size) > buf->chan->subbuf_size)) {
888 offsets->end_switch_old = 1; /* For offsets->old */
889 offsets->begin_switch = 1; /* For offsets->begin */
890 }
891 }
892 if (unlikely(offsets->begin_switch)) {
893 long subbuf_index;
894
895 /*
896 * We are typically not filling the previous buffer completely.
897 */
898 if (likely(offsets->end_switch_old))
899 offsets->begin = SUBBUF_ALIGN(offsets->begin,
900 buf->chan);
901 offsets->begin = offsets->begin + ltt_subbuffer_header_size();
902 /* Test new buffer integrity */
903 subbuf_index = SUBBUF_INDEX(offsets->begin, buf->chan);
904 reserve_commit_diff =
905 (BUFFER_TRUNC(offsets->begin, buf->chan)
906 >> chan->n_subbufs_order)
b102c2b0 907 - (uatomic_read(&buf->commit_count[subbuf_index].cc_sb)
b73a4c47
PMF
908 & chan->commit_count_mask);
909 if (likely(reserve_commit_diff == 0)) {
910 /* Next buffer not corrupted. */
911 if (unlikely(!chan->overwrite &&
912 (SUBBUF_TRUNC(offsets->begin, buf->chan)
b102c2b0 913 - SUBBUF_TRUNC(uatomic_read(
b73a4c47
PMF
914 &buf->consumed),
915 buf->chan))
916 >= chan->alloc_size)) {
917 /*
918 * We do not overwrite non consumed buffers
919 * and we are full : event is lost.
920 */
b102c2b0 921 uatomic_inc(&buf->events_lost);
b73a4c47
PMF
922 return -1;
923 } else {
924 /*
925 * next buffer not corrupted, we are either in
926 * overwrite mode or the buffer is not full.
927 * It's safe to write in this new subbuffer.
928 */
929 }
930 } else {
931 /*
932 * Next subbuffer corrupted. Drop event in normal and
933 * overwrite mode. Caused by either a writer OOPS or
934 * too many nested writes over a reserve/commit pair.
935 */
b102c2b0 936 uatomic_inc(&buf->events_lost);
b73a4c47
PMF
937 return -1;
938 }
939 offsets->size = ust_get_header_size(chan,
940 offsets->begin, data_size,
941 &offsets->before_hdr_pad, *rflags);
942 offsets->size += ltt_align(offsets->begin + offsets->size,
943 largest_align)
944 + data_size;
945 if (unlikely((SUBBUF_OFFSET(offsets->begin, buf->chan)
946 + offsets->size) > buf->chan->subbuf_size)) {
947 /*
948 * Event too big for subbuffers, report error, don't
949 * complete the sub-buffer switch.
950 */
b102c2b0 951 uatomic_inc(&buf->events_lost);
b73a4c47
PMF
952 return -1;
953 } else {
954 /*
955 * We just made a successful buffer switch and the event
956 * fits in the new subbuffer. Let's write.
957 */
958 }
959 } else {
960 /*
961 * Event fits in the current buffer and we are not on a switch
962 * boundary. It's safe to write.
963 */
964 }
965 offsets->end = offsets->begin + offsets->size;
966
967 if (unlikely((SUBBUF_OFFSET(offsets->end, buf->chan)) == 0)) {
968 /*
969 * The offset_end will fall at the very beginning of the next
970 * subbuffer.
971 */
972 offsets->end_switch_current = 1; /* For offsets->begin */
973 }
b5b073e2
PMF
974 return 0;
975}
976
b73a4c47
PMF
977/**
978 * ltt_relay_reserve_slot_lockless_slow - Atomic slot reservation in a buffer.
979 * @trace: the trace structure to log to.
980 * @ltt_channel: channel structure
981 * @transport_data: data structure specific to ltt relay
982 * @data_size: size of the variable length data to log.
983 * @slot_size: pointer to total size of the slot (out)
984 * @buf_offset : pointer to reserved buffer offset (out)
985 * @tsc: pointer to the tsc at the slot reservation (out)
986 * @cpu: cpuid
b5b073e2 987 *
b73a4c47
PMF
988 * Return : -ENOSPC if not enough space, else returns 0.
989 * It will take care of sub-buffer switching.
b5b073e2 990 */
12e81b07
PMF
991int ltt_reserve_slot_lockless_slow(struct ust_channel *chan,
992 struct ust_trace *trace, size_t data_size,
993 int largest_align, int cpu,
994 struct ust_buffer **ret_buf,
995 size_t *slot_size, long *buf_offset,
996 u64 *tsc, unsigned int *rflags)
b5b073e2 997{
12e81b07 998 struct ust_buffer *buf = *ret_buf = chan->buf[cpu];
b5b073e2 999 struct ltt_reserve_switch_offsets offsets;
b5b073e2 1000
b5b073e2
PMF
1001 offsets.size = 0;
1002
b5b073e2 1003 do {
b73a4c47
PMF
1004 if (unlikely(ltt_relay_try_reserve_slow(chan, buf, &offsets,
1005 data_size, tsc, rflags, largest_align)))
1006 return -ENOSPC;
b102c2b0 1007 } while (unlikely(uatomic_cmpxchg(&buf->offset, offsets.old,
b73a4c47 1008 offsets.end) != offsets.old));
b5b073e2
PMF
1009
1010 /*
1011 * Atomically update last_tsc. This update races against concurrent
1012 * atomic updates, but the race will always cause supplementary full TSC
1013 * events, never the opposite (missing a full TSC event when it would be
1014 * needed).
1015 */
b73a4c47 1016 save_last_tsc(buf, *tsc);
b5b073e2
PMF
1017
1018 /*
1019 * Push the reader if necessary
1020 */
b73a4c47
PMF
1021 ltt_reserve_push_reader(chan, buf, offsets.end - 1);
1022
b5b073e2
PMF
1023 /*
1024 * Switch old subbuffer if needed.
1025 */
b73a4c47 1026 if (unlikely(offsets.end_switch_old)) {
b73a4c47 1027 ltt_reserve_switch_old_subbuf(chan, buf, &offsets, tsc);
10dd3941 1028 DBG("Switching %s_%d", chan->channel_name, cpu);
b73a4c47 1029 }
b5b073e2
PMF
1030
1031 /*
1032 * Populate new subbuffer.
1033 */
b73a4c47
PMF
1034 if (unlikely(offsets.begin_switch))
1035 ltt_reserve_switch_new_subbuf(chan, buf, &offsets, tsc);
1036
1037 if (unlikely(offsets.end_switch_current))
1038 ltt_reserve_end_switch_current(chan, buf, &offsets, tsc);
1039
1040 *slot_size = offsets.size;
1041 *buf_offset = offsets.begin + offsets.before_hdr_pad;
1042 return 0;
b5b073e2
PMF
1043}
1044
b5b073e2
PMF
1045static struct ltt_transport ust_relay_transport = {
1046 .name = "ustrelay",
1047 .ops = {
ef15e552
NC
1048 .create_channel = create_channel,
1049 .finish_channel = finish_channel,
1050 .remove_channel = remove_channel,
b5b073e2 1051 .wakeup_channel = ltt_relay_async_wakeup_chan,
b5b073e2
PMF
1052 },
1053};
1054
b5b073e2
PMF
1055static char initialized = 0;
1056
1057void __attribute__((constructor)) init_ustrelay_transport(void)
1058{
5e96a467 1059 init_usterr();
b5b073e2
PMF
1060 if(!initialized) {
1061 ltt_transport_register(&ust_relay_transport);
1062 initialized = 1;
1063 }
1064}
1065
b73a4c47 1066static void __attribute__((destructor)) ust_buffers_exit(void)
b5b073e2
PMF
1067{
1068 ltt_transport_unregister(&ust_relay_transport);
1069}
b73a4c47 1070
12e81b07 1071size_t ltt_write_event_header_slow(struct ust_channel *channel,
b73a4c47
PMF
1072 struct ust_buffer *buf, long buf_offset,
1073 u16 eID, u32 event_size,
1074 u64 tsc, unsigned int rflags)
1075{
1076 struct ltt_event_header header;
1077 u16 small_size;
1078
1079 switch (rflags) {
1080 case LTT_RFLAG_ID_SIZE_TSC:
1081 header.id_time = 29 << LTT_TSC_BITS;
1082 break;
1083 case LTT_RFLAG_ID_SIZE:
1084 header.id_time = 30 << LTT_TSC_BITS;
1085 break;
1086 case LTT_RFLAG_ID:
1087 header.id_time = 31 << LTT_TSC_BITS;
1088 break;
e2b46575
DG
1089 default:
1090 WARN_ON_ONCE(1);
1091 header.id_time = 0;
1092 break;
b73a4c47
PMF
1093 }
1094
1095 header.id_time |= (u32)tsc & LTT_TSC_MASK;
1096 ust_buffers_write(buf, buf_offset, &header, sizeof(header));
1097 buf_offset += sizeof(header);
1098
1099 switch (rflags) {
1100 case LTT_RFLAG_ID_SIZE_TSC:
1101 small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE);
1102 ust_buffers_write(buf, buf_offset,
1103 &eID, sizeof(u16));
1104 buf_offset += sizeof(u16);
1105 ust_buffers_write(buf, buf_offset,
1106 &small_size, sizeof(u16));
1107 buf_offset += sizeof(u16);
1108 if (small_size == LTT_MAX_SMALL_SIZE) {
1109 ust_buffers_write(buf, buf_offset,
1110 &event_size, sizeof(u32));
1111 buf_offset += sizeof(u32);
1112 }
1113 buf_offset += ltt_align(buf_offset, sizeof(u64));
1114 ust_buffers_write(buf, buf_offset,
1115 &tsc, sizeof(u64));
1116 buf_offset += sizeof(u64);
1117 break;
1118 case LTT_RFLAG_ID_SIZE:
1119 small_size = (u16)min_t(u32, event_size, LTT_MAX_SMALL_SIZE);
1120 ust_buffers_write(buf, buf_offset,
1121 &eID, sizeof(u16));
1122 buf_offset += sizeof(u16);
1123 ust_buffers_write(buf, buf_offset,
1124 &small_size, sizeof(u16));
1125 buf_offset += sizeof(u16);
1126 if (small_size == LTT_MAX_SMALL_SIZE) {
1127 ust_buffers_write(buf, buf_offset,
1128 &event_size, sizeof(u32));
1129 buf_offset += sizeof(u32);
1130 }
1131 break;
1132 case LTT_RFLAG_ID:
1133 ust_buffers_write(buf, buf_offset,
1134 &eID, sizeof(u16));
1135 buf_offset += sizeof(u16);
1136 break;
1137 }
1138
1139 return buf_offset;
1140}
This page took 0.080118 seconds and 4 git commands to generate.