2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include <urcu/uatomic.h>
24 #include <common/defaults.h>
30 * Return the atomically incremented value of next_output_id.
32 static inline unsigned long get_next_output_id(struct snapshot
*snapshot
)
34 return uatomic_add_return(&snapshot
->next_output_id
, 1);
38 * Initialized snapshot output with the given values.
40 * Return 0 on success or else a negative value.
42 static int output_init(uint64_t max_size
, const char *name
,
43 struct lttng_uri
*uris
, size_t nb_uri
,
44 struct consumer_output
*consumer
, struct snapshot_output
*output
,
45 struct snapshot
*snapshot
)
49 memset(output
, 0, sizeof(struct snapshot_output
));
52 * max_size of -1ULL means unset. Set to default (unlimited).
54 if (max_size
== (uint64_t) -1ULL) {
57 output
->max_size
= max_size
;
60 output
->id
= get_next_output_id(snapshot
);
62 lttng_ht_node_init_ulong(&output
->node
, (unsigned long) output
->id
);
64 if (name
&& name
[0] != '\0') {
65 if (lttng_strncpy(output
->name
, name
, sizeof(output
->name
))) {
66 ret
= -LTTNG_ERR_INVALID
;
70 /* Set default name. */
71 ret
= snprintf(output
->name
, sizeof(output
->name
), "%s-%" PRIu32
,
72 DEFAULT_SNAPSHOT_NAME
, output
->id
);
83 output
->consumer
= consumer_copy_output(consumer
);
84 if (!output
->consumer
) {
88 output
->consumer
->snapshot
= 1;
96 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
97 memset(output
->consumer
->dst
.session_root_path
, 0,
98 sizeof(output
->consumer
->dst
.session_root_path
));
99 if (lttng_strncpy(output
->consumer
->dst
.session_root_path
,
101 sizeof(output
->consumer
->dst
.session_root_path
))) {
102 ret
= -LTTNG_ERR_INVALID
;
105 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
111 /* Absolutely needs two URIs for network. */
112 ret
= -LTTNG_ERR_INVALID
;
116 for (i
= 0; i
< nb_uri
; i
++) {
118 ret
= consumer_set_network_uri(output
->consumer
, &uris
[i
]);
130 * Initialize a snapshot output object using the given parameters and URI(s).
131 * The name value and uris can be NULL.
133 * Return 0 on success or else a negative value.
135 int snapshot_output_init_with_uri(uint64_t max_size
, const char *name
,
136 struct lttng_uri
*uris
, size_t nb_uri
,
137 struct consumer_output
*consumer
, struct snapshot_output
*output
,
138 struct snapshot
*snapshot
)
140 return output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
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(uint64_t max_size
, const char *name
,
151 const char *ctrl_url
, const char *data_url
,
152 struct consumer_output
*consumer
, struct snapshot_output
*output
,
153 struct snapshot
*snapshot
)
156 struct lttng_uri
*uris
= NULL
;
158 /* Create an array of URIs from URLs. */
159 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
165 ret
= output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
173 struct snapshot_output
*snapshot_output_alloc(void)
175 return zmalloc(sizeof(struct snapshot_output
));
179 * Delete output from the snapshot object.
181 void snapshot_delete_output(struct snapshot
*snapshot
,
182 struct snapshot_output
*output
)
185 struct lttng_ht_iter iter
;
188 assert(snapshot
->output_ht
);
191 iter
.iter
.node
= &output
->node
.node
;
193 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
197 * This is safe because the ownership of a snapshot object is in a session
198 * for which the session lock need to be acquired to read and modify it.
200 snapshot
->nb_output
--;
204 * Add output object to the snapshot.
206 void snapshot_add_output(struct snapshot
*snapshot
,
207 struct snapshot_output
*output
)
210 assert(snapshot
->output_ht
);
214 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
217 * This is safe because the ownership of a snapshot object is in a session
218 * for which the session lock need to be acquired to read and modify it.
220 snapshot
->nb_output
++;
224 * Destroy and free a snapshot output object.
226 void snapshot_output_destroy(struct snapshot_output
*obj
)
231 consumer_output_send_destroy_relayd(obj
->consumer
);
232 consumer_output_put(obj
->consumer
);
238 * RCU read side lock MUST be acquired before calling this since the returned
239 * pointer is in a RCU hash table.
241 * Return the reference on success or else NULL.
243 struct snapshot_output
*snapshot_find_output_by_name(const char *name
,
244 struct snapshot
*snapshot
)
246 struct lttng_ht_iter iter
;
247 struct snapshot_output
*output
= NULL
;
252 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
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
,
270 struct snapshot
*snapshot
)
272 struct lttng_ht_node_ulong
*node
;
273 struct lttng_ht_iter iter
;
274 struct snapshot_output
*output
= NULL
;
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
= caa_container_of(node
, struct 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
;
327 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
329 snapshot_delete_output(obj
, output
);
330 snapshot_output_destroy(output
);
333 ht_cleanup_push(obj
->output_ht
);
This page took 0.035473 seconds and 4 git commands to generate.