2 * Copyright (C) 2012 - Julien Desfossez <jdesfossez@efficios.com>
3 * David Goulet <dgoulet@efficios.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License, version 2 only,
7 * as published by the Free Software Foundation.
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
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #ifndef LTTNG_RELAYD_H
20 #define LTTNG_RELAYD_H
25 #include <urcu/wfqueue.h>
26 #include <urcu/list.h>
28 #include <common/hashtable/hashtable.h>
29 #include <common/index/ctf-index.h>
31 #include "ctf-trace.h"
34 * Queue used to enqueue relay requests
36 struct relay_cmd_queue
{
37 struct cds_wfq_queue queue
;
41 enum connection_type
{
44 RELAY_VIEWER_COMMAND
= 3,
45 RELAY_VIEWER_NOTIFICATION
= 4,
49 * When we receive a stream, it gets stored in a list (on a per connection
50 * basis) until we have all the streams of the same channel and the metadata
51 * associated with it, then it gets flagged with viewer_ready.
53 struct relay_stream_recv_handle
{
54 uint64_t id
; /* stream handle */
55 struct cds_list_head node
;
59 * Represents a session for the relay point of view
61 struct relay_session
{
63 * This session id is used to identify a set of stream to a tracing session
64 * but also make sure we have a unique session id associated with a session
65 * daemon which can provide multiple data source.
68 struct lttcomm_sock
*sock
;
69 char session_name
[NAME_MAX
];
70 char hostname
[HOST_NAME_MAX
];
72 struct lttng_ht_node_ulong session_n
;
73 struct rcu_head rcu_node
;
74 uint32_t viewer_attached
;
75 uint32_t stream_count
;
76 /* Tell if this session is for a snapshot or not. */
77 unsigned int snapshot
:1;
80 * Indicate version protocol for this session. This is especially useful
81 * for the data thread that has no idea which version it operates on since
82 * linking control/data sockets is non trivial.
87 * Flag checked and exchanged with uatomic_cmpxchg to tell the
88 * viewer-side if new streams got added since the last check.
90 unsigned long new_streams
;
94 * Represents a stream in the relay
97 uint64_t stream_handle
;
98 uint64_t prev_seq
; /* previous data sequence number encountered */
99 struct lttng_ht_node_ulong stream_n
;
100 struct relay_session
*session
;
101 struct rcu_head rcu_node
;
103 /* FD on which to write the index data. */
105 /* FD on which to read the index data for the viewer. */
110 /* on-disk circular buffer of tracefiles */
111 uint64_t tracefile_size
;
112 uint64_t tracefile_size_current
;
113 uint64_t tracefile_count
;
114 uint64_t tracefile_count_current
;
115 /* To inform the viewer up to where it can go back in time. */
116 uint64_t oldest_tracefile_id
;
118 uint64_t total_index_received
;
119 struct relay_viewer_stream
*viewer_stream
;
120 uint64_t last_net_seq_num
;
123 * This node is added to the *control* connection hash table and the
124 * pointer is copied in here so we can access it when deleting this object.
125 * When deleting this, the ctf trace ht MUST NOT be destroyed. This happens
126 * at connection deletion.
128 struct lttng_ht_node_str ctf_trace_node
;
129 struct lttng_ht
*ctf_traces_ht
;
132 * To protect from concurrent read/update between the
133 * streaming-side and the viewer-side.
134 * This lock must be held, we reading/updating the
137 pthread_mutex_t lock
;
139 struct ctf_trace
*ctf_trace
;
141 * If the stream is inactive, this field is updated with the live beacon
142 * timestamp end, when it is active, this field == -1ULL.
144 uint64_t beacon_ts_end
;
146 * To protect the update of the close_write_flag and the checks of
147 * the tracefile_count_current.
148 * It is taken before checking whenever we need to know if the
149 * writer and reader are working in the same tracefile.
151 pthread_mutex_t viewer_stream_rotation_lock
;
153 /* Information telling us when to close the stream */
154 unsigned int close_flag
:1;
155 /* Indicate if the stream was initialized for a data pending command. */
156 unsigned int data_pending_check_done
:1;
157 unsigned int metadata_flag
:1;
159 * To detect when we start overwriting old data, it is used to
160 * update the oldest_tracefile_id.
162 unsigned int tracefile_overwrite
:1;
164 * Can this stream be used by a viewer or are we waiting for additional
167 unsigned int viewer_ready
:1;
171 * Shadow copy of the relay_stream structure for the viewer side. The only
172 * fields updated by the writer (streaming side) after allocation are :
173 * total_index_received and close_flag. Everything else is updated by the
174 * reader (viewer side).
176 struct relay_viewer_stream
{
177 uint64_t stream_handle
;
183 uint64_t last_sent_index
;
184 uint64_t total_index_received
;
185 uint64_t tracefile_count
;
186 uint64_t tracefile_count_current
;
187 /* Stop after reading this tracefile. */
188 uint64_t tracefile_count_last
;
189 struct lttng_ht_node_u64 stream_n
;
190 struct rcu_head rcu_node
;
191 struct ctf_trace
*ctf_trace
;
193 * This lock blocks only when the writer is about to start overwriting
194 * a file currently read by the reader.
196 * This is nested INSIDE the viewer_stream_rotation_lock.
198 pthread_mutex_t overwrite_lock
;
199 /* Information telling us if the stream is a metadata stream. */
200 unsigned int metadata_flag
:1;
202 * Information telling us that the stream is closed in write, so
203 * we don't expect new indexes and we can read up to EOF.
205 unsigned int close_write_flag
:1;
207 * If the streaming side closes a FD in use in the viewer side,
208 * it sets this flag to inform that it is a normal error.
210 unsigned int abort_flag
:1;
214 * Internal structure to map a socket with the corresponding session.
215 * A hashtable indexed on the socket FD is used for the lookups.
217 struct relay_command
{
218 struct lttcomm_sock
*sock
;
219 struct relay_session
*session
;
220 struct cds_wfq_node node
;
221 struct lttng_ht_node_ulong sock_n
;
222 struct rcu_head rcu_node
;
223 enum connection_type type
;
224 /* protocol version to use for this session */
227 struct lttng_ht
*ctf_traces_ht
; /* indexed by path name */
229 struct cds_list_head recv_head
;
230 unsigned int version_check_done
:1;
233 struct relay_local_data
{
234 struct lttng_ht
*sessions_ht
;
237 extern char *opt_output_path
;
239 extern struct lttng_ht
*relay_streams_ht
;
240 extern struct lttng_ht
*viewer_streams_ht
;
241 extern struct lttng_ht
*indexes_ht
;
243 extern const char *tracing_group_name
;
245 extern const char * const config_section_name
;
247 extern int thread_quit_pipe
[2];
249 struct relay_stream
*relay_stream_find_by_id(uint64_t stream_id
);
250 void lttng_relay_notify_ready(void);
252 #endif /* LTTNG_RELAYD_H */