2 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
12 #include <urcu/uatomic.h>
14 #include <common/defaults.h>
20 * Return the atomically incremented value of next_output_id.
22 static inline unsigned long get_next_output_id(struct snapshot
*snapshot
)
24 return uatomic_add_return(&snapshot
->next_output_id
, 1);
28 * Initialized snapshot output with the given values.
30 * Return 0 on success or else a negative value.
32 static int output_init(const struct ltt_session
*session
,
33 uint64_t max_size
, const char *name
,
34 struct lttng_uri
*uris
, size_t nb_uri
,
35 struct consumer_output
*consumer
, struct snapshot_output
*output
,
36 struct snapshot
*snapshot
)
40 memset(output
, 0, sizeof(struct snapshot_output
));
43 * max_size of -1ULL means unset. Set to default (unlimited).
45 if (max_size
== (uint64_t) -1ULL) {
48 output
->max_size
= max_size
;
51 output
->id
= get_next_output_id(snapshot
);
53 lttng_ht_node_init_ulong(&output
->node
, (unsigned long) output
->id
);
55 if (name
&& name
[0] != '\0') {
56 if (lttng_strncpy(output
->name
, name
, sizeof(output
->name
))) {
57 ret
= -LTTNG_ERR_INVALID
;
61 /* Set default name. */
62 ret
= snprintf(output
->name
, sizeof(output
->name
), "%s-%" PRIu32
,
63 DEFAULT_SNAPSHOT_NAME
, output
->id
);
74 output
->consumer
= consumer_copy_output(consumer
);
75 if (!output
->consumer
) {
79 output
->consumer
->snapshot
= 1;
87 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
88 memset(output
->consumer
->dst
.session_root_path
, 0,
89 sizeof(output
->consumer
->dst
.session_root_path
));
90 if (lttng_strncpy(output
->consumer
->dst
.session_root_path
,
92 sizeof(output
->consumer
->dst
.session_root_path
))) {
93 ret
= -LTTNG_ERR_INVALID
;
96 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
102 /* Absolutely needs two URIs for network. */
103 ret
= -LTTNG_ERR_INVALID
;
107 for (i
= 0; i
< nb_uri
; i
++) {
109 ret
= consumer_set_network_uri(session
, output
->consumer
,
122 * Initialize a snapshot output object using the given parameters and URI(s).
123 * The name value and uris can be NULL.
125 * Return 0 on success or else a negative value.
127 int snapshot_output_init_with_uri(const struct ltt_session
*session
,
128 uint64_t max_size
, const char *name
,
129 struct lttng_uri
*uris
, size_t nb_uri
,
130 struct consumer_output
*consumer
, struct snapshot_output
*output
,
131 struct snapshot
*snapshot
)
133 return output_init(session
, max_size
, name
, uris
, nb_uri
, consumer
,
138 * Initialize a snapshot output object using the given parameters. The name
139 * value and url can be NULL.
141 * Return 0 on success or else a negative value.
143 int snapshot_output_init(const struct ltt_session
*session
,
144 uint64_t max_size
, const char *name
,
145 const char *ctrl_url
, const char *data_url
,
146 struct consumer_output
*consumer
, struct snapshot_output
*output
,
147 struct snapshot
*snapshot
)
150 struct lttng_uri
*uris
= NULL
;
152 /* Create an array of URIs from URLs. */
153 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
159 ret
= output_init(session
, max_size
, name
, uris
, nb_uri
, consumer
,
167 struct snapshot_output
*snapshot_output_alloc(void)
169 return zmalloc(sizeof(struct snapshot_output
));
173 * Delete output from the snapshot object.
175 void snapshot_delete_output(struct snapshot
*snapshot
,
176 struct snapshot_output
*output
)
179 struct lttng_ht_iter iter
;
182 assert(snapshot
->output_ht
);
185 iter
.iter
.node
= &output
->node
.node
;
187 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
191 * This is safe because the ownership of a snapshot object is in a session
192 * for which the session lock need to be acquired to read and modify it.
194 snapshot
->nb_output
--;
198 * Add output object to the snapshot.
200 void snapshot_add_output(struct snapshot
*snapshot
,
201 struct snapshot_output
*output
)
204 assert(snapshot
->output_ht
);
208 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
211 * This is safe because the ownership of a snapshot object is in a session
212 * for which the session lock need to be acquired to read and modify it.
214 snapshot
->nb_output
++;
218 * Destroy and free a snapshot output object.
220 void snapshot_output_destroy(struct snapshot_output
*obj
)
225 consumer_output_send_destroy_relayd(obj
->consumer
);
226 consumer_output_put(obj
->consumer
);
232 * RCU read side lock MUST be acquired before calling this since the returned
233 * pointer is in a RCU hash table.
235 * Return the reference on success or else NULL.
237 struct snapshot_output
*snapshot_find_output_by_name(const char *name
,
238 struct snapshot
*snapshot
)
240 struct lttng_ht_iter iter
;
241 struct snapshot_output
*output
= NULL
;
246 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
248 if (!strncmp(output
->name
, name
, strlen(name
))) {
258 * RCU read side lock MUST be acquired before calling this since the returned
259 * pointer is in a RCU hash table.
261 * Return the reference on success or else NULL.
263 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
,
264 struct snapshot
*snapshot
)
266 struct lttng_ht_node_ulong
*node
;
267 struct lttng_ht_iter iter
;
268 struct snapshot_output
*output
= NULL
;
272 lttng_ht_lookup(snapshot
->output_ht
, (void *)((unsigned long) id
), &iter
);
273 node
= lttng_ht_iter_get_node_ulong(&iter
);
275 DBG3("Snapshot output not found with id %" PRId32
, id
);
278 output
= caa_container_of(node
, struct snapshot_output
, node
);
285 * Initialized a snapshot object that was already allocated.
287 * Return 0 on success or else a negative errno value.
289 int snapshot_init(struct snapshot
*obj
)
295 memset(obj
, 0, sizeof(struct snapshot
));
297 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
298 if (!obj
->output_ht
) {
310 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
313 void snapshot_destroy(struct snapshot
*obj
)
315 struct lttng_ht_iter iter
;
316 struct snapshot_output
*output
;
318 if (!obj
->output_ht
) {
323 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
325 snapshot_delete_output(obj
, output
);
326 snapshot_output_destroy(output
);
329 ht_cleanup_push(obj
->output_ht
);
This page took 0.036169 seconds and 4 git commands to generate.