Commit | Line | Data |
---|---|---|
0b2dc8df MD |
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 | ||
6c1c0768 | 18 | #define _LGPL_SOURCE |
0b2dc8df MD |
19 | #include <assert.h> |
20 | ||
21 | #include <common/hashtable/hashtable.h> | |
22 | #include <common/common.h> | |
23 | #include <common/utils.h> | |
5e97de00 | 24 | #include <pthread.h> |
0b2dc8df MD |
25 | |
26 | #include "lttng-sessiond.h" | |
8782cc74 | 27 | #include "health-sessiond.h" |
9ad42ec1 | 28 | #include "testpoint.h" |
5e97de00 | 29 | #include "utils.h" |
a3707772 | 30 | #include "ht-cleanup.h" |
0b2dc8df | 31 | |
a3707772 | 32 | static int ht_cleanup_quit_pipe[2] = { -1, -1 }; |
5e97de00 JG |
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 | ||
a3707772 JG |
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 | ||
5e97de00 | 101 | static void *thread_ht_cleanup(void *data) |
0b2dc8df MD |
102 | { |
103 | int ret, i, pollfd, err = -1; | |
6cd525e8 | 104 | ssize_t size_ret; |
0b2dc8df MD |
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 | ||
6c71277b | 113 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_HT_CLEANUP); |
0b2dc8df | 114 | |
9ad42ec1 | 115 | if (testpoint(sessiond_thread_ht_cleanup)) { |
74588b4d | 116 | DBG("[ht-thread] testpoint."); |
9ad42ec1 MD |
117 | goto error_testpoint; |
118 | } | |
119 | ||
0b2dc8df MD |
120 | health_code_update(); |
121 | ||
5e97de00 | 122 | ret = set_pollset(&events, 2); |
0b2dc8df | 123 | if (ret < 0) { |
74588b4d | 124 | DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret); |
0b2dc8df MD |
125 | goto error_poll_create; |
126 | } | |
127 | ||
0b2dc8df MD |
128 | health_code_update(); |
129 | ||
130 | while (1) { | |
df119599 | 131 | restart: |
7fa2082e | 132 | DBG3("[ht-thread] Polling."); |
0b2dc8df MD |
133 | health_poll_entry(); |
134 | ret = lttng_poll_wait(&events, -1); | |
7fa2082e MD |
135 | DBG3("[ht-thread] Returning from poll on %d fds.", |
136 | LTTNG_POLL_GETNB(&events)); | |
0b2dc8df MD |
137 | health_poll_exit(); |
138 | if (ret < 0) { | |
139 | /* | |
140 | * Restart interrupted system call. | |
141 | */ | |
142 | if (errno == EINTR) { | |
5e97de00 | 143 | continue; |
0b2dc8df MD |
144 | } |
145 | goto error; | |
146 | } | |
147 | ||
148 | nb_fd = ret; | |
0b2dc8df MD |
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 | ||
4a15001e MD |
158 | if (pollfd != ht_cleanup_pipe[0]) { |
159 | continue; | |
0b2dc8df | 160 | } |
0b2dc8df | 161 | |
03e43155 MD |
162 | if (revents & LPOLLIN) { |
163 | /* Get socket from dispatch thread. */ | |
164 | size_ret = lttng_read(ht_cleanup_pipe[0], &ht, | |
165 | sizeof(ht)); | |
166 | if (size_ret < sizeof(ht)) { | |
167 | PERROR("ht cleanup notify pipe"); | |
168 | goto error; | |
169 | } | |
170 | health_code_update(); | |
171 | /* | |
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. | |
176 | */ | |
177 | lttng_ht_destroy(ht); | |
178 | ||
179 | health_code_update(); | |
df119599 JG |
180 | |
181 | /* | |
182 | * Ensure that we never process the quit pipe | |
183 | * event while there is still data available | |
184 | * on the ht clean pipe. | |
185 | */ | |
186 | goto restart; | |
03e43155 | 187 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
0b2dc8df MD |
188 | ERR("ht cleanup pipe error"); |
189 | goto error; | |
03e43155 MD |
190 | } else { |
191 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); | |
0b2dc8df MD |
192 | goto error; |
193 | } | |
0b2dc8df | 194 | } |
4a15001e | 195 | |
4a15001e MD |
196 | for (i = 0; i < nb_fd; i++) { |
197 | health_code_update(); | |
198 | ||
199 | /* Fetch once the poll data */ | |
200 | revents = LTTNG_POLL_GETEV(&events, i); | |
201 | pollfd = LTTNG_POLL_GETFD(&events, i); | |
202 | ||
fd20dac9 MD |
203 | if (!revents) { |
204 | /* No activity for this FD (poll implementation). */ | |
205 | continue; | |
206 | } | |
207 | ||
4a15001e MD |
208 | if (pollfd == ht_cleanup_pipe[0]) { |
209 | continue; | |
210 | } | |
211 | ||
212 | /* Thread quit pipe has been closed. Killing thread. */ | |
5e97de00 | 213 | ret = check_quit_pipe(pollfd, revents); |
4a15001e MD |
214 | if (ret) { |
215 | err = 0; | |
74588b4d | 216 | DBG("[ht-cleanup] quit."); |
4a15001e MD |
217 | goto exit; |
218 | } | |
219 | } | |
0b2dc8df MD |
220 | } |
221 | ||
222 | exit: | |
223 | error: | |
224 | lttng_poll_clean(&events); | |
225 | error_poll_create: | |
9ad42ec1 | 226 | error_testpoint: |
4a15001e | 227 | DBG("[ht-cleanup] Thread terminates."); |
0b2dc8df MD |
228 | if (err) { |
229 | health_error(); | |
230 | ERR("Health error occurred in %s", __func__); | |
231 | } | |
8782cc74 | 232 | health_unregister(health_sessiond); |
0b2dc8df MD |
233 | rcu_thread_offline(); |
234 | rcu_unregister_thread(); | |
235 | return NULL; | |
236 | } | |
5e97de00 | 237 | |
a3707772 JG |
238 | static bool shutdown_ht_cleanup_thread(void *data) |
239 | { | |
240 | int ret; | |
241 | ||
242 | ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]); | |
243 | if (ret < 0) { | |
244 | ERR("write error on ht_cleanup quit pipe"); | |
245 | goto end; | |
246 | } | |
247 | end: | |
248 | return ret; | |
249 | } | |
250 | ||
251 | struct lttng_thread *launch_ht_cleanup_thread(void) | |
5e97de00 JG |
252 | { |
253 | int ret; | |
a3707772 | 254 | struct lttng_thread *thread; |
5e97de00 JG |
255 | |
256 | ret = init_pipe(ht_cleanup_pipe); | |
257 | if (ret) { | |
258 | goto error; | |
259 | } | |
260 | ||
77fc2bc2 | 261 | ret = init_pipe(ht_cleanup_quit_pipe); |
5e97de00 | 262 | if (ret) { |
a3707772 | 263 | goto error; |
5e97de00 JG |
264 | } |
265 | ||
a3707772 JG |
266 | thread = lttng_thread_create("HT cleanup", |
267 | thread_ht_cleanup, | |
268 | shutdown_ht_cleanup_thread, | |
269 | cleanup_ht_cleanup_thread, | |
1a1a34b4 | 270 | NULL); |
a3707772 | 271 | if (!thread) { |
a3707772 | 272 | goto error; |
5e97de00 | 273 | } |
a3707772 | 274 | return thread; |
5e97de00 | 275 | error: |
a3707772 JG |
276 | cleanup_ht_cleanup_thread(NULL); |
277 | return NULL; | |
5e97de00 | 278 | } |