Commit | Line | Data |
---|---|---|
7cdc2bab MD |
1 | /* |
2 | * Copyright 2016 - Philippe Proulx <pproulx@efficios.com> | |
3 | * Copyright 2016 - Jérémie Galarneau <jeremie.galarneau@efficios.com> | |
4 | * Copyright 2010-2011 - EfficiOS Inc. and Linux Foundation | |
5 | * | |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
22 | * SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdint.h> | |
27 | #include <stdlib.h> | |
28 | #include <stdbool.h> | |
29 | #include <glib.h> | |
30 | #include <inttypes.h> | |
31 | #include <sys/mman.h> | |
32 | #include <babeltrace/ctf-ir/stream.h> | |
33 | #include "../common/notif-iter/notif-iter.h" | |
34 | #include <assert.h> | |
35 | #include "data-stream.h" | |
36 | ||
37 | #define PRINT_ERR_STREAM lttng_live->error_fp | |
38 | #define PRINT_PREFIX "lttng-live-data-stream" | |
39 | #define PRINT_DBG_CHECK lttng_live_debug | |
40 | #include "../print.h" | |
41 | ||
42 | static | |
43 | enum bt_ctf_notif_iter_medium_status medop_request_bytes( | |
44 | size_t request_sz, uint8_t **buffer_addr, | |
45 | size_t *buffer_sz, void *data) | |
46 | { | |
47 | enum bt_ctf_notif_iter_medium_status status = | |
48 | BT_CTF_NOTIF_ITER_MEDIUM_STATUS_OK; | |
49 | struct lttng_live_stream_iterator *stream = data; | |
50 | struct lttng_live_trace *trace = stream->trace; | |
51 | struct lttng_live_session *session = trace->session; | |
52 | struct lttng_live_component *lttng_live = session->lttng_live; | |
53 | uint64_t recv_len = 0; | |
54 | uint64_t len_left; | |
55 | uint64_t read_len; | |
56 | //int i; | |
57 | ||
58 | len_left = stream->base_offset + stream->len - stream->offset; | |
59 | if (!len_left) { | |
60 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; | |
61 | status = BT_CTF_NOTIF_ITER_MEDIUM_STATUS_AGAIN; | |
62 | return status; | |
63 | } | |
64 | read_len = MIN(request_sz, stream->buflen); | |
65 | read_len = MIN(read_len, len_left); | |
66 | status = lttng_live_get_stream_bytes(lttng_live, | |
67 | stream, stream->buf, stream->offset, | |
68 | read_len, &recv_len); | |
69 | #if 0 //DEBUG | |
70 | for (i = 0; i < recv_len; i++) { | |
71 | fprintf(stderr, "%x ", stream->buf[i]); | |
72 | } | |
73 | fprintf(stderr, "\n"); | |
74 | #endif | |
75 | *buffer_addr = stream->buf; | |
76 | *buffer_sz = recv_len; | |
77 | stream->offset += recv_len; | |
78 | return status; | |
79 | } | |
80 | ||
81 | static | |
82 | struct bt_ctf_stream *medop_get_stream( | |
83 | struct bt_ctf_stream_class *stream_class, void *data) | |
84 | { | |
85 | struct lttng_live_stream_iterator *lttng_live_stream = data; | |
86 | struct lttng_live_trace *trace = lttng_live_stream->trace; | |
87 | struct lttng_live_session *session = trace->session; | |
88 | struct lttng_live_component *lttng_live = session->lttng_live; | |
89 | ||
90 | if (!lttng_live_stream->stream) { | |
91 | int64_t id = bt_ctf_stream_class_get_id(stream_class); | |
92 | ||
93 | PDBG("Creating stream %s out of stream class %" PRId64 "\n", | |
94 | lttng_live_stream->name, id); | |
95 | lttng_live_stream->stream = bt_ctf_stream_create(stream_class, | |
96 | lttng_live_stream->name); | |
97 | if (!lttng_live_stream->stream) { | |
98 | PERR("Cannot create stream %s (stream class %" PRId64 ")\n", | |
99 | lttng_live_stream->name, id); | |
100 | } | |
101 | } | |
102 | ||
103 | return lttng_live_stream->stream; | |
104 | } | |
105 | ||
106 | static struct bt_ctf_notif_iter_medium_ops medops = { | |
107 | .request_bytes = medop_request_bytes, | |
108 | .get_stream = medop_get_stream, | |
109 | }; | |
110 | ||
111 | BT_HIDDEN | |
112 | enum bt_ctf_lttng_live_iterator_status lttng_live_lazy_notif_init( | |
113 | struct lttng_live_session *session) | |
114 | { | |
115 | struct lttng_live_component *lttng_live = session->lttng_live; | |
116 | struct lttng_live_trace *trace; | |
117 | ||
118 | if (!session->lazy_stream_notif_init) { | |
119 | return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
120 | } | |
121 | ||
122 | bt_list_for_each_entry(trace, &session->traces, node) { | |
123 | struct lttng_live_stream_iterator *stream; | |
124 | ||
125 | bt_list_for_each_entry(stream, &trace->streams, node) { | |
126 | if (stream->notif_iter) { | |
127 | continue; | |
128 | } | |
129 | stream->notif_iter = bt_ctf_notif_iter_create(trace->trace, | |
130 | lttng_live->max_query_size, medops, | |
131 | stream, lttng_live->error_fp); | |
132 | if (!stream->notif_iter) { | |
133 | goto error; | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | session->lazy_stream_notif_init = false; | |
139 | ||
140 | return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_OK; | |
141 | ||
142 | error: | |
143 | return BT_CTF_LTTNG_LIVE_ITERATOR_STATUS_ERROR; | |
144 | } | |
145 | ||
146 | BT_HIDDEN | |
147 | struct lttng_live_stream_iterator *lttng_live_stream_iterator_create( | |
148 | struct lttng_live_session *session, | |
149 | uint64_t ctf_trace_id, | |
150 | uint64_t stream_id) | |
151 | { | |
152 | struct lttng_live_component *lttng_live = session->lttng_live; | |
153 | struct lttng_live_stream_iterator *stream = | |
154 | g_new0(struct lttng_live_stream_iterator, 1); | |
155 | struct lttng_live_trace *trace; | |
156 | int ret; | |
157 | ||
158 | trace = lttng_live_ref_trace(session, ctf_trace_id); | |
159 | if (!trace) { | |
160 | goto error; | |
161 | } | |
162 | ||
163 | stream->p.type = LIVE_STREAM_TYPE_STREAM; | |
164 | stream->trace = trace; | |
165 | stream->state = LTTNG_LIVE_STREAM_ACTIVE_NO_DATA; | |
166 | stream->viewer_stream_id = stream_id; | |
167 | stream->ctf_stream_class_id = -1ULL; | |
168 | stream->last_returned_inactivity_timestamp = INT64_MIN; | |
169 | ||
170 | if (trace->trace) { | |
171 | stream->notif_iter = bt_ctf_notif_iter_create(trace->trace, | |
172 | lttng_live->max_query_size, medops, | |
173 | stream, lttng_live->error_fp); | |
174 | if (!stream->notif_iter) { | |
175 | goto error; | |
176 | } | |
177 | } | |
178 | stream->buf = g_new0(uint8_t, session->lttng_live->max_query_size); | |
179 | stream->buflen = session->lttng_live->max_query_size; | |
180 | ||
181 | ret = lttng_live_add_port(lttng_live, stream); | |
182 | assert(!ret); | |
183 | ||
184 | bt_list_add(&stream->node, &trace->streams); | |
185 | ||
186 | goto end; | |
187 | error: | |
188 | /* Do not touch "borrowed" file. */ | |
189 | lttng_live_stream_iterator_destroy(stream); | |
190 | stream = NULL; | |
191 | end: | |
192 | return stream; | |
193 | } | |
194 | ||
195 | BT_HIDDEN | |
196 | void lttng_live_stream_iterator_destroy(struct lttng_live_stream_iterator *stream) | |
197 | { | |
198 | struct lttng_live_component *lttng_live; | |
199 | int ret; | |
200 | ||
201 | if (!stream) { | |
202 | return; | |
203 | } | |
204 | ||
205 | lttng_live = stream->trace->session->lttng_live; | |
206 | ret = lttng_live_remove_port(lttng_live, stream->port); | |
207 | assert(!ret); | |
208 | ||
209 | if (stream->stream) { | |
210 | BT_PUT(stream->stream); | |
211 | } | |
212 | ||
213 | if (stream->notif_iter) { | |
214 | bt_ctf_notif_iter_destroy(stream->notif_iter); | |
215 | } | |
216 | g_free(stream->buf); | |
217 | BT_PUT(stream->packet_end_notif_queue); | |
218 | bt_list_del(&stream->node); | |
219 | /* | |
220 | * Ensure we poke the trace metadata in the future, which is | |
221 | * required to release the metadata reference on the trace. | |
222 | */ | |
223 | stream->trace->new_metadata_needed = true; | |
224 | lttng_live_unref_trace(stream->trace); | |
225 | g_free(stream); | |
226 | } |