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