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