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.
22 #include <common/hashtable/hashtable.h>
23 #include <common/common.h>
24 #include <common/utils.h>
26 #include "lttng-sessiond.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
30 void *thread_ht_cleanup(void *data
)
32 int ret
, i
, pollfd
, err
= -1;
34 uint32_t revents
, nb_fd
;
35 struct lttng_poll_event events
;
37 DBG("[ht-thread] startup.");
39 rcu_register_thread();
42 health_register(health_sessiond
, HEALTH_SESSIOND_TYPE_HT_CLEANUP
);
44 if (testpoint(sessiond_thread_ht_cleanup
)) {
45 DBG("[ht-thread] testpoint.");
51 ret
= sessiond_set_ht_cleanup_thread_pollset(&events
, 2);
53 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret
);
54 goto error_poll_create
;
57 /* Add pipe to the pollset. */
58 ret
= lttng_poll_add(&events
, ht_cleanup_pipe
[0], LPOLLIN
| LPOLLERR
);
60 DBG("[ht-thread] lttng_poll_add error %d.", ret
);
69 DBG3("[ht-thread] Polling.");
71 /* Inifinite blocking call, waiting for transmission */
75 ret
= lttng_poll_wait(&events
, -1);
76 DBG3("[ht-thread] Returning from poll on %d fds.",
77 LTTNG_POLL_GETNB(&events
));
81 * Restart interrupted system call.
91 for (i
= 0; i
< nb_fd
; i
++) {
96 /* Fetch once the poll data */
97 revents
= LTTNG_POLL_GETEV(&events
, i
);
98 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
101 /* No activity for this FD (poll implementation). */
105 if (pollfd
!= ht_cleanup_pipe
[0]) {
109 if (revents
& (LPOLLERR
| LPOLLHUP
| LPOLLRDHUP
)) {
110 ERR("ht cleanup pipe error");
112 } else if (!(revents
& LPOLLIN
)) {
113 /* No POLLIN and not a catched error, stop the thread. */
114 ERR("ht cleanup failed. revent: %u", revents
);
118 /* Get socket from dispatch thread. */
119 size_ret
= lttng_read(ht_cleanup_pipe
[0], &ht
,
121 if (size_ret
< sizeof(ht
)) {
122 PERROR("ht cleanup notify pipe");
125 health_code_update();
127 * The whole point of this thread is to call
128 * lttng_ht_destroy from a context that is NOT:
129 * 1) a read-side RCU lock,
130 * 2) a call_rcu thread.
132 lttng_ht_destroy(ht
);
134 health_code_update();
137 /* Only check cleanup quit when no more work to do. */
142 for (i
= 0; i
< nb_fd
; i
++) {
143 health_code_update();
145 /* Fetch once the poll data */
146 revents
= LTTNG_POLL_GETEV(&events
, i
);
147 pollfd
= LTTNG_POLL_GETFD(&events
, i
);
150 /* No activity for this FD (poll implementation). */
154 if (pollfd
== ht_cleanup_pipe
[0]) {
158 /* Thread quit pipe has been closed. Killing thread. */
159 ret
= sessiond_check_ht_cleanup_quit(pollfd
, revents
);
162 DBG("[ht-cleanup] quit.");
170 lttng_poll_clean(&events
);
173 DBG("[ht-cleanup] Thread terminates.");
176 ERR("Health error occurred in %s", __func__
);
178 health_unregister(health_sessiond
);
179 rcu_thread_offline();
180 rcu_unregister_thread();