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