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