2 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
10 #include <common/common.h>
11 #include <common/utils.h>
12 #include <common/defaults.h>
14 #include "tracefile-array.h"
16 struct tracefile_array
*tracefile_array_create(size_t count
)
18 struct tracefile_array
*tfa
= NULL
;
21 tfa
= zmalloc(sizeof(*tfa
));
25 tfa
->tf
= zmalloc(sizeof(*tfa
->tf
) * count
);
30 for (i
= 0; i
< count
; i
++) {
31 tfa
->tf
[i
].seq_head
= -1ULL;
32 tfa
->tf
[i
].seq_tail
= -1ULL;
34 tfa
->seq_head
= -1ULL;
35 tfa
->seq_tail
= -1ULL;
46 void tracefile_array_destroy(struct tracefile_array
*tfa
)
55 void tracefile_array_reset(struct tracefile_array
*tfa
)
60 for (i
= 0; i
< count
; i
++) {
61 tfa
->tf
[i
].seq_head
= -1ULL;
62 tfa
->tf
[i
].seq_tail
= -1ULL;
64 tfa
->seq_head
= -1ULL;
65 tfa
->seq_tail
= -1ULL;
66 tfa
->file_head_read
= 0;
67 tfa
->file_head_write
= 0;
71 void tracefile_array_file_rotate(struct tracefile_array
*tfa
,
72 enum tracefile_rotate_type type
)
74 uint64_t *headp
, *tailp
;
77 /* Not in tracefile rotation mode. */
81 case TRACEFILE_ROTATE_READ
:
83 * Rotate read head to write head position, thus allowing
84 * reader to consume the newly rotated head file.
86 tfa
->file_head_read
= tfa
->file_head_write
;
88 case TRACEFILE_ROTATE_WRITE
:
89 /* Rotate write head to next file, pushing tail if needed. */
90 tfa
->file_head_write
= (tfa
->file_head_write
+ 1) % tfa
->count
;
91 if (tfa
->file_head_write
== tfa
->file_tail
) {
93 tfa
->file_tail
= (tfa
->file_tail
+ 1) % tfa
->count
;
95 headp
= &tfa
->tf
[tfa
->file_head_write
].seq_head
;
96 tailp
= &tfa
->tf
[tfa
->file_head_write
].seq_tail
;
98 * If we overwrite a file with content, we need to push the tail
99 * to the position following the content we are overwriting.
101 if (*headp
!= -1ULL) {
102 tfa
->seq_tail
= tfa
->tf
[tfa
->file_tail
].seq_tail
;
104 /* Reset this file head/tail (overwrite). */
113 void tracefile_array_commit_seq(struct tracefile_array
*tfa
,
114 uint64_t new_seq_head
)
116 uint64_t *headp
, *tailp
;
118 /* Increment overall head. */
119 tfa
->seq_head
= new_seq_head
;
120 /* If we are committing our first index overall, set tail to head. */
121 if (tfa
->seq_tail
== -1ULL) {
122 tfa
->seq_tail
= new_seq_head
;
125 /* Not in tracefile rotation mode. */
128 headp
= &tfa
->tf
[tfa
->file_head_write
].seq_head
;
129 tailp
= &tfa
->tf
[tfa
->file_head_write
].seq_tail
;
130 /* Update head tracefile seq_head. */
131 *headp
= tfa
->seq_head
;
133 * If we are committing our first index in this packet, set tail
134 * to this index seq count.
136 if (*tailp
== -1ULL) {
137 *tailp
= tfa
->seq_head
;
141 uint64_t tracefile_array_get_read_file_index_head(struct tracefile_array
*tfa
)
143 return tfa
->file_head_read
;
146 uint64_t tracefile_array_get_seq_head(struct tracefile_array
*tfa
)
148 return tfa
->seq_head
;
151 uint64_t tracefile_array_get_file_index_tail(struct tracefile_array
*tfa
)
153 return tfa
->file_tail
;
156 uint64_t tracefile_array_get_seq_tail(struct tracefile_array
*tfa
)
158 return tfa
->seq_tail
;
161 bool tracefile_array_seq_in_file(struct tracefile_array
*tfa
,
162 uint64_t file_index
, uint64_t seq
)
166 * Not in tracefile rotation mode; we are guaranteed to have the
167 * index in this file.
171 assert(file_index
< tfa
->count
);
175 if (seq
>= tfa
->tf
[file_index
].seq_tail
176 && seq
<= tfa
->tf
[file_index
].seq_head
) {