| 1 | /* |
| 2 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 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. |
| 7 | * |
| 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 |
| 11 | * more details. |
| 12 | * |
| 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. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #include <assert.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <time.h> |
| 24 | |
| 25 | #include <common/defaults.h> |
| 26 | #include <common/error.h> |
| 27 | |
| 28 | #include "health.h" |
| 29 | |
| 30 | static const struct timespec time_delta = { |
| 31 | .tv_sec = DEFAULT_HEALTH_CHECK_DELTA_S, |
| 32 | .tv_nsec = DEFAULT_HEALTH_CHECK_DELTA_NS, |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * Set time difference in res from time_a and time_b. |
| 37 | */ |
| 38 | static void time_diff(const struct timespec *time_a, |
| 39 | const struct timespec *time_b, struct timespec *res) |
| 40 | { |
| 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; |
| 44 | } else { |
| 45 | res->tv_sec = time_a->tv_sec - time_b->tv_sec; |
| 46 | res->tv_nsec = time_a->tv_nsec - time_b->tv_nsec; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Return true if time_a - time_b > diff, else false. |
| 52 | */ |
| 53 | static int time_diff_gt(const struct timespec *time_a, |
| 54 | const struct timespec *time_b, const struct timespec *diff) |
| 55 | { |
| 56 | struct timespec res; |
| 57 | |
| 58 | time_diff(time_a, time_b, &res); |
| 59 | time_diff(&res, diff, &res); |
| 60 | |
| 61 | if (res.tv_sec > 0) { |
| 62 | return 1; |
| 63 | } else if (res.tv_sec == 0 && res.tv_nsec > 0) { |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * Check health of a specific health state counter. |
| 72 | * |
| 73 | * Return 0 if health is bad or else 1. |
| 74 | */ |
| 75 | int health_check_state(struct health_state *state) |
| 76 | { |
| 77 | int retval = 1, ret; |
| 78 | unsigned long current, last; |
| 79 | struct timespec current_time; |
| 80 | |
| 81 | assert(state); |
| 82 | |
| 83 | last = state->last; |
| 84 | current = uatomic_read(&state->current); |
| 85 | |
| 86 | ret = clock_gettime(CLOCK_MONOTONIC, ¤t_time); |
| 87 | if (ret < 0) { |
| 88 | PERROR("Error reading time\n"); |
| 89 | /* error */ |
| 90 | retval = 0; |
| 91 | goto end; |
| 92 | } |
| 93 | |
| 94 | /* |
| 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. |
| 98 | */ |
| 99 | if (uatomic_read(&state->flags) & HEALTH_ERROR) { |
| 100 | retval = 0; |
| 101 | goto end; |
| 102 | } |
| 103 | |
| 104 | /* |
| 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. |
| 108 | */ |
| 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)); |
| 113 | } else { |
| 114 | if (time_diff_gt(¤t_time, &state->last_time, &time_delta)) { |
| 115 | if (current == last && !HEALTH_IS_IN_POLL(current)) { |
| 116 | /* error */ |
| 117 | retval = 0; |
| 118 | } |
| 119 | /* update last counter and last sample time */ |
| 120 | state->last = current; |
| 121 | memcpy(&state->last_time, ¤t_time, sizeof(current_time)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | end: |
| 126 | DBG("Health state current %lu, last %lu, ret %d", |
| 127 | current, last, ret); |
| 128 | |
| 129 | return retval; |
| 130 | } |