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>
29 * Return the atomically incremented value of next_output_id.
31 static inline unsigned long get_next_output_id(struct snapshot
*snapshot
)
33 return uatomic_add_return(&snapshot
->next_output_id
, 1);
37 * Initialized snapshot output with the given values.
39 * Return 0 on success or else a negative value.
41 static int output_init(uint64_t max_size
, const char *name
,
42 struct lttng_uri
*uris
, size_t nb_uri
,
43 struct consumer_output
*consumer
, struct snapshot_output
*output
,
44 struct snapshot
*snapshot
)
50 memset(output
, 0, sizeof(struct snapshot_output
));
52 if (max_size
== (uint64_t) -1ULL) {
55 output
->max_size
= max_size
;
58 output
->id
= get_next_output_id(snapshot
);
60 lttng_ht_node_init_ulong(&output
->node
, (unsigned long) output
->id
);
62 if (name
&& name
[0] != '\0') {
63 strncpy(output
->name
, name
, sizeof(output
->name
));
65 /* Set default name. */
66 ret
= snprintf(output
->name
, sizeof(output
->name
), "%s-%" PRIu32
,
67 DEFAULT_SNAPSHOT_NAME
, output
->id
);
78 output
->consumer
= consumer_copy_output(consumer
);
79 if (!output
->consumer
) {
90 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
91 memset(output
->consumer
->dst
.trace_path
, 0,
92 sizeof(output
->consumer
->dst
.trace_path
));
93 strncpy(output
->consumer
->dst
.trace_path
, uris
[0].dst
.path
,
94 sizeof(output
->consumer
->dst
.trace_path
));
95 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
101 /* Absolutely needs two URIs for network. */
102 ret
= -LTTNG_ERR_INVALID
;
106 for (i
= 0; i
< nb_uri
; i
++) {
108 ret
= consumer_set_network_uri(output
->consumer
, &uris
[i
]);
120 * Initialize a snapshot output object using the given parameters and URI(s).
121 * The name value and uris can be NULL.
123 * Return 0 on success or else a negative value.
125 int snapshot_output_init_with_uri(uint64_t max_size
, const char *name
,
126 struct lttng_uri
*uris
, size_t nb_uri
,
127 struct consumer_output
*consumer
, struct snapshot_output
*output
,
128 struct snapshot
*snapshot
)
130 return output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
135 * Initialize a snapshot output object using the given parameters. The name
136 * value and url can be NULL.
138 * Return 0 on success or else a negative value.
140 int snapshot_output_init(uint64_t max_size
, const char *name
,
141 const char *ctrl_url
, const char *data_url
,
142 struct consumer_output
*consumer
, struct snapshot_output
*output
,
143 struct snapshot
*snapshot
)
146 struct lttng_uri
*uris
= NULL
;
148 /* Create an array of URIs from URLs. */
149 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
155 ret
= output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
163 struct snapshot_output
*snapshot_output_alloc(void)
165 return zmalloc(sizeof(struct snapshot_output
));
169 * Delete output from the snapshot object.
171 void snapshot_delete_output(struct snapshot
*snapshot
,
172 struct snapshot_output
*output
)
175 struct lttng_ht_iter iter
;
178 assert(snapshot
->output_ht
);
181 iter
.iter
.node
= &output
->node
.node
;
183 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
187 * This is safe because the ownership of a snapshot object is in a session
188 * for which the session lock need to be acquired to read and modify it.
190 snapshot
->nb_output
--;
194 * Add output object to the snapshot.
196 void snapshot_add_output(struct snapshot
*snapshot
,
197 struct snapshot_output
*output
)
200 assert(snapshot
->output_ht
);
204 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
207 * This is safe because the ownership of a snapshot object is in a session
208 * for which the session lock need to be acquired to read and modify it.
210 snapshot
->nb_output
++;
214 * Destroy and free a snapshot output object.
216 void snapshot_output_destroy(struct snapshot_output
*obj
)
221 consumer_output_send_destroy_relayd(obj
->consumer
);
222 consumer_destroy_output(obj
->consumer
);
228 * RCU read side lock MUST be acquired before calling this since the returned
229 * pointer is in a RCU hash table.
231 * Return the reference on success or else NULL.
233 struct snapshot_output
*snapshot_find_output_by_name(const char *name
,
234 struct snapshot
*snapshot
)
236 struct lttng_ht_iter iter
;
237 struct snapshot_output
*output
= NULL
;
242 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
244 if (!strncmp(output
->name
, name
, strlen(name
))) {
254 * RCU read side lock MUST be acquired before calling this since the returned
255 * pointer is in a RCU hash table.
257 * Return the reference on success or else NULL.
259 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
,
260 struct snapshot
*snapshot
)
262 struct lttng_ht_node_ulong
*node
;
263 struct lttng_ht_iter iter
;
264 struct snapshot_output
*output
= NULL
;
268 lttng_ht_lookup(snapshot
->output_ht
, (void *)((unsigned long) id
), &iter
);
269 node
= lttng_ht_iter_get_node_ulong(&iter
);
271 DBG3("Snapshot output not found with id %" PRId32
, id
);
274 output
= caa_container_of(node
, struct snapshot_output
, node
);
281 * Initialized a snapshot object that was already allocated.
283 * Return 0 on success or else a negative errno value.
285 int snapshot_init(struct snapshot
*obj
)
291 memset(obj
, 0, sizeof(struct snapshot
));
293 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
294 if (!obj
->output_ht
) {
306 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
309 void snapshot_destroy(struct snapshot
*obj
)
311 struct lttng_ht_iter iter
;
312 struct snapshot_output
*output
;
317 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
319 snapshot_delete_output(obj
, output
);
320 snapshot_output_destroy(output
);
This page took 0.056671 seconds and 4 git commands to generate.