consumerd: refactor: combine duplicated check_*_functions
[lttng-tools.git] / src / common / consumer / consumer-timer.c
CommitLineData
331744e3
JD
1/*
2 * Copyright (C) 2012 - Julien Desfossez <julien.desfossez@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
6c1c0768 19#define _LGPL_SOURCE
331744e3
JD
20#include <assert.h>
21#include <inttypes.h>
22#include <signal.h>
23
51a9e1c7 24#include <bin/lttng-consumerd/health-consumerd.h>
331744e3 25#include <common/common.h>
f263b7fd 26#include <common/compat/endian.h>
d3e2ba59
JD
27#include <common/kernel-ctl/kernel-ctl.h>
28#include <common/kernel-consumer/kernel-consumer.h>
c8fea79c
JR
29#include <common/consumer/consumer-stream.h>
30#include <common/consumer/consumer-timer.h>
31#include <common/consumer/consumer-testpoint.h>
32#include <common/ust-consumer/ust-consumer.h>
331744e3 33
9304a209
JG
34typedef int (*sample_positions_cb)(struct lttng_consumer_stream *stream);
35typedef int (*get_consumed_cb)(struct lttng_consumer_stream *stream,
36 unsigned long *consumed);
37typedef int (*get_produced_cb)(struct lttng_consumer_stream *stream,
38 unsigned long *produced);
39typedef int (*flush_index_cb)(struct lttng_consumer_stream *stream);
40
2b8f8754
MD
41static struct timer_signal_data timer_signal = {
42 .tid = 0,
43 .setup_done = 0,
44 .qs_done = 0,
45 .lock = PTHREAD_MUTEX_INITIALIZER,
46};
331744e3
JD
47
48/*
49 * Set custom signal mask to current thread.
50 */
51static void setmask(sigset_t *mask)
52{
53 int ret;
54
55 ret = sigemptyset(mask);
56 if (ret) {
57 PERROR("sigemptyset");
58 }
59 ret = sigaddset(mask, LTTNG_CONSUMER_SIG_SWITCH);
60 if (ret) {
d3e2ba59 61 PERROR("sigaddset switch");
331744e3
JD
62 }
63 ret = sigaddset(mask, LTTNG_CONSUMER_SIG_TEARDOWN);
64 if (ret) {
d3e2ba59
JD
65 PERROR("sigaddset teardown");
66 }
67 ret = sigaddset(mask, LTTNG_CONSUMER_SIG_LIVE);
68 if (ret) {
69 PERROR("sigaddset live");
331744e3 70 }
5383626c
MD
71 ret = sigaddset(mask, LTTNG_CONSUMER_SIG_EXIT);
72 if (ret) {
73 PERROR("sigaddset exit");
74 }
331744e3
JD
75}
76
77/*
78 * Execute action on a timer switch.
d98a47c7
MD
79 *
80 * Beware: metadata_switch_timer() should *never* take a mutex also held
81 * while consumer_timer_switch_stop() is called. It would result in
82 * deadlocks.
331744e3
JD
83 */
84static void metadata_switch_timer(struct lttng_consumer_local_data *ctx,
85 int sig, siginfo_t *si, void *uc)
86{
87 int ret;
88 struct lttng_consumer_channel *channel;
89
90 channel = si->si_value.sival_ptr;
91 assert(channel);
92
4419b4fb
MD
93 if (channel->switch_timer_error) {
94 return;
95 }
96
331744e3
JD
97 DBG("Switch timer for channel %" PRIu64, channel->key);
98 switch (ctx->type) {
99 case LTTNG_CONSUMER32_UST:
100 case LTTNG_CONSUMER64_UST:
4fa3dc0e
MD
101 /*
102 * Locks taken by lttng_ustconsumer_request_metadata():
103 * - metadata_socket_lock
104 * - Calling lttng_ustconsumer_recv_metadata():
f82d9449 105 * - channel->metadata_cache->lock
4fa3dc0e 106 * - Calling consumer_metadata_cache_flushed():
5e41ebe1
MD
107 * - channel->timer_lock
108 * - channel->metadata_cache->lock
4fa3dc0e 109 *
5e41ebe1
MD
110 * Ensure that neither consumer_data.lock nor
111 * channel->lock are taken within this function, since
112 * they are held while consumer_timer_switch_stop() is
113 * called.
4fa3dc0e 114 */
94d49140 115 ret = lttng_ustconsumer_request_metadata(ctx, channel, 1, 1);
331744e3 116 if (ret < 0) {
4419b4fb 117 channel->switch_timer_error = 1;
331744e3
JD
118 }
119 break;
120 case LTTNG_CONSUMER_KERNEL:
121 case LTTNG_CONSUMER_UNKNOWN:
122 assert(0);
123 break;
124 }
125}
126
528f2ffa
JD
127static int send_empty_index(struct lttng_consumer_stream *stream, uint64_t ts,
128 uint64_t stream_id)
d3e2ba59
JD
129{
130 int ret;
50adc264 131 struct ctf_packet_index index;
d3e2ba59
JD
132
133 memset(&index, 0, sizeof(index));
528f2ffa 134 index.stream_id = htobe64(stream_id);
d3e2ba59
JD
135 index.timestamp_end = htobe64(ts);
136 ret = consumer_stream_write_index(stream, &index);
137 if (ret < 0) {
138 goto error;
139 }
140
141error:
142 return ret;
143}
144
c585821b 145int consumer_flush_kernel_index(struct lttng_consumer_stream *stream)
d3e2ba59 146{
528f2ffa 147 uint64_t ts, stream_id;
d3e2ba59
JD
148 int ret;
149
d3e2ba59
JD
150 ret = kernctl_get_current_timestamp(stream->wait_fd, &ts);
151 if (ret < 0) {
152 ERR("Failed to get the current timestamp");
c585821b 153 goto end;
d3e2ba59
JD
154 }
155 ret = kernctl_buffer_flush(stream->wait_fd);
156 if (ret < 0) {
157 ERR("Failed to flush kernel stream");
c585821b 158 goto end;
d3e2ba59
JD
159 }
160 ret = kernctl_snapshot(stream->wait_fd);
161 if (ret < 0) {
32af2c95 162 if (ret != -EAGAIN && ret != -ENODATA) {
08b1dcd3 163 PERROR("live timer kernel snapshot");
d3e2ba59 164 ret = -1;
c585821b 165 goto end;
d3e2ba59 166 }
528f2ffa
JD
167 ret = kernctl_get_stream_id(stream->wait_fd, &stream_id);
168 if (ret < 0) {
169 PERROR("kernctl_get_stream_id");
c585821b 170 goto end;
528f2ffa 171 }
d3e2ba59 172 DBG("Stream %" PRIu64 " empty, sending beacon", stream->key);
528f2ffa 173 ret = send_empty_index(stream, ts, stream_id);
d3e2ba59 174 if (ret < 0) {
c585821b 175 goto end;
d3e2ba59
JD
176 }
177 }
178 ret = 0;
c585821b 179end:
d3e2ba59
JD
180 return ret;
181}
182
9304a209
JG
183static int check_stream(struct lttng_consumer_stream *stream,
184 flush_index_cb flush_index)
d3e2ba59 185{
d3e2ba59
JD
186 int ret;
187
d3e2ba59
JD
188 /*
189 * While holding the stream mutex, try to take a snapshot, if it
190 * succeeds, it means that data is ready to be sent, just let the data
191 * thread handle that. Otherwise, if the snapshot returns EAGAIN, it
192 * means that there is no data to read after the flush, so we can
193 * safely send the empty index.
c585821b
MD
194 *
195 * Doing a trylock and checking if waiting on metadata if
196 * trylock fails. Bail out of the stream is indeed waiting for
197 * metadata to be pushed. Busy wait on trylock otherwise.
d3e2ba59 198 */
c585821b
MD
199 for (;;) {
200 ret = pthread_mutex_trylock(&stream->lock);
201 switch (ret) {
202 case 0:
203 break; /* We have the lock. */
204 case EBUSY:
205 pthread_mutex_lock(&stream->metadata_timer_lock);
206 if (stream->waiting_on_metadata) {
207 ret = 0;
208 stream->missed_metadata_flush = true;
209 pthread_mutex_unlock(&stream->metadata_timer_lock);
210 goto end; /* Bail out. */
211 }
212 pthread_mutex_unlock(&stream->metadata_timer_lock);
213 /* Try again. */
214 caa_cpu_relax();
215 continue;
216 default:
217 ERR("Unexpected pthread_mutex_trylock error %d", ret);
218 ret = -1;
219 goto end;
220 }
221 break;
222 }
9304a209 223 ret = flush_index(stream);
c585821b
MD
224 pthread_mutex_unlock(&stream->lock);
225end:
226 return ret;
227}
228
229int consumer_flush_ust_index(struct lttng_consumer_stream *stream)
230{
231 uint64_t ts, stream_id;
232 int ret;
233
94d49140
JD
234 ret = cds_lfht_is_node_deleted(&stream->node.node);
235 if (ret) {
c585821b 236 goto end;
94d49140
JD
237 }
238
84a182ce 239 ret = lttng_ustconsumer_get_current_timestamp(stream, &ts);
d3e2ba59
JD
240 if (ret < 0) {
241 ERR("Failed to get the current timestamp");
c585821b 242 goto end;
d3e2ba59 243 }
84a182ce
DG
244 lttng_ustconsumer_flush_buffer(stream, 1);
245 ret = lttng_ustconsumer_take_snapshot(stream);
d3e2ba59 246 if (ret < 0) {
94d49140 247 if (ret != -EAGAIN) {
d3e2ba59
JD
248 ERR("Taking UST snapshot");
249 ret = -1;
c585821b 250 goto end;
d3e2ba59 251 }
70190e1c 252 ret = lttng_ustconsumer_get_stream_id(stream, &stream_id);
528f2ffa
JD
253 if (ret < 0) {
254 PERROR("ustctl_get_stream_id");
c585821b 255 goto end;
528f2ffa 256 }
d3e2ba59 257 DBG("Stream %" PRIu64 " empty, sending beacon", stream->key);
528f2ffa 258 ret = send_empty_index(stream, ts, stream_id);
d3e2ba59 259 if (ret < 0) {
c585821b 260 goto end;
d3e2ba59
JD
261 }
262 }
263 ret = 0;
c585821b
MD
264end:
265 return ret;
266}
d3e2ba59 267
d3e2ba59
JD
268/*
269 * Execute action on a live timer
270 */
271static void live_timer(struct lttng_consumer_local_data *ctx,
272 int sig, siginfo_t *si, void *uc)
273{
274 int ret;
275 struct lttng_consumer_channel *channel;
276 struct lttng_consumer_stream *stream;
d3e2ba59 277 struct lttng_ht_iter iter;
9304a209
JG
278 const struct lttng_ht *ht = consumer_data.stream_per_chan_id_ht;
279 const flush_index_cb flush_index =
280 ctx->type == LTTNG_CONSUMER_KERNEL ?
281 consumer_flush_kernel_index :
282 consumer_flush_ust_index;
d3e2ba59
JD
283
284 channel = si->si_value.sival_ptr;
285 assert(channel);
286
287 if (channel->switch_timer_error) {
288 goto error;
289 }
d3e2ba59
JD
290
291 DBG("Live timer for channel %" PRIu64, channel->key);
292
293 rcu_read_lock();
9304a209
JG
294 cds_lfht_for_each_entry_duplicate(ht->ht,
295 ht->hash_fct(&channel->key, lttng_ht_seed),
296 ht->match_fct, &channel->key, &iter.iter,
297 stream, node_channel_id.node) {
298 ret = check_stream(stream, flush_index);
299 if (ret < 0) {
300 goto error_unlock;
d3e2ba59 301 }
d3e2ba59
JD
302 }
303
304error_unlock:
305 rcu_read_unlock();
306
307error:
308 return;
309}
310
2b8f8754
MD
311static
312void consumer_timer_signal_thread_qs(unsigned int signr)
313{
314 sigset_t pending_set;
315 int ret;
316
317 /*
318 * We need to be the only thread interacting with the thread
319 * that manages signals for teardown synchronization.
320 */
321 pthread_mutex_lock(&timer_signal.lock);
322
323 /* Ensure we don't have any signal queued for this channel. */
324 for (;;) {
325 ret = sigemptyset(&pending_set);
326 if (ret == -1) {
327 PERROR("sigemptyset");
328 }
329 ret = sigpending(&pending_set);
330 if (ret == -1) {
331 PERROR("sigpending");
332 }
6f7cc0be 333 if (!sigismember(&pending_set, signr)) {
2b8f8754
MD
334 break;
335 }
336 caa_cpu_relax();
337 }
338
339 /*
340 * From this point, no new signal handler will be fired that would try to
341 * access "chan". However, we still need to wait for any currently
342 * executing handler to complete.
343 */
344 cmm_smp_mb();
345 CMM_STORE_SHARED(timer_signal.qs_done, 0);
346 cmm_smp_mb();
347
348 /*
349 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
350 * up.
351 */
352 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN);
353
354 while (!CMM_LOAD_SHARED(timer_signal.qs_done)) {
355 caa_cpu_relax();
356 }
357 cmm_smp_mb();
358
359 pthread_mutex_unlock(&timer_signal.lock);
360}
361
331744e3
JD
362/*
363 * Set the timer for periodical metadata flush.
364 */
365void consumer_timer_switch_start(struct lttng_consumer_channel *channel,
366 unsigned int switch_timer_interval)
367{
368 int ret;
369 struct sigevent sev;
370 struct itimerspec its;
371
372 assert(channel);
373 assert(channel->key);
374
375 if (switch_timer_interval == 0) {
376 return;
377 }
378
379 sev.sigev_notify = SIGEV_SIGNAL;
380 sev.sigev_signo = LTTNG_CONSUMER_SIG_SWITCH;
381 sev.sigev_value.sival_ptr = channel;
382 ret = timer_create(CLOCKID, &sev, &channel->switch_timer);
383 if (ret == -1) {
384 PERROR("timer_create");
385 }
386 channel->switch_timer_enabled = 1;
387
388 its.it_value.tv_sec = switch_timer_interval / 1000000;
69f60d21 389 its.it_value.tv_nsec = (switch_timer_interval % 1000000) * 1000;
331744e3
JD
390 its.it_interval.tv_sec = its.it_value.tv_sec;
391 its.it_interval.tv_nsec = its.it_value.tv_nsec;
392
393 ret = timer_settime(channel->switch_timer, 0, &its, NULL);
394 if (ret == -1) {
395 PERROR("timer_settime");
396 }
397}
398
399/*
400 * Stop and delete timer.
401 */
402void consumer_timer_switch_stop(struct lttng_consumer_channel *channel)
403{
404 int ret;
331744e3
JD
405
406 assert(channel);
407
408 ret = timer_delete(channel->switch_timer);
409 if (ret == -1) {
410 PERROR("timer_delete");
411 }
412
2b8f8754 413 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH);
331744e3 414
2b8f8754
MD
415 channel->switch_timer = 0;
416 channel->switch_timer_enabled = 0;
331744e3
JD
417}
418
d3e2ba59
JD
419/*
420 * Set the timer for the live mode.
421 */
422void consumer_timer_live_start(struct lttng_consumer_channel *channel,
423 int live_timer_interval)
424{
425 int ret;
426 struct sigevent sev;
427 struct itimerspec its;
428
429 assert(channel);
430 assert(channel->key);
431
fac41e72 432 if (live_timer_interval <= 0) {
d3e2ba59
JD
433 return;
434 }
435
436 sev.sigev_notify = SIGEV_SIGNAL;
437 sev.sigev_signo = LTTNG_CONSUMER_SIG_LIVE;
438 sev.sigev_value.sival_ptr = channel;
439 ret = timer_create(CLOCKID, &sev, &channel->live_timer);
440 if (ret == -1) {
441 PERROR("timer_create");
442 }
443 channel->live_timer_enabled = 1;
444
445 its.it_value.tv_sec = live_timer_interval / 1000000;
69f60d21 446 its.it_value.tv_nsec = (live_timer_interval % 1000000) * 1000;
d3e2ba59
JD
447 its.it_interval.tv_sec = its.it_value.tv_sec;
448 its.it_interval.tv_nsec = its.it_value.tv_nsec;
449
450 ret = timer_settime(channel->live_timer, 0, &its, NULL);
451 if (ret == -1) {
452 PERROR("timer_settime");
453 }
454}
455
456/*
457 * Stop and delete timer.
458 */
459void consumer_timer_live_stop(struct lttng_consumer_channel *channel)
460{
461 int ret;
462
463 assert(channel);
464
465 ret = timer_delete(channel->live_timer);
466 if (ret == -1) {
467 PERROR("timer_delete");
468 }
469
470 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_LIVE);
471
472 channel->live_timer = 0;
473 channel->live_timer_enabled = 0;
474}
475
331744e3
JD
476/*
477 * Block the RT signals for the entire process. It must be called from the
478 * consumer main before creating the threads
479 */
73664f81 480int consumer_signal_init(void)
331744e3
JD
481{
482 int ret;
483 sigset_t mask;
484
485 /* Block signal for entire process, so only our thread processes it. */
486 setmask(&mask);
487 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
488 if (ret) {
489 errno = ret;
490 PERROR("pthread_sigmask");
73664f81 491 return -1;
331744e3 492 }
73664f81 493 return 0;
331744e3
JD
494}
495
496/*
d3e2ba59 497 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH,
5383626c
MD
498 * LTTNG_CONSUMER_SIG_TEARDOWN, LTTNG_CONSUMER_SIG_LIVE, and
499 * LTTNG_CONSUMER_SIG_EXIT.
331744e3 500 */
d3e2ba59 501void *consumer_timer_thread(void *data)
331744e3
JD
502{
503 int signr;
504 sigset_t mask;
505 siginfo_t info;
506 struct lttng_consumer_local_data *ctx = data;
507
8a9acb74
MD
508 rcu_register_thread();
509
1fc79fb4
MD
510 health_register(health_consumerd, HEALTH_CONSUMERD_TYPE_METADATA_TIMER);
511
2d57de81
MD
512 if (testpoint(consumerd_thread_metadata_timer)) {
513 goto error_testpoint;
514 }
515
9ce5646a
MD
516 health_code_update();
517
331744e3
JD
518 /* Only self thread will receive signal mask. */
519 setmask(&mask);
520 CMM_STORE_SHARED(timer_signal.tid, pthread_self());
521
522 while (1) {
9ce5646a
MD
523 health_code_update();
524
525 health_poll_entry();
331744e3 526 signr = sigwaitinfo(&mask, &info);
9ce5646a 527 health_poll_exit();
331744e3
JD
528 if (signr == -1) {
529 if (errno != EINTR) {
530 PERROR("sigwaitinfo");
531 }
532 continue;
533 } else if (signr == LTTNG_CONSUMER_SIG_SWITCH) {
534 metadata_switch_timer(ctx, info.si_signo, &info, NULL);
535 } else if (signr == LTTNG_CONSUMER_SIG_TEARDOWN) {
536 cmm_smp_mb();
537 CMM_STORE_SHARED(timer_signal.qs_done, 1);
538 cmm_smp_mb();
539 DBG("Signal timer metadata thread teardown");
d3e2ba59
JD
540 } else if (signr == LTTNG_CONSUMER_SIG_LIVE) {
541 live_timer(ctx, info.si_signo, &info, NULL);
5383626c
MD
542 } else if (signr == LTTNG_CONSUMER_SIG_EXIT) {
543 assert(consumer_quit);
544 goto end;
331744e3
JD
545 } else {
546 ERR("Unexpected signal %d\n", info.si_signo);
547 }
548 }
549
2d57de81
MD
550error_testpoint:
551 /* Only reached in testpoint error */
552 health_error();
5383626c 553end:
1fc79fb4 554 health_unregister(health_consumerd);
8a9acb74 555 rcu_unregister_thread();
331744e3
JD
556 return NULL;
557}
This page took 0.076685 seconds and 5 git commands to generate.