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
)
51 memset(output
, 0, sizeof(struct snapshot_output
));
53 if (max_size
== (uint64_t) -1ULL) {
56 output
->max_size
= max_size
;
59 output
->id
= get_next_output_id(snapshot
);
61 lttng_ht_node_init_ulong(&output
->node
, (unsigned long) output
->id
);
63 if (name
&& name
[0] != '\0') {
64 strncpy(output
->name
, name
, sizeof(output
->name
));
66 /* Set default name. */
67 ret
= snprintf(output
->name
, sizeof(output
->name
), "%s-%" PRIu32
,
68 DEFAULT_SNAPSHOT_NAME
, output
->id
);
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
.trace_path
, 0,
94 sizeof(output
->consumer
->dst
.trace_path
));
95 strncpy(output
->consumer
->dst
.trace_path
, uris
[0].dst
.path
,
96 sizeof(output
->consumer
->dst
.trace_path
));
97 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
103 /* Absolutely needs two URIs for network. */
104 ret
= -LTTNG_ERR_INVALID
;
108 for (i
= 0; i
< nb_uri
; i
++) {
110 ret
= consumer_set_network_uri(output
->consumer
, &uris
[i
]);
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(uint64_t max_size
, const char *name
,
128 struct lttng_uri
*uris
, size_t nb_uri
,
129 struct consumer_output
*consumer
, struct snapshot_output
*output
,
130 struct snapshot
*snapshot
)
132 return output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
137 * Initialize a snapshot output object using the given parameters. The name
138 * value and url can be NULL.
140 * Return 0 on success or else a negative value.
142 int snapshot_output_init(uint64_t max_size
, const char *name
,
143 const char *ctrl_url
, const char *data_url
,
144 struct consumer_output
*consumer
, struct snapshot_output
*output
,
145 struct snapshot
*snapshot
)
148 struct lttng_uri
*uris
= NULL
;
150 /* Create an array of URIs from URLs. */
151 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
157 ret
= output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
165 struct snapshot_output
*snapshot_output_alloc(void)
167 return zmalloc(sizeof(struct snapshot_output
));
171 * Delete output from the snapshot object.
173 void snapshot_delete_output(struct snapshot
*snapshot
,
174 struct snapshot_output
*output
)
177 struct lttng_ht_iter iter
;
180 assert(snapshot
->output_ht
);
183 iter
.iter
.node
= &output
->node
.node
;
185 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
189 * This is safe because the ownership of a snapshot object is in a session
190 * for which the session lock need to be acquired to read and modify it.
192 snapshot
->nb_output
--;
196 * Add output object to the snapshot.
198 void snapshot_add_output(struct snapshot
*snapshot
,
199 struct snapshot_output
*output
)
202 assert(snapshot
->output_ht
);
206 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
209 * This is safe because the ownership of a snapshot object is in a session
210 * for which the session lock need to be acquired to read and modify it.
212 snapshot
->nb_output
++;
216 * Destroy and free a snapshot output object.
218 void snapshot_output_destroy(struct snapshot_output
*obj
)
223 consumer_output_send_destroy_relayd(obj
->consumer
);
224 consumer_destroy_output(obj
->consumer
);
230 * RCU read side lock MUST be acquired before calling this since the returned
231 * pointer is in a RCU hash table.
233 * Return the reference on success or else NULL.
235 struct snapshot_output
*snapshot_find_output_by_name(const char *name
,
236 struct snapshot
*snapshot
)
238 struct lttng_ht_iter iter
;
239 struct snapshot_output
*output
= NULL
;
244 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
246 if (!strncmp(output
->name
, name
, strlen(name
))) {
256 * RCU read side lock MUST be acquired before calling this since the returned
257 * pointer is in a RCU hash table.
259 * Return the reference on success or else NULL.
261 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
,
262 struct snapshot
*snapshot
)
264 struct lttng_ht_node_ulong
*node
;
265 struct lttng_ht_iter iter
;
266 struct snapshot_output
*output
= NULL
;
270 lttng_ht_lookup(snapshot
->output_ht
, (void *)((unsigned long) id
), &iter
);
271 node
= lttng_ht_iter_get_node_ulong(&iter
);
273 DBG3("Snapshot output not found with id %" PRId32
, id
);
276 output
= caa_container_of(node
, struct snapshot_output
, node
);
283 * Initialized a snapshot object that was already allocated.
285 * Return 0 on success or else a negative errno value.
287 int snapshot_init(struct snapshot
*obj
)
293 memset(obj
, 0, sizeof(struct snapshot
));
295 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
296 if (!obj
->output_ht
) {
308 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
311 void snapshot_destroy(struct snapshot
*obj
)
313 struct lttng_ht_iter iter
;
314 struct snapshot_output
*output
;
319 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
321 snapshot_delete_output(obj
, output
);
322 snapshot_output_destroy(output
);
325 ht_cleanup_push(obj
->output_ht
);
This page took 0.038184 seconds and 4 git commands to generate.