Fix deadlock: don't take channel lock in timer
[lttng-tools.git] / src / common / consumer-timer.c
... / ...
CommitLineData
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
19#define _GNU_SOURCE
20#include <assert.h>
21#include <inttypes.h>
22#include <signal.h>
23
24#include <common/common.h>
25
26#include "consumer-timer.h"
27#include "ust-consumer/ust-consumer.h"
28
29static struct timer_signal_data timer_signal = {
30 .tid = 0,
31 .setup_done = 0,
32 .qs_done = 0,
33 .lock = PTHREAD_MUTEX_INITIALIZER,
34};
35
36/*
37 * Set custom signal mask to current thread.
38 */
39static void setmask(sigset_t *mask)
40{
41 int ret;
42
43 ret = sigemptyset(mask);
44 if (ret) {
45 PERROR("sigemptyset");
46 }
47 ret = sigaddset(mask, LTTNG_CONSUMER_SIG_SWITCH);
48 if (ret) {
49 PERROR("sigaddset");
50 }
51 ret = sigaddset(mask, LTTNG_CONSUMER_SIG_TEARDOWN);
52 if (ret) {
53 PERROR("sigaddset");
54 }
55}
56
57/*
58 * Execute action on a timer switch.
59 *
60 * Beware: metadata_switch_timer() should *never* take a mutex also held
61 * while consumer_timer_switch_stop() is called. It would result in
62 * deadlocks.
63 */
64static void metadata_switch_timer(struct lttng_consumer_local_data *ctx,
65 int sig, siginfo_t *si, void *uc)
66{
67 int ret;
68 struct lttng_consumer_channel *channel;
69
70 channel = si->si_value.sival_ptr;
71 assert(channel);
72
73 if (channel->switch_timer_error) {
74 return;
75 }
76
77 DBG("Switch timer for channel %" PRIu64, channel->key);
78 switch (ctx->type) {
79 case LTTNG_CONSUMER32_UST:
80 case LTTNG_CONSUMER64_UST:
81 /*
82 * Locks taken by lttng_ustconsumer_request_metadata():
83 * - metadata_socket_lock
84 * - Calling lttng_ustconsumer_recv_metadata():
85 * - channel->metadata_cache->lock
86 * - Calling consumer_metadata_cache_flushed():
87 * - channel->timer_lock
88 * - channel->metadata_cache->lock
89 *
90 * Ensure that neither consumer_data.lock nor
91 * channel->lock are taken within this function, since
92 * they are held while consumer_timer_switch_stop() is
93 * called.
94 */
95 ret = lttng_ustconsumer_request_metadata(ctx, channel, 1);
96 if (ret < 0) {
97 channel->switch_timer_error = 1;
98 }
99 break;
100 case LTTNG_CONSUMER_KERNEL:
101 case LTTNG_CONSUMER_UNKNOWN:
102 assert(0);
103 break;
104 }
105}
106
107static
108void consumer_timer_signal_thread_qs(unsigned int signr)
109{
110 sigset_t pending_set;
111 int ret;
112
113 /*
114 * We need to be the only thread interacting with the thread
115 * that manages signals for teardown synchronization.
116 */
117 pthread_mutex_lock(&timer_signal.lock);
118
119 /* Ensure we don't have any signal queued for this channel. */
120 for (;;) {
121 ret = sigemptyset(&pending_set);
122 if (ret == -1) {
123 PERROR("sigemptyset");
124 }
125 ret = sigpending(&pending_set);
126 if (ret == -1) {
127 PERROR("sigpending");
128 }
129 if (!sigismember(&pending_set, LTTNG_CONSUMER_SIG_SWITCH)) {
130 break;
131 }
132 caa_cpu_relax();
133 }
134
135 /*
136 * From this point, no new signal handler will be fired that would try to
137 * access "chan". However, we still need to wait for any currently
138 * executing handler to complete.
139 */
140 cmm_smp_mb();
141 CMM_STORE_SHARED(timer_signal.qs_done, 0);
142 cmm_smp_mb();
143
144 /*
145 * Kill with LTTNG_CONSUMER_SIG_TEARDOWN, so signal management thread wakes
146 * up.
147 */
148 kill(getpid(), LTTNG_CONSUMER_SIG_TEARDOWN);
149
150 while (!CMM_LOAD_SHARED(timer_signal.qs_done)) {
151 caa_cpu_relax();
152 }
153 cmm_smp_mb();
154
155 pthread_mutex_unlock(&timer_signal.lock);
156}
157
158/*
159 * Set the timer for periodical metadata flush.
160 */
161void consumer_timer_switch_start(struct lttng_consumer_channel *channel,
162 unsigned int switch_timer_interval)
163{
164 int ret;
165 struct sigevent sev;
166 struct itimerspec its;
167
168 assert(channel);
169 assert(channel->key);
170
171 if (switch_timer_interval == 0) {
172 return;
173 }
174
175 sev.sigev_notify = SIGEV_SIGNAL;
176 sev.sigev_signo = LTTNG_CONSUMER_SIG_SWITCH;
177 sev.sigev_value.sival_ptr = channel;
178 ret = timer_create(CLOCKID, &sev, &channel->switch_timer);
179 if (ret == -1) {
180 PERROR("timer_create");
181 }
182 channel->switch_timer_enabled = 1;
183
184 its.it_value.tv_sec = switch_timer_interval / 1000000;
185 its.it_value.tv_nsec = switch_timer_interval % 1000000;
186 its.it_interval.tv_sec = its.it_value.tv_sec;
187 its.it_interval.tv_nsec = its.it_value.tv_nsec;
188
189 ret = timer_settime(channel->switch_timer, 0, &its, NULL);
190 if (ret == -1) {
191 PERROR("timer_settime");
192 }
193}
194
195/*
196 * Stop and delete timer.
197 */
198void consumer_timer_switch_stop(struct lttng_consumer_channel *channel)
199{
200 int ret;
201
202 assert(channel);
203
204 ret = timer_delete(channel->switch_timer);
205 if (ret == -1) {
206 PERROR("timer_delete");
207 }
208
209 consumer_timer_signal_thread_qs(LTTNG_CONSUMER_SIG_SWITCH);
210
211 channel->switch_timer = 0;
212 channel->switch_timer_enabled = 0;
213}
214
215/*
216 * Block the RT signals for the entire process. It must be called from the
217 * consumer main before creating the threads
218 */
219void consumer_signal_init(void)
220{
221 int ret;
222 sigset_t mask;
223
224 /* Block signal for entire process, so only our thread processes it. */
225 setmask(&mask);
226 ret = pthread_sigmask(SIG_BLOCK, &mask, NULL);
227 if (ret) {
228 errno = ret;
229 PERROR("pthread_sigmask");
230 }
231}
232
233/*
234 * This thread is the sighandler for signals LTTNG_CONSUMER_SIG_SWITCH and
235 * LTTNG_CONSUMER_SIG_TEARDOWN that are emitted by the periodic timer to check
236 * if new metadata is available.
237 */
238void *consumer_timer_metadata_thread(void *data)
239{
240 int signr;
241 sigset_t mask;
242 siginfo_t info;
243 struct lttng_consumer_local_data *ctx = data;
244
245 /* Only self thread will receive signal mask. */
246 setmask(&mask);
247 CMM_STORE_SHARED(timer_signal.tid, pthread_self());
248
249 while (1) {
250 signr = sigwaitinfo(&mask, &info);
251 if (signr == -1) {
252 if (errno != EINTR) {
253 PERROR("sigwaitinfo");
254 }
255 continue;
256 } else if (signr == LTTNG_CONSUMER_SIG_SWITCH) {
257 metadata_switch_timer(ctx, info.si_signo, &info, NULL);
258 } else if (signr == LTTNG_CONSUMER_SIG_TEARDOWN) {
259 cmm_smp_mb();
260 CMM_STORE_SHARED(timer_signal.qs_done, 1);
261 cmm_smp_mb();
262 DBG("Signal timer metadata thread teardown");
263 } else {
264 ERR("Unexpected signal %d\n", info.si_signo);
265 }
266 }
267
268 return NULL;
269}
This page took 0.027276 seconds and 4 git commands to generate.