2 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "tracefile-array.hpp"
11 #include <common/common.hpp>
12 #include <common/defaults.hpp>
13 #include <common/utils.hpp>
15 struct tracefile_array
*tracefile_array_create(size_t count
)
17 struct tracefile_array
*tfa
= nullptr;
20 tfa
= zmalloc
<tracefile_array
>();
24 tfa
->tf
= calloc
<tracefile
>(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
, enum tracefile_rotate_type type
)
72 uint64_t *headp
, *tailp
;
75 /* Not in tracefile rotation mode. */
79 case TRACEFILE_ROTATE_READ
:
81 * Rotate read head to write head position, thus allowing
82 * reader to consume the newly rotated head file.
84 tfa
->file_head_read
= tfa
->file_head_write
;
86 case TRACEFILE_ROTATE_WRITE
:
87 /* Rotate write head to next file, pushing tail if needed. */
88 tfa
->file_head_write
= (tfa
->file_head_write
+ 1) % tfa
->count
;
89 if (tfa
->file_head_write
== tfa
->file_tail
) {
91 tfa
->file_tail
= (tfa
->file_tail
+ 1) % tfa
->count
;
93 headp
= &tfa
->tf
[tfa
->file_head_write
].seq_head
;
94 tailp
= &tfa
->tf
[tfa
->file_head_write
].seq_tail
;
96 * If we overwrite a file with content, we need to push the tail
97 * to the position following the content we are overwriting.
99 if (*headp
!= -1ULL) {
100 tfa
->seq_tail
= tfa
->tf
[tfa
->file_tail
].seq_tail
;
102 /* Reset this file head/tail (overwrite). */
111 void tracefile_array_commit_seq(struct tracefile_array
*tfa
, uint64_t new_seq_head
)
113 uint64_t *headp
, *tailp
;
115 /* Increment overall head. */
116 tfa
->seq_head
= new_seq_head
;
117 /* If we are committing our first index overall, set tail to head. */
118 if (tfa
->seq_tail
== -1ULL) {
119 tfa
->seq_tail
= new_seq_head
;
122 /* Not in tracefile rotation mode. */
125 headp
= &tfa
->tf
[tfa
->file_head_write
].seq_head
;
126 tailp
= &tfa
->tf
[tfa
->file_head_write
].seq_tail
;
127 /* Update head tracefile seq_head. */
128 *headp
= tfa
->seq_head
;
130 * If we are committing our first index in this packet, set tail
131 * to this index seq count.
133 if (*tailp
== -1ULL) {
134 *tailp
= tfa
->seq_head
;
138 uint64_t tracefile_array_get_read_file_index_head(struct tracefile_array
*tfa
)
140 return tfa
->file_head_read
;
143 uint64_t tracefile_array_get_seq_head(struct tracefile_array
*tfa
)
145 return tfa
->seq_head
;
148 uint64_t tracefile_array_get_file_index_tail(struct tracefile_array
*tfa
)
150 return tfa
->file_tail
;
153 uint64_t tracefile_array_get_seq_tail(struct tracefile_array
*tfa
)
155 return tfa
->seq_tail
;
158 bool tracefile_array_seq_in_file(struct tracefile_array
*tfa
, uint64_t file_index
, uint64_t seq
)
162 * Not in tracefile rotation mode; we are guaranteed to have the
163 * index in this file.
167 LTTNG_ASSERT(file_index
< tfa
->count
);
171 return seq
>= tfa
->tf
[file_index
].seq_tail
&& seq
<= tfa
->tf
[file_index
].seq_head
;