Commit | Line | Data |
---|---|---|
a44ca2ca MD |
1 | /* |
2 | * Copyright (C) 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms of the GNU General Public License, version 2 only, as | |
6 | * published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
15 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
a44ca2ca MD |
18 | #define _LGPL_SOURCE |
19 | #include <assert.h> | |
20 | #include <common/common.h> | |
21 | #include <common/utils.h> | |
22 | #include <common/defaults.h> | |
23 | ||
24 | #include "tracefile-array.h" | |
25 | ||
26 | struct tracefile_array *tracefile_array_create(size_t count) | |
27 | { | |
28 | struct tracefile_array *tfa = NULL; | |
29 | int i; | |
30 | ||
31 | tfa = zmalloc(sizeof(*tfa)); | |
32 | if (!tfa) { | |
33 | goto error; | |
34 | } | |
35 | tfa->tf = zmalloc(sizeof(*tfa->tf) * count); | |
36 | if (!tfa->tf) { | |
37 | goto error; | |
38 | } | |
39 | tfa->count = count; | |
40 | for (i = 0; i < count; i++) { | |
41 | tfa->tf[i].seq_head = -1ULL; | |
42 | tfa->tf[i].seq_tail = -1ULL; | |
43 | } | |
44 | tfa->seq_head = -1ULL; | |
45 | tfa->seq_tail = -1ULL; | |
46 | return tfa; | |
47 | ||
48 | error: | |
49 | if (tfa) { | |
50 | free(tfa->tf); | |
51 | } | |
52 | free(tfa); | |
53 | return NULL; | |
54 | } | |
55 | ||
56 | void tracefile_array_destroy(struct tracefile_array *tfa) | |
57 | { | |
58 | if (!tfa) { | |
59 | return; | |
60 | } | |
61 | free(tfa->tf); | |
62 | free(tfa); | |
63 | } | |
64 | ||
82b4cfcc MD |
65 | void tracefile_array_reset(struct tracefile_array *tfa) |
66 | { | |
67 | size_t count, i; | |
68 | ||
69 | count = tfa->count; | |
70 | for (i = 0; i < count; i++) { | |
71 | tfa->tf[i].seq_head = -1ULL; | |
72 | tfa->tf[i].seq_tail = -1ULL; | |
73 | } | |
74 | tfa->seq_head = -1ULL; | |
75 | tfa->seq_tail = -1ULL; | |
76 | tfa->file_head = 0; | |
77 | tfa->file_tail = 0; | |
78 | } | |
79 | ||
a44ca2ca MD |
80 | void tracefile_array_file_rotate(struct tracefile_array *tfa) |
81 | { | |
82 | uint64_t *headp, *tailp; | |
83 | ||
84 | if (!tfa->count) { | |
85 | /* Not in tracefile rotation mode. */ | |
86 | return; | |
87 | } | |
88 | /* Rotate to next file. */ | |
89 | tfa->file_head = (tfa->file_head + 1) % tfa->count; | |
90 | if (tfa->file_head == tfa->file_tail) { | |
91 | /* Move tail. */ | |
92 | tfa->file_tail = (tfa->file_tail + 1) % tfa->count; | |
93 | } | |
94 | headp = &tfa->tf[tfa->file_head].seq_head; | |
95 | tailp = &tfa->tf[tfa->file_head].seq_tail; | |
96 | /* | |
97 | * If we overwrite a file with content, we need to push the tail | |
98 | * to the position following the content we are overwriting. | |
99 | */ | |
100 | if (*headp != -1ULL) { | |
101 | tfa->seq_tail = tfa->tf[tfa->file_tail].seq_tail; | |
102 | } | |
103 | /* Reset this file head/tail (overwrite). */ | |
104 | *headp = -1ULL; | |
105 | *tailp = -1ULL; | |
106 | } | |
107 | ||
82b4cfcc MD |
108 | void tracefile_array_commit_seq(struct tracefile_array *tfa, |
109 | uint64_t new_seq_head) | |
a44ca2ca MD |
110 | { |
111 | uint64_t *headp, *tailp; | |
112 | ||
113 | /* Increment overall head. */ | |
82b4cfcc MD |
114 | tfa->seq_head = new_seq_head; |
115 | /* If we are committing our first index overall, set tail to head. */ | |
a44ca2ca | 116 | if (tfa->seq_tail == -1ULL) { |
82b4cfcc | 117 | tfa->seq_tail = new_seq_head; |
a44ca2ca MD |
118 | } |
119 | if (!tfa->count) { | |
120 | /* Not in tracefile rotation mode. */ | |
121 | return; | |
122 | } | |
123 | headp = &tfa->tf[tfa->file_head].seq_head; | |
124 | tailp = &tfa->tf[tfa->file_head].seq_tail; | |
125 | /* Update head tracefile seq_head. */ | |
126 | *headp = tfa->seq_head; | |
127 | /* | |
128 | * If we are committing our first index in this packet, set tail | |
129 | * to this index seq count. | |
130 | */ | |
131 | if (*tailp == -1ULL) { | |
132 | *tailp = tfa->seq_head; | |
133 | } | |
134 | } | |
135 | ||
136 | uint64_t tracefile_array_get_file_index_head(struct tracefile_array *tfa) | |
137 | { | |
138 | return tfa->file_head; | |
139 | } | |
140 | ||
141 | uint64_t tracefile_array_get_seq_head(struct tracefile_array *tfa) | |
142 | { | |
143 | return tfa->seq_head; | |
144 | } | |
145 | ||
146 | uint64_t tracefile_array_get_file_index_tail(struct tracefile_array *tfa) | |
147 | { | |
148 | return tfa->file_tail; | |
149 | } | |
150 | ||
151 | uint64_t tracefile_array_get_seq_tail(struct tracefile_array *tfa) | |
152 | { | |
153 | return tfa->seq_tail; | |
154 | } | |
155 | ||
156 | bool tracefile_array_seq_in_file(struct tracefile_array *tfa, | |
157 | uint64_t file_index, uint64_t seq) | |
158 | { | |
159 | if (!tfa->count) { | |
160 | /* | |
161 | * Not in tracefile rotation mode; we are guaranteed to have the | |
162 | * index in this file. | |
163 | */ | |
164 | return true; | |
165 | } | |
a44ca2ca MD |
166 | if (seq == -1ULL) { |
167 | return false; | |
168 | } | |
169 | if (seq >= tfa->tf[file_index].seq_tail | |
170 | && seq <= tfa->tf[file_index].seq_head) { | |
171 | return true; | |
172 | } else { | |
173 | return false; | |
174 | } | |
175 | } |