Commit | Line | Data |
---|---|---|
44a5e5eb DG |
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 | ||
86acf0da | 21 | #include <assert.h> |
8809eec0 | 22 | #include <time.h> |
927ca06a DG |
23 | #include <pthread.h> |
24 | #include <urcu/tls-compat.h> | |
44a5e5eb | 25 | #include <urcu/uatomic.h> |
927ca06a | 26 | #include <urcu/list.h> |
44a5e5eb DG |
27 | |
28 | /* | |
29 | * These are the value added to the current state depending of the position in | |
30 | * the thread where is either waiting on a poll() or running in the code. | |
31 | */ | |
139ac872 MD |
32 | #define HEALTH_POLL_VALUE (1UL << 0) |
33 | #define HEALTH_CODE_VALUE (1UL << 1) | |
44a5e5eb | 34 | |
139ac872 MD |
35 | #define HEALTH_IS_IN_POLL(x) ((x) & HEALTH_POLL_VALUE) |
36 | ||
8782cc74 MD |
37 | struct health_app; |
38 | ||
139ac872 | 39 | enum health_flags { |
9edd46e7 | 40 | HEALTH_ERROR = (1U << 0), |
927ca06a DG |
41 | }; |
42 | ||
44a5e5eb | 43 | struct health_state { |
139ac872 | 44 | /* |
8809eec0 | 45 | * last counter and last_time are only read and updated by the health_check |
139ac872 MD |
46 | * thread (single updater). |
47 | */ | |
48 | unsigned long last; | |
8809eec0 MD |
49 | struct timespec last_time; |
50 | ||
139ac872 MD |
51 | /* |
52 | * current and flags are updated by multiple threads concurrently. | |
53 | */ | |
54 | unsigned long current; /* progress counter, updated atomically */ | |
55 | enum health_flags flags; /* other flags, updated atomically */ | |
8782cc74 | 56 | int type; /* Indicates the nature of the thread. */ |
927ca06a DG |
57 | /* Node of the global TLS state list. */ |
58 | struct cds_list_head node; | |
44a5e5eb DG |
59 | }; |
60 | ||
927ca06a DG |
61 | /* Declare TLS health state. */ |
62 | extern DECLARE_URCU_TLS(struct health_state, health_state); | |
63 | ||
44a5e5eb | 64 | /* |
a78af745 DG |
65 | * Update current counter by 1 to indicate that the thread entered or left a |
66 | * blocking state caused by a poll(). If the counter's value is not an even | |
67 | * number (meaning a code execution flow), an assert() is raised. | |
44a5e5eb | 68 | */ |
a78af745 | 69 | static inline void health_poll_entry(void) |
44a5e5eb | 70 | { |
a78af745 DG |
71 | /* Code MUST be in code execution state which is an even number. */ |
72 | assert(!(uatomic_read(&URCU_TLS(health_state).current) | |
73 | & HEALTH_POLL_VALUE)); | |
74 | ||
75 | uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE); | |
76 | } | |
77 | ||
78 | /* | |
79 | * Update current counter by 1 indicating the exit of a poll or blocking call. | |
80 | * If the counter's value is not an odd number (a poll execution), an assert() | |
81 | * is raised. | |
82 | */ | |
83 | static inline void health_poll_exit(void) | |
84 | { | |
85 | /* Code MUST be in poll execution state which is an odd number. */ | |
86 | assert(uatomic_read(&URCU_TLS(health_state).current) | |
87 | & HEALTH_POLL_VALUE); | |
88 | ||
927ca06a | 89 | uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE); |
44a5e5eb DG |
90 | } |
91 | ||
92 | /* | |
139ac872 MD |
93 | * Update current counter by 2 indicates progress in execution of a |
94 | * thread. | |
44a5e5eb | 95 | */ |
840cb59c | 96 | static inline void health_code_update(void) |
44a5e5eb | 97 | { |
927ca06a | 98 | uatomic_add(&URCU_TLS(health_state).current, HEALTH_CODE_VALUE); |
139ac872 | 99 | } |
44a5e5eb | 100 | |
139ac872 MD |
101 | /* |
102 | * Set health "error" flag. | |
103 | */ | |
840cb59c | 104 | static inline void health_error(void) |
139ac872 | 105 | { |
927ca06a | 106 | uatomic_or(&URCU_TLS(health_state).flags, HEALTH_ERROR); |
44a5e5eb DG |
107 | } |
108 | ||
8782cc74 MD |
109 | struct health_app *health_app_create(int nr_types); |
110 | void health_app_destroy(struct health_app *ha); | |
111 | int health_check_state(struct health_app *ha, int type); | |
112 | void health_register(struct health_app *ha, int type); | |
113 | void health_unregister(struct health_app *ha); | |
114 | void health_init(struct health_app *ha); | |
44a5e5eb DG |
115 | |
116 | #endif /* _HEALTH_H */ |