| 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 | #ifndef _HEALTH_H |
| 19 | #define _HEALTH_H |
| 20 | |
| 21 | #include <assert.h> |
| 22 | #include <time.h> |
| 23 | #include <urcu/uatomic.h> |
| 24 | |
| 25 | /* |
| 26 | * These are the value added to the current state depending of the position in |
| 27 | * the thread where is either waiting on a poll() or running in the code. |
| 28 | */ |
| 29 | #define HEALTH_POLL_VALUE (1UL << 0) |
| 30 | #define HEALTH_CODE_VALUE (1UL << 1) |
| 31 | |
| 32 | #define HEALTH_IS_IN_POLL(x) ((x) & HEALTH_POLL_VALUE) |
| 33 | |
| 34 | enum health_flags { |
| 35 | HEALTH_EXIT = (1U << 0), |
| 36 | HEALTH_ERROR = (1U << 1), |
| 37 | }; |
| 38 | |
| 39 | struct health_state { |
| 40 | /* |
| 41 | * last counter and last_time are only read and updated by the health_check |
| 42 | * thread (single updater). |
| 43 | */ |
| 44 | unsigned long last; |
| 45 | struct timespec last_time; |
| 46 | |
| 47 | /* |
| 48 | * current and flags are updated by multiple threads concurrently. |
| 49 | */ |
| 50 | unsigned long current; /* progress counter, updated atomically */ |
| 51 | enum health_flags flags; /* other flags, updated atomically */ |
| 52 | }; |
| 53 | |
| 54 | /* Health state counters for the client command thread */ |
| 55 | extern struct health_state health_thread_cmd; |
| 56 | |
| 57 | /* Health state counters for the application management thread */ |
| 58 | extern struct health_state health_thread_app_manage; |
| 59 | |
| 60 | /* Health state counters for the application registration thread */ |
| 61 | extern struct health_state health_thread_app_reg; |
| 62 | |
| 63 | /* Health state counters for the kernel thread */ |
| 64 | extern struct health_state health_thread_kernel; |
| 65 | |
| 66 | /* |
| 67 | * Update current counter by 1 to indicate that the thread entered or |
| 68 | * left a blocking state caused by a poll(). |
| 69 | */ |
| 70 | static inline void health_poll_update(struct health_state *state) |
| 71 | { |
| 72 | assert(state); |
| 73 | uatomic_add(&state->current, HEALTH_POLL_VALUE); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Update current counter by 2 indicates progress in execution of a |
| 78 | * thread. |
| 79 | */ |
| 80 | static inline void health_code_update(struct health_state *state) |
| 81 | { |
| 82 | assert(state); |
| 83 | uatomic_add(&state->current, HEALTH_CODE_VALUE); |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * Set health "exit" flag. |
| 88 | */ |
| 89 | static inline void health_exit(struct health_state *state) |
| 90 | { |
| 91 | assert(state); |
| 92 | uatomic_or(&state->flags, HEALTH_EXIT); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * Set health "error" flag. |
| 97 | */ |
| 98 | static inline void health_error(struct health_state *state) |
| 99 | { |
| 100 | assert(state); |
| 101 | uatomic_or(&state->flags, HEALTH_ERROR); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Init health state. |
| 106 | */ |
| 107 | static inline void health_init(struct health_state *state) |
| 108 | { |
| 109 | assert(state); |
| 110 | state->last = 0; |
| 111 | state->last_time.tv_sec = 0; |
| 112 | state->last_time.tv_nsec = 0; |
| 113 | uatomic_set(&state->current, 0); |
| 114 | uatomic_set(&state->flags, 0); |
| 115 | } |
| 116 | |
| 117 | int health_check_state(struct health_state *state); |
| 118 | |
| 119 | #endif /* _HEALTH_H */ |