| 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 _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | |
| 21 | #include <common/hashtable/hashtable.h> |
| 22 | #include <common/common.h> |
| 23 | #include <common/utils.h> |
| 24 | |
| 25 | #include "lttng-sessiond.h" |
| 26 | #include "health-sessiond.h" |
| 27 | #include "testpoint.h" |
| 28 | |
| 29 | void *thread_ht_cleanup(void *data) |
| 30 | { |
| 31 | int ret, i, pollfd, err = -1; |
| 32 | ssize_t size_ret; |
| 33 | uint32_t revents, nb_fd; |
| 34 | struct lttng_poll_event events; |
| 35 | |
| 36 | DBG("[ht-thread] startup."); |
| 37 | |
| 38 | rcu_register_thread(); |
| 39 | rcu_thread_online(); |
| 40 | |
| 41 | health_register(health_sessiond, HEALTH_SESSIOND_TYPE_HT_CLEANUP); |
| 42 | |
| 43 | if (testpoint(sessiond_thread_ht_cleanup)) { |
| 44 | goto error_testpoint; |
| 45 | } |
| 46 | |
| 47 | health_code_update(); |
| 48 | |
| 49 | ret = sessiond_set_thread_pollset(&events, 2); |
| 50 | if (ret < 0) { |
| 51 | goto error_poll_create; |
| 52 | } |
| 53 | |
| 54 | /* Add pipe to the pollset. */ |
| 55 | ret = lttng_poll_add(&events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR); |
| 56 | if (ret < 0) { |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | health_code_update(); |
| 61 | |
| 62 | while (1) { |
| 63 | DBG3("[ht-thread] Polling on %d fds.", |
| 64 | LTTNG_POLL_GETNB(&events)); |
| 65 | |
| 66 | /* Inifinite blocking call, waiting for transmission */ |
| 67 | restart: |
| 68 | health_poll_entry(); |
| 69 | ret = lttng_poll_wait(&events, -1); |
| 70 | health_poll_exit(); |
| 71 | if (ret < 0) { |
| 72 | /* |
| 73 | * Restart interrupted system call. |
| 74 | */ |
| 75 | if (errno == EINTR) { |
| 76 | goto restart; |
| 77 | } |
| 78 | goto error; |
| 79 | } |
| 80 | |
| 81 | nb_fd = ret; |
| 82 | |
| 83 | for (i = 0; i < nb_fd; i++) { |
| 84 | struct lttng_ht *ht; |
| 85 | |
| 86 | health_code_update(); |
| 87 | |
| 88 | /* Fetch once the poll data */ |
| 89 | revents = LTTNG_POLL_GETEV(&events, i); |
| 90 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 91 | |
| 92 | if (!revents) { |
| 93 | /* |
| 94 | * No activity for this FD |
| 95 | * (poll implementation). |
| 96 | */ |
| 97 | continue; |
| 98 | } |
| 99 | |
| 100 | /* Thread quit pipe has been closed. Killing thread. */ |
| 101 | ret = sessiond_check_thread_quit_pipe(pollfd, revents); |
| 102 | if (ret) { |
| 103 | err = 0; |
| 104 | goto exit; |
| 105 | } |
| 106 | assert(pollfd == ht_cleanup_pipe[0]); |
| 107 | |
| 108 | if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 109 | ERR("ht cleanup pipe error"); |
| 110 | goto error; |
| 111 | } else if (!(revents & LPOLLIN)) { |
| 112 | /* No POLLIN and not a catched error, stop the thread. */ |
| 113 | ERR("ht cleanup failed. revent: %u", revents); |
| 114 | goto error; |
| 115 | } |
| 116 | |
| 117 | /* Get socket from dispatch thread. */ |
| 118 | size_ret = lttng_read(ht_cleanup_pipe[0], &ht, |
| 119 | sizeof(ht)); |
| 120 | if (size_ret < sizeof(ht)) { |
| 121 | PERROR("ht cleanup notify pipe"); |
| 122 | goto error; |
| 123 | } |
| 124 | health_code_update(); |
| 125 | /* |
| 126 | * The whole point of this thread is to call |
| 127 | * lttng_ht_destroy from a context that is NOT: |
| 128 | * 1) a read-side RCU lock, |
| 129 | * 2) a call_rcu thread. |
| 130 | */ |
| 131 | lttng_ht_destroy(ht); |
| 132 | |
| 133 | health_code_update(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | exit: |
| 138 | error: |
| 139 | lttng_poll_clean(&events); |
| 140 | error_poll_create: |
| 141 | error_testpoint: |
| 142 | utils_close_pipe(ht_cleanup_pipe); |
| 143 | ht_cleanup_pipe[0] = ht_cleanup_pipe[1] = -1; |
| 144 | DBG("[ust-thread] cleanup complete."); |
| 145 | if (err) { |
| 146 | health_error(); |
| 147 | ERR("Health error occurred in %s", __func__); |
| 148 | } |
| 149 | health_unregister(health_sessiond); |
| 150 | rcu_thread_offline(); |
| 151 | rcu_unregister_thread(); |
| 152 | return NULL; |
| 153 | } |