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
15 #include <common/defaults.h>
16 #include <common/error.h>
17 #include <common/macros.h>
18 #include <common/sessiond-comm/inet.h>
20 #include <lttng/health-internal.h>
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.
31 /* List of health state, for each application thread */
32 struct cds_list_head list
;
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.
40 struct timespec time_delta
;
41 /* Health flags containing thread type error state */
42 enum health_flags
*flags
;
45 /* Define TLS health state. */
46 DEFINE_URCU_TLS(struct health_state
, health_state
);
49 * Initialize health check subsytem.
52 void health_init(struct health_app
*ha
)
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.
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
);
65 struct health_app
*health_app_create(int nr_types
)
67 struct health_app
*ha
;
69 ha
= zmalloc(sizeof(*ha
));
73 ha
->flags
= zmalloc(sizeof(*ha
->flags
) * nr_types
);
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
;
90 void health_app_destroy(struct health_app
*ha
)
97 * Lock health state global list mutex.
99 static void state_lock(struct health_app
*ha
)
101 pthread_mutex_lock(&ha
->lock
);
105 * Unlock health state global list mutex.
107 static void state_unlock(struct health_app
*ha
)
109 pthread_mutex_unlock(&ha
->lock
);
113 * Set time difference in res from time_a and time_b.
115 static void time_diff(const struct timespec
*time_a
,
116 const struct timespec
*time_b
, struct timespec
*res
)
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
;
122 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
;
123 res
->tv_nsec
= time_a
->tv_nsec
- time_b
->tv_nsec
;
128 * Return true if time_a - time_b > diff, else false.
130 static int time_diff_gt(const struct timespec
*time_a
,
131 const struct timespec
*time_b
, const struct timespec
*diff
)
135 time_diff(time_a
, time_b
, &res
);
136 time_diff(&res
, diff
, &res
);
138 if (res
.tv_sec
> 0) {
140 } else if (res
.tv_sec
== 0 && res
.tv_nsec
> 0) {
148 * Validate health state. Checks for the error flag or health conditions.
150 * Return 0 if health is bad or else 1.
152 static int validate_state(struct health_app
*ha
, struct health_state
*state
)
155 unsigned long current
, last
;
156 struct timespec current_time
;
161 current
= uatomic_read(&state
->current
);
163 ret
= lttng_clock_gettime(CLOCK_MONOTONIC
, ¤t_time
);
165 PERROR("Error reading time\n");
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.
176 if (uatomic_read(&state
->flags
) & HEALTH_ERROR
) {
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.
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
, ¤t_time
, sizeof(current_time
));
191 if (time_diff_gt(¤t_time
, &state
->last_time
,
193 if (current
== last
&& !HEALTH_IS_IN_POLL(current
)) {
197 /* update last counter and last sample time */
198 state
->last
= current
;
199 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
201 /* On error, stop right now and notify caller. */
209 DBG("Health state current %lu, last %lu, ret %d",
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
219 * Return 0 if health is bad or else 1.
221 int health_check_state(struct health_app
*ha
, int type
)
224 struct health_state
*state
;
226 LTTNG_ASSERT(type
< ha
->nr_types
);
230 cds_list_for_each_entry(state
, &ha
->list
, node
) {
233 if (state
->type
!= type
) {
237 ret
= validate_state(ha
, state
);
244 /* Check the global state since some state might not be visible anymore. */
245 if (ha
->flags
[type
] & HEALTH_ERROR
) {
252 DBG("Health check for type %d is %s", (int) type
,
253 (retval
== 0) ? "BAD" : "GOOD");
260 void health_register(struct health_app
*ha
, int type
)
262 LTTNG_ASSERT(type
< ha
->nr_types
);
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
);
272 /* Add it to the global TLS state list. */
274 cds_list_add(&URCU_TLS(health_state
).node
, &ha
->list
);
279 * Remove node from global list.
281 void health_unregister(struct health_app
*ha
)
285 * On error, set the global_error_state since we are about to remove
286 * the node from the global list.
288 if (uatomic_read(&URCU_TLS(health_state
).flags
) & HEALTH_ERROR
) {
289 uatomic_set(&ha
->flags
[URCU_TLS(health_state
).type
],
292 cds_list_del(&URCU_TLS(health_state
).node
);