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.
23 #include <urcu/uatomic.h>
25 #include <common/defaults.h>
31 * Return the atomically incremented value of next_output_id.
33 static inline unsigned long get_next_output_id(struct snapshot
*snapshot
)
35 return uatomic_add_return(&snapshot
->next_output_id
, 1);
39 * Initialized snapshot output with the given values.
41 * Return 0 on success or else a negative value.
43 static int output_init(uint64_t max_size
, const char *name
,
44 struct lttng_uri
*uris
, size_t nb_uri
,
45 struct consumer_output
*consumer
, struct snapshot_output
*output
,
46 struct snapshot
*snapshot
)
52 memset(output
, 0, sizeof(struct snapshot_output
));
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 strncpy(output
->name
, name
, sizeof(output
->name
));
67 /* Set default name. */
68 ret
= snprintf(output
->name
, sizeof(output
->name
), "%s-%" PRIu32
,
69 DEFAULT_SNAPSHOT_NAME
, output
->id
);
80 output
->consumer
= consumer_copy_output(consumer
);
81 if (!output
->consumer
) {
85 output
->consumer
->snapshot
= 1;
93 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
94 memset(output
->consumer
->dst
.trace_path
, 0,
95 sizeof(output
->consumer
->dst
.trace_path
));
96 strncpy(output
->consumer
->dst
.trace_path
, uris
[0].dst
.path
,
97 sizeof(output
->consumer
->dst
.trace_path
));
98 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
104 /* Absolutely needs two URIs for network. */
105 ret
= -LTTNG_ERR_INVALID
;
109 for (i
= 0; i
< nb_uri
; i
++) {
111 ret
= consumer_set_network_uri(output
->consumer
, &uris
[i
]);
123 * Initialize a snapshot output object using the given parameters and URI(s).
124 * The name value and uris can be NULL.
126 * Return 0 on success or else a negative value.
128 int snapshot_output_init_with_uri(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(max_size
, name
, uris
, nb_uri
, consumer
, output
,
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(uint64_t max_size
, const char *name
,
144 const char *ctrl_url
, const char *data_url
,
145 struct consumer_output
*consumer
, struct snapshot_output
*output
,
146 struct snapshot
*snapshot
)
149 struct lttng_uri
*uris
= NULL
;
151 /* Create an array of URIs from URLs. */
152 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
158 ret
= output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
166 struct snapshot_output
*snapshot_output_alloc(void)
168 return zmalloc(sizeof(struct snapshot_output
));
172 * Delete output from the snapshot object.
174 void snapshot_delete_output(struct snapshot
*snapshot
,
175 struct snapshot_output
*output
)
178 struct lttng_ht_iter iter
;
181 assert(snapshot
->output_ht
);
184 iter
.iter
.node
= &output
->node
.node
;
186 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
190 * This is safe because the ownership of a snapshot object is in a session
191 * for which the session lock need to be acquired to read and modify it.
193 snapshot
->nb_output
--;
197 * Add output object to the snapshot.
199 void snapshot_add_output(struct snapshot
*snapshot
,
200 struct snapshot_output
*output
)
203 assert(snapshot
->output_ht
);
207 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
210 * This is safe because the ownership of a snapshot object is in a session
211 * for which the session lock need to be acquired to read and modify it.
213 snapshot
->nb_output
++;
217 * Destroy and free a snapshot output object.
219 void snapshot_output_destroy(struct snapshot_output
*obj
)
224 consumer_output_send_destroy_relayd(obj
->consumer
);
225 consumer_destroy_output(obj
->consumer
);
231 * RCU read side lock MUST be acquired before calling this since the returned
232 * pointer is in a RCU hash table.
234 * Return the reference on success or else NULL.
236 struct snapshot_output
*snapshot_find_output_by_name(const char *name
,
237 struct snapshot
*snapshot
)
239 struct lttng_ht_iter iter
;
240 struct snapshot_output
*output
= NULL
;
245 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
247 if (!strncmp(output
->name
, name
, strlen(name
))) {
257 * RCU read side lock MUST be acquired before calling this since the returned
258 * pointer is in a RCU hash table.
260 * Return the reference on success or else NULL.
262 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
,
263 struct snapshot
*snapshot
)
265 struct lttng_ht_node_ulong
*node
;
266 struct lttng_ht_iter iter
;
267 struct snapshot_output
*output
= NULL
;
271 lttng_ht_lookup(snapshot
->output_ht
, (void *)((unsigned long) id
), &iter
);
272 node
= lttng_ht_iter_get_node_ulong(&iter
);
274 DBG3("Snapshot output not found with id %" PRId32
, id
);
277 output
= caa_container_of(node
, struct snapshot_output
, node
);
284 * Initialized a snapshot object that was already allocated.
286 * Return 0 on success or else a negative errno value.
288 int snapshot_init(struct snapshot
*obj
)
294 memset(obj
, 0, sizeof(struct snapshot
));
296 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
297 if (!obj
->output_ht
) {
309 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
312 void snapshot_destroy(struct snapshot
*obj
)
314 struct lttng_ht_iter iter
;
315 struct snapshot_output
*output
;
320 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
322 snapshot_delete_output(obj
, output
);
323 snapshot_output_destroy(output
);
326 ht_cleanup_push(obj
->output_ht
);
This page took 0.038684 seconds and 5 git commands to generate.