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>
6 * SPDX-License-Identifier: GPL-2.0-only
12 #include <common/common.hpp>
13 #include <common/utils.hpp>
14 #include <common/compat/endian.hpp>
16 #include "lttng-relayd.hpp"
19 #include "connection.hpp"
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
26 * Called with stream mutex held.
27 * Return allocated object or else NULL on error.
29 static struct relay_index
*relay_index_create(struct relay_stream
*stream
,
32 struct relay_index
*index
;
34 DBG2("Creating relay index for stream id %" PRIu64
" and seqnum %" PRIu64
,
35 stream
->stream_handle
, net_seq_num
);
37 index
= zmalloc
<relay_index
>();
39 PERROR("Relay index zmalloc");
42 if (!stream_get(stream
)) {
43 ERR("Cannot get stream");
48 index
->stream
= stream
;
50 lttng_ht_node_init_u64(&index
->index_n
, net_seq_num
);
51 pthread_mutex_init(&index
->lock
, NULL
);
52 urcu_ref_init(&index
->ref
);
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.
62 * RCU read side lock MUST be acquired.
64 static struct relay_index
*relay_index_add_unique(struct relay_stream
*stream
,
65 struct relay_index
*index
)
67 struct cds_lfht_node
*node_ptr
;
68 struct relay_index
*_index
;
70 ASSERT_RCU_READ_LOCKED();
72 DBG2("Adding relay index with stream id %" PRIu64
" and seqnum %" PRIu64
,
73 stream
->stream_handle
, index
->index_n
.key
);
75 node_ptr
= cds_lfht_add_unique(stream
->indexes_ht
->ht
,
76 stream
->indexes_ht
->hash_fct(&index
->index_n
, lttng_ht_seed
),
77 stream
->indexes_ht
->match_fct
, &index
->index_n
,
78 &index
->index_n
.node
);
79 if (node_ptr
!= &index
->index_n
.node
) {
80 _index
= caa_container_of(node_ptr
, struct relay_index
,
89 * Should be called with RCU read-side lock held.
91 static bool relay_index_get(struct relay_index
*index
)
93 ASSERT_RCU_READ_LOCKED();
95 DBG2("index get for stream id %" PRIu64
" and seqnum %" PRIu64
" refcount %d",
96 index
->stream
->stream_handle
, index
->index_n
.key
,
97 (int) index
->ref
.refcount
);
99 return urcu_ref_get_unless_zero(&index
->ref
);
103 * Get a relayd index in within the given stream, or create it if not
106 * Called with stream mutex held.
107 * Return index object or else NULL on error.
109 struct relay_index
*relay_index_get_by_id_or_create(struct relay_stream
*stream
,
110 uint64_t net_seq_num
)
112 struct lttng_ht_node_u64
*node
;
113 struct lttng_ht_iter iter
;
114 struct relay_index
*index
= NULL
;
116 DBG3("Finding index for stream id %" PRIu64
" and seq_num %" PRIu64
,
117 stream
->stream_handle
, net_seq_num
);
120 lttng_ht_lookup(stream
->indexes_ht
, &net_seq_num
, &iter
);
121 node
= lttng_ht_iter_get_node_u64(&iter
);
123 index
= lttng::utils::container_of(node
, &relay_index::index_n
);
125 struct relay_index
*oldindex
;
127 index
= relay_index_create(stream
, net_seq_num
);
129 ERR("Cannot create index for stream id %" PRIu64
" and seq_num %" PRIu64
,
130 stream
->stream_handle
, net_seq_num
);
133 oldindex
= relay_index_add_unique(stream
, index
);
135 /* Added concurrently, keep old. */
136 relay_index_put(index
);
138 if (!relay_index_get(index
)) {
142 stream
->indexes_in_flight
++;
143 index
->in_hash_table
= true;
148 DBG2("Index %sfound or created in HT for stream ID %" PRIu64
" and seqnum %" PRIu64
,
149 (index
== NULL
) ? "NOT " : "", stream
->stream_handle
, net_seq_num
);
153 int relay_index_set_file(struct relay_index
*index
,
154 struct lttng_index_file
*index_file
,
155 uint64_t data_offset
)
159 pthread_mutex_lock(&index
->lock
);
160 if (index
->index_file
) {
164 lttng_index_file_get(index_file
);
165 index
->index_file
= index_file
;
166 index
->index_data
.offset
= data_offset
;
168 pthread_mutex_unlock(&index
->lock
);
172 int relay_index_set_data(struct relay_index
*index
,
173 const struct ctf_packet_index
*data
)
177 pthread_mutex_lock(&index
->lock
);
178 if (index
->has_index_data
) {
182 /* Set everything except data_offset. */
183 index
->index_data
.packet_size
= data
->packet_size
;
184 index
->index_data
.content_size
= data
->content_size
;
185 index
->index_data
.timestamp_begin
= data
->timestamp_begin
;
186 index
->index_data
.timestamp_end
= data
->timestamp_end
;
187 index
->index_data
.events_discarded
= data
->events_discarded
;
188 index
->index_data
.stream_id
= data
->stream_id
;
189 index
->has_index_data
= true;
191 pthread_mutex_unlock(&index
->lock
);
195 static void index_destroy(struct relay_index
*index
)
200 static void index_destroy_rcu(struct rcu_head
*rcu_head
)
202 struct relay_index
*index
=
203 lttng::utils::container_of(rcu_head
, &relay_index::rcu_node
);
205 index_destroy(index
);
208 /* Stream lock must be held by the caller. */
209 static void index_release(struct urcu_ref
*ref
)
211 struct relay_index
*index
= lttng::utils::container_of(ref
, &relay_index::ref
);
212 struct relay_stream
*stream
= index
->stream
;
214 struct lttng_ht_iter iter
;
216 if (index
->index_file
) {
217 lttng_index_file_put(index
->index_file
);
218 index
->index_file
= NULL
;
220 if (index
->in_hash_table
) {
221 /* Delete index from hash table. */
222 iter
.iter
.node
= &index
->index_n
.node
;
223 ret
= lttng_ht_del(stream
->indexes_ht
, &iter
);
225 stream
->indexes_in_flight
--;
228 stream_put(index
->stream
);
229 index
->stream
= NULL
;
231 call_rcu(&index
->rcu_node
, index_destroy_rcu
);
235 * Called with stream mutex held.
237 * Stream lock must be held by the caller.
239 void relay_index_put(struct relay_index
*index
)
241 DBG2("index put for stream id %" PRIu64
" and seqnum %" PRIu64
" refcount %d",
242 index
->stream
->stream_handle
, index
->index_n
.key
,
243 (int) index
->ref
.refcount
);
245 * Ensure existence of index->lock for index unlock.
249 * Index lock ensures that concurrent test and update of stream
252 LTTNG_ASSERT(index
->ref
.refcount
!= 0);
253 urcu_ref_put(&index
->ref
, index_release
);
258 * Try to flush index to disk. Releases self-reference to index once
261 * Stream lock must be held by the caller.
262 * Return 0 on successful flush, a negative value on error, or positive
263 * value if no flush was performed.
265 int relay_index_try_flush(struct relay_index
*index
)
268 bool flushed
= false;
270 pthread_mutex_lock(&index
->lock
);
271 if (index
->flushed
) {
274 /* Check if we are ready to flush. */
275 if (!index
->has_index_data
|| !index
->index_file
) {
279 DBG2("Writing index for stream ID %" PRIu64
" and seq num %" PRIu64
,
280 index
->stream
->stream_handle
, index
->index_n
.key
);
282 index
->flushed
= true;
283 ret
= lttng_index_file_write(index
->index_file
, &index
->index_data
);
285 pthread_mutex_unlock(&index
->lock
);
288 /* Put self-ref from index now that it has been flushed. */
289 relay_index_put(index
);
295 * Close every relay index within a given stream, without flushing
298 void relay_index_close_all(struct relay_stream
*stream
)
300 struct lttng_ht_iter iter
;
301 struct relay_index
*index
;
304 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
305 index
, index_n
.node
) {
306 /* Put self-ref from index. */
307 relay_index_put(index
);
312 void relay_index_close_partial_fd(struct relay_stream
*stream
)
314 struct lttng_ht_iter iter
;
315 struct relay_index
*index
;
318 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
319 index
, index_n
.node
) {
320 if (!index
->index_file
) {
324 * Partial index has its index_file: we have only
325 * received its info from the data socket.
326 * Put self-ref from index.
328 relay_index_put(index
);
333 uint64_t relay_index_find_last(struct relay_stream
*stream
)
335 struct lttng_ht_iter iter
;
336 struct relay_index
*index
;
337 uint64_t net_seq_num
= -1ULL;
340 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
341 index
, index_n
.node
) {
342 if (net_seq_num
== -1ULL ||
343 index
->index_n
.key
> net_seq_num
) {
344 net_seq_num
= index
->index_n
.key
;
352 * Update the index file of an already existing relay_index.
353 * Offsets by 'removed_data_count' the offset field of an index.
356 int relay_index_switch_file(struct relay_index
*index
,
357 struct lttng_index_file
*new_index_file
,
358 uint64_t removed_data_count
)
363 pthread_mutex_lock(&index
->lock
);
364 if (!index
->index_file
) {
365 ERR("No index_file");
370 lttng_index_file_put(index
->index_file
);
371 lttng_index_file_get(new_index_file
);
372 index
->index_file
= new_index_file
;
373 offset
= be64toh(index
->index_data
.offset
);
374 index
->index_data
.offset
= htobe64(offset
- removed_data_count
);
377 pthread_mutex_unlock(&index
->lock
);
382 * Switch the index file of all pending indexes for a stream and update the
383 * data offset by substracting the last safe position.
384 * Stream lock must be held.
386 int relay_index_switch_all_files(struct relay_stream
*stream
)
388 struct lttng_ht_iter iter
;
389 struct relay_index
*index
;
393 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
394 index
, index_n
.node
) {
395 ret
= relay_index_switch_file(index
, stream
->index_file
,
396 stream
->pos_after_last_complete_data_index
);
407 * Set index data from the control port to a given index object.
409 int relay_index_set_control_data(struct relay_index
*index
,
410 const struct lttcomm_relayd_index
*data
,
411 unsigned int minor_version
)
413 /* The index on disk is encoded in big endian. */
414 ctf_packet_index index_data
{};
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
);
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
);
427 uint64_t unset_value
= -1ULL;
429 index
->index_data
.stream_instance_id
= htobe64(unset_value
);
430 index
->index_data
.packet_seq_num
= htobe64(unset_value
);
433 return relay_index_set_data(index
, &index_data
);