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
) {
83 output
->consumer
->snapshot
= 1;
91 if (uris
[0].dtype
== LTTNG_DST_PATH
) {
92 memset(output
->consumer
->dst
.trace_path
, 0,
93 sizeof(output
->consumer
->dst
.trace_path
));
94 strncpy(output
->consumer
->dst
.trace_path
, uris
[0].dst
.path
,
95 sizeof(output
->consumer
->dst
.trace_path
));
96 output
->consumer
->type
= CONSUMER_DST_LOCAL
;
102 /* Absolutely needs two URIs for network. */
103 ret
= -LTTNG_ERR_INVALID
;
107 for (i
= 0; i
< nb_uri
; i
++) {
109 ret
= consumer_set_network_uri(output
->consumer
, &uris
[i
]);
121 * Initialize a snapshot output object using the given parameters and URI(s).
122 * The name value and uris can be NULL.
124 * Return 0 on success or else a negative value.
126 int snapshot_output_init_with_uri(uint64_t max_size
, const char *name
,
127 struct lttng_uri
*uris
, size_t nb_uri
,
128 struct consumer_output
*consumer
, struct snapshot_output
*output
,
129 struct snapshot
*snapshot
)
131 return output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
136 * Initialize a snapshot output object using the given parameters. The name
137 * value and url can be NULL.
139 * Return 0 on success or else a negative value.
141 int snapshot_output_init(uint64_t max_size
, const char *name
,
142 const char *ctrl_url
, const char *data_url
,
143 struct consumer_output
*consumer
, struct snapshot_output
*output
,
144 struct snapshot
*snapshot
)
147 struct lttng_uri
*uris
= NULL
;
149 /* Create an array of URIs from URLs. */
150 nb_uri
= uri_parse_str_urls(ctrl_url
, data_url
, &uris
);
156 ret
= output_init(max_size
, name
, uris
, nb_uri
, consumer
, output
,
164 struct snapshot_output
*snapshot_output_alloc(void)
166 return zmalloc(sizeof(struct snapshot_output
));
170 * Delete output from the snapshot object.
172 void snapshot_delete_output(struct snapshot
*snapshot
,
173 struct snapshot_output
*output
)
176 struct lttng_ht_iter iter
;
179 assert(snapshot
->output_ht
);
182 iter
.iter
.node
= &output
->node
.node
;
184 ret
= lttng_ht_del(snapshot
->output_ht
, &iter
);
188 * This is safe because the ownership of a snapshot object is in a session
189 * for which the session lock need to be acquired to read and modify it.
191 snapshot
->nb_output
--;
195 * Add output object to the snapshot.
197 void snapshot_add_output(struct snapshot
*snapshot
,
198 struct snapshot_output
*output
)
201 assert(snapshot
->output_ht
);
205 lttng_ht_add_unique_ulong(snapshot
->output_ht
, &output
->node
);
208 * This is safe because the ownership of a snapshot object is in a session
209 * for which the session lock need to be acquired to read and modify it.
211 snapshot
->nb_output
++;
215 * Destroy and free a snapshot output object.
217 void snapshot_output_destroy(struct snapshot_output
*obj
)
222 consumer_output_send_destroy_relayd(obj
->consumer
);
223 consumer_destroy_output(obj
->consumer
);
229 * RCU read side lock MUST be acquired before calling this since the returned
230 * pointer is in a RCU hash table.
232 * Return the reference on success or else NULL.
234 struct snapshot_output
*snapshot_find_output_by_name(const char *name
,
235 struct snapshot
*snapshot
)
237 struct lttng_ht_iter iter
;
238 struct snapshot_output
*output
= NULL
;
243 cds_lfht_for_each_entry(snapshot
->output_ht
->ht
, &iter
.iter
, output
,
245 if (!strncmp(output
->name
, name
, strlen(name
))) {
255 * RCU read side lock MUST be acquired before calling this since the returned
256 * pointer is in a RCU hash table.
258 * Return the reference on success or else NULL.
260 struct snapshot_output
*snapshot_find_output_by_id(uint32_t id
,
261 struct snapshot
*snapshot
)
263 struct lttng_ht_node_ulong
*node
;
264 struct lttng_ht_iter iter
;
265 struct snapshot_output
*output
= NULL
;
269 lttng_ht_lookup(snapshot
->output_ht
, (void *)((unsigned long) id
), &iter
);
270 node
= lttng_ht_iter_get_node_ulong(&iter
);
272 DBG3("Snapshot output not found with id %" PRId32
, id
);
275 output
= caa_container_of(node
, struct snapshot_output
, node
);
282 * Initialized a snapshot object that was already allocated.
284 * Return 0 on success or else a negative errno value.
286 int snapshot_init(struct snapshot
*obj
)
292 memset(obj
, 0, sizeof(struct snapshot
));
294 obj
->output_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_ULONG
);
295 if (!obj
->output_ht
) {
307 * Destroy snapshot object but the pointer is not freed so it's safe to pass a
310 void snapshot_destroy(struct snapshot
*obj
)
312 struct lttng_ht_iter iter
;
313 struct snapshot_output
*output
;
318 cds_lfht_for_each_entry(obj
->output_ht
->ht
, &iter
.iter
, output
,
320 snapshot_delete_output(obj
, output
);
321 snapshot_output_destroy(output
);
This page took 0.039842 seconds and 5 git commands to generate.