Fix: file-descriptor: missing include guards
[lttng-tools.git] / src / bin / lttng-relayd / tracefile-array.cpp
CommitLineData
a44ca2ca 1/*
ab5be9fa 2 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
a44ca2ca 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
a44ca2ca 5 *
a44ca2ca
MD
6 */
7
a44ca2ca 8#define _LGPL_SOURCE
28ab034a
JG
9#include "tracefile-array.hpp"
10
c9e313bc 11#include <common/common.hpp>
c9e313bc 12#include <common/defaults.hpp>
28ab034a 13#include <common/utils.hpp>
a44ca2ca
MD
14
15struct tracefile_array *tracefile_array_create(size_t count)
16{
cd9adb8b 17 struct tracefile_array *tfa = nullptr;
a44ca2ca
MD
18 int i;
19
64803277 20 tfa = zmalloc<tracefile_array>();
a44ca2ca
MD
21 if (!tfa) {
22 goto error;
23 }
64803277 24 tfa->tf = calloc<tracefile>(count);
a44ca2ca
MD
25 if (!tfa->tf) {
26 goto error;
27 }
28 tfa->count = count;
29 for (i = 0; i < count; i++) {
30 tfa->tf[i].seq_head = -1ULL;
31 tfa->tf[i].seq_tail = -1ULL;
32 }
33 tfa->seq_head = -1ULL;
34 tfa->seq_tail = -1ULL;
35 return tfa;
36
37error:
38 if (tfa) {
39 free(tfa->tf);
40 }
41 free(tfa);
cd9adb8b 42 return nullptr;
a44ca2ca
MD
43}
44
45void tracefile_array_destroy(struct tracefile_array *tfa)
46{
47 if (!tfa) {
48 return;
49 }
50 free(tfa->tf);
51 free(tfa);
52}
53
c31a8092
MD
54void tracefile_array_reset(struct tracefile_array *tfa)
55{
56 size_t count, i;
57
58 count = tfa->count;
59 for (i = 0; i < count; i++) {
60 tfa->tf[i].seq_head = -1ULL;
61 tfa->tf[i].seq_tail = -1ULL;
62 }
63 tfa->seq_head = -1ULL;
64 tfa->seq_tail = -1ULL;
65 tfa->file_head_read = 0;
66 tfa->file_head_write = 0;
67 tfa->file_tail = 0;
68}
69
28ab034a 70void tracefile_array_file_rotate(struct tracefile_array *tfa, enum tracefile_rotate_type type)
a44ca2ca
MD
71{
72 uint64_t *headp, *tailp;
73
74 if (!tfa->count) {
75 /* Not in tracefile rotation mode. */
76 return;
77 }
78118e3b
MD
78 switch (type) {
79 case TRACEFILE_ROTATE_READ:
80 /*
81 * Rotate read head to write head position, thus allowing
82 * reader to consume the newly rotated head file.
83 */
84 tfa->file_head_read = tfa->file_head_write;
85 break;
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) {
90 /* Move tail. */
91 tfa->file_tail = (tfa->file_tail + 1) % tfa->count;
92 }
93 headp = &tfa->tf[tfa->file_head_write].seq_head;
94 tailp = &tfa->tf[tfa->file_head_write].seq_tail;
95 /*
96 * If we overwrite a file with content, we need to push the tail
97 * to the position following the content we are overwriting.
98 */
99 if (*headp != -1ULL) {
100 tfa->seq_tail = tfa->tf[tfa->file_tail].seq_tail;
101 }
102 /* Reset this file head/tail (overwrite). */
103 *headp = -1ULL;
104 *tailp = -1ULL;
105 break;
106 default:
107 abort();
a44ca2ca 108 }
a44ca2ca
MD
109}
110
28ab034a 111void tracefile_array_commit_seq(struct tracefile_array *tfa, uint64_t new_seq_head)
a44ca2ca
MD
112{
113 uint64_t *headp, *tailp;
114
115 /* Increment overall head. */
62f6e9ef
MD
116 tfa->seq_head = new_seq_head;
117 /* If we are committing our first index overall, set tail to head. */
a44ca2ca 118 if (tfa->seq_tail == -1ULL) {
62f6e9ef 119 tfa->seq_tail = new_seq_head;
a44ca2ca
MD
120 }
121 if (!tfa->count) {
122 /* Not in tracefile rotation mode. */
123 return;
124 }
78118e3b
MD
125 headp = &tfa->tf[tfa->file_head_write].seq_head;
126 tailp = &tfa->tf[tfa->file_head_write].seq_tail;
a44ca2ca
MD
127 /* Update head tracefile seq_head. */
128 *headp = tfa->seq_head;
129 /*
130 * If we are committing our first index in this packet, set tail
131 * to this index seq count.
132 */
133 if (*tailp == -1ULL) {
134 *tailp = tfa->seq_head;
135 }
136}
137
78118e3b 138uint64_t tracefile_array_get_read_file_index_head(struct tracefile_array *tfa)
a44ca2ca 139{
78118e3b 140 return tfa->file_head_read;
a44ca2ca
MD
141}
142
143uint64_t tracefile_array_get_seq_head(struct tracefile_array *tfa)
144{
145 return tfa->seq_head;
146}
147
148uint64_t tracefile_array_get_file_index_tail(struct tracefile_array *tfa)
149{
150 return tfa->file_tail;
151}
152
153uint64_t tracefile_array_get_seq_tail(struct tracefile_array *tfa)
154{
155 return tfa->seq_tail;
156}
157
28ab034a 158bool tracefile_array_seq_in_file(struct tracefile_array *tfa, uint64_t file_index, uint64_t seq)
a44ca2ca
MD
159{
160 if (!tfa->count) {
161 /*
162 * Not in tracefile rotation mode; we are guaranteed to have the
163 * index in this file.
164 */
165 return true;
166 }
a0377dfe 167 LTTNG_ASSERT(file_index < tfa->count);
a44ca2ca
MD
168 if (seq == -1ULL) {
169 return false;
170 }
e0252788 171 return seq >= tfa->tf[file_index].seq_tail && seq <= tfa->tf[file_index].seq_head;
a44ca2ca 172}
This page took 0.088744 seconds and 4 git commands to generate.