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