2 * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
4 * 2015 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License, version 2 only, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 51
17 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <common/common.h>
24 #include <common/utils.h>
26 #include "lttng-relayd.h"
31 * Allocate a new relay index object. Pass the stream in which it is
32 * contained as parameter. The sequence number will be used as the hash
35 * Called with stream mutex held.
36 * Return allocated object or else NULL on error.
38 static struct relay_index
*relay_index_create(struct relay_stream
*stream
,
41 struct relay_index
*index
;
43 DBG2("Creating relay index for stream id %" PRIu64
" and seqnum %" PRIu64
,
44 stream
->stream_handle
, net_seq_num
);
46 index
= zmalloc(sizeof(*index
));
48 PERROR("Relay index zmalloc");
51 if (!stream_get(stream
)) {
52 ERR("Cannot get stream");
57 index
->stream
= stream
;
59 lttng_ht_node_init_u64(&index
->index_n
, net_seq_num
);
60 pthread_mutex_init(&index
->lock
, NULL
);
61 urcu_ref_init(&index
->ref
);
68 * Add unique relay index to the given hash table. In case of a collision, the
69 * already existing object is put in the given _index variable.
71 * RCU read side lock MUST be acquired.
73 static struct relay_index
*relay_index_add_unique(struct relay_stream
*stream
,
74 struct relay_index
*index
)
76 struct cds_lfht_node
*node_ptr
;
77 struct relay_index
*_index
;
79 DBG2("Adding relay index with stream id %" PRIu64
" and seqnum %" PRIu64
,
80 stream
->stream_handle
, index
->index_n
.key
);
82 node_ptr
= cds_lfht_add_unique(stream
->indexes_ht
->ht
,
83 stream
->indexes_ht
->hash_fct(&index
->index_n
, lttng_ht_seed
),
84 stream
->indexes_ht
->match_fct
, &index
->index_n
,
85 &index
->index_n
.node
);
86 if (node_ptr
!= &index
->index_n
.node
) {
87 _index
= caa_container_of(node_ptr
, struct relay_index
,
96 * Should be called with RCU read-side lock held.
98 static bool relay_index_get(struct relay_index
*index
)
100 DBG2("index get for stream id %" PRIu64
" and seqnum %" PRIu64
" refcount %d",
101 index
->stream
->stream_handle
, index
->index_n
.key
,
102 (int) index
->ref
.refcount
);
104 return urcu_ref_get_unless_zero(&index
->ref
);
108 * Get a relayd index in within the given stream, or create it if not
111 * Called with stream mutex held.
112 * Return index object or else NULL on error.
114 struct relay_index
*relay_index_get_by_id_or_create(struct relay_stream
*stream
,
115 uint64_t net_seq_num
)
117 struct lttng_ht_node_u64
*node
;
118 struct lttng_ht_iter iter
;
119 struct relay_index
*index
= NULL
;
121 DBG3("Finding index for stream id %" PRIu64
" and seq_num %" PRIu64
,
122 stream
->stream_handle
, net_seq_num
);
125 lttng_ht_lookup(stream
->indexes_ht
, &net_seq_num
, &iter
);
126 node
= lttng_ht_iter_get_node_u64(&iter
);
128 index
= caa_container_of(node
, struct relay_index
, index_n
);
130 struct relay_index
*oldindex
;
132 index
= relay_index_create(stream
, net_seq_num
);
134 ERR("Cannot create index for stream id %" PRIu64
" and seq_num %" PRIu64
,
135 stream
->stream_handle
, net_seq_num
);
138 oldindex
= relay_index_add_unique(stream
, index
);
140 /* Added concurrently, keep old. */
141 relay_index_put(index
);
143 if (!relay_index_get(index
)) {
147 stream
->indexes_in_flight
++;
148 index
->in_hash_table
= true;
153 DBG2("Index %sfound or created in HT for stream ID %" PRIu64
" and seqnum %" PRIu64
,
154 (index
== NULL
) ? "NOT " : "", stream
->stream_handle
, net_seq_num
);
158 int relay_index_set_file(struct relay_index
*index
,
159 struct lttng_index_file
*index_file
,
160 uint64_t data_offset
)
164 pthread_mutex_lock(&index
->lock
);
165 if (index
->index_file
) {
169 lttng_index_file_get(index_file
);
170 index
->index_file
= index_file
;
171 index
->index_data
.offset
= data_offset
;
173 pthread_mutex_unlock(&index
->lock
);
177 int relay_index_set_data(struct relay_index
*index
,
178 const struct ctf_packet_index
*data
)
182 pthread_mutex_lock(&index
->lock
);
183 if (index
->has_index_data
) {
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;
196 pthread_mutex_unlock(&index
->lock
);
200 static void index_destroy(struct relay_index
*index
)
205 static void index_destroy_rcu(struct rcu_head
*rcu_head
)
207 struct relay_index
*index
=
208 caa_container_of(rcu_head
, struct relay_index
, rcu_node
);
210 index_destroy(index
);
213 /* Stream lock must be held by the caller. */
214 static void index_release(struct urcu_ref
*ref
)
216 struct relay_index
*index
= caa_container_of(ref
, struct relay_index
, ref
);
217 struct relay_stream
*stream
= index
->stream
;
219 struct lttng_ht_iter iter
;
221 if (index
->index_file
) {
222 lttng_index_file_put(index
->index_file
);
223 index
->index_file
= NULL
;
225 if (index
->in_hash_table
) {
226 /* Delete index from hash table. */
227 iter
.iter
.node
= &index
->index_n
.node
;
228 ret
= lttng_ht_del(stream
->indexes_ht
, &iter
);
230 stream
->indexes_in_flight
--;
233 stream_put(index
->stream
);
234 index
->stream
= NULL
;
236 call_rcu(&index
->rcu_node
, index_destroy_rcu
);
240 * Called with stream mutex held.
242 * Stream lock must be held by the caller.
244 void relay_index_put(struct relay_index
*index
)
246 DBG2("index put for stream id %" PRIu64
" and seqnum %" PRIu64
" refcount %d",
247 index
->stream
->stream_handle
, index
->index_n
.key
,
248 (int) index
->ref
.refcount
);
250 * Ensure existance of index->lock for index unlock.
254 * Index lock ensures that concurrent test and update of stream
257 assert(index
->ref
.refcount
!= 0);
258 urcu_ref_put(&index
->ref
, index_release
);
263 * Try to flush index to disk. Releases self-reference to index once
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.
270 int relay_index_try_flush(struct relay_index
*index
)
273 bool flushed
= false;
276 pthread_mutex_lock(&index
->lock
);
277 if (index
->flushed
) {
280 /* Check if we are ready to flush. */
281 if (!index
->has_index_data
|| !index
->index_file
) {
284 fd
= index
->index_file
->fd
;
285 DBG2("Writing index for stream ID %" PRIu64
" and seq num %" PRIu64
286 " on fd %d", index
->stream
->stream_handle
,
287 index
->index_n
.key
, fd
);
289 index
->flushed
= true;
290 ret
= lttng_index_file_write(index
->index_file
, &index
->index_data
);
292 pthread_mutex_unlock(&index
->lock
);
295 /* Put self-ref from index now that it has been flushed. */
296 relay_index_put(index
);
302 * Close every relay index within a given stream, without flushing
305 void relay_index_close_all(struct relay_stream
*stream
)
307 struct lttng_ht_iter iter
;
308 struct relay_index
*index
;
311 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
312 index
, index_n
.node
) {
313 /* Put self-ref from index. */
314 relay_index_put(index
);
319 void relay_index_close_partial_fd(struct relay_stream
*stream
)
321 struct lttng_ht_iter iter
;
322 struct relay_index
*index
;
325 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
326 index
, index_n
.node
) {
327 if (!index
->index_file
) {
331 * Partial index has its index_file: we have only
332 * received its info from the data socket.
333 * Put self-ref from index.
335 relay_index_put(index
);
340 uint64_t relay_index_find_last(struct relay_stream
*stream
)
342 struct lttng_ht_iter iter
;
343 struct relay_index
*index
;
344 uint64_t net_seq_num
= -1ULL;
347 cds_lfht_for_each_entry(stream
->indexes_ht
->ht
, &iter
.iter
,
348 index
, index_n
.node
) {
349 if (net_seq_num
== -1ULL ||
350 index
->index_n
.key
> net_seq_num
) {
351 net_seq_num
= index
->index_n
.key
;
This page took 0.036668 seconds and 4 git commands to generate.