| 1 | #ifndef HEALTH_INTERNAL_H |
| 2 | #define HEALTH_INTERNAL_H |
| 3 | |
| 4 | /* |
| 5 | * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com> |
| 6 | * Copyright (C) 2013 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License, version 2 only, as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * this program; if not, write to the Free Software Foundation, Inc., 51 |
| 19 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | */ |
| 21 | |
| 22 | #include <assert.h> |
| 23 | #include <time.h> |
| 24 | #include <pthread.h> |
| 25 | #include <urcu/tls-compat.h> |
| 26 | #include <urcu/uatomic.h> |
| 27 | #include <urcu/list.h> |
| 28 | #include <lttng/health.h> |
| 29 | #include <common/macros.h> |
| 30 | |
| 31 | /* |
| 32 | * These are the value added to the current state depending of the position in |
| 33 | * the thread where is either waiting on a poll() or running in the code. |
| 34 | */ |
| 35 | #define HEALTH_POLL_VALUE (1UL << 0) |
| 36 | #define HEALTH_CODE_VALUE (1UL << 1) |
| 37 | |
| 38 | #define HEALTH_IS_IN_POLL(x) ((x) & HEALTH_POLL_VALUE) |
| 39 | |
| 40 | struct health_app; |
| 41 | |
| 42 | enum health_flags { |
| 43 | HEALTH_ERROR = (1U << 0), |
| 44 | }; |
| 45 | |
| 46 | struct health_state { |
| 47 | /* |
| 48 | * last counter and last_time are only read and updated by the health_check |
| 49 | * thread (single updater). |
| 50 | */ |
| 51 | unsigned long last; |
| 52 | struct timespec last_time; |
| 53 | |
| 54 | /* |
| 55 | * current and flags are updated by multiple threads concurrently. |
| 56 | */ |
| 57 | unsigned long current; /* progress counter, updated atomically */ |
| 58 | enum health_flags flags; /* other flags, updated atomically */ |
| 59 | int type; /* Indicates the nature of the thread. */ |
| 60 | /* Node of the global TLS state list. */ |
| 61 | struct cds_list_head node; |
| 62 | }; |
| 63 | |
| 64 | enum health_cmd { |
| 65 | HEALTH_CMD_CHECK = 0, |
| 66 | }; |
| 67 | |
| 68 | struct health_comm_msg { |
| 69 | uint32_t cmd; /* enum health_cmd */ |
| 70 | } LTTNG_PACKED; |
| 71 | |
| 72 | struct health_comm_reply { |
| 73 | uint64_t ret_code; /* bitmask of threads in bad health */ |
| 74 | } LTTNG_PACKED; |
| 75 | |
| 76 | /* Declare TLS health state. */ |
| 77 | extern DECLARE_URCU_TLS(struct health_state, health_state); |
| 78 | |
| 79 | /* |
| 80 | * Update current counter by 1 to indicate that the thread entered or left a |
| 81 | * blocking state caused by a poll(). If the counter's value is not an even |
| 82 | * number (meaning a code execution flow), an assert() is raised. |
| 83 | */ |
| 84 | static inline void health_poll_entry(void) |
| 85 | { |
| 86 | /* Code MUST be in code execution state which is an even number. */ |
| 87 | assert(!(uatomic_read(&URCU_TLS(health_state).current) |
| 88 | & HEALTH_POLL_VALUE)); |
| 89 | |
| 90 | uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE); |
| 91 | } |
| 92 | |
| 93 | /* |
| 94 | * Update current counter by 1 indicating the exit of a poll or blocking call. |
| 95 | * If the counter's value is not an odd number (a poll execution), an assert() |
| 96 | * is raised. |
| 97 | */ |
| 98 | static inline void health_poll_exit(void) |
| 99 | { |
| 100 | /* Code MUST be in poll execution state which is an odd number. */ |
| 101 | assert(uatomic_read(&URCU_TLS(health_state).current) |
| 102 | & HEALTH_POLL_VALUE); |
| 103 | |
| 104 | uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Update current counter by 2 indicates progress in execution of a |
| 109 | * thread. |
| 110 | */ |
| 111 | static inline void health_code_update(void) |
| 112 | { |
| 113 | uatomic_add(&URCU_TLS(health_state).current, HEALTH_CODE_VALUE); |
| 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Set health "error" flag. |
| 118 | */ |
| 119 | static inline void health_error(void) |
| 120 | { |
| 121 | uatomic_or(&URCU_TLS(health_state).flags, HEALTH_ERROR); |
| 122 | } |
| 123 | |
| 124 | struct health_app *health_app_create(int nr_types); |
| 125 | void health_app_destroy(struct health_app *ha); |
| 126 | int health_check_state(struct health_app *ha, int type); |
| 127 | void health_register(struct health_app *ha, int type); |
| 128 | void health_unregister(struct health_app *ha); |
| 129 | |
| 130 | #endif /* HEALTH_INTERNAL_H */ |