91c0544c3c8a28c8c8391b1d5f76b0927a48bab5
[lttng-tools.git] / src / bin / lttng-sessiond / ht-cleanup.c
1 /*
2 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
3 *
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.
7 *
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.
12 *
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.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20
21 #include <common/hashtable/hashtable.h>
22 #include <common/common.h>
23 #include <common/utils.h>
24 #include <pthread.h>
25
26 #include "lttng-sessiond.h"
27 #include "health-sessiond.h"
28 #include "testpoint.h"
29 #include "utils.h"
30 #include "ht-cleanup.h"
31
32 static int ht_cleanup_quit_pipe[2] = { -1, -1 };
33
34 /*
35 * Check if the ht_cleanup thread quit pipe was triggered.
36 *
37 * Return true if it was triggered else false;
38 */
39 static bool check_quit_pipe(int fd, uint32_t events)
40 {
41 return (fd == ht_cleanup_quit_pipe[0] && (events & LPOLLIN));
42 }
43
44 static int init_pipe(int *pipe_fds)
45 {
46 int ret, i;
47
48 ret = pipe(pipe_fds);
49 if (ret < 0) {
50 PERROR("ht_cleanup thread quit pipe");
51 goto error;
52 }
53
54 for (i = 0; i < 2; i++) {
55 ret = fcntl(pipe_fds[i], F_SETFD, FD_CLOEXEC);
56 if (ret < 0) {
57 PERROR("fcntl ht_cleanup_quit_pipe");
58 goto error;
59 }
60 }
61 error:
62 return ret;
63 }
64
65 /*
66 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
67 */
68 static int set_pollset(struct lttng_poll_event *events, size_t size)
69 {
70 int ret;
71
72 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
73 if (ret < 0) {
74 goto error;
75 }
76
77 ret = lttng_poll_add(events, ht_cleanup_quit_pipe[0],
78 LPOLLIN | LPOLLERR);
79 if (ret < 0) {
80 goto error;
81 }
82
83 ret = lttng_poll_add(events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR);
84 if (ret < 0) {
85 DBG("[ht-thread] lttng_poll_add error %d.", ret);
86 goto error;
87 }
88
89 return 0;
90
91 error:
92 return ret;
93 }
94
95 static void cleanup_ht_cleanup_thread(void *data)
96 {
97 utils_close_pipe(ht_cleanup_quit_pipe);
98 utils_close_pipe(ht_cleanup_pipe);
99 }
100
101 static void *thread_ht_cleanup(void *data)
102 {
103 int ret, i, pollfd, err = -1;
104 ssize_t size_ret;
105 uint32_t revents, nb_fd;
106 struct lttng_poll_event events;
107
108 DBG("[ht-thread] startup.");
109
110 rcu_register_thread();
111 rcu_thread_online();
112
113 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_HT_CLEANUP);
114
115 if (testpoint(sessiond_thread_ht_cleanup)) {
116 DBG("[ht-thread] testpoint.");
117 goto error_testpoint;
118 }
119
120 health_code_update();
121
122 ret = set_pollset(&events, 2);
123 if (ret < 0) {
124 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret);
125 goto error_poll_create;
126 }
127
128 health_code_update();
129
130 while (1) {
131 restart:
132 DBG3("[ht-thread] Polling.");
133 health_poll_entry();
134 ret = lttng_poll_wait(&events, -1);
135 DBG3("[ht-thread] Returning from poll on %d fds.",
136 LTTNG_POLL_GETNB(&events));
137 health_poll_exit();
138 if (ret < 0) {
139 /*
140 * Restart interrupted system call.
141 */
142 if (errno == EINTR) {
143 continue;
144 }
145 goto error;
146 }
147
148 nb_fd = ret;
149 for (i = 0; i < nb_fd; i++) {
150 struct lttng_ht *ht;
151
152 health_code_update();
153
154 /* Fetch once the poll data */
155 revents = LTTNG_POLL_GETEV(&events, i);
156 pollfd = LTTNG_POLL_GETFD(&events, i);
157
158 if (!revents) {
159 /* No activity for this FD (poll implementation). */
160 continue;
161 }
162
163 if (pollfd != ht_cleanup_pipe[0]) {
164 continue;
165 }
166
167 if (revents & LPOLLIN) {
168 /* Get socket from dispatch thread. */
169 size_ret = lttng_read(ht_cleanup_pipe[0], &ht,
170 sizeof(ht));
171 if (size_ret < sizeof(ht)) {
172 PERROR("ht cleanup notify pipe");
173 goto error;
174 }
175 health_code_update();
176 /*
177 * The whole point of this thread is to call
178 * lttng_ht_destroy from a context that is NOT:
179 * 1) a read-side RCU lock,
180 * 2) a call_rcu thread.
181 */
182 lttng_ht_destroy(ht);
183
184 health_code_update();
185
186 /*
187 * Ensure that we never process the quit pipe
188 * event while there is still data available
189 * on the ht clean pipe.
190 */
191 goto restart;
192 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
193 ERR("ht cleanup pipe error");
194 goto error;
195 } else {
196 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
197 goto error;
198 }
199 }
200
201 for (i = 0; i < nb_fd; i++) {
202 health_code_update();
203
204 /* Fetch once the poll data */
205 revents = LTTNG_POLL_GETEV(&events, i);
206 pollfd = LTTNG_POLL_GETFD(&events, i);
207
208 if (!revents) {
209 /* No activity for this FD (poll implementation). */
210 continue;
211 }
212
213 if (pollfd == ht_cleanup_pipe[0]) {
214 continue;
215 }
216
217 /* Thread quit pipe has been closed. Killing thread. */
218 ret = check_quit_pipe(pollfd, revents);
219 if (ret) {
220 err = 0;
221 DBG("[ht-cleanup] quit.");
222 goto exit;
223 }
224 }
225 }
226
227 exit:
228 error:
229 lttng_poll_clean(&events);
230 error_poll_create:
231 error_testpoint:
232 DBG("[ht-cleanup] Thread terminates.");
233 if (err) {
234 health_error();
235 ERR("Health error occurred in %s", __func__);
236 }
237 health_unregister(health_sessiond);
238 rcu_thread_offline();
239 rcu_unregister_thread();
240 return NULL;
241 }
242
243 static bool shutdown_ht_cleanup_thread(void *data)
244 {
245 int ret;
246
247 ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]);
248 if (ret < 0) {
249 ERR("write error on ht_cleanup quit pipe");
250 goto end;
251 }
252 end:
253 return ret;
254 }
255
256 struct lttng_thread *launch_ht_cleanup_thread(void)
257 {
258 int ret;
259 struct lttng_thread *thread;
260
261 ret = init_pipe(ht_cleanup_pipe);
262 if (ret) {
263 goto error;
264 }
265
266 ret = init_pipe(ht_cleanup_quit_pipe);
267 if (ret) {
268 goto error;
269 }
270
271 thread = lttng_thread_create("HT cleanup",
272 thread_ht_cleanup,
273 shutdown_ht_cleanup_thread,
274 cleanup_ht_cleanup_thread,
275 NULL);
276 if (!thread) {
277 ret = -1;
278 goto error;
279 }
280 return thread;
281 error:
282 cleanup_ht_cleanup_thread(NULL);
283 return NULL;
284 }
This page took 0.034843 seconds and 4 git commands to generate.