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.
21 #include <common/common.h>
22 #include <common/utils.h>
23 #include <common/defaults.h>
25 #include "tracefile-array.h"
27 struct tracefile_array
*tracefile_array_create(size_t count
)
29 struct tracefile_array
*tfa
= NULL
;
32 tfa
= zmalloc(sizeof(*tfa
));
36 tfa
->tf
= zmalloc(sizeof(*tfa
->tf
) * count
);
41 for (i
= 0; i
< count
; i
++) {
42 tfa
->tf
[i
].seq_head
= -1ULL;
43 tfa
->tf
[i
].seq_tail
= -1ULL;
45 tfa
->seq_head
= -1ULL;
46 tfa
->seq_tail
= -1ULL;
57 void tracefile_array_destroy(struct tracefile_array
*tfa
)
66 void tracefile_array_file_rotate(struct tracefile_array
*tfa
)
68 uint64_t *headp
, *tailp
;
71 /* Not in tracefile rotation mode. */
74 /* Rotate to next file. */
75 tfa
->file_head
= (tfa
->file_head
+ 1) % tfa
->count
;
76 if (tfa
->file_head
== tfa
->file_tail
) {
78 tfa
->file_tail
= (tfa
->file_tail
+ 1) % tfa
->count
;
80 headp
= &tfa
->tf
[tfa
->file_head
].seq_head
;
81 tailp
= &tfa
->tf
[tfa
->file_head
].seq_tail
;
83 * If we overwrite a file with content, we need to push the tail
84 * to the position following the content we are overwriting.
86 if (*headp
!= -1ULL) {
87 tfa
->seq_tail
= tfa
->tf
[tfa
->file_tail
].seq_tail
;
89 /* Reset this file head/tail (overwrite). */
94 void tracefile_array_commit_seq(struct tracefile_array
*tfa
)
96 uint64_t *headp
, *tailp
;
98 /* Increment overall head. */
100 /* If we are committing our first index overall, set tail to 0. */
101 if (tfa
->seq_tail
== -1ULL) {
105 /* Not in tracefile rotation mode. */
108 headp
= &tfa
->tf
[tfa
->file_head
].seq_head
;
109 tailp
= &tfa
->tf
[tfa
->file_head
].seq_tail
;
110 /* Update head tracefile seq_head. */
111 *headp
= tfa
->seq_head
;
113 * If we are committing our first index in this packet, set tail
114 * to this index seq count.
116 if (*tailp
== -1ULL) {
117 *tailp
= tfa
->seq_head
;
121 uint64_t tracefile_array_get_file_index_head(struct tracefile_array
*tfa
)
123 return tfa
->file_head
;
126 uint64_t tracefile_array_get_seq_head(struct tracefile_array
*tfa
)
128 return tfa
->seq_head
;
131 uint64_t tracefile_array_get_file_index_tail(struct tracefile_array
*tfa
)
133 return tfa
->file_tail
;
136 uint64_t tracefile_array_get_seq_tail(struct tracefile_array
*tfa
)
138 return tfa
->seq_tail
;
141 bool tracefile_array_seq_in_file(struct tracefile_array
*tfa
,
142 uint64_t file_index
, uint64_t seq
)
146 * Not in tracefile rotation mode; we are guaranteed to have the
147 * index in this file.
151 assert(file_index
< tfa
->count
);
155 if (seq
>= tfa
->tf
[file_index
].seq_tail
156 && seq
<= tfa
->tf
[file_index
].seq_head
) {
This page took 0.032126 seconds and 4 git commands to generate.