Fix: poll: show the correct number of fds
[lttng-tools.git] / src / bin / lttng-sessiond / ht-cleanup.c
CommitLineData
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
6c1c0768 19#define _LGPL_SOURCE
0b2dc8df
MD
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"
8782cc74 27#include "health-sessiond.h"
9ad42ec1 28#include "testpoint.h"
0b2dc8df
MD
29
30void *thread_ht_cleanup(void *data)
31{
32 int ret, i, pollfd, err = -1;
6cd525e8 33 ssize_t size_ret;
0b2dc8df
MD
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
6c71277b 42 health_register(health_sessiond, HEALTH_SESSIOND_TYPE_HT_CLEANUP);
0b2dc8df 43
9ad42ec1 44 if (testpoint(sessiond_thread_ht_cleanup)) {
74588b4d 45 DBG("[ht-thread] testpoint.");
9ad42ec1
MD
46 goto error_testpoint;
47 }
48
0b2dc8df
MD
49 health_code_update();
50
4a15001e 51 ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);
0b2dc8df 52 if (ret < 0) {
74588b4d 53 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret);
0b2dc8df
MD
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) {
74588b4d 60 DBG("[ht-thread] lttng_poll_add error %d.", ret);
0b2dc8df
MD
61 goto error;
62 }
63
64 health_code_update();
65
66 while (1) {
4a15001e
MD
67 int handled_event;
68
7fa2082e 69 DBG3("[ht-thread] Polling.");
0b2dc8df
MD
70
71 /* Inifinite blocking call, waiting for transmission */
72restart:
4a15001e 73 handled_event = 0;
0b2dc8df
MD
74 health_poll_entry();
75 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
76 DBG3("[ht-thread] Returning from poll on %d fds.",
77 LTTNG_POLL_GETNB(&events));
0b2dc8df
MD
78 health_poll_exit();
79 if (ret < 0) {
80 /*
81 * Restart interrupted system call.
82 */
83 if (errno == EINTR) {
84 goto restart;
85 }
86 goto error;
87 }
88
89 nb_fd = ret;
90
91 for (i = 0; i < nb_fd; i++) {
92 struct lttng_ht *ht;
93
94 health_code_update();
95
96 /* Fetch once the poll data */
97 revents = LTTNG_POLL_GETEV(&events, i);
98 pollfd = LTTNG_POLL_GETFD(&events, i);
99
fd20dac9
MD
100 if (!revents) {
101 /* No activity for this FD (poll implementation). */
102 continue;
103 }
104
4a15001e
MD
105 if (pollfd != ht_cleanup_pipe[0]) {
106 continue;
0b2dc8df 107 }
0b2dc8df
MD
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 }
4a15001e
MD
136
137 /* Only check cleanup quit when no more work to do. */
138 if (handled_event) {
139 continue;
140 }
141
142 for (i = 0; i < nb_fd; i++) {
143 health_code_update();
144
145 /* Fetch once the poll data */
146 revents = LTTNG_POLL_GETEV(&events, i);
147 pollfd = LTTNG_POLL_GETFD(&events, i);
148
fd20dac9
MD
149 if (!revents) {
150 /* No activity for this FD (poll implementation). */
151 continue;
152 }
153
4a15001e
MD
154 if (pollfd == ht_cleanup_pipe[0]) {
155 continue;
156 }
157
158 /* Thread quit pipe has been closed. Killing thread. */
159 ret = sessiond_check_ht_cleanup_quit(pollfd, revents);
160 if (ret) {
161 err = 0;
74588b4d 162 DBG("[ht-cleanup] quit.");
4a15001e
MD
163 goto exit;
164 }
165 }
0b2dc8df
MD
166 }
167
168exit:
169error:
170 lttng_poll_clean(&events);
171error_poll_create:
9ad42ec1 172error_testpoint:
4a15001e 173 DBG("[ht-cleanup] Thread terminates.");
0b2dc8df
MD
174 if (err) {
175 health_error();
176 ERR("Health error occurred in %s", __func__);
177 }
8782cc74 178 health_unregister(health_sessiond);
0b2dc8df
MD
179 rcu_thread_offline();
180 rcu_unregister_thread();
181 return NULL;
182}
This page took 0.03582 seconds and 4 git commands to generate.