2 * Copyright (C) 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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.
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
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.
20 #include <common/common.h>
21 #include <common/utils.h>
22 #include <common/defaults.h>
24 #include "tracefile-array.h"
26 struct tracefile_array
*tracefile_array_create(size_t count
)
28 struct tracefile_array
*tfa
= NULL
;
31 tfa
= zmalloc(sizeof(*tfa
));
35 tfa
->tf
= zmalloc(sizeof(*tfa
->tf
) * count
);
40 for (i
= 0; i
< count
; i
++) {
41 tfa
->tf
[i
].seq_head
= -1ULL;
42 tfa
->tf
[i
].seq_tail
= -1ULL;
44 tfa
->seq_head
= -1ULL;
45 tfa
->seq_tail
= -1ULL;
56 void tracefile_array_destroy(struct tracefile_array
*tfa
)
65 void tracefile_array_reset(struct tracefile_array
*tfa
)
70 for (i
= 0; i
< count
; i
++) {
71 tfa
->tf
[i
].seq_head
= -1ULL;
72 tfa
->tf
[i
].seq_tail
= -1ULL;
74 tfa
->seq_head
= -1ULL;
75 tfa
->seq_tail
= -1ULL;
80 void tracefile_array_file_rotate(struct tracefile_array
*tfa
)
82 uint64_t *headp
, *tailp
;
85 /* Not in tracefile rotation mode. */
88 /* Rotate to next file. */
89 tfa
->file_head
= (tfa
->file_head
+ 1) % tfa
->count
;
90 if (tfa
->file_head
== tfa
->file_tail
) {
92 tfa
->file_tail
= (tfa
->file_tail
+ 1) % tfa
->count
;
94 headp
= &tfa
->tf
[tfa
->file_head
].seq_head
;
95 tailp
= &tfa
->tf
[tfa
->file_head
].seq_tail
;
97 * If we overwrite a file with content, we need to push the tail
98 * to the position following the content we are overwriting.
100 if (*headp
!= -1ULL) {
101 tfa
->seq_tail
= tfa
->tf
[tfa
->file_tail
].seq_tail
;
103 /* Reset this file head/tail (overwrite). */
108 void tracefile_array_commit_seq(struct tracefile_array
*tfa
,
109 uint64_t new_seq_head
)
111 uint64_t *headp
, *tailp
;
113 /* Increment overall head. */
114 tfa
->seq_head
= new_seq_head
;
115 /* If we are committing our first index overall, set tail to head. */
116 if (tfa
->seq_tail
== -1ULL) {
117 tfa
->seq_tail
= new_seq_head
;
120 /* Not in tracefile rotation mode. */
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
;
128 * If we are committing our first index in this packet, set tail
129 * to this index seq count.
131 if (*tailp
== -1ULL) {
132 *tailp
= tfa
->seq_head
;
136 uint64_t tracefile_array_get_file_index_head(struct tracefile_array
*tfa
)
138 return tfa
->file_head
;
141 uint64_t tracefile_array_get_seq_head(struct tracefile_array
*tfa
)
143 return tfa
->seq_head
;
146 uint64_t tracefile_array_get_file_index_tail(struct tracefile_array
*tfa
)
148 return tfa
->file_tail
;
151 uint64_t tracefile_array_get_seq_tail(struct tracefile_array
*tfa
)
153 return tfa
->seq_tail
;
156 bool tracefile_array_seq_in_file(struct tracefile_array
*tfa
,
157 uint64_t file_index
, uint64_t seq
)
161 * Not in tracefile rotation mode; we are guaranteed to have the
162 * index in this file.
169 if (seq
>= tfa
->tf
[file_index
].seq_tail
170 && seq
<= tfa
->tf
[file_index
].seq_head
) {
This page took 0.035685 seconds and 6 git commands to generate.