docs: Add supported versions and fix-backport policy
[lttng-tools.git] / include / lttng / health-internal.hpp
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 * SPDX-License-Identifier: GPL-2.0-only
9 *
10 */
11
12 #include <common/compat/time.hpp>
13 #include <common/macros.hpp>
14
15 #include <lttng/health.h>
16
17 #include <pthread.h>
18 #include <urcu/list.h>
19 #include <urcu/tls-compat.h>
20 #include <urcu/uatomic.h>
21
22 /*
23 * These are the value added to the current state depending of the position in
24 * the thread where is either waiting on a poll() or running in the code.
25 */
26 #define HEALTH_POLL_VALUE (1UL << 0)
27 #define HEALTH_CODE_VALUE (1UL << 1)
28
29 #define HEALTH_IS_IN_POLL(x) ((x) &HEALTH_POLL_VALUE)
30
31 struct health_app;
32
33 enum health_flags {
34 HEALTH_ERROR = (1U << 0),
35 };
36
37 struct health_state {
38 /*
39 * last counter and last_time are only read and updated by the health_check
40 * thread (single updater).
41 */
42 unsigned long last;
43 struct timespec last_time;
44
45 /*
46 * current and flags are updated by multiple threads concurrently.
47 */
48 unsigned long current; /* progress counter, updated atomically */
49 enum health_flags flags; /* other flags, updated atomically */
50 int type; /* Indicates the nature of the thread. */
51 /* Node of the global TLS state list. */
52 struct cds_list_head node;
53 };
54
55 enum health_cmd {
56 HEALTH_CMD_CHECK = 0,
57 };
58
59 struct health_comm_msg {
60 uint32_t cmd; /* enum health_cmd */
61 } LTTNG_PACKED;
62
63 struct health_comm_reply {
64 uint64_t ret_code; /* bitmask of threads in bad health */
65 } LTTNG_PACKED;
66
67 /* Declare TLS health state. */
68 extern thread_local struct health_state health_state;
69
70 /*
71 * Update current counter by 1 to indicate that the thread entered or left a
72 * blocking state caused by a poll(). If the counter's value is not an even
73 * number (meaning a code execution flow), an LTTNG_ASSERT() is raised.
74 */
75 static inline void health_poll_entry()
76 {
77 /* Code MUST be in code execution state which is an even number. */
78 LTTNG_ASSERT(!(uatomic_read(&health_state.current) & HEALTH_POLL_VALUE));
79
80 uatomic_add(&health_state.current, HEALTH_POLL_VALUE);
81 }
82
83 /*
84 * Update current counter by 1 indicating the exit of a poll or blocking call.
85 * If the counter's value is not an odd number (a poll execution), an LTTNG_ASSERT()
86 * is raised.
87 */
88 static inline void health_poll_exit()
89 {
90 /* Code MUST be in poll execution state which is an odd number. */
91 LTTNG_ASSERT(uatomic_read(&health_state.current) & HEALTH_POLL_VALUE);
92
93 uatomic_add(&health_state.current, HEALTH_POLL_VALUE);
94 }
95
96 /*
97 * Update current counter by 2 indicates progress in execution of a
98 * thread.
99 */
100 static inline void health_code_update()
101 {
102 uatomic_add(&health_state.current, HEALTH_CODE_VALUE);
103 }
104
105 /*
106 * Set health "error" flag.
107 */
108 static inline void health_error()
109 {
110 uatomic_or(&health_state.flags, HEALTH_ERROR);
111 }
112
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);
118
119 #endif /* HEALTH_INTERNAL_H */
This page took 0.030785 seconds and 4 git commands to generate.