Commit | Line | Data |
---|---|---|
991adae2 DG |
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 | ||
eb702af5 DG |
19 | #define _GNU_SOURCE |
20 | #include <common/common.h> | |
21 | ||
22 | #include "ctf-trace.h" | |
991adae2 | 23 | #include "session.h" |
eb702af5 DG |
24 | #include "stream.h" |
25 | ||
26 | /* Global session id used in the session creation. */ | |
27 | static uint64_t last_relay_session_id; | |
28 | ||
29 | static void rcu_destroy_session(struct rcu_head *head) | |
30 | { | |
31 | struct relay_session *session = | |
32 | caa_container_of(head, struct relay_session, rcu_node); | |
33 | ||
34 | free(session); | |
35 | } | |
36 | ||
37 | /* | |
38 | * Create a new session by assigning a new session ID. | |
39 | * | |
40 | * Return allocated session or else NULL. | |
41 | */ | |
42 | struct relay_session *session_create(void) | |
43 | { | |
44 | struct relay_session *session; | |
45 | ||
46 | session = zmalloc(sizeof(*session)); | |
47 | if (!session) { | |
48 | PERROR("relay session zmalloc"); | |
49 | goto error; | |
50 | } | |
51 | ||
52 | session->ctf_traces_ht = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
53 | if (!session->ctf_traces_ht) { | |
54 | free(session); | |
ab8b1c54 | 55 | session = NULL; |
eb702af5 DG |
56 | goto error; |
57 | } | |
58 | ||
59 | pthread_mutex_init(&session->viewer_ready_lock, NULL); | |
60 | session->id = ++last_relay_session_id; | |
61 | lttng_ht_node_init_u64(&session->session_n, session->id); | |
62 | ||
63 | error: | |
64 | return session; | |
65 | } | |
991adae2 DG |
66 | |
67 | /* | |
68 | * Lookup a session within the given hash table and session id. RCU read side | |
69 | * lock MUST be acquired before calling this and as long as the caller has a | |
70 | * reference to the object. | |
71 | * | |
72 | * Return session or NULL if not found. | |
73 | */ | |
74 | struct relay_session *session_find_by_id(struct lttng_ht *ht, uint64_t id) | |
75 | { | |
76 | struct relay_session *session = NULL; | |
eb702af5 | 77 | struct lttng_ht_node_u64 *node; |
991adae2 DG |
78 | struct lttng_ht_iter iter; |
79 | ||
80 | assert(ht); | |
81 | ||
eb702af5 DG |
82 | lttng_ht_lookup(ht, &id, &iter); |
83 | node = lttng_ht_iter_get_node_u64(&iter); | |
991adae2 | 84 | if (!node) { |
eb702af5 | 85 | DBG("Session find by ID %" PRIu64 " id NOT found", id); |
991adae2 DG |
86 | goto end; |
87 | } | |
88 | session = caa_container_of(node, struct relay_session, session_n); | |
eb702af5 | 89 | DBG("Session find by ID %" PRIu64 " id found", id); |
991adae2 DG |
90 | |
91 | end: | |
92 | return session; | |
93 | } | |
eb702af5 DG |
94 | |
95 | /* | |
96 | * Delete session from the given hash table. | |
97 | * | |
98 | * Return lttng ht del error code being 0 on success and 1 on failure. | |
99 | */ | |
100 | int session_delete(struct lttng_ht *ht, struct relay_session *session) | |
101 | { | |
102 | struct lttng_ht_iter iter; | |
103 | ||
104 | assert(ht); | |
105 | assert(session); | |
106 | ||
107 | iter.iter.node = &session->session_n.node; | |
108 | return lttng_ht_del(ht, &iter); | |
109 | } | |
110 | ||
111 | /* | |
112 | * The caller MUST be from the viewer thread since the viewer refcount is | |
113 | * decremented. With this calue down to 0, it will try to destroy the session. | |
114 | */ | |
115 | void session_viewer_try_destroy(struct lttng_ht *ht, | |
116 | struct relay_session *session) | |
117 | { | |
118 | unsigned long ret_ref; | |
119 | ||
120 | assert(session); | |
121 | ||
122 | ret_ref = uatomic_add_return(&session->viewer_refcount, -1); | |
123 | if (ret_ref == 0) { | |
124 | session_try_destroy(ht, session); | |
125 | } | |
126 | } | |
127 | ||
128 | /* | |
129 | * Should only be called from the main streaming thread since it does not touch | |
130 | * the viewer refcount. If this refcount is down to 0, destroy the session only | |
131 | * and only if the session deletion succeeds. This is done because the viewer | |
132 | * *and* the streaming thread can both concurently try to destroy the session | |
133 | * thus the first come first serve. | |
134 | */ | |
135 | void session_try_destroy(struct lttng_ht *ht, struct relay_session *session) | |
136 | { | |
137 | int ret = 0; | |
138 | unsigned long ret_ref; | |
139 | ||
140 | assert(session); | |
141 | ||
142 | ret_ref = uatomic_read(&session->viewer_refcount); | |
143 | if (ret_ref == 0 && session->close_flag) { | |
144 | if (ht) { | |
145 | ret = session_delete(ht, session); | |
146 | } | |
147 | if (!ret) { | |
148 | /* Only destroy the session if the deletion was successful. */ | |
149 | session_destroy(session); | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
154 | /* | |
155 | * Destroy a session object. | |
156 | */ | |
157 | void session_destroy(struct relay_session *session) | |
158 | { | |
159 | struct ctf_trace *ctf_trace; | |
160 | struct lttng_ht_iter iter; | |
161 | ||
162 | assert(session); | |
163 | ||
164 | DBG("Relay destroying session %" PRIu64, session->id); | |
165 | ||
166 | /* | |
167 | * Empty the ctf trace hash table which will destroy the stream contained | |
168 | * in that table. | |
169 | */ | |
170 | rcu_read_lock(); | |
171 | cds_lfht_for_each_entry(session->ctf_traces_ht->ht, &iter.iter, ctf_trace, | |
172 | node.node) { | |
173 | ctf_trace_delete(session->ctf_traces_ht, ctf_trace); | |
174 | ctf_trace_destroy(ctf_trace); | |
175 | } | |
176 | lttng_ht_destroy(session->ctf_traces_ht); | |
177 | rcu_read_unlock(); | |
178 | ||
179 | call_rcu(&session->rcu_node, rcu_destroy_session); | |
180 | } |