Warning fix: forward declare struct lttng_ust_calibrate
[lttng-tools.git] / src / bin / lttng-sessiond / ht-cleanup.c
CommitLineData
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"
0b2dc8df 30
5e97de00
JG
31int ht_cleanup_quit_pipe[2] = { -1, -1 };
32
33/*
34 * Check if the ht_cleanup thread quit pipe was triggered.
35 *
36 * Return true if it was triggered else false;
37 */
38static bool check_quit_pipe(int fd, uint32_t events)
39{
40 return (fd == ht_cleanup_quit_pipe[0] && (events & LPOLLIN));
41}
42
43static int init_pipe(int *pipe_fds)
44{
45 int ret, i;
46
47 ret = pipe(pipe_fds);
48 if (ret < 0) {
49 PERROR("ht_cleanup thread quit pipe");
50 goto error;
51 }
52
53 for (i = 0; i < 2; i++) {
54 ret = fcntl(pipe_fds[i], F_SETFD, FD_CLOEXEC);
55 if (ret < 0) {
56 PERROR("fcntl ht_cleanup_quit_pipe");
57 goto error;
58 }
59 }
60error:
61 return ret;
62}
63
64/*
65 * Create a poll set with O_CLOEXEC and add the thread quit pipe to the set.
66 */
67static int set_pollset(struct lttng_poll_event *events, size_t size)
68{
69 int ret;
70
71 ret = lttng_poll_create(events, size, LTTNG_CLOEXEC);
72 if (ret < 0) {
73 goto error;
74 }
75
76 ret = lttng_poll_add(events, ht_cleanup_quit_pipe[0],
77 LPOLLIN | LPOLLERR);
78 if (ret < 0) {
79 goto error;
80 }
81
82 ret = lttng_poll_add(events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR);
83 if (ret < 0) {
84 DBG("[ht-thread] lttng_poll_add error %d.", ret);
85 goto error;
86 }
87
88 return 0;
89
90error:
91 return ret;
92}
93
94static void *thread_ht_cleanup(void *data)
0b2dc8df
MD
95{
96 int ret, i, pollfd, err = -1;
6cd525e8 97 ssize_t size_ret;
0b2dc8df
MD
98 uint32_t revents, nb_fd;
99 struct lttng_poll_event events;
100
101 DBG("[ht-thread] startup.");
102
103 rcu_register_thread();
104 rcu_thread_online();
105
6c71277b 106 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_HT_CLEANUP);
0b2dc8df 107
9ad42ec1 108 if (testpoint(sessiond_thread_ht_cleanup)) {
74588b4d 109 DBG("[ht-thread] testpoint.");
9ad42ec1
MD
110 goto error_testpoint;
111 }
112
0b2dc8df
MD
113 health_code_update();
114
5e97de00 115 ret = set_pollset(&events, 2);
0b2dc8df 116 if (ret < 0) {
74588b4d 117 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret);
0b2dc8df
MD
118 goto error_poll_create;
119 }
120
0b2dc8df
MD
121 health_code_update();
122
123 while (1) {
7fa2082e 124 DBG3("[ht-thread] Polling.");
0b2dc8df
MD
125 health_poll_entry();
126 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
127 DBG3("[ht-thread] Returning from poll on %d fds.",
128 LTTNG_POLL_GETNB(&events));
0b2dc8df
MD
129 health_poll_exit();
130 if (ret < 0) {
131 /*
132 * Restart interrupted system call.
133 */
134 if (errno == EINTR) {
5e97de00 135 continue;
0b2dc8df
MD
136 }
137 goto error;
138 }
139
140 nb_fd = ret;
0b2dc8df
MD
141 for (i = 0; i < nb_fd; i++) {
142 struct lttng_ht *ht;
143
144 health_code_update();
145
146 /* Fetch once the poll data */
147 revents = LTTNG_POLL_GETEV(&events, i);
148 pollfd = LTTNG_POLL_GETFD(&events, i);
149
fd20dac9
MD
150 if (!revents) {
151 /* No activity for this FD (poll implementation). */
152 continue;
153 }
154
4a15001e
MD
155 if (pollfd != ht_cleanup_pipe[0]) {
156 continue;
0b2dc8df 157 }
0b2dc8df 158
03e43155
MD
159 if (revents & LPOLLIN) {
160 /* Get socket from dispatch thread. */
161 size_ret = lttng_read(ht_cleanup_pipe[0], &ht,
162 sizeof(ht));
163 if (size_ret < sizeof(ht)) {
164 PERROR("ht cleanup notify pipe");
165 goto error;
166 }
167 health_code_update();
168 /*
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.
173 */
174 lttng_ht_destroy(ht);
175
176 health_code_update();
177 } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) {
0b2dc8df
MD
178 ERR("ht cleanup pipe error");
179 goto error;
03e43155
MD
180 } else {
181 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
0b2dc8df
MD
182 goto error;
183 }
0b2dc8df 184 }
4a15001e 185
4a15001e
MD
186 for (i = 0; i < nb_fd; i++) {
187 health_code_update();
188
189 /* Fetch once the poll data */
190 revents = LTTNG_POLL_GETEV(&events, i);
191 pollfd = LTTNG_POLL_GETFD(&events, i);
192
fd20dac9
MD
193 if (!revents) {
194 /* No activity for this FD (poll implementation). */
195 continue;
196 }
197
4a15001e
MD
198 if (pollfd == ht_cleanup_pipe[0]) {
199 continue;
200 }
201
202 /* Thread quit pipe has been closed. Killing thread. */
5e97de00 203 ret = check_quit_pipe(pollfd, revents);
4a15001e
MD
204 if (ret) {
205 err = 0;
74588b4d 206 DBG("[ht-cleanup] quit.");
4a15001e
MD
207 goto exit;
208 }
209 }
0b2dc8df
MD
210 }
211
212exit:
213error:
214 lttng_poll_clean(&events);
215error_poll_create:
9ad42ec1 216error_testpoint:
4a15001e 217 DBG("[ht-cleanup] Thread terminates.");
0b2dc8df
MD
218 if (err) {
219 health_error();
220 ERR("Health error occurred in %s", __func__);
221 }
8782cc74 222 health_unregister(health_sessiond);
0b2dc8df
MD
223 rcu_thread_offline();
224 rcu_unregister_thread();
225 return NULL;
226}
5e97de00
JG
227
228int init_ht_cleanup_thread(pthread_t *thread)
229{
230 int ret;
231
232 ret = init_pipe(ht_cleanup_pipe);
233 if (ret) {
234 goto error;
235 }
236
77fc2bc2 237 ret = init_pipe(ht_cleanup_quit_pipe);
5e97de00
JG
238 if (ret) {
239 goto error_quit_pipe;
240 }
241
1a1a34b4
MJ
242 ret = pthread_create(thread, default_pthread_attr(), thread_ht_cleanup,
243 NULL);
5e97de00
JG
244 if (ret) {
245 errno = ret;
246 PERROR("pthread_create ht_cleanup");
247 goto error_thread;
248 }
249
250error:
251 return ret;
252
253error_thread:
254 utils_close_pipe(ht_cleanup_quit_pipe);
255error_quit_pipe:
256 utils_close_pipe(ht_cleanup_pipe);
257 return ret;
258}
259
260int fini_ht_cleanup_thread(pthread_t *thread)
261{
262 int ret;
263
264 ret = notify_thread_pipe(ht_cleanup_quit_pipe[1]);
265 if (ret < 0) {
266 ERR("write error on ht_cleanup quit pipe");
267 goto end;
268 }
269
270 ret = pthread_join(*thread, NULL);
271 if (ret) {
272 errno = ret;
273 PERROR("pthread_join ht cleanup thread");
274 }
275 utils_close_pipe(ht_cleanup_pipe);
276 utils_close_pipe(ht_cleanup_quit_pipe);
277end:
278 return ret;
279}
This page took 0.096224 seconds and 4 git commands to generate.