2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <common/defaults.h>
26 #include <common/error.h>
30 static const struct timespec time_delta
= {
31 .tv_sec
= DEFAULT_HEALTH_CHECK_DELTA_S
,
32 .tv_nsec
= DEFAULT_HEALTH_CHECK_DELTA_NS
,
36 * Set time difference in res from time_a and time_b.
38 static void time_diff(const struct timespec
*time_a
,
39 const struct timespec
*time_b
, struct timespec
*res
)
41 if (time_a
->tv_nsec
- time_b
->tv_nsec
< 0) {
42 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
- 1;
43 res
->tv_nsec
= 1000000000L + time_a
->tv_sec
- time_b
->tv_sec
;
45 res
->tv_sec
= time_a
->tv_sec
- time_b
->tv_sec
;
46 res
->tv_nsec
= time_a
->tv_nsec
- time_b
->tv_nsec
;
51 * Return true if time_a - time_b > diff, else false.
53 static int time_diff_gt(const struct timespec
*time_a
,
54 const struct timespec
*time_b
, const struct timespec
*diff
)
58 time_diff(time_a
, time_b
, &res
);
59 time_diff(&res
, diff
, &res
);
63 } else if (res
.tv_sec
== 0 && res
.tv_nsec
> 0) {
71 * Check health of a specific health state counter.
73 * Return 0 if health is bad or else 1.
75 int health_check_state(struct health_state
*state
)
78 unsigned long current
, last
;
79 struct timespec current_time
;
84 current
= uatomic_read(&state
->current
);
86 ret
= clock_gettime(CLOCK_MONOTONIC
, ¤t_time
);
88 PERROR("Error reading time\n");
95 * Thread is in bad health if flag HEALTH_ERROR is set. It is also in bad
96 * health if, after the delta delay has passed, its the progress counter
97 * has not moved and it has NOT been waiting for a poll() call.
99 if (uatomic_read(&state
->flags
) & HEALTH_ERROR
) {
105 * Initial condition need to update the last counter and sample time, but
106 * should not check health in this initial case, because we don't know how
107 * much time has passed.
109 if (state
->last_time
.tv_sec
== 0 && state
->last_time
.tv_nsec
== 0) {
110 /* update last counter and last sample time */
111 state
->last
= current
;
112 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
114 if (time_diff_gt(¤t_time
, &state
->last_time
, &time_delta
)) {
115 if (current
== last
&& !HEALTH_IS_IN_POLL(current
)) {
119 /* update last counter and last sample time */
120 state
->last
= current
;
121 memcpy(&state
->last_time
, ¤t_time
, sizeof(current_time
));
126 DBG("Health state current %lu, last %lu, ret %d",