Fix: define _LGPL_SOURCE in C files
[lttng-tools.git] / src / bin / lttng-relayd / ctf-trace.c
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 #define _LGPL_SOURCE
21 #include <assert.h>
22
23 #include <common/common.h>
24 #include <common/utils.h>
25
26 #include "ctf-trace.h"
27 #include "lttng-relayd.h"
28 #include "stream.h"
29
30 static uint64_t last_relay_ctf_trace_id;
31
32 static void rcu_destroy_ctf_trace(struct rcu_head *head)
33 {
34 struct lttng_ht_node_str *node =
35 caa_container_of(head, struct lttng_ht_node_str, head);
36 struct ctf_trace *trace=
37 caa_container_of(node, struct ctf_trace, node);
38
39 free(trace);
40 }
41
42 /*
43 * Destroy a ctf trace and all stream contained in it.
44 *
45 * MUST be called with the RCU read side lock.
46 */
47 void ctf_trace_destroy(struct ctf_trace *obj)
48 {
49 struct relay_stream *stream, *tmp_stream;
50
51 assert(obj);
52 /*
53 * Getting to this point, every stream referenced to that object have put
54 * back their ref since the've been closed by the control side.
55 */
56 assert(!obj->refcount);
57
58 cds_list_for_each_entry_safe(stream, tmp_stream, &obj->stream_list,
59 trace_list) {
60 stream_delete(relay_streams_ht, stream);
61 stream_destroy(stream);
62 }
63
64 call_rcu(&obj->node.head, rcu_destroy_ctf_trace);
65 }
66
67 void ctf_trace_try_destroy(struct relay_session *session,
68 struct ctf_trace *ctf_trace)
69 {
70 assert(session);
71 assert(ctf_trace);
72
73 /*
74 * Considering no viewer attach to the session and the trace having no more
75 * stream attached, wipe the trace.
76 */
77 if (uatomic_read(&session->viewer_refcount) == 0 &&
78 uatomic_read(&ctf_trace->refcount) == 0) {
79 ctf_trace_delete(session->ctf_traces_ht, ctf_trace);
80 ctf_trace_destroy(ctf_trace);
81 }
82 }
83
84 /*
85 * Create and return an allocated ctf_trace object. NULL on error.
86 */
87 struct ctf_trace *ctf_trace_create(char *path_name)
88 {
89 struct ctf_trace *obj;
90
91 assert(path_name);
92
93 obj = zmalloc(sizeof(*obj));
94 if (!obj) {
95 PERROR("ctf_trace alloc");
96 goto error;
97 }
98
99 CDS_INIT_LIST_HEAD(&obj->stream_list);
100
101 obj->id = ++last_relay_ctf_trace_id;
102 lttng_ht_node_init_str(&obj->node, path_name);
103
104 DBG("Created ctf_trace %" PRIu64 " with path: %s", obj->id, path_name);
105
106 error:
107 return obj;
108 }
109
110 /*
111 * Return a ctf_trace object if found by id in the given hash table else NULL.
112 */
113 struct ctf_trace *ctf_trace_find_by_path(struct lttng_ht *ht,
114 char *path_name)
115 {
116 struct lttng_ht_node_str *node;
117 struct lttng_ht_iter iter;
118 struct ctf_trace *trace = NULL;
119
120 assert(ht);
121
122 lttng_ht_lookup(ht, (void *) path_name, &iter);
123 node = lttng_ht_iter_get_node_str(&iter);
124 if (!node) {
125 DBG("CTF Trace path %s not found", path_name);
126 goto end;
127 }
128 trace = caa_container_of(node, struct ctf_trace, node);
129
130 end:
131 return trace;
132 }
133
134 /*
135 * Add stream to a given hash table.
136 */
137 void ctf_trace_add(struct lttng_ht *ht, struct ctf_trace *trace)
138 {
139 assert(ht);
140 assert(trace);
141
142 lttng_ht_add_str(ht, &trace->node);
143 }
144
145 /*
146 * Delete stream from a given hash table.
147 */
148 void ctf_trace_delete(struct lttng_ht *ht, struct ctf_trace *trace)
149 {
150 int ret;
151 struct lttng_ht_iter iter;
152
153 assert(ht);
154 assert(trace);
155
156 iter.iter.node = &trace->node.node;
157 ret = lttng_ht_del(ht, &iter);
158 assert(!ret);
159 }
This page took 0.031616 seconds and 4 git commands to generate.