clang-tidy: add Chrome-inspired checks
[lttng-tools.git] / src / bin / lttng-relayd / index.cpp
CommitLineData
1c20f0e2 1/*
ab5be9fa
MJ
2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
1c20f0e2 5 *
ab5be9fa 6 * SPDX-License-Identifier: GPL-2.0-only
1c20f0e2 7 *
1c20f0e2
JD
8 */
9
6c1c0768 10#define _LGPL_SOURCE
1c20f0e2 11
28ab034a
JG
12#include "connection.hpp"
13#include "index.hpp"
c9e313bc
SM
14#include "lttng-relayd.hpp"
15#include "stream.hpp"
28ab034a
JG
16
17#include <common/common.hpp>
18#include <common/compat/endian.hpp>
19#include <common/utils.hpp>
1c20f0e2
JD
20
21/*
7591bab1
MD
22 * Allocate a new relay index object. Pass the stream in which it is
23 * contained as parameter. The sequence number will be used as the hash
24 * table key.
25 *
26 * Called with stream mutex held.
27 * Return allocated object or else NULL on error.
1c20f0e2 28 */
28ab034a 29static struct relay_index *relay_index_create(struct relay_stream *stream, uint64_t net_seq_num)
1c20f0e2 30{
7591bab1 31 struct relay_index *index;
1c20f0e2 32
7591bab1 33 DBG2("Creating relay index for stream id %" PRIu64 " and seqnum %" PRIu64,
28ab034a
JG
34 stream->stream_handle,
35 net_seq_num);
1c20f0e2 36
64803277 37 index = zmalloc<relay_index>();
7591bab1
MD
38 if (!index) {
39 PERROR("Relay index zmalloc");
40 goto end;
41 }
42 if (!stream_get(stream)) {
43 ERR("Cannot get stream");
44 free(index);
cd9adb8b 45 index = nullptr;
7591bab1 46 goto end;
1c20f0e2 47 }
7591bab1 48 index->stream = stream;
1c20f0e2 49
7591bab1 50 lttng_ht_node_init_u64(&index->index_n, net_seq_num);
cd9adb8b 51 pthread_mutex_init(&index->lock, nullptr);
7591bab1
MD
52 urcu_ref_init(&index->ref);
53
54end:
55 return index;
1c20f0e2
JD
56}
57
58/*
7591bab1
MD
59 * Add unique relay index to the given hash table. In case of a collision, the
60 * already existing object is put in the given _index variable.
1c20f0e2 61 *
7591bab1 62 * RCU read side lock MUST be acquired.
1c20f0e2 63 */
7591bab1 64static struct relay_index *relay_index_add_unique(struct relay_stream *stream,
28ab034a 65 struct relay_index *index)
1c20f0e2 66{
7591bab1
MD
67 struct cds_lfht_node *node_ptr;
68 struct relay_index *_index;
1c20f0e2 69
48b7cdc2
FD
70 ASSERT_RCU_READ_LOCKED();
71
7591bab1 72 DBG2("Adding relay index with stream id %" PRIu64 " and seqnum %" PRIu64,
28ab034a
JG
73 stream->stream_handle,
74 index->index_n.key);
1c20f0e2 75
7591bab1 76 node_ptr = cds_lfht_add_unique(stream->indexes_ht->ht,
28ab034a
JG
77 stream->indexes_ht->hash_fct(&index->index_n, lttng_ht_seed),
78 stream->indexes_ht->match_fct,
79 &index->index_n,
80 &index->index_n.node);
7591bab1 81 if (node_ptr != &index->index_n.node) {
28ab034a 82 _index = caa_container_of(node_ptr, struct relay_index, index_n.node);
7591bab1 83 } else {
cd9adb8b 84 _index = nullptr;
1c20f0e2 85 }
7591bab1
MD
86 return _index;
87}
88
89/*
90 * Should be called with RCU read-side lock held.
91 */
92static bool relay_index_get(struct relay_index *index)
93{
48b7cdc2
FD
94 ASSERT_RCU_READ_LOCKED();
95
7591bab1 96 DBG2("index get for stream id %" PRIu64 " and seqnum %" PRIu64 " refcount %d",
28ab034a
JG
97 index->stream->stream_handle,
98 index->index_n.key,
99 (int) index->ref.refcount);
1c20f0e2 100
ce4d4083 101 return urcu_ref_get_unless_zero(&index->ref);
1c20f0e2
JD
102}
103
104/*
7591bab1
MD
105 * Get a relayd index in within the given stream, or create it if not
106 * present.
1c20f0e2 107 *
7591bab1 108 * Called with stream mutex held.
1c20f0e2
JD
109 * Return index object or else NULL on error.
110 */
7591bab1 111struct relay_index *relay_index_get_by_id_or_create(struct relay_stream *stream,
28ab034a 112 uint64_t net_seq_num)
1c20f0e2 113{
7591bab1 114 struct lttng_ht_node_u64 *node;
1c20f0e2 115 struct lttng_ht_iter iter;
cd9adb8b 116 struct relay_index *index = nullptr;
1c20f0e2 117
1c20f0e2 118 DBG3("Finding index for stream id %" PRIu64 " and seq_num %" PRIu64,
28ab034a
JG
119 stream->stream_handle,
120 net_seq_num);
1c20f0e2 121
7591bab1
MD
122 rcu_read_lock();
123 lttng_ht_lookup(stream->indexes_ht, &net_seq_num, &iter);
124 node = lttng_ht_iter_get_node_u64(&iter);
125 if (node) {
0114db0e 126 index = lttng::utils::container_of(node, &relay_index::index_n);
7591bab1
MD
127 } else {
128 struct relay_index *oldindex;
129
130 index = relay_index_create(stream, net_seq_num);
131 if (!index) {
132 ERR("Cannot create index for stream id %" PRIu64 " and seq_num %" PRIu64,
28ab034a
JG
133 stream->stream_handle,
134 net_seq_num);
7591bab1
MD
135 goto end;
136 }
137 oldindex = relay_index_add_unique(stream, index);
138 if (oldindex) {
139 /* Added concurrently, keep old. */
140 relay_index_put(index);
141 index = oldindex;
142 if (!relay_index_get(index)) {
cd9adb8b 143 index = nullptr;
7591bab1
MD
144 }
145 } else {
146 stream->indexes_in_flight++;
147 index->in_hash_table = true;
148 }
1c20f0e2 149 }
1c20f0e2 150end:
7591bab1
MD
151 rcu_read_unlock();
152 DBG2("Index %sfound or created in HT for stream ID %" PRIu64 " and seqnum %" PRIu64,
28ab034a
JG
153 (index == NULL) ? "NOT " : "",
154 stream->stream_handle,
155 net_seq_num);
1c20f0e2
JD
156 return index;
157}
158
f8f3885c 159int relay_index_set_file(struct relay_index *index,
28ab034a
JG
160 struct lttng_index_file *index_file,
161 uint64_t data_offset)
1c20f0e2 162{
7591bab1 163 int ret = 0;
1c20f0e2 164
7591bab1 165 pthread_mutex_lock(&index->lock);
f8f3885c 166 if (index->index_file) {
7591bab1
MD
167 ret = -1;
168 goto end;
169 }
f8f3885c
MD
170 lttng_index_file_get(index_file);
171 index->index_file = index_file;
7591bab1
MD
172 index->index_data.offset = data_offset;
173end:
174 pthread_mutex_unlock(&index->lock);
175 return ret;
176}
1c20f0e2 177
28ab034a 178int relay_index_set_data(struct relay_index *index, const struct ctf_packet_index *data)
7591bab1
MD
179{
180 int ret = 0;
1c20f0e2 181
7591bab1
MD
182 pthread_mutex_lock(&index->lock);
183 if (index->has_index_data) {
184 ret = -1;
185 goto end;
1c20f0e2 186 }
7591bab1
MD
187 /* Set everything except data_offset. */
188 index->index_data.packet_size = data->packet_size;
189 index->index_data.content_size = data->content_size;
190 index->index_data.timestamp_begin = data->timestamp_begin;
191 index->index_data.timestamp_end = data->timestamp_end;
192 index->index_data.events_discarded = data->events_discarded;
193 index->index_data.stream_id = data->stream_id;
194 index->has_index_data = true;
195end:
196 pthread_mutex_unlock(&index->lock);
197 return ret;
1c20f0e2
JD
198}
199
7591bab1 200static void index_destroy(struct relay_index *index)
1c20f0e2 201{
7591bab1
MD
202 free(index);
203}
1c20f0e2 204
7591bab1
MD
205static void index_destroy_rcu(struct rcu_head *rcu_head)
206{
28ab034a 207 struct relay_index *index = lttng::utils::container_of(rcu_head, &relay_index::rcu_node);
1c20f0e2 208
7591bab1 209 index_destroy(index);
1c20f0e2
JD
210}
211
7591bab1
MD
212/* Stream lock must be held by the caller. */
213static void index_release(struct urcu_ref *ref)
1c20f0e2 214{
0114db0e 215 struct relay_index *index = lttng::utils::container_of(ref, &relay_index::ref);
7591bab1
MD
216 struct relay_stream *stream = index->stream;
217 int ret;
218 struct lttng_ht_iter iter;
219
f8f3885c
MD
220 if (index->index_file) {
221 lttng_index_file_put(index->index_file);
cd9adb8b 222 index->index_file = nullptr;
7591bab1
MD
223 }
224 if (index->in_hash_table) {
225 /* Delete index from hash table. */
226 iter.iter.node = &index->index_n.node;
227 ret = lttng_ht_del(stream->indexes_ht, &iter);
a0377dfe 228 LTTNG_ASSERT(!ret);
7591bab1
MD
229 stream->indexes_in_flight--;
230 }
231
232 stream_put(index->stream);
cd9adb8b 233 index->stream = nullptr;
7591bab1
MD
234
235 call_rcu(&index->rcu_node, index_destroy_rcu);
1c20f0e2
JD
236}
237
238/*
7591bab1
MD
239 * Called with stream mutex held.
240 *
241 * Stream lock must be held by the caller.
1c20f0e2 242 */
7591bab1 243void relay_index_put(struct relay_index *index)
1c20f0e2 244{
7591bab1 245 DBG2("index put for stream id %" PRIu64 " and seqnum %" PRIu64 " refcount %d",
28ab034a
JG
246 index->stream->stream_handle,
247 index->index_n.key,
248 (int) index->ref.refcount);
7591bab1 249 /*
0f1b1d25 250 * Ensure existence of index->lock for index unlock.
7591bab1
MD
251 */
252 rcu_read_lock();
253 /*
254 * Index lock ensures that concurrent test and update of stream
255 * ref is atomic.
256 */
a0377dfe 257 LTTNG_ASSERT(index->ref.refcount != 0);
7591bab1 258 urcu_ref_put(&index->ref, index_release);
7591bab1 259 rcu_read_unlock();
1c20f0e2
JD
260}
261
262/*
7591bab1
MD
263 * Try to flush index to disk. Releases self-reference to index once
264 * flush succeeds.
1c20f0e2 265 *
7591bab1
MD
266 * Stream lock must be held by the caller.
267 * Return 0 on successful flush, a negative value on error, or positive
268 * value if no flush was performed.
1c20f0e2 269 */
7591bab1 270int relay_index_try_flush(struct relay_index *index)
1c20f0e2 271{
7591bab1
MD
272 int ret = 1;
273 bool flushed = false;
1c20f0e2 274
7591bab1
MD
275 pthread_mutex_lock(&index->lock);
276 if (index->flushed) {
277 goto skip;
278 }
279 /* Check if we are ready to flush. */
f8f3885c 280 if (!index->has_index_data || !index->index_file) {
7591bab1
MD
281 goto skip;
282 }
8bb66c3c
JG
283
284 DBG2("Writing index for stream ID %" PRIu64 " and seq num %" PRIu64,
28ab034a
JG
285 index->stream->stream_handle,
286 index->index_n.key);
7591bab1
MD
287 flushed = true;
288 index->flushed = true;
f8f3885c 289 ret = lttng_index_file_write(index->index_file, &index->index_data);
7591bab1
MD
290skip:
291 pthread_mutex_unlock(&index->lock);
1c20f0e2 292
7591bab1
MD
293 if (flushed) {
294 /* Put self-ref from index now that it has been flushed. */
295 relay_index_put(index);
296 }
297 return ret;
1c20f0e2
JD
298}
299
300/*
7591bab1
MD
301 * Close every relay index within a given stream, without flushing
302 * them.
1c20f0e2 303 */
7591bab1 304void relay_index_close_all(struct relay_stream *stream)
1c20f0e2
JD
305{
306 struct lttng_ht_iter iter;
307 struct relay_index *index;
308
1c20f0e2 309 rcu_read_lock();
28ab034a 310 cds_lfht_for_each_entry (stream->indexes_ht->ht, &iter.iter, index, index_n.node) {
7591bab1
MD
311 /* Put self-ref from index. */
312 relay_index_put(index);
1c20f0e2
JD
313 }
314 rcu_read_unlock();
315}
3d07a857
MD
316
317void relay_index_close_partial_fd(struct relay_stream *stream)
318{
319 struct lttng_ht_iter iter;
320 struct relay_index *index;
321
322 rcu_read_lock();
28ab034a 323 cds_lfht_for_each_entry (stream->indexes_ht->ht, &iter.iter, index, index_n.node) {
f8f3885c 324 if (!index->index_file) {
3d07a857
MD
325 continue;
326 }
327 /*
f8f3885c 328 * Partial index has its index_file: we have only
3d07a857
MD
329 * received its info from the data socket.
330 * Put self-ref from index.
331 */
332 relay_index_put(index);
333 }
334 rcu_read_unlock();
335}
336
337uint64_t relay_index_find_last(struct relay_stream *stream)
338{
339 struct lttng_ht_iter iter;
340 struct relay_index *index;
341 uint64_t net_seq_num = -1ULL;
342
343 rcu_read_lock();
28ab034a
JG
344 cds_lfht_for_each_entry (stream->indexes_ht->ht, &iter.iter, index, index_n.node) {
345 if (net_seq_num == -1ULL || index->index_n.key > net_seq_num) {
3d07a857
MD
346 net_seq_num = index->index_n.key;
347 }
348 }
349 rcu_read_unlock();
350 return net_seq_num;
351}
d3ecc550
JD
352
353/*
354 * Update the index file of an already existing relay_index.
355 * Offsets by 'removed_data_count' the offset field of an index.
356 */
28ab034a
JG
357static int relay_index_switch_file(struct relay_index *index,
358 struct lttng_index_file *new_index_file,
359 uint64_t removed_data_count)
d3ecc550
JD
360{
361 int ret = 0;
362 uint64_t offset;
363
364 pthread_mutex_lock(&index->lock);
365 if (!index->index_file) {
366 ERR("No index_file");
367 ret = 0;
368 goto end;
369 }
370
371 lttng_index_file_put(index->index_file);
372 lttng_index_file_get(new_index_file);
373 index->index_file = new_index_file;
374 offset = be64toh(index->index_data.offset);
375 index->index_data.offset = htobe64(offset - removed_data_count);
376
377end:
378 pthread_mutex_unlock(&index->lock);
379 return ret;
380}
381
382/*
383 * Switch the index file of all pending indexes for a stream and update the
384 * data offset by substracting the last safe position.
385 * Stream lock must be held.
386 */
387int relay_index_switch_all_files(struct relay_stream *stream)
388{
389 struct lttng_ht_iter iter;
390 struct relay_index *index;
391 int ret = 0;
392
393 rcu_read_lock();
28ab034a
JG
394 cds_lfht_for_each_entry (stream->indexes_ht->ht, &iter.iter, index, index_n.node) {
395 ret = relay_index_switch_file(
396 index, stream->index_file, stream->pos_after_last_complete_data_index);
d3ecc550
JD
397 if (ret) {
398 goto end;
399 }
400 }
401end:
402 rcu_read_unlock();
403 return ret;
404}
c35f9726
JG
405
406/*
407 * Set index data from the control port to a given index object.
408 */
409int relay_index_set_control_data(struct relay_index *index,
28ab034a
JG
410 const struct lttcomm_relayd_index *data,
411 unsigned int minor_version)
c35f9726
JG
412{
413 /* The index on disk is encoded in big endian. */
28ab034a 414 ctf_packet_index index_data{};
ac497a37
SM
415
416 index_data.packet_size = htobe64(data->packet_size);
417 index_data.content_size = htobe64(data->content_size);
418 index_data.timestamp_begin = htobe64(data->timestamp_begin);
419 index_data.timestamp_end = htobe64(data->timestamp_end);
420 index_data.events_discarded = htobe64(data->events_discarded);
421 index_data.stream_id = htobe64(data->stream_id);
c35f9726
JG
422
423 if (minor_version >= 8) {
424 index->index_data.stream_instance_id = htobe64(data->stream_instance_id);
425 index->index_data.packet_seq_num = htobe64(data->packet_seq_num);
0f83d1cc
MD
426 } else {
427 uint64_t unset_value = -1ULL;
428
429 index->index_data.stream_instance_id = htobe64(unset_value);
430 index->index_data.packet_seq_num = htobe64(unset_value);
c35f9726
JG
431 }
432
433 return relay_index_set_data(index, &index_data);
434}
This page took 0.079454 seconds and 4 git commands to generate.