| 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 | |
| 24 | #include <common/error.h> |
| 25 | |
| 26 | #include "health.h" |
| 27 | |
| 28 | /* |
| 29 | * Check health of a specific health state counter. |
| 30 | * |
| 31 | * Return 0 if health is bad or else 1. |
| 32 | */ |
| 33 | int health_check_state(struct health_state *state) |
| 34 | { |
| 35 | int ret; |
| 36 | uint64_t current; |
| 37 | uint64_t last; |
| 38 | |
| 39 | assert(state); |
| 40 | |
| 41 | current = uatomic_read(&state->current); |
| 42 | last = uatomic_read(&state->last); |
| 43 | |
| 44 | /* |
| 45 | * Here are the conditions for a bad health. Current state set to 0 or the |
| 46 | * current state is the same as the last one and we are NOT waiting for a |
| 47 | * poll() call. |
| 48 | */ |
| 49 | if (current == 0 || (current == last && HEALTH_IS_IN_CODE(current))) { |
| 50 | ret = 0; |
| 51 | goto error; |
| 52 | } |
| 53 | |
| 54 | /* All good */ |
| 55 | ret = 1; |
| 56 | |
| 57 | error: |
| 58 | DBG("Health state current %" PRIu64 ", last %" PRIu64 ", ret %d", |
| 59 | current, last, ret); |
| 60 | |
| 61 | /* Exchange current state counter into last one */ |
| 62 | uatomic_xchg(&state->last, state->current); |
| 63 | return ret; |
| 64 | } |