Commit | Line | Data |
---|---|---|
c39c72ee PMF |
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 | ||
909bc43f | 18 | #include <stdlib.h> |
53107b8f | 19 | #include <assert.h> |
02af3e60 | 20 | #include <byteswap.h> |
53107b8f | 21 | |
b73a4c47 | 22 | #include "buffers.h" |
c93858f1 | 23 | #include "tracer.h" |
0b0cd937 | 24 | #include "ustd.h" |
6af64c43 | 25 | #include "usterr.h" |
0b0cd937 | 26 | |
d748a7de | 27 | /* This truncates to an offset in the buffer. */ |
0b0cd937 PMF |
28 | #define USTD_BUFFER_TRUNC(offset, bufinfo) \ |
29 | ((offset) & (~(((bufinfo)->subbuf_size*(bufinfo)->n_subbufs)-1))) | |
30 | ||
02af3e60 PMF |
31 | #define LTT_MAGIC_NUMBER 0x00D6B7ED |
32 | #define LTT_REV_MAGIC_NUMBER 0xEDB7D600 | |
33 | ||
34 | /* Returns the size of a subbuffer size. This is the size that | |
35 | * will need to be written to disk. | |
36 | * | |
37 | * @subbuffer: pointer to the beginning of the subbuffer (the | |
38 | * beginning of its header) | |
39 | */ | |
40 | ||
41 | size_t subbuffer_data_size(void *subbuf) | |
42 | { | |
43 | struct ltt_subbuffer_header *header = subbuf; | |
44 | int reverse; | |
45 | u32 data_size; | |
46 | ||
47 | if(header->magic_number == LTT_MAGIC_NUMBER) { | |
48 | reverse = 0; | |
49 | } | |
50 | else if(header->magic_number == LTT_REV_MAGIC_NUMBER) { | |
51 | reverse = 1; | |
52 | } | |
53 | else { | |
54 | return -1; | |
55 | } | |
56 | ||
57 | data_size = header->sb_size; | |
58 | if(reverse) | |
59 | data_size = bswap_32(data_size); | |
60 | ||
61 | return data_size; | |
62 | } | |
63 | ||
64 | ||
0b0cd937 PMF |
65 | void finish_consuming_dead_subbuffer(struct buffer_info *buf) |
66 | { | |
b04051b0 PMF |
67 | int result; |
68 | ||
b5b073e2 | 69 | struct ust_buffer *ustbuf = buf->bufstruct_mem; |
0b0cd937 | 70 | |
b102c2b0 PMF |
71 | long write_offset = uatomic_read(&ustbuf->offset); |
72 | long consumed_offset = uatomic_read(&ustbuf->consumed); | |
0b0cd937 PMF |
73 | |
74 | long i_subbuf; | |
75 | ||
920bdf71 PMF |
76 | DBG("processing dead buffer (%s)", buf->name); |
77 | DBG("consumed offset is %ld (%s)", consumed_offset, buf->name); | |
78 | DBG("write offset is %ld (%s)", write_offset, buf->name); | |
0b0cd937 | 79 | |
d748a7de PMF |
80 | /* First subbuf that we need to consume now. It is not modulo'd. |
81 | * Consumed_offset is the next byte to consume. */ | |
53107b8f | 82 | long first_subbuf = consumed_offset / buf->subbuf_size; |
d748a7de PMF |
83 | /* Last subbuf that we need to consume now. It is not modulo'd. |
84 | * Write_offset is the next place to write so write_offset-1 is the | |
85 | * last place written. */ | |
86 | long last_subbuf = (write_offset - 1) / buf->subbuf_size; | |
0b0cd937 | 87 | |
211c34d2 PMF |
88 | DBG("first_subbuf=%ld", first_subbuf); |
89 | DBG("last_subbuf=%ld", last_subbuf); | |
d748a7de PMF |
90 | |
91 | if(last_subbuf - first_subbuf >= buf->n_subbufs) { | |
0b0cd937 PMF |
92 | DBG("an overflow has occurred, nothing can be recovered"); |
93 | return; | |
94 | } | |
95 | ||
d748a7de | 96 | /* Iterate on subbuffers to recover. */ |
54b74473 | 97 | for(i_subbuf = first_subbuf % buf->n_subbufs; ; i_subbuf++, i_subbuf %= buf->n_subbufs) { |
53107b8f | 98 | void *tmp; |
d748a7de PMF |
99 | /* commit_seq is the offset in the buffer of the end of the last sequential commit. |
100 | * Bytes beyond this limit cannot be recovered. This is a free-running counter. */ | |
b102c2b0 | 101 | long commit_seq = uatomic_read(&ustbuf->commit_seq[i_subbuf]); |
0b0cd937 PMF |
102 | |
103 | unsigned long valid_length = buf->subbuf_size; | |
104 | long n_subbufs_order = get_count_order(buf->n_subbufs); | |
53107b8f PMF |
105 | long commit_seq_mask = (~0UL >> n_subbufs_order); |
106 | ||
d748a7de PMF |
107 | struct ltt_subbuffer_header *header = (struct ltt_subbuffer_header *)((char *)buf->mem+i_subbuf*buf->subbuf_size); |
108 | ||
02af3e60 PMF |
109 | int pad_size; |
110 | ||
d748a7de PMF |
111 | if((commit_seq & commit_seq_mask) == 0) { |
112 | /* There is nothing to do. */ | |
113 | /* FIXME: is this needed? */ | |
53107b8f | 114 | break; |
d748a7de | 115 | } |
0b0cd937 | 116 | |
d748a7de | 117 | /* Check if subbuf was fully written. This is from Mathieu's algorithm/paper. */ |
14e859d8 PMF |
118 | /* FIXME: not sure data_size = 0xffffffff when the buffer is not full. It might |
119 | * take the value of the header size initially */ | |
d748a7de | 120 | if (((commit_seq - buf->subbuf_size) & commit_seq_mask) |
02af3e60 PMF |
121 | - (USTD_BUFFER_TRUNC(consumed_offset, buf) >> n_subbufs_order) == 0 |
122 | && header->data_size != 0xffffffff && header->sb_size != 0xffffffff) { | |
8c36d1ee PMF |
123 | /* If it was, we only check the data_size. This is the amount of valid data at |
124 | * the beginning of the subbuffer. */ | |
125 | valid_length = header->data_size; | |
1d59f7eb | 126 | DBG("writing full subbuffer (%d) with valid_length = %ld", i_subbuf, valid_length); |
0b0cd937 | 127 | } |
53107b8f | 128 | else { |
8c36d1ee | 129 | /* If the subbuffer was not fully written, then we don't check data_size because |
d748a7de | 130 | * it hasn't been written yet. Instead we check commit_seq and use it to choose |
8c36d1ee | 131 | * a value for data_size. The viewer will need this value when parsing. |
d748a7de | 132 | */ |
53107b8f | 133 | |
d748a7de | 134 | valid_length = commit_seq & (buf->subbuf_size-1); |
1d59f7eb | 135 | DBG("writing unfull subbuffer (%d) with valid_length = %ld", i_subbuf, valid_length); |
8c36d1ee | 136 | header->data_size = valid_length; |
02af3e60 | 137 | header->sb_size = PAGE_ALIGN(valid_length); |
d748a7de | 138 | assert(i_subbuf == (last_subbuf % buf->n_subbufs)); |
53107b8f PMF |
139 | } |
140 | ||
d748a7de | 141 | |
b04051b0 PMF |
142 | result = patient_write(buf->file_fd, buf->mem + i_subbuf * buf->subbuf_size, valid_length); |
143 | if(result == -1) { | |
144 | ERR("Error writing to buffer file"); | |
145 | return; | |
146 | } | |
0b0cd937 | 147 | |
53107b8f | 148 | /* pad with empty bytes */ |
02af3e60 PMF |
149 | pad_size = PAGE_ALIGN(valid_length)-valid_length; |
150 | if(pad_size) { | |
151 | tmp = malloc(pad_size); | |
152 | memset(tmp, 0, pad_size); | |
b04051b0 PMF |
153 | result = patient_write(buf->file_fd, tmp, pad_size); |
154 | if(result == -1) { | |
155 | ERR("Error writing to buffer file"); | |
156 | return; | |
157 | } | |
02af3e60 PMF |
158 | free(tmp); |
159 | } | |
0b0cd937 | 160 | |
d748a7de | 161 | if(i_subbuf == last_subbuf % buf->n_subbufs) |
0b0cd937 PMF |
162 | break; |
163 | } | |
164 | } | |
165 |