Commit | Line | Data |
---|---|---|
d3e2ba59 JD |
1 | /* |
2 | * Copyright (C) 2013 - Julien Desfossez <jdesfossez@efficios.com> | |
3 | * David Goulet <dgoulet@efficios.com> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License, version 2 only, as | |
7 | * published by the Free Software Foundation. | |
8 | * | |
9 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
12 | * more details. | |
13 | * | |
14 | * You should have received a copy of the GNU General Public License along with | |
15 | * this program; if not, write to the Free Software Foundation, Inc., 51 | |
16 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
17 | */ | |
18 | ||
19 | #define _GNU_SOURCE | |
20 | #include <assert.h> | |
21 | ||
22 | #include <common/common.h> | |
23 | #include <common/utils.h> | |
24 | ||
25 | #include "ctf-trace.h" | |
eb702af5 DG |
26 | #include "lttng-relayd.h" |
27 | #include "stream.h" | |
d3e2ba59 JD |
28 | |
29 | static uint64_t last_relay_ctf_trace_id; | |
30 | ||
eb702af5 DG |
31 | static void rcu_destroy_ctf_trace(struct rcu_head *head) |
32 | { | |
33 | struct lttng_ht_node_str *node = | |
34 | caa_container_of(head, struct lttng_ht_node_str, head); | |
35 | struct ctf_trace *trace= | |
36 | caa_container_of(node, struct ctf_trace, node); | |
37 | ||
38 | free(trace); | |
39 | } | |
40 | ||
d3e2ba59 | 41 | /* |
eb702af5 DG |
42 | * Destroy a ctf trace and all stream contained in it. |
43 | * | |
44 | * MUST be called with the RCU read side lock. | |
d3e2ba59 | 45 | */ |
eb702af5 | 46 | void ctf_trace_destroy(struct ctf_trace *obj) |
d3e2ba59 | 47 | { |
eb702af5 DG |
48 | struct relay_stream *stream, *tmp_stream; |
49 | ||
50 | assert(obj); | |
51 | /* | |
52 | * Getting to this point, every stream referenced to that object have put | |
53 | * back their ref since the've been closed by the control side. | |
54 | */ | |
55 | assert(!obj->refcount); | |
56 | ||
57 | cds_list_for_each_entry_safe(stream, tmp_stream, &obj->stream_list, | |
58 | trace_list) { | |
59 | stream_delete(relay_streams_ht, stream); | |
60 | stream_destroy(stream); | |
d3e2ba59 JD |
61 | } |
62 | ||
eb702af5 DG |
63 | call_rcu(&obj->node.head, rcu_destroy_ctf_trace); |
64 | } | |
65 | ||
66 | void ctf_trace_try_destroy(struct relay_session *session, | |
67 | struct ctf_trace *ctf_trace) | |
68 | { | |
69 | assert(session); | |
70 | assert(ctf_trace); | |
71 | ||
72 | /* | |
73 | * Considering no viewer attach to the session and the trace having no more | |
74 | * stream attached, wipe the trace. | |
75 | */ | |
76 | if (uatomic_read(&session->viewer_refcount) == 0 && | |
77 | uatomic_read(&ctf_trace->refcount) == 0) { | |
78 | ctf_trace_destroy(ctf_trace); | |
d3e2ba59 JD |
79 | } |
80 | } | |
81 | ||
82 | /* | |
83 | * Create and return an allocated ctf_trace object. NULL on error. | |
84 | */ | |
eb702af5 | 85 | struct ctf_trace *ctf_trace_create(char *path_name) |
d3e2ba59 JD |
86 | { |
87 | struct ctf_trace *obj; | |
88 | ||
eb702af5 DG |
89 | assert(path_name); |
90 | ||
d3e2ba59 JD |
91 | obj = zmalloc(sizeof(*obj)); |
92 | if (!obj) { | |
93 | PERROR("ctf_trace alloc"); | |
94 | goto error; | |
95 | } | |
96 | ||
eb702af5 DG |
97 | CDS_INIT_LIST_HEAD(&obj->stream_list); |
98 | ||
d3e2ba59 | 99 | obj->id = ++last_relay_ctf_trace_id; |
eb702af5 DG |
100 | lttng_ht_node_init_str(&obj->node, path_name); |
101 | ||
102 | DBG("Created ctf_trace %" PRIu64 " with path: %s", obj->id, path_name); | |
d3e2ba59 JD |
103 | |
104 | error: | |
105 | return obj; | |
106 | } | |
107 | ||
108 | /* | |
eb702af5 | 109 | * Return a ctf_trace object if found by id in the given hash table else NULL. |
d3e2ba59 | 110 | */ |
eb702af5 DG |
111 | struct ctf_trace *ctf_trace_find_by_path(struct lttng_ht *ht, |
112 | char *path_name) | |
d3e2ba59 | 113 | { |
eb702af5 | 114 | struct lttng_ht_node_str *node; |
d3e2ba59 | 115 | struct lttng_ht_iter iter; |
eb702af5 | 116 | struct ctf_trace *trace = NULL; |
d3e2ba59 JD |
117 | |
118 | assert(ht); | |
eb702af5 DG |
119 | |
120 | lttng_ht_lookup(ht, (void *) path_name, &iter); | |
121 | node = lttng_ht_iter_get_node_str(&iter); | |
122 | if (!node) { | |
123 | DBG("CTF Trace path %s not found", path_name); | |
124 | goto end; | |
d3e2ba59 | 125 | } |
eb702af5 | 126 | trace = caa_container_of(node, struct ctf_trace, node); |
d3e2ba59 JD |
127 | |
128 | end: | |
eb702af5 | 129 | return trace; |
d3e2ba59 JD |
130 | } |
131 | ||
eb702af5 DG |
132 | /* |
133 | * Add stream to a given hash table. | |
134 | */ | |
135 | void ctf_trace_add(struct lttng_ht *ht, struct ctf_trace *trace) | |
136 | { | |
137 | assert(ht); | |
138 | assert(trace); | |
139 | ||
140 | lttng_ht_add_str(ht, &trace->node); | |
141 | } | |
142 | ||
143 | /* | |
144 | * Delete stream from a given hash table. | |
145 | */ | |
146 | void ctf_trace_delete(struct lttng_ht *ht, struct ctf_trace *trace) | |
147 | { | |
148 | int ret; | |
149 | struct lttng_ht_iter iter; | |
150 | ||
151 | assert(ht); | |
152 | assert(trace); | |
153 | ||
154 | iter.iter.node = &trace->node.node; | |
155 | ret = lttng_ht_del(ht, &iter); | |
156 | assert(!ret); | |
157 | } |