| 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 | |
| 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 | DBG("[ht-thread] testpoint."); |
| 45 | goto error_testpoint; |
| 46 | } |
| 47 | |
| 48 | health_code_update(); |
| 49 | |
| 50 | ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2); |
| 51 | if (ret < 0) { |
| 52 | DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret); |
| 53 | goto error_poll_create; |
| 54 | } |
| 55 | |
| 56 | /* Add pipe to the pollset. */ |
| 57 | ret = lttng_poll_add(&events, ht_cleanup_pipe[0], LPOLLIN | LPOLLERR); |
| 58 | if (ret < 0) { |
| 59 | DBG("[ht-thread] lttng_poll_add error %d.", ret); |
| 60 | goto error; |
| 61 | } |
| 62 | |
| 63 | health_code_update(); |
| 64 | |
| 65 | while (1) { |
| 66 | DBG3("[ht-thread] Polling."); |
| 67 | |
| 68 | /* Inifinite blocking call, waiting for transmission */ |
| 69 | restart: |
| 70 | health_poll_entry(); |
| 71 | ret = lttng_poll_wait(&events, -1); |
| 72 | DBG3("[ht-thread] Returning from poll on %d fds.", |
| 73 | LTTNG_POLL_GETNB(&events)); |
| 74 | health_poll_exit(); |
| 75 | if (ret < 0) { |
| 76 | /* |
| 77 | * Restart interrupted system call. |
| 78 | */ |
| 79 | if (errno == EINTR) { |
| 80 | goto restart; |
| 81 | } |
| 82 | goto error; |
| 83 | } |
| 84 | |
| 85 | nb_fd = ret; |
| 86 | |
| 87 | for (i = 0; i < nb_fd; i++) { |
| 88 | struct lttng_ht *ht; |
| 89 | |
| 90 | health_code_update(); |
| 91 | |
| 92 | /* Fetch once the poll data */ |
| 93 | revents = LTTNG_POLL_GETEV(&events, i); |
| 94 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 95 | |
| 96 | if (!revents) { |
| 97 | /* No activity for this FD (poll implementation). */ |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if (pollfd != ht_cleanup_pipe[0]) { |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | if (revents & LPOLLIN) { |
| 106 | /* Get socket from dispatch thread. */ |
| 107 | size_ret = lttng_read(ht_cleanup_pipe[0], &ht, |
| 108 | sizeof(ht)); |
| 109 | if (size_ret < sizeof(ht)) { |
| 110 | PERROR("ht cleanup notify pipe"); |
| 111 | goto error; |
| 112 | } |
| 113 | health_code_update(); |
| 114 | /* |
| 115 | * The whole point of this thread is to call |
| 116 | * lttng_ht_destroy from a context that is NOT: |
| 117 | * 1) a read-side RCU lock, |
| 118 | * 2) a call_rcu thread. |
| 119 | */ |
| 120 | lttng_ht_destroy(ht); |
| 121 | |
| 122 | health_code_update(); |
| 123 | } else if (revents & (LPOLLERR | LPOLLHUP | LPOLLRDHUP)) { |
| 124 | ERR("ht cleanup pipe error"); |
| 125 | goto error; |
| 126 | } else { |
| 127 | ERR("Unexpected poll events %u for sock %d", revents, pollfd); |
| 128 | goto error; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | for (i = 0; i < nb_fd; i++) { |
| 133 | health_code_update(); |
| 134 | |
| 135 | /* Fetch once the poll data */ |
| 136 | revents = LTTNG_POLL_GETEV(&events, i); |
| 137 | pollfd = LTTNG_POLL_GETFD(&events, i); |
| 138 | |
| 139 | if (!revents) { |
| 140 | /* No activity for this FD (poll implementation). */ |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | if (pollfd == ht_cleanup_pipe[0]) { |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | /* Thread quit pipe has been closed. Killing thread. */ |
| 149 | ret = sessiond_check_ht_cleanup_quit(pollfd, revents); |
| 150 | if (ret) { |
| 151 | err = 0; |
| 152 | DBG("[ht-cleanup] quit."); |
| 153 | goto exit; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | exit: |
| 159 | error: |
| 160 | lttng_poll_clean(&events); |
| 161 | error_poll_create: |
| 162 | error_testpoint: |
| 163 | DBG("[ht-cleanup] Thread terminates."); |
| 164 | if (err) { |
| 165 | health_error(); |
| 166 | ERR("Health error occurred in %s", __func__); |
| 167 | } |
| 168 | health_unregister(health_sessiond); |
| 169 | rcu_thread_offline(); |
| 170 | rcu_unregister_thread(); |
| 171 | return NULL; |
| 172 | } |