2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include <common/hashtable/hashtable.h>
22 #include <common/common.h>
23 #include <common/utils.h>
26 #include "lttng-sessiond.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
31 int ht_cleanup_quit_pipe
[2] = { -1, -1 };
34 * Check if the ht_cleanup thread quit pipe was triggered.
36 * Return true if it was triggered else false;
38 static bool check_quit_pipe(int fd
, uint32_t events
)
40 return (fd
== ht_cleanup_quit_pipe
[0] && (events
& LPOLLIN
));
43 static int init_pipe(int *pipe_fds
)
49 PERROR("ht_cleanup thread quit pipe");
53 for (i
= 0; i
< 2; i
++) {
54 ret
= fcntl(pipe_fds
[i
], F_SETFD
, FD_CLOEXEC
);
56 PERROR("fcntl ht_cleanup_quit_pipe");
65 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
67 static int set_pollset(struct lttng_poll_event
*events
, size_t size
)
71 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
76 ret
= lttng_poll_add(events
, ht_cleanup_quit_pipe
[0],
82 ret
= lttng_poll_add(events
, ht_cleanup_pipe
[0], LPOLLIN
| LPOLLERR
);
84 DBG("[ht-thread] lttng_poll_add error %d.", ret
);
94 static void *thread_ht_cleanup(void *data
)
96 int ret
, i
, pollfd
, err
= -1;
98 uint32_t revents
, nb_fd
;
99 struct lttng_poll_event events
;
101 DBG("[ht-thread] startup.");
103 rcu_register_thread();
106 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_HT_CLEANUP
);
108 if (testpoint(sessiond_thread_ht_cleanup
)) {
109 DBG("[ht-thread] testpoint.");
110 goto error_testpoint
;
113 health_code_update();
115 ret
= set_pollset(&events
, 2);
117 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret
);
118 goto error_poll_create
;
121 health_code_update();
124 DBG3("[ht-thread] Polling.");
126 ret
= lttng_poll_wait(&events
, -1);
127 DBG3("[ht-thread] Returning from poll on %d fds.",
128 LTTNG_POLL_GETNB(&events
));
132 * Restart interrupted system call.
134 if (errno
== EINTR
) {
141 for (i
= 0; i
< nb_fd
; i
++) {
144 health_code_update();
146 /* Fetch once the poll data */
147 revents
= LTTNG_POLL_GETEV(&events
, i
);
148 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
151 /* No activity for this FD (poll implementation). */
155 if (pollfd
!= ht_cleanup_pipe
[0]) {
159 if (revents
& LPOLLIN
) {
160 /* Get socket from dispatch thread. */
161 size_ret
= lttng_read(ht_cleanup_pipe
[0], &ht
,
163 if (size_ret
< sizeof(ht
)) {
164 PERROR("ht cleanup notify pipe");
167 health_code_update();
169 * The whole point of this thread is to call
170 * lttng_ht_destroy from a context that is NOT:
171 * 1) a read-side RCU lock,
172 * 2) a call_rcu thread.
174 lttng_ht_destroy(ht
);
176 health_code_update();
177 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
178 ERR("ht cleanup pipe error");
181 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
186 for (i
= 0; i
< nb_fd
; i
++) {
187 health_code_update();
189 /* Fetch once the poll data */
190 revents
= LTTNG_POLL_GETEV(&events
, i
);
191 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
194 /* No activity for this FD (poll implementation). */
198 if (pollfd
== ht_cleanup_pipe
[0]) {
202 /* Thread quit pipe has been closed. Killing thread. */
203 ret
= check_quit_pipe(pollfd
, revents
);
206 DBG("[ht-cleanup] quit.");
214 lttng_poll_clean(&events
);
217 DBG("[ht-cleanup] Thread terminates.");
220 ERR("Health error occurred in %s", __func__
);
222 health_unregister(health_sessiond
);
223 rcu_thread_offline();
224 rcu_unregister_thread();
228 int init_ht_cleanup_thread(pthread_t
*thread
)
232 ret
= init_pipe(ht_cleanup_pipe
);
237 ret
= init_pipe(ht_cleanup_quit_pipe
);
239 goto error_quit_pipe
;
242 ret
= pthread_create(thread
, default_pthread_attr(), thread_ht_cleanup
,
246 PERROR("pthread_create ht_cleanup");
254 utils_close_pipe(ht_cleanup_quit_pipe
);
256 utils_close_pipe(ht_cleanup_pipe
);
260 int fini_ht_cleanup_thread(pthread_t
*thread
)
264 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
266 ERR("write error on ht_cleanup quit pipe");
270 ret
= pthread_join(*thread
, NULL
);
273 PERROR("pthread_join ht cleanup thread");
275 utils_close_pipe(ht_cleanup_pipe
);
276 utils_close_pipe(ht_cleanup_quit_pipe
);