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