2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License, version 2 only, as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 51
16 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/defaults.h>
27 #include <common/error.h>
28 #include <common/macros.h>
29 #include <common/sessiond-comm/inet.h>
34 * An application-specific error state for unregistered thread keeps
35 * track of thread errors. A thread reporting a health error, normally
36 * unregisters and quits. This makes the TLS health state not available
37 * to the health_check_state() call so on unregister we update this
38 * global error array so we can keep track of which thread was on error
39 * if the TLS health state has been removed.
42 /* List of health state, for each application thread */
43 struct cds_list_head list
;
45 * This lock ensures that TLS memory used for the node and its
46 * container structure don't get reclaimed after the TLS owner
47 * thread exits until we have finished using it.
51 struct timespec time_delta
;
52 /* Health flags containing thread type error state */
53 enum health_flags
*flags
;
56 /* Define TLS health state. */
57 DEFINE_URCU_TLS(struct health_state
, health_state
);
59 struct health_app
*health_app_create(int nr_types
)
61 struct health_app
*ha
;
63 ha
= zmalloc(sizeof(*ha
));
67 ha
->flags
= zmalloc(sizeof(*ha
->flags
));
71 CDS_INIT_LIST_HEAD(&ha
->list
);
72 pthread_mutex_init(&ha
->lock
, NULL
);
73 ha
->nr_types
= nr_types
;
74 ha
->time_delta
.tv_sec
= DEFAULT_HEALTH_CHECK_DELTA_S
;
75 ha
->time_delta
.tv_nsec
= DEFAULT_HEALTH_CHECK_DELTA_NS
;
83 void health_app_destroy(struct health_app
*ha
)
90 * Lock health state global list mutex.
92 static void state_lock(struct health_app
*ha
)
94 pthread_mutex_lock(&ha
->lock
);
98 * Unlock health state global list mutex.
100 static void state_unlock(struct health_app
*ha
)
102 pthread_mutex_unlock(&ha
->lock
);
106 * Set time difference in res from time_a and time_b.
108 static void time_diff(const struct timespec
*time_a
,
109 const struct timespec
*time_b
, struct timespec
*res
)
111 if (time_a
->tv_nsec
- time_b
->tv_nsec
< 0) {
112 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
- 1;
113 res
->tv_nsec
= 1000000000L + time_a
->tv_sec
- time_b
->tv_sec
;
115 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
;
116 res
->tv_nsec
= time_a
->tv_nsec
- time_b
->tv_nsec
;
121 * Return true if time_a - time_b > diff, else false.
123 static int time_diff_gt(const struct timespec
*time_a
,
124 const struct timespec
*time_b
, const struct timespec
*diff
)
128 time_diff(time_a
, time_b
, &res
);
129 time_diff(&res
, diff
, &res
);
131 if (res
.tv_sec
> 0) {
133 } else if (res
.tv_sec
== 0 && res
.tv_nsec
> 0) {
141 * Validate health state. Checks for the error flag or health conditions.
143 * Return 0 if health is bad or else 1.
145 static int validate_state(struct health_app
*ha
, struct health_state
*state
)
148 unsigned long current
, last
;
149 struct timespec current_time
;
154 current
= uatomic_read(&state
->current
);
156 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t_time
);
158 PERROR("Error reading time\n");
165 * Thread is in bad health if flag HEALTH_ERROR is set. It is also in bad
166 * health if, after the delta delay has passed, its the progress counter
167 * has not moved and it has NOT been waiting for a poll() call.
169 if (uatomic_read(&state
->flags
) & HEALTH_ERROR
) {
175 * Initial condition need to update the last counter and sample time, but
176 * should not check health in this initial case, because we don't know how
177 * much time has passed.
179 if (state
->last_time
.tv_sec
== 0 && state
->last_time
.tv_nsec
== 0) {
180 /* update last counter and last sample time */
181 state
->last
= current
;
182 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
184 if (time_diff_gt(¤t_time
, &state
->last_time
,
186 if (current
== last
&& !HEALTH_IS_IN_POLL(current
)) {
190 /* update last counter and last sample time */
191 state
->last
= current
;
192 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
194 /* On error, stop right now and notify caller. */
202 DBG("Health state current %lu, last %lu, ret %d",
208 * Check health of a specific health type. Note that if a thread has not yet
209 * initialize its health subsystem or has quit, it's considered in a good
212 * Return 0 if health is bad or else 1.
214 int health_check_state(struct health_app
*ha
, int type
)
217 struct health_state
*state
;
219 assert(type
< ha
->nr_types
);
223 cds_list_for_each_entry(state
, &ha
->list
, node
) {
226 if (state
->type
!= type
) {
230 ret
= validate_state(ha
, state
);
237 /* Check the global state since some state might not be visible anymore. */
238 if (ha
->flags
[type
] & HEALTH_ERROR
) {
245 DBG("Health check for type %d is %s", (int) type
,
246 (retval
== 0) ? "BAD" : "GOOD");
253 void health_register(struct health_app
*ha
, int type
)
255 assert(type
< ha
->nr_types
);
257 /* Init TLS state. */
258 uatomic_set(&URCU_TLS(health_state
).last
, 0);
259 uatomic_set(&URCU_TLS(health_state
).last_time
.tv_sec
, 0);
260 uatomic_set(&URCU_TLS(health_state
).last_time
.tv_nsec
, 0);
261 uatomic_set(&URCU_TLS(health_state
).current
, 0);
262 uatomic_set(&URCU_TLS(health_state
).flags
, 0);
263 uatomic_set(&URCU_TLS(health_state
).type
, type
);
265 /* Add it to the global TLS state list. */
267 cds_list_add(&URCU_TLS(health_state
).node
, &ha
->list
);
272 * Remove node from global list.
274 void health_unregister(struct health_app
*ha
)
278 * On error, set the global_error_state since we are about to remove
279 * the node from the global list.
281 if (uatomic_read(&URCU_TLS(health_state
).flags
) & HEALTH_ERROR
) {
282 uatomic_set(&ha
->flags
[URCU_TLS(health_state
).type
],
285 cds_list_del(&URCU_TLS(health_state
).node
);
290 * Initiliazie health check subsytem. This should be called before any health
293 void health_init(struct health_app
*ha
)
296 * Get the maximum value between the default delta value and the TCP
297 * timeout with a safety net of the default health check delta.
299 ha
->time_delta
.tv_sec
= max_t(unsigned long,
300 lttcomm_inet_tcp_timeout
+ DEFAULT_HEALTH_CHECK_DELTA_S
,
301 ha
->time_delta
.tv_sec
);
302 DBG("Health check time delta in seconds set to %lu",
303 ha
->time_delta
.tv_sec
);