2 * Copyright (C) 2012 David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2013 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * SPDX-License-Identifier: GPL-2.0-only
16 #include <common/defaults.h>
17 #include <common/error.h>
18 #include <common/macros.h>
19 #include <common/sessiond-comm/inet.h>
21 #include <lttng/health-internal.h>
24 * An application-specific error state for unregistered thread keeps
25 * track of thread errors. A thread reporting a health error, normally
26 * unregisters and quits. This makes the TLS health state not available
27 * to the health_check_state() call so on unregister we update this
28 * global error array so we can keep track of which thread was on error
29 * if the TLS health state has been removed.
32 /* List of health state, for each application thread */
33 struct cds_list_head list
;
35 * This lock ensures that TLS memory used for the node and its
36 * container structure don't get reclaimed after the TLS owner
37 * thread exits until we have finished using it.
41 struct timespec time_delta
;
42 /* Health flags containing thread type error state */
43 enum health_flags
*flags
;
46 /* Define TLS health state. */
47 DEFINE_URCU_TLS(struct health_state
, health_state
);
50 * Initialize health check subsytem.
53 void health_init(struct health_app
*ha
)
56 * Get the maximum value between the default delta value and the TCP
57 * timeout with a safety net of the default health check delta.
59 ha
->time_delta
.tv_sec
= std::max
<unsigned long>(
60 lttcomm_inet_tcp_timeout
+ DEFAULT_HEALTH_CHECK_DELTA_S
,
61 ha
->time_delta
.tv_sec
);
62 DBG("Health check time delta in seconds set to %lu",
63 ha
->time_delta
.tv_sec
);
66 struct health_app
*health_app_create(int nr_types
)
68 struct health_app
*ha
;
70 ha
= (health_app
*) zmalloc(sizeof(*ha
));
74 ha
->flags
= (health_flags
*) zmalloc(sizeof(*ha
->flags
) * nr_types
);
78 CDS_INIT_LIST_HEAD(&ha
->list
);
79 pthread_mutex_init(&ha
->lock
, NULL
);
80 ha
->nr_types
= nr_types
;
81 ha
->time_delta
.tv_sec
= DEFAULT_HEALTH_CHECK_DELTA_S
;
82 ha
->time_delta
.tv_nsec
= DEFAULT_HEALTH_CHECK_DELTA_NS
;
91 void health_app_destroy(struct health_app
*ha
)
98 * Lock health state global list mutex.
100 static void state_lock(struct health_app
*ha
)
102 pthread_mutex_lock(&ha
->lock
);
106 * Unlock health state global list mutex.
108 static void state_unlock(struct health_app
*ha
)
110 pthread_mutex_unlock(&ha
->lock
);
114 * Set time difference in res from time_a and time_b.
116 static void time_diff(const struct timespec
*time_a
,
117 const struct timespec
*time_b
, struct timespec
*res
)
119 if (time_a
->tv_nsec
- time_b
->tv_nsec
< 0) {
120 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
- 1;
121 res
->tv_nsec
= 1000000000L + time_a
->tv_sec
- time_b
->tv_sec
;
123 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
;
124 res
->tv_nsec
= time_a
->tv_nsec
- time_b
->tv_nsec
;
129 * Return true if time_a - time_b > diff, else false.
131 static int time_diff_gt(const struct timespec
*time_a
,
132 const struct timespec
*time_b
, const struct timespec
*diff
)
136 time_diff(time_a
, time_b
, &res
);
137 time_diff(&res
, diff
, &res
);
139 if (res
.tv_sec
> 0) {
141 } else if (res
.tv_sec
== 0 && res
.tv_nsec
> 0) {
149 * Validate health state. Checks for the error flag or health conditions.
151 * Return 0 if health is bad or else 1.
153 static int validate_state(struct health_app
*ha
, struct health_state
*state
)
156 unsigned long current
, last
;
157 struct timespec current_time
;
162 current
= uatomic_read(&state
->current
);
164 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, ¤t_time
);
166 PERROR("Error reading time\n");
173 * Thread is in bad health if flag HEALTH_ERROR is set. It is also in bad
174 * health if, after the delta delay has passed, its the progress counter
175 * has not moved and it has NOT been waiting for a poll() call.
177 if (uatomic_read(&state
->flags
) & HEALTH_ERROR
) {
183 * Initial condition need to update the last counter and sample time, but
184 * should not check health in this initial case, because we don't know how
185 * much time has passed.
187 if (state
->last_time
.tv_sec
== 0 && state
->last_time
.tv_nsec
== 0) {
188 /* update last counter and last sample time */
189 state
->last
= current
;
190 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
192 if (time_diff_gt(¤t_time
, &state
->last_time
,
194 if (current
== last
&& !HEALTH_IS_IN_POLL(current
)) {
198 /* update last counter and last sample time */
199 state
->last
= current
;
200 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
202 /* On error, stop right now and notify caller. */
210 DBG("Health state current %lu, last %lu, ret %d",
216 * Check health of a specific health type. Note that if a thread has not yet
217 * initialize its health subsystem or has quit, it's considered in a good
220 * Return 0 if health is bad or else 1.
222 int health_check_state(struct health_app
*ha
, int type
)
225 struct health_state
*state
;
227 LTTNG_ASSERT(type
< ha
->nr_types
);
231 cds_list_for_each_entry(state
, &ha
->list
, node
) {
234 if (state
->type
!= type
) {
238 ret
= validate_state(ha
, state
);
245 /* Check the global state since some state might not be visible anymore. */
246 if (ha
->flags
[type
] & HEALTH_ERROR
) {
253 DBG("Health check for type %d is %s", (int) type
,
254 (retval
== 0) ? "BAD" : "GOOD");
261 void health_register(struct health_app
*ha
, int type
)
263 LTTNG_ASSERT(type
< ha
->nr_types
);
265 /* Init TLS state. */
266 uatomic_set(&URCU_TLS(health_state
).last
, 0);
267 uatomic_set(&URCU_TLS(health_state
).last_time
.tv_sec
, 0);
268 uatomic_set(&URCU_TLS(health_state
).last_time
.tv_nsec
, 0);
269 uatomic_set(&URCU_TLS(health_state
).current
, 0);
270 uatomic_set(&URCU_TLS(health_state
).flags
, (health_flags
) 0);
271 uatomic_set(&URCU_TLS(health_state
).type
, type
);
273 /* Add it to the global TLS state list. */
275 cds_list_add(&URCU_TLS(health_state
).node
, &ha
->list
);
280 * Remove node from global list.
282 void health_unregister(struct health_app
*ha
)
286 * On error, set the global_error_state since we are about to remove
287 * the node from the global list.
289 if (uatomic_read(&URCU_TLS(health_state
).flags
) & HEALTH_ERROR
) {
290 uatomic_set(&ha
->flags
[URCU_TLS(health_state
).type
],
293 cds_list_del(&URCU_TLS(health_state
).node
);