lttng: show man page when using command's --help
[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
6c1c0768 18#define _LGPL_SOURCE
0b2dc8df
MD
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
29void *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 43 if (testpoint(sessiond_thread_ht_cleanup)) {
74588b4d 44 DBG("[ht-thread] testpoint.");
9ad42ec1
MD
45 goto error_testpoint;
46 }
47
0b2dc8df
MD
48 health_code_update();
49
4a15001e 50 ret = sessiond_set_ht_cleanup_thread_pollset(&events, 2);
0b2dc8df 51 if (ret < 0) {
74588b4d 52 DBG("[ht-thread] sessiond_set_ht_cleanup_thread_pollset error %d.", ret);
0b2dc8df
MD
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) {
74588b4d 59 DBG("[ht-thread] lttng_poll_add error %d.", ret);
0b2dc8df
MD
60 goto error;
61 }
62
63 health_code_update();
64
65 while (1) {
7fa2082e 66 DBG3("[ht-thread] Polling.");
0b2dc8df
MD
67
68 /* Inifinite blocking call, waiting for transmission */
69restart:
70 health_poll_entry();
71 ret = lttng_poll_wait(&events, -1);
7fa2082e
MD
72 DBG3("[ht-thread] Returning from poll on %d fds.",
73 LTTNG_POLL_GETNB(&events));
0b2dc8df
MD
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
fd20dac9
MD
96 if (!revents) {
97 /* No activity for this FD (poll implementation). */
98 continue;
99 }
100
4a15001e
MD
101 if (pollfd != ht_cleanup_pipe[0]) {
102 continue;
0b2dc8df 103 }
0b2dc8df 104
03e43155
MD
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)) {
0b2dc8df
MD
124 ERR("ht cleanup pipe error");
125 goto error;
03e43155
MD
126 } else {
127 ERR("Unexpected poll events %u for sock %d", revents, pollfd);
0b2dc8df
MD
128 goto error;
129 }
0b2dc8df 130 }
4a15001e 131
4a15001e
MD
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
fd20dac9
MD
139 if (!revents) {
140 /* No activity for this FD (poll implementation). */
141 continue;
142 }
143
4a15001e
MD
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;
74588b4d 152 DBG("[ht-cleanup] quit.");
4a15001e
MD
153 goto exit;
154 }
155 }
0b2dc8df
MD
156 }
157
158exit:
159error:
160 lttng_poll_clean(&events);
161error_poll_create:
9ad42ec1 162error_testpoint:
4a15001e 163 DBG("[ht-cleanup] Thread terminates.");
0b2dc8df
MD
164 if (err) {
165 health_error();
166 ERR("Health error occurred in %s", __func__);
167 }
8782cc74 168 health_unregister(health_sessiond);
0b2dc8df
MD
169 rcu_thread_offline();
170 rcu_unregister_thread();
171 return NULL;
172}
This page took 0.049797 seconds and 5 git commands to generate.