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"
30 #include "ht-cleanup.h"
32 static int ht_cleanup_quit_pipe
[2] = { -1, -1 };
35 * Check if the ht_cleanup thread quit pipe was triggered.
37 * Return true if it was triggered else false;
39 static bool check_quit_pipe(int fd
, uint32_t events
)
41 return (fd
== ht_cleanup_quit_pipe
[0] && (events
& LPOLLIN
));
44 static int init_pipe(int *pipe_fds
)
50 PERROR("ht_cleanup thread quit pipe");
54 for (i
= 0; i
< 2; i
++) {
55 ret
= fcntl(pipe_fds
[i
], F_SETFD
, FD_CLOEXEC
);
57 PERROR("fcntl ht_cleanup_quit_pipe");
66 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
68 static int set_pollset(struct lttng_poll_event
*events
, size_t size
)
72 ret
= lttng_poll_create(events
, size
, LTTNG_CLOEXEC
);
77 ret
= lttng_poll_add(events
, ht_cleanup_quit_pipe
[0],
83 ret
= lttng_poll_add(events
, ht_cleanup_pipe
[0], LPOLLIN
| LPOLLERR
);
85 DBG("[ht-thread] lttng_poll_add error %d.", ret
);
95 static void cleanup_ht_cleanup_thread(void *data
)
97 utils_close_pipe(ht_cleanup_quit_pipe
);
98 utils_close_pipe(ht_cleanup_pipe
);
101 static void *thread_ht_cleanup(void *data
)
103 int ret
, i
, pollfd
, err
= -1;
105 uint32_t revents
, nb_fd
;
106 struct lttng_poll_event events
;
108 DBG("[ht-thread] startup.");
110 rcu_register_thread();
113 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_HT_CLEANUP
);
115 if (testpoint(sessiond_thread_ht_cleanup
)) {
116 DBG("[ht-thread] testpoint.");
117 goto error_testpoint
;
120 health_code_update();
122 ret
= set_pollset(&events
, 2);
124 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret
);
125 goto error_poll_create
;
128 health_code_update();
132 DBG3("[ht-thread] Polling.");
134 ret
= lttng_poll_wait(&events
, -1);
135 DBG3("[ht-thread] Returning from poll on %d fds.",
136 LTTNG_POLL_GETNB(&events
));
140 * Restart interrupted system call.
142 if (errno
== EINTR
) {
149 for (i
= 0; i
< nb_fd
; i
++) {
152 health_code_update();
154 /* Fetch once the poll data */
155 revents
= LTTNG_POLL_GETEV(&events
, i
);
156 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
158 if (pollfd
!= ht_cleanup_pipe
[0]) {
162 if (revents
& LPOLLIN
) {
163 /* Get socket from dispatch thread. */
164 size_ret
= lttng_read(ht_cleanup_pipe
[0], &ht
,
166 if (size_ret
< sizeof(ht
)) {
167 PERROR("ht cleanup notify pipe");
170 health_code_update();
172 * The whole point of this thread is to call
173 * lttng_ht_destroy from a context that is NOT:
174 * 1) a read-side RCU lock,
175 * 2) a call_rcu thread.
177 lttng_ht_destroy(ht
);
179 health_code_update();
182 * Ensure that we never process the quit pipe
183 * event while there is still data available
184 * on the ht clean pipe.
187 } else if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
188 ERR("ht cleanup pipe error");
191 ERR("Unexpected poll events %u for sock %d", revents
, pollfd
);
196 for (i
= 0; i
< nb_fd
; i
++) {
197 health_code_update();
199 /* Fetch once the poll data */
200 revents
= LTTNG_POLL_GETEV(&events
, i
);
201 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
204 /* No activity for this FD (poll implementation). */
208 if (pollfd
== ht_cleanup_pipe
[0]) {
212 /* Thread quit pipe has been closed. Killing thread. */
213 ret
= check_quit_pipe(pollfd
, revents
);
216 DBG("[ht-cleanup] quit.");
224 lttng_poll_clean(&events
);
227 DBG("[ht-cleanup] Thread terminates.");
230 ERR("Health error occurred in %s", __func__
);
232 health_unregister(health_sessiond
);
233 rcu_thread_offline();
234 rcu_unregister_thread();
238 static bool shutdown_ht_cleanup_thread(void *data
)
242 ret
= notify_thread_pipe(ht_cleanup_quit_pipe
[1]);
244 ERR("write error on ht_cleanup quit pipe");
251 struct lttng_thread
*launch_ht_cleanup_thread(void)
254 struct lttng_thread
*thread
;
256 ret
= init_pipe(ht_cleanup_pipe
);
261 ret
= init_pipe(ht_cleanup_quit_pipe
);
266 thread
= lttng_thread_create("HT cleanup",
268 shutdown_ht_cleanup_thread
,
269 cleanup_ht_cleanup_thread
,
276 cleanup_ht_cleanup_thread(NULL
);