common: compile libcompat as C++
[lttng-tools.git] / src / common / health / health.c
1 /*
2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9 #define _LGPL_SOURCE
10 #include <inttypes.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <time.h>
14
15 #include <common/defaults.h>
16 #include <common/error.h>
17 #include <common/macros.h>
18 #include <common/sessiond-comm/inet.h>
19
20 #include <lttng/health-internal.h>
21
22 /*
23 * An application-specific error state for unregistered thread keeps
24 * track of thread errors. A thread reporting a health error, normally
25 * unregisters and quits. This makes the TLS health state not available
26 * to the health_check_state() call so on unregister we update this
27 * global error array so we can keep track of which thread was on error
28 * if the TLS health state has been removed.
29 */
30 struct health_app {
31 /* List of health state, for each application thread */
32 struct cds_list_head list;
33 /*
34 * This lock ensures that TLS memory used for the node and its
35 * container structure don't get reclaimed after the TLS owner
36 * thread exits until we have finished using it.
37 */
38 pthread_mutex_t lock;
39 int nr_types;
40 struct timespec time_delta;
41 /* Health flags containing thread type error state */
42 enum health_flags *flags;
43 };
44
45 /* Define TLS health state. */
46 DEFINE_URCU_TLS(struct health_state, health_state);
47
48 /*
49 * Initialize health check subsytem.
50 */
51 static
52 void health_init(struct health_app *ha)
53 {
54 /*
55 * Get the maximum value between the default delta value and the TCP
56 * timeout with a safety net of the default health check delta.
57 */
58 ha->time_delta.tv_sec = max_t(unsigned long,
59 lttcomm_inet_tcp_timeout + DEFAULT_HEALTH_CHECK_DELTA_S,
60 ha->time_delta.tv_sec);
61 DBG("Health check time delta in seconds set to %lu",
62 ha->time_delta.tv_sec);
63 }
64
65 struct health_app *health_app_create(int nr_types)
66 {
67 struct health_app *ha;
68
69 ha = zmalloc(sizeof(*ha));
70 if (!ha) {
71 return NULL;
72 }
73 ha->flags = zmalloc(sizeof(*ha->flags) * nr_types);
74 if (!ha->flags) {
75 goto error_flags;
76 }
77 CDS_INIT_LIST_HEAD(&ha->list);
78 pthread_mutex_init(&ha->lock, NULL);
79 ha->nr_types = nr_types;
80 ha->time_delta.tv_sec = DEFAULT_HEALTH_CHECK_DELTA_S;
81 ha->time_delta.tv_nsec = DEFAULT_HEALTH_CHECK_DELTA_NS;
82 health_init(ha);
83 return ha;
84
85 error_flags:
86 free(ha);
87 return NULL;
88 }
89
90 void health_app_destroy(struct health_app *ha)
91 {
92 free(ha->flags);
93 free(ha);
94 }
95
96 /*
97 * Lock health state global list mutex.
98 */
99 static void state_lock(struct health_app *ha)
100 {
101 pthread_mutex_lock(&ha->lock);
102 }
103
104 /*
105 * Unlock health state global list mutex.
106 */
107 static void state_unlock(struct health_app *ha)
108 {
109 pthread_mutex_unlock(&ha->lock);
110 }
111
112 /*
113 * Set time difference in res from time_a and time_b.
114 */
115 static void time_diff(const struct timespec *time_a,
116 const struct timespec *time_b, struct timespec *res)
117 {
118 if (time_a->tv_nsec - time_b->tv_nsec < 0) {
119 res->tv_sec = time_a->tv_sec - time_b->tv_sec - 1;
120 res->tv_nsec = 1000000000L + time_a->tv_sec - time_b->tv_sec;
121 } else {
122 res->tv_sec = time_a->tv_sec - time_b->tv_sec;
123 res->tv_nsec = time_a->tv_nsec - time_b->tv_nsec;
124 }
125 }
126
127 /*
128 * Return true if time_a - time_b > diff, else false.
129 */
130 static int time_diff_gt(const struct timespec *time_a,
131 const struct timespec *time_b, const struct timespec *diff)
132 {
133 struct timespec res;
134
135 time_diff(time_a, time_b, &res);
136 time_diff(&res, diff, &res);
137
138 if (res.tv_sec > 0) {
139 return 1;
140 } else if (res.tv_sec == 0 && res.tv_nsec > 0) {
141 return 1;
142 }
143
144 return 0;
145 }
146
147 /*
148 * Validate health state. Checks for the error flag or health conditions.
149 *
150 * Return 0 if health is bad or else 1.
151 */
152 static int validate_state(struct health_app *ha, struct health_state *state)
153 {
154 int retval = 1, ret;
155 unsigned long current, last;
156 struct timespec current_time;
157
158 LTTNG_ASSERT(state);
159
160 last = state->last;
161 current = uatomic_read(&state->current);
162
163 ret = lttng_clock_gettime(CLOCK_MONOTONIC, &current_time);
164 if (ret < 0) {
165 PERROR("Error reading time\n");
166 /* error */
167 retval = 0;
168 goto end;
169 }
170
171 /*
172 * Thread is in bad health if flag HEALTH_ERROR is set. It is also in bad
173 * health if, after the delta delay has passed, its the progress counter
174 * has not moved and it has NOT been waiting for a poll() call.
175 */
176 if (uatomic_read(&state->flags) & HEALTH_ERROR) {
177 retval = 0;
178 goto end;
179 }
180
181 /*
182 * Initial condition need to update the last counter and sample time, but
183 * should not check health in this initial case, because we don't know how
184 * much time has passed.
185 */
186 if (state->last_time.tv_sec == 0 && state->last_time.tv_nsec == 0) {
187 /* update last counter and last sample time */
188 state->last = current;
189 memcpy(&state->last_time, &current_time, sizeof(current_time));
190 } else {
191 if (time_diff_gt(&current_time, &state->last_time,
192 &ha->time_delta)) {
193 if (current == last && !HEALTH_IS_IN_POLL(current)) {
194 /* error */
195 retval = 0;
196 }
197 /* update last counter and last sample time */
198 state->last = current;
199 memcpy(&state->last_time, &current_time, sizeof(current_time));
200
201 /* On error, stop right now and notify caller. */
202 if (retval == 0) {
203 goto end;
204 }
205 }
206 }
207
208 end:
209 DBG("Health state current %lu, last %lu, ret %d",
210 current, last, ret);
211 return retval;
212 }
213
214 /*
215 * Check health of a specific health type. Note that if a thread has not yet
216 * initialize its health subsystem or has quit, it's considered in a good
217 * state.
218 *
219 * Return 0 if health is bad or else 1.
220 */
221 int health_check_state(struct health_app *ha, int type)
222 {
223 int retval = 1;
224 struct health_state *state;
225
226 LTTNG_ASSERT(type < ha->nr_types);
227
228 state_lock(ha);
229
230 cds_list_for_each_entry(state, &ha->list, node) {
231 int ret;
232
233 if (state->type != type) {
234 continue;
235 }
236
237 ret = validate_state(ha, state);
238 if (!ret) {
239 retval = 0;
240 goto end;
241 }
242 }
243
244 /* Check the global state since some state might not be visible anymore. */
245 if (ha->flags[type] & HEALTH_ERROR) {
246 retval = 0;
247 }
248
249 end:
250 state_unlock(ha);
251
252 DBG("Health check for type %d is %s", (int) type,
253 (retval == 0) ? "BAD" : "GOOD");
254 return retval;
255 }
256
257 /*
258 * Init health state.
259 */
260 void health_register(struct health_app *ha, int type)
261 {
262 LTTNG_ASSERT(type < ha->nr_types);
263
264 /* Init TLS state. */
265 uatomic_set(&URCU_TLS(health_state).last, 0);
266 uatomic_set(&URCU_TLS(health_state).last_time.tv_sec, 0);
267 uatomic_set(&URCU_TLS(health_state).last_time.tv_nsec, 0);
268 uatomic_set(&URCU_TLS(health_state).current, 0);
269 uatomic_set(&URCU_TLS(health_state).flags, 0);
270 uatomic_set(&URCU_TLS(health_state).type, type);
271
272 /* Add it to the global TLS state list. */
273 state_lock(ha);
274 cds_list_add(&URCU_TLS(health_state).node, &ha->list);
275 state_unlock(ha);
276 }
277
278 /*
279 * Remove node from global list.
280 */
281 void health_unregister(struct health_app *ha)
282 {
283 state_lock(ha);
284 /*
285 * On error, set the global_error_state since we are about to remove
286 * the node from the global list.
287 */
288 if (uatomic_read(&URCU_TLS(health_state).flags) & HEALTH_ERROR) {
289 uatomic_set(&ha->flags[URCU_TLS(health_state).type],
290 HEALTH_ERROR);
291 }
292 cds_list_del(&URCU_TLS(health_state).node);
293 state_unlock(ha);
294 }
This page took 0.036342 seconds and 4 git commands to generate.