1 /* Copyright (C) 2009 Pierre-Marc Fournier
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.
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.
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
27 /* This truncates to an offset in the buffer. */
28 #define USTD_BUFFER_TRUNC(offset, bufinfo) \
29 ((offset) & (~(((bufinfo)->subbuf_size*(bufinfo)->n_subbufs)-1)))
31 #define LTT_MAGIC_NUMBER 0x00D6B7ED
32 #define LTT_REV_MAGIC_NUMBER 0xEDB7D600
34 /* Returns the size of a subbuffer size. This is the size that
35 * will need to be written to disk.
37 * @subbuffer: pointer to the beginning of the subbuffer (the
38 * beginning of its header)
41 size_t subbuffer_data_size(void *subbuf
)
43 struct ltt_subbuffer_header
*header
= subbuf
;
47 if(header
->magic_number
== LTT_MAGIC_NUMBER
) {
50 else if(header
->magic_number
== LTT_REV_MAGIC_NUMBER
) {
57 data_size
= header
->sb_size
;
59 data_size
= bswap_32(data_size
);
65 void finish_consuming_dead_subbuffer(struct buffer_info
*buf
)
67 struct ust_buffer
*ustbuf
= buf
->bufstruct_mem
;
69 long write_offset
= uatomic_read(&ustbuf
->offset
);
70 long consumed_offset
= uatomic_read(&ustbuf
->consumed
);
74 DBG("processing dead buffer (%s)", buf
->name
);
75 DBG("consumed offset is %ld (%s)", consumed_offset
, buf
->name
);
76 DBG("write offset is %ld (%s)", write_offset
, buf
->name
);
78 /* First subbuf that we need to consume now. It is not modulo'd.
79 * Consumed_offset is the next byte to consume. */
80 long first_subbuf
= consumed_offset
/ buf
->subbuf_size
;
81 /* Last subbuf that we need to consume now. It is not modulo'd.
82 * Write_offset is the next place to write so write_offset-1 is the
83 * last place written. */
84 long last_subbuf
= (write_offset
- 1) / buf
->subbuf_size
;
86 DBG("first_subbuf=%ld", first_subbuf
);
87 DBG("last_subbuf=%ld", last_subbuf
);
89 if(last_subbuf
- first_subbuf
>= buf
->n_subbufs
) {
90 DBG("an overflow has occurred, nothing can be recovered");
94 /* Iterate on subbuffers to recover. */
95 for(i_subbuf
= first_subbuf
% buf
->n_subbufs
; ; i_subbuf
++, i_subbuf
%= buf
->n_subbufs
) {
97 /* commit_seq is the offset in the buffer of the end of the last sequential commit.
98 * Bytes beyond this limit cannot be recovered. This is a free-running counter. */
99 long commit_seq
= uatomic_read(&ustbuf
->commit_seq
[i_subbuf
]);
101 unsigned long valid_length
= buf
->subbuf_size
;
102 long n_subbufs_order
= get_count_order(buf
->n_subbufs
);
103 long commit_seq_mask
= (~0UL >> n_subbufs_order
);
105 struct ltt_subbuffer_header
*header
= (struct ltt_subbuffer_header
*)((char *)buf
->mem
+i_subbuf
*buf
->subbuf_size
);
109 if((commit_seq
& commit_seq_mask
) == 0) {
110 /* There is nothing to do. */
111 /* FIXME: is this needed? */
115 /* Check if subbuf was fully written. This is from Mathieu's algorithm/paper. */
116 /* FIXME: not sure data_size = 0xffffffff when the buffer is not full. It might
117 * take the value of the header size initially */
118 if (((commit_seq
- buf
->subbuf_size
) & commit_seq_mask
)
119 - (USTD_BUFFER_TRUNC(consumed_offset
, buf
) >> n_subbufs_order
) == 0
120 && header
->data_size
!= 0xffffffff && header
->sb_size
!= 0xffffffff) {
121 /* If it was, we only check the data_size. This is the amount of valid data at
122 * the beginning of the subbuffer. */
123 valid_length
= header
->data_size
;
124 DBG("writing full subbuffer (%d) with valid_length = %ld", i_subbuf
, valid_length
);
127 /* If the subbuffer was not fully written, then we don't check data_size because
128 * it hasn't been written yet. Instead we check commit_seq and use it to choose
129 * a value for data_size. The viewer will need this value when parsing.
132 valid_length
= commit_seq
& (buf
->subbuf_size
-1);
133 DBG("writing unfull subbuffer (%d) with valid_length = %ld", i_subbuf
, valid_length
);
134 header
->data_size
= valid_length
;
135 header
->sb_size
= PAGE_ALIGN(valid_length
);
136 assert(i_subbuf
== (last_subbuf
% buf
->n_subbufs
));
140 patient_write(buf
->file_fd
, buf
->mem
+ i_subbuf
* buf
->subbuf_size
, valid_length
);
142 /* pad with empty bytes */
143 pad_size
= PAGE_ALIGN(valid_length
)-valid_length
;
145 tmp
= malloc(pad_size
);
146 memset(tmp
, 0, pad_size
);
147 patient_write(buf
->file_fd
, tmp
, pad_size
);
151 if(i_subbuf
== last_subbuf
% buf
->n_subbufs
)
This page took 0.035379 seconds and 4 git commands to generate.