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 * Initialize a snapshot output object using the given parameters. The name
38 * value and url can be NULL.
40 * Return 0 on success or else a negative value.
42 int snapshot_output_init(uint64_t max_size
, const char *name
,
43 const char *ctrl_url
, const char *data_url
,
44 struct consumer_output
*consumer
, struct snapshot_output
*output
,
45 struct snapshot
*snapshot
)
47 int ret
= 0, nb_uri
, i
;
48 struct lttng_uri
*uris
= NULL
;
52 DBG2("Snapshot output initializing with max size %" PRIu64
", name %s "
53 "ctrl URL %s, data URL %s", max_size
, name
, ctrl_url
, data_url
);
55 output
->max_size
= max_size
;
57 output
->id
= get_next_output_id(snapshot
);
59 lttng_ht_node_init_ulong(&output
->node
, (unsigned long) output
->id
);
61 if (name
&& name
[0] != '\0') {
62 strncpy(output
->name
, name
, sizeof(output
->name
));
64 /* Set default name. */
65 ret
= snprintf(output
->name
, sizeof(output
->name
), "%s-%" PRIu32
,
66 DEFAULT_SNAPSHOT_NAME
, output
->id
);
77 /* Create an array of URIs from URLs. */
78 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
84 output
->consumer
= consumer_copy_output(consumer
);
85 if (!output
->consumer
) {
96 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
97 memset(output
->consumer
->dst
.trace_path
, 0,
98 sizeof(output
->consumer
->dst
.trace_path
));
99 strncpy(output
->consumer
->dst
.trace_path
, uris
[0].dst
.path
,
100 sizeof(output
->consumer
->dst
.trace_path
));
101 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
107 /* Absolutely needs two URIs for network. */
108 ret
= -LTTNG_ERR_INVALID
;
112 for (i
= 0; i
< nb_uri
; i
++) {
114 ret
= consumer_set_network_uri(output
->consumer
, &uris
[i
]);
126 struct snapshot_output
*snapshot_output_alloc(void)
128 return zmalloc(sizeof(struct snapshot_output
));
132 * Delete output from the snapshot object.
134 void snapshot_delete_output(struct snapshot
*snapshot
,
135 struct snapshot_output
*output
)
138 struct lttng_ht_iter iter
;
141 assert(snapshot
->output_ht
);
144 iter
.iter
.node
= &output
->node
.node
;
146 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
150 * This is safe because the ownership of a snapshot object is in a session
151 * for which the session lock need to be acquired to read and modify it.
153 snapshot
->nb_output
--;
157 * Add output object to the snapshot.
159 void snapshot_add_output(struct snapshot
*snapshot
,
160 struct snapshot_output
*output
)
163 assert(snapshot
->output_ht
);
167 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
170 * This is safe because the ownership of a snapshot object is in a session
171 * for which the session lock need to be acquired to read and modify it.
173 snapshot
->nb_output
++;
177 * Destroy and free a snapshot output object.
179 void snapshot_output_destroy(struct snapshot_output
*obj
)
184 consumer_output_send_destroy_relayd(obj
->consumer
);
185 consumer_destroy_output(obj
->consumer
);
191 * RCU read side lock MUST be acquired before calling this since the returned
192 * pointer is in a RCU hash table.
194 * Return the reference on success or else NULL.
196 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
,
197 struct snapshot
*snapshot
)
199 struct lttng_ht_node_ulong
*node
;
200 struct lttng_ht_iter iter
;
201 struct snapshot_output
*output
= NULL
;
205 lttng_ht_lookup(snapshot
->output_ht
, (void *)((unsigned long) id
), &iter
);
206 node
= lttng_ht_iter_get_node_ulong(&iter
);
208 DBG3("Snapshot output not found with id %" PRId32
, id
);
211 output
= caa_container_of(node
, struct snapshot_output
, node
);
218 * Initialized a snapshot object that was already allocated.
220 * Return 0 on success or else a negative errno value.
222 int snapshot_init(struct snapshot
*obj
)
228 memset(obj
, 0, sizeof(struct snapshot
));
230 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
231 if (!obj
->output_ht
) {
243 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
246 void snapshot_destroy(struct snapshot
*obj
)
248 struct lttng_ht_iter iter
;
249 struct snapshot_output
*output
;
254 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
256 snapshot_delete_output(obj
, output
);
257 snapshot_output_destroy(output
);
This page took 0.03735 seconds and 6 git commands to generate.