2 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include <common/common.h>
10 #include <common/utils.h>
11 #include <common/defaults.h>
13 #include "tracefile-array.h"
15 struct tracefile_array
*tracefile_array_create(size_t count
)
17 struct tracefile_array
*tfa
= NULL
;
20 tfa
= zmalloc(sizeof(*tfa
));
24 tfa
->tf
= zmalloc(sizeof(*tfa
->tf
) * count
);
29 for (i
= 0; i
< count
; i
++) {
30 tfa
->tf
[i
].seq_head
= -1ULL;
31 tfa
->tf
[i
].seq_tail
= -1ULL;
33 tfa
->seq_head
= -1ULL;
34 tfa
->seq_tail
= -1ULL;
45 void tracefile_array_destroy(struct tracefile_array
*tfa
)
54 void tracefile_array_reset(struct tracefile_array
*tfa
)
59 for (i
= 0; i
< count
; i
++) {
60 tfa
->tf
[i
].seq_head
= -1ULL;
61 tfa
->tf
[i
].seq_tail
= -1ULL;
63 tfa
->seq_head
= -1ULL;
64 tfa
->seq_tail
= -1ULL;
65 tfa
->file_head_read
= 0;
66 tfa
->file_head_write
= 0;
70 void tracefile_array_file_rotate(struct tracefile_array
*tfa
,
71 enum tracefile_rotate_type type
)
73 uint64_t *headp
, *tailp
;
76 /* Not in tracefile rotation mode. */
80 case TRACEFILE_ROTATE_READ
:
82 * Rotate read head to write head position, thus allowing
83 * reader to consume the newly rotated head file.
85 tfa
->file_head_read
= tfa
->file_head_write
;
87 case TRACEFILE_ROTATE_WRITE
:
88 /* Rotate write head to next file, pushing tail if needed. */
89 tfa
->file_head_write
= (tfa
->file_head_write
+ 1) % tfa
->count
;
90 if (tfa
->file_head_write
== tfa
->file_tail
) {
92 tfa
->file_tail
= (tfa
->file_tail
+ 1) % tfa
->count
;
94 headp
= &tfa
->tf
[tfa
->file_head_write
].seq_head
;
95 tailp
= &tfa
->tf
[tfa
->file_head_write
].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). */
112 void tracefile_array_commit_seq(struct tracefile_array
*tfa
,
113 uint64_t new_seq_head
)
115 uint64_t *headp
, *tailp
;
117 /* Increment overall head. */
118 tfa
->seq_head
= new_seq_head
;
119 /* If we are committing our first index overall, set tail to head. */
120 if (tfa
->seq_tail
== -1ULL) {
121 tfa
->seq_tail
= new_seq_head
;
124 /* Not in tracefile rotation mode. */
127 headp
= &tfa
->tf
[tfa
->file_head_write
].seq_head
;
128 tailp
= &tfa
->tf
[tfa
->file_head_write
].seq_tail
;
129 /* Update head tracefile seq_head. */
130 *headp
= tfa
->seq_head
;
132 * If we are committing our first index in this packet, set tail
133 * to this index seq count.
135 if (*tailp
== -1ULL) {
136 *tailp
= tfa
->seq_head
;
140 uint64_t tracefile_array_get_read_file_index_head(struct tracefile_array
*tfa
)
142 return tfa
->file_head_read
;
145 uint64_t tracefile_array_get_seq_head(struct tracefile_array
*tfa
)
147 return tfa
->seq_head
;
150 uint64_t tracefile_array_get_file_index_tail(struct tracefile_array
*tfa
)
152 return tfa
->file_tail
;
155 uint64_t tracefile_array_get_seq_tail(struct tracefile_array
*tfa
)
157 return tfa
->seq_tail
;
160 bool tracefile_array_seq_in_file(struct tracefile_array
*tfa
,
161 uint64_t file_index
, uint64_t seq
)
165 * Not in tracefile rotation mode; we are guaranteed to have the
166 * index in this file.
170 LTTNG_ASSERT(file_index
< tfa
->count
);
174 if (seq
>= tfa
->tf
[file_index
].seq_tail
175 && seq
<= tfa
->tf
[file_index
].seq_head
) {