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 | ||
53107b8f PMF |
18 | #include <assert.h> |
19 | ||
0b0cd937 PMF |
20 | #include "tracer.h" |
21 | #include "ustd.h" | |
6af64c43 | 22 | #include "usterr.h" |
0b0cd937 | 23 | |
d748a7de | 24 | /* This truncates to an offset in the buffer. */ |
0b0cd937 PMF |
25 | #define USTD_BUFFER_TRUNC(offset, bufinfo) \ |
26 | ((offset) & (~(((bufinfo)->subbuf_size*(bufinfo)->n_subbufs)-1))) | |
27 | ||
28 | void finish_consuming_dead_subbuffer(struct buffer_info *buf) | |
29 | { | |
30 | struct ltt_channel_buf_struct *ltt_buf = buf->bufstruct_mem; | |
31 | ||
32 | long write_offset = local_read(<t_buf->offset); | |
33 | long consumed_offset = atomic_long_read(<t_buf->consumed); | |
34 | ||
35 | long i_subbuf; | |
36 | ||
37 | DBG("processing died buffer"); | |
38 | DBG("consumed offset is %ld", consumed_offset); | |
39 | DBG("write offset is %ld", write_offset); | |
40 | ||
d748a7de PMF |
41 | /* First subbuf that we need to consume now. It is not modulo'd. |
42 | * Consumed_offset is the next byte to consume. */ | |
53107b8f | 43 | long first_subbuf = consumed_offset / buf->subbuf_size; |
d748a7de PMF |
44 | /* Last subbuf that we need to consume now. It is not modulo'd. |
45 | * Write_offset is the next place to write so write_offset-1 is the | |
46 | * last place written. */ | |
47 | long last_subbuf = (write_offset - 1) / buf->subbuf_size; | |
0b0cd937 | 48 | |
211c34d2 PMF |
49 | DBG("first_subbuf=%ld", first_subbuf); |
50 | DBG("last_subbuf=%ld", last_subbuf); | |
d748a7de PMF |
51 | |
52 | if(last_subbuf - first_subbuf >= buf->n_subbufs) { | |
0b0cd937 PMF |
53 | DBG("an overflow has occurred, nothing can be recovered"); |
54 | return; | |
55 | } | |
56 | ||
d748a7de | 57 | /* Iterate on subbuffers to recover. */ |
0b0cd937 | 58 | for(i_subbuf=first_subbuf; ; i_subbuf++, i_subbuf %= buf->n_subbufs) { |
53107b8f | 59 | void *tmp; |
d748a7de PMF |
60 | /* commit_seq is the offset in the buffer of the end of the last sequential commit. |
61 | * Bytes beyond this limit cannot be recovered. This is a free-running counter. */ | |
53107b8f | 62 | long commit_seq = local_read(<t_buf->commit_seq[i_subbuf]); |
0b0cd937 PMF |
63 | |
64 | unsigned long valid_length = buf->subbuf_size; | |
65 | long n_subbufs_order = get_count_order(buf->n_subbufs); | |
53107b8f PMF |
66 | long commit_seq_mask = (~0UL >> n_subbufs_order); |
67 | ||
d748a7de PMF |
68 | struct ltt_subbuffer_header *header = (struct ltt_subbuffer_header *)((char *)buf->mem+i_subbuf*buf->subbuf_size); |
69 | ||
70 | if((commit_seq & commit_seq_mask) == 0) { | |
71 | /* There is nothing to do. */ | |
72 | /* FIXME: is this needed? */ | |
53107b8f | 73 | break; |
d748a7de | 74 | } |
0b0cd937 | 75 | |
d748a7de PMF |
76 | /* Check if subbuf was fully written. This is from Mathieu's algorithm/paper. */ |
77 | if (((commit_seq - buf->subbuf_size) & commit_seq_mask) | |
0b0cd937 | 78 | - (USTD_BUFFER_TRUNC(consumed_offset, buf) >> n_subbufs_order) |
d748a7de PMF |
79 | == 0) { |
80 | /* If it was, we only check the lost_size. This is the lost padding at the end of | |
81 | * the subbuffer. */ | |
53107b8f | 82 | valid_length = (unsigned long)buf->subbuf_size - header->lost_size; |
0b0cd937 | 83 | } |
53107b8f | 84 | else { |
d748a7de PMF |
85 | /* If the subbuffer was not fully written, then we don't check lost_size because |
86 | * it hasn't been written yet. Instead we check commit_seq and use it to choose | |
87 | * a value for lost_size. The viewer will need this value when parsing. | |
88 | */ | |
53107b8f | 89 | |
d748a7de | 90 | valid_length = commit_seq & (buf->subbuf_size-1); |
53107b8f | 91 | header->lost_size = buf->subbuf_size-valid_length; |
d748a7de | 92 | assert(i_subbuf == (last_subbuf % buf->n_subbufs)); |
53107b8f PMF |
93 | } |
94 | ||
d748a7de | 95 | |
53107b8f | 96 | patient_write(buf->file_fd, buf->mem + i_subbuf * buf->subbuf_size, valid_length); |
0b0cd937 | 97 | |
53107b8f PMF |
98 | /* pad with empty bytes */ |
99 | tmp = malloc(buf->subbuf_size-valid_length); | |
100 | memset(tmp, 0, buf->subbuf_size-valid_length); | |
101 | patient_write(buf->file_fd, tmp, buf->subbuf_size-valid_length); | |
102 | free(tmp); | |
0b0cd937 | 103 | |
d748a7de | 104 | if(i_subbuf == last_subbuf % buf->n_subbufs) |
0b0cd937 PMF |
105 | break; |
106 | } | |
107 | } | |
108 |