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