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