2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "snapshot.hpp"
12 #include <common/defaults.hpp>
16 #include <urcu/uatomic.h>
19 * Return the atomically incremented value of next_output_id.
21 static inline unsigned long get_next_output_id(struct snapshot
*snapshot
)
23 return uatomic_add_return(&snapshot
->next_output_id
, 1);
27 * Initialized snapshot output with the given values.
29 * Return 0 on success or else a negative value.
31 static int output_init(const struct ltt_session
*session
,
34 struct lttng_uri
*uris
,
36 struct consumer_output
*consumer
,
37 struct snapshot_output
*output
,
38 struct snapshot
*snapshot
)
42 memset(output
, 0, sizeof(struct snapshot_output
));
45 * max_size of -1ULL means unset. Set to default (unlimited).
47 if (max_size
== (uint64_t) -1ULL) {
50 output
->max_size
= max_size
;
53 output
->id
= get_next_output_id(snapshot
);
55 lttng_ht_node_init_ulong(&output
->node
, (unsigned long) output
->id
);
57 if (name
&& name
[0] != '\0') {
58 if (lttng_strncpy(output
->name
, name
, sizeof(output
->name
))) {
59 ret
= -LTTNG_ERR_INVALID
;
63 /* Set default name. */
64 ret
= snprintf(output
->name
,
67 DEFAULT_SNAPSHOT_NAME
,
79 output
->consumer
= consumer_copy_output(consumer
);
80 if (!output
->consumer
) {
84 output
->consumer
->snapshot
= 1;
92 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
93 memset(output
->consumer
->dst
.session_root_path
,
95 sizeof(output
->consumer
->dst
.session_root_path
));
96 if (lttng_strncpy(output
->consumer
->dst
.session_root_path
,
98 sizeof(output
->consumer
->dst
.session_root_path
))) {
99 ret
= -LTTNG_ERR_INVALID
;
102 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
108 /* Absolutely needs two URIs for network. */
109 ret
= -LTTNG_ERR_INVALID
;
113 for (i
= 0; i
< nb_uri
; i
++) {
115 ret
= consumer_set_network_uri(session
, output
->consumer
, &uris
[i
]);
127 * Initialize a snapshot output object using the given parameters and URI(s).
128 * The name value and uris can be NULL.
130 * Return 0 on success or else a negative value.
132 int snapshot_output_init_with_uri(const struct ltt_session
*session
,
135 struct lttng_uri
*uris
,
137 struct consumer_output
*consumer
,
138 struct snapshot_output
*output
,
139 struct snapshot
*snapshot
)
141 return output_init(session
, max_size
, name
, uris
, nb_uri
, consumer
, output
, snapshot
);
145 * Initialize a snapshot output object using the given parameters. The name
146 * value and url can be NULL.
148 * Return 0 on success or else a negative value.
150 int snapshot_output_init(const struct ltt_session
*session
,
153 const char *ctrl_url
,
154 const char *data_url
,
155 struct consumer_output
*consumer
,
156 struct snapshot_output
*output
,
157 struct snapshot
*snapshot
)
160 struct lttng_uri
*uris
= nullptr;
162 /* Create an array of URIs from URLs. */
163 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
169 ret
= output_init(session
, max_size
, name
, uris
, nb_uri
, consumer
, output
, snapshot
);
176 struct snapshot_output
*snapshot_output_alloc()
178 return zmalloc
<snapshot_output
>();
182 * Delete output from the snapshot object.
184 void snapshot_delete_output(struct snapshot
*snapshot
, struct snapshot_output
*output
)
187 struct lttng_ht_iter iter
;
189 LTTNG_ASSERT(snapshot
);
190 LTTNG_ASSERT(snapshot
->output_ht
);
191 LTTNG_ASSERT(output
);
193 iter
.iter
.node
= &output
->node
.node
;
195 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
199 * This is safe because the ownership of a snapshot object is in a session
200 * for which the session lock need to be acquired to read and modify it.
202 snapshot
->nb_output
--;
206 * Add output object to the snapshot.
208 void snapshot_add_output(struct snapshot
*snapshot
, struct snapshot_output
*output
)
210 LTTNG_ASSERT(snapshot
);
211 LTTNG_ASSERT(snapshot
->output_ht
);
212 LTTNG_ASSERT(output
);
215 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
218 * This is safe because the ownership of a snapshot object is in a session
219 * for which the session lock need to be acquired to read and modify it.
221 snapshot
->nb_output
++;
225 * Destroy and free a snapshot output object.
227 void snapshot_output_destroy(struct snapshot_output
*obj
)
232 consumer_output_send_destroy_relayd(obj
->consumer
);
233 consumer_output_put(obj
->consumer
);
239 * RCU read side lock MUST be acquired before calling this since the returned
240 * pointer is in a RCU hash table.
242 * Return the reference on success or else NULL.
244 struct snapshot_output
*snapshot_find_output_by_name(const char *name
, struct snapshot
*snapshot
)
246 struct lttng_ht_iter iter
;
247 struct snapshot_output
*output
= nullptr;
249 LTTNG_ASSERT(snapshot
);
251 ASSERT_RCU_READ_LOCKED();
253 cds_lfht_for_each_entry (snapshot
->output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
254 if (!strncmp(output
->name
, name
, strlen(name
))) {
264 * RCU read side lock MUST be acquired before calling this since the returned
265 * pointer is in a RCU hash table.
267 * Return the reference on success or else NULL.
269 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
, struct snapshot
*snapshot
)
271 struct lttng_ht_node_ulong
*node
;
272 struct lttng_ht_iter iter
;
273 struct snapshot_output
*output
= nullptr;
275 LTTNG_ASSERT(snapshot
);
276 ASSERT_RCU_READ_LOCKED();
278 lttng_ht_lookup(snapshot
->output_ht
, (void *) ((unsigned long) id
), &iter
);
279 node
= lttng_ht_iter_get_node_ulong(&iter
);
281 DBG3("Snapshot output not found with id %" PRId32
, id
);
284 output
= lttng::utils::container_of(node
, &snapshot_output::node
);
291 * Initialized a snapshot object that was already allocated.
293 * Return 0 on success or else a negative errno value.
295 int snapshot_init(struct snapshot
*obj
)
301 memset(obj
, 0, sizeof(struct snapshot
));
303 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
304 if (!obj
->output_ht
) {
316 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
319 void snapshot_destroy(struct snapshot
*obj
)
321 struct lttng_ht_iter iter
;
322 struct snapshot_output
*output
;
324 if (!obj
->output_ht
) {
329 cds_lfht_for_each_entry (obj
->output_ht
->ht
, &iter
.iter
, output
, node
.node
) {
330 snapshot_delete_output(obj
, output
);
331 snapshot_output_destroy(output
);
334 lttng_ht_destroy(obj
->output_ht
);
This page took 0.04455 seconds and 4 git commands to generate.