Use compiler-agnostic defines to silence warning
[lttng-tools.git] / src / bin / lttng-relayd / viewer-session.cpp
... / ...
CommitLineData
1/*
2 * Copyright (C) 2013 Julien Desfossez <jdesfossez@efficios.com>
3 * Copyright (C) 2013 David Goulet <dgoulet@efficios.com>
4 * Copyright (C) 2015 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * SPDX-License-Identifier: GPL-2.0-only
7 *
8 */
9
10#define _LGPL_SOURCE
11#include "ctf-trace.hpp"
12#include "live.hpp"
13#include "lttng-relayd.hpp"
14#include "session.hpp"
15#include "stream.hpp"
16#include "viewer-session.hpp"
17#include "viewer-stream.hpp"
18
19#include <common/common.hpp>
20#include <common/urcu.hpp>
21
22#include <urcu/rculist.h>
23
24/* Global session id used in the session creation. */
25static uint64_t last_viewer_session_id;
26static pthread_mutex_t last_viewer_session_id_lock = PTHREAD_MUTEX_INITIALIZER;
27
28struct relay_viewer_session *viewer_session_create()
29{
30 struct relay_viewer_session *vsession;
31
32 vsession = zmalloc<relay_viewer_session>();
33 if (!vsession) {
34 goto end;
35 }
36 pthread_mutex_lock(&last_viewer_session_id_lock);
37 vsession->id = ++last_viewer_session_id;
38 pthread_mutex_unlock(&last_viewer_session_id_lock);
39 CDS_INIT_LIST_HEAD(&vsession->session_list);
40 CDS_INIT_LIST_HEAD(&vsession->unannounced_stream_list);
41 pthread_mutex_init(&vsession->unannounced_stream_list_lock, nullptr);
42 lttng_ht_node_init_u64(&vsession->viewer_session_n, vsession->id);
43 lttng_ht_add_unique_u64(viewer_sessions_ht, &vsession->viewer_session_n);
44end:
45 return vsession;
46}
47
48int viewer_session_set_trace_chunk_copy(struct relay_viewer_session *vsession,
49 struct lttng_trace_chunk *relay_session_trace_chunk)
50{
51 int ret = 0;
52 struct lttng_trace_chunk *viewer_chunk;
53
54 lttng_trace_chunk_put(vsession->current_trace_chunk);
55 vsession->current_trace_chunk = nullptr;
56
57 DBG("Copying relay session's current trace chunk to the viewer session");
58 if (!relay_session_trace_chunk) {
59 goto end;
60 }
61
62 viewer_chunk = lttng_trace_chunk_copy(relay_session_trace_chunk);
63 if (!viewer_chunk) {
64 ERR("Failed to create a viewer trace chunk from the relay session's current chunk");
65 ret = -1;
66 goto end;
67 }
68
69 vsession->current_trace_chunk = viewer_chunk;
70end:
71 return ret;
72}
73
74/* The existence of session must be guaranteed by the caller. */
75enum lttng_viewer_attach_return_code viewer_session_attach(struct relay_viewer_session *vsession,
76 struct relay_session *session)
77{
78 enum lttng_viewer_attach_return_code viewer_attach_status = LTTNG_VIEWER_ATTACH_OK;
79
80 ASSERT_LOCKED(session->lock);
81
82 /* Will not fail, as per the ownership guarantee. */
83 if (!session_get(session)) {
84 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
85 goto end;
86 }
87 if (session->viewer_attached) {
88 viewer_attach_status = LTTNG_VIEWER_ATTACH_ALREADY;
89 } else {
90 int ret;
91
92 session->viewer_attached = true;
93
94 ret = viewer_session_set_trace_chunk_copy(vsession, session->current_trace_chunk);
95 if (ret) {
96 /*
97 * The live protocol does not define a generic error
98 * value for the "attach" command. The "unknown"
99 * status is used so that the viewer may handle this
100 * failure as if the session didn't exist anymore.
101 */
102 DBG("Failed to create a viewer trace chunk from the current trace chunk of session \"%s\", returning LTTNG_VIEWER_ATTACH_UNK",
103 session->session_name);
104 viewer_attach_status = LTTNG_VIEWER_ATTACH_UNK;
105 }
106 }
107
108 if (viewer_attach_status == LTTNG_VIEWER_ATTACH_OK) {
109 pthread_mutex_lock(&vsession->session_list_lock);
110 /* Ownership is transfered to the list. */
111 cds_list_add_rcu(&session->viewer_session_node, &vsession->session_list);
112 pthread_mutex_unlock(&vsession->session_list_lock);
113
114 /*
115 * Immediately create new viewer streams for the attached session
116 * so that the viewer streams hold a reference on the any relay
117 * streams that could be unpublished between now and the next
118 * GET_NEW_STREAMS command from the live viewer.
119 */
120 uint32_t created = 0;
121 uint32_t total = 0;
122 uint32_t unsent = 0;
123 bool closed = false;
124 const int make_viewer_streams_ret = make_viewer_streams(session,
125 vsession,
126 LTTNG_VIEWER_SEEK_BEGINNING,
127 &total,
128 &unsent,
129 &created,
130 &closed);
131
132 if (make_viewer_streams_ret == 0) {
133 DBG("Created %d new viewer streams while attaching to relay session %" PRIu64,
134 created,
135 session->id);
136 } else {
137 /*
138 * Warning, since the creation of the streams will be retried when
139 * the viewer next sends the GET_NEW_STREAMS commands.
140 */
141 WARN("Failed to create new viewer streams while attaching to relay session %" PRIu64
142 ", ret=%d, total=%d, unsent=%d, created=%d, closed=%d",
143 session->id,
144 make_viewer_streams_ret,
145 total,
146 unsent,
147 created,
148 closed);
149 }
150 } else {
151 /* Put our local ref. */
152 session_put(session);
153 }
154end:
155 return viewer_attach_status;
156}
157
158/* The existence of session must be guaranteed by the caller. */
159static int viewer_session_detach(struct relay_viewer_session *vsession,
160 struct relay_session *session)
161{
162 int ret = 0;
163
164 pthread_mutex_lock(&session->lock);
165 if (!session->viewer_attached) {
166 ret = -1;
167 } else {
168 session->viewer_attached = false;
169 }
170
171 if (!ret) {
172 pthread_mutex_lock(&vsession->session_list_lock);
173 cds_list_del_rcu(&session->viewer_session_node);
174 pthread_mutex_unlock(&vsession->session_list_lock);
175 /* Release reference held by the list. */
176 session_put(session);
177 }
178
179 /* Safe since we know the session exists. */
180 pthread_mutex_unlock(&session->lock);
181 return ret;
182}
183
184void viewer_session_destroy(struct relay_viewer_session *vsession)
185{
186 struct lttng_ht_iter iter;
187
188 LTTNG_ASSERT(cds_list_empty(&vsession->unannounced_stream_list));
189
190 iter.iter.node = &vsession->viewer_session_n.node;
191 lttng_ht_del(viewer_sessions_ht, &iter);
192 lttng_trace_chunk_put(vsession->current_trace_chunk);
193 free(vsession);
194}
195
196/*
197 * Release ownership of all the streams of one session and detach the viewer.
198 */
199void viewer_session_close_one_session(struct relay_viewer_session *vsession,
200 struct relay_session *session)
201{
202 /*
203 * TODO: improvement: create more efficient list of
204 * vstream per session.
205 */
206 for (auto *vstream :
207 lttng::urcu::lfht_iteration_adapter<relay_viewer_stream,
208 decltype(relay_viewer_stream::stream_n),
209 &relay_viewer_stream::stream_n>(
210 *viewer_streams_ht->ht)) {
211 if (!viewer_stream_get(vstream)) {
212 continue;
213 }
214
215 if (vstream->stream->trace->session != session) {
216 viewer_stream_put(vstream);
217 continue;
218 }
219 /* Put local reference. */
220 viewer_stream_put(vstream);
221 /*
222 * We have reached one of the viewer stream's lifetime
223 * end condition. This "put" will cause the proper
224 * teardown of the viewer stream.
225 */
226 viewer_stream_put(vstream);
227 }
228
229 for (auto *vstream :
230 lttng::urcu::rcu_list_iteration_adapter<relay_viewer_stream,
231 &relay_viewer_stream::viewer_stream_node>(
232 vsession->unannounced_stream_list)) {
233 if (!viewer_stream_get(vstream)) {
234 continue;
235 }
236 if (vstream->stream->trace->session != session) {
237 viewer_stream_put(vstream);
238 continue;
239 }
240 pthread_mutex_lock(&vsession->unannounced_stream_list_lock);
241 cds_list_del_rcu(&vstream->viewer_stream_node);
242 pthread_mutex_unlock(&vsession->unannounced_stream_list_lock);
243 /* Local reference */
244 viewer_stream_put(vstream);
245 /* Reference from unannounced_stream_list */
246 viewer_stream_put(vstream);
247 }
248
249 lttng_trace_chunk_put(vsession->current_trace_chunk);
250 vsession->current_trace_chunk = nullptr;
251 viewer_session_detach(vsession, session);
252}
253
254void viewer_session_close(struct relay_viewer_session *vsession)
255{
256 for (auto *session :
257 lttng::urcu::rcu_list_iteration_adapter<relay_session,
258 &relay_session::viewer_session_node>(
259 vsession->session_list)) {
260 viewer_session_close_one_session(vsession, session);
261 }
262}
263
264/*
265 * Check if a connection is attached to a session.
266 * Return 1 if attached, 0 if not attached, a negative value on error.
267 */
268int viewer_session_is_attached(struct relay_viewer_session *vsession, struct relay_session *session)
269{
270 int found = 0;
271
272 pthread_mutex_lock(&session->lock);
273 if (!vsession) {
274 goto end;
275 }
276 if (!session->viewer_attached) {
277 goto end;
278 }
279
280 for (auto *session_it :
281 lttng::urcu::rcu_list_iteration_adapter<relay_session,
282 &relay_session::viewer_session_node>(
283 vsession->session_list)) {
284 if (session == session_it) {
285 found = 1;
286 break;
287 }
288 }
289
290end:
291 pthread_mutex_unlock(&session->lock);
292 return found;
293}
This page took 0.024841 seconds and 5 git commands to generate.