Port changes from lttng-kt
[ust.git] / ustd / lowlevel.c
1 /* Copyright (C) 2009 Pierre-Marc Fournier
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public
5 * License as published by the Free Software Foundation; either
6 * version 2.1 of the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 #include <assert.h>
19
20 #include "buffers.h"
21 #include "tracer.h"
22 #include "ustd.h"
23 #include "usterr.h"
24
25 /* This truncates to an offset in the buffer. */
26 #define USTD_BUFFER_TRUNC(offset, bufinfo) \
27 ((offset) & (~(((bufinfo)->subbuf_size*(bufinfo)->n_subbufs)-1)))
28
29 void finish_consuming_dead_subbuffer(struct buffer_info *buf)
30 {
31 struct ust_buffer *ustbuf = buf->bufstruct_mem;
32
33 long write_offset = local_read(&ustbuf->offset);
34 long consumed_offset = atomic_long_read(&ustbuf->consumed);
35
36 long i_subbuf;
37
38 DBG("processing died buffer");
39 DBG("consumed offset is %ld", consumed_offset);
40 DBG("write offset is %ld", write_offset);
41
42 /* First subbuf that we need to consume now. It is not modulo'd.
43 * Consumed_offset is the next byte to consume. */
44 long first_subbuf = consumed_offset / buf->subbuf_size;
45 /* Last subbuf that we need to consume now. It is not modulo'd.
46 * Write_offset is the next place to write so write_offset-1 is the
47 * last place written. */
48 long last_subbuf = (write_offset - 1) / buf->subbuf_size;
49
50 DBG("first_subbuf=%ld", first_subbuf);
51 DBG("last_subbuf=%ld", last_subbuf);
52
53 if(last_subbuf - first_subbuf >= buf->n_subbufs) {
54 DBG("an overflow has occurred, nothing can be recovered");
55 return;
56 }
57
58 /* Iterate on subbuffers to recover. */
59 for(i_subbuf=first_subbuf; ; i_subbuf++, i_subbuf %= buf->n_subbufs) {
60 void *tmp;
61 /* commit_seq is the offset in the buffer of the end of the last sequential commit.
62 * Bytes beyond this limit cannot be recovered. This is a free-running counter. */
63 long commit_seq = local_read(&ustbuf->commit_seq[i_subbuf]);
64
65 unsigned long valid_length = buf->subbuf_size;
66 long n_subbufs_order = get_count_order(buf->n_subbufs);
67 long commit_seq_mask = (~0UL >> n_subbufs_order);
68
69 struct ltt_subbuffer_header *header = (struct ltt_subbuffer_header *)((char *)buf->mem+i_subbuf*buf->subbuf_size);
70
71 if((commit_seq & commit_seq_mask) == 0) {
72 /* There is nothing to do. */
73 /* FIXME: is this needed? */
74 break;
75 }
76
77 /* Check if subbuf was fully written. This is from Mathieu's algorithm/paper. */
78 if (((commit_seq - buf->subbuf_size) & commit_seq_mask)
79 - (USTD_BUFFER_TRUNC(consumed_offset, buf) >> n_subbufs_order)
80 == 0) {
81 /* If it was, we only check the lost_size. This is the lost padding at the end of
82 * the subbuffer. */
83 valid_length = (unsigned long)buf->subbuf_size - header->lost_size;
84 }
85 else {
86 /* If the subbuffer was not fully written, then we don't check lost_size because
87 * it hasn't been written yet. Instead we check commit_seq and use it to choose
88 * a value for lost_size. The viewer will need this value when parsing.
89 */
90
91 valid_length = commit_seq & (buf->subbuf_size-1);
92 header->lost_size = buf->subbuf_size-valid_length;
93 assert(i_subbuf == (last_subbuf % buf->n_subbufs));
94 }
95
96
97 patient_write(buf->file_fd, buf->mem + i_subbuf * buf->subbuf_size, valid_length);
98
99 /* pad with empty bytes */
100 tmp = malloc(buf->subbuf_size-valid_length);
101 memset(tmp, 0, buf->subbuf_size-valid_length);
102 patient_write(buf->file_fd, tmp, buf->subbuf_size-valid_length);
103 free(tmp);
104
105 if(i_subbuf == last_subbuf % buf->n_subbufs)
106 break;
107 }
108 }
109
This page took 0.031719 seconds and 4 git commands to generate.