Implement health check for app registration dispatch
[lttng-tools.git] / src / bin / lttng-sessiond / health.h
index 9a1ef391a55b4bb00a7de30a5e867aacfbdaf2cf..d04d18b78ff4b436f59dae699892638f98e8c565 100644 (file)
 #ifndef _HEALTH_H
 #define _HEALTH_H
 
-#include <stdint.h>
+#include <assert.h>
+#include <time.h>
+#include <pthread.h>
+#include <urcu/tls-compat.h>
 #include <urcu/uatomic.h>
+#include <urcu/list.h>
 
 /*
  * These are the value added to the current state depending of the position in
  * the thread where is either waiting on a poll() or running in the code.
  */
-#define HEALTH_POLL_VALUE 1
-#define HEALTH_CODE_VALUE 2
+#define HEALTH_POLL_VALUE      (1UL << 0)
+#define HEALTH_CODE_VALUE      (1UL << 1)
 
-#define HEALTH_IS_IN_POLL(x) (x % HEALTH_CODE_VALUE)
-#define HEALTH_IS_IN_CODE(x) (x % HEALTH_POLL_VALUE)
+#define HEALTH_IS_IN_POLL(x)   ((x) & HEALTH_POLL_VALUE)
 
-struct health_state {
-       uint64_t last;
-       uint64_t current;
+enum health_flags {
+       HEALTH_ERROR = (1U << 0),
+};
+
+enum health_type {
+       HEALTH_TYPE_CMD                 = 0,
+       HEALTH_TYPE_APP_MANAGE  = 1,
+       HEALTH_TYPE_APP_REG             = 2,
+       HEALTH_TYPE_KERNEL              = 3,
+       HEALTH_TYPE_CONSUMER    = 4,
+       HEALTH_TYPE_HT_CLEANUP          = 5,
+       HEALTH_TYPE_APP_MANAGE_NOTIFY   = 6,
+       HEALTH_TYPE_APP_REG_DISPATCH    = 7,
+
+       HEALTH_NUM_TYPE,
 };
 
-/* Health state counters for the client command thread */
-extern struct health_state health_thread_cmd;
+struct health_tls_state_list {
+       struct cds_list_head head;
+};
 
-/* Health state counters for the application registration thread */
-extern struct health_state health_thread_app_reg;
+struct health_state {
+       /*
+        * last counter and last_time are only read and updated by the health_check
+        * thread (single updater).
+        */
+       unsigned long last;
+       struct timespec last_time;
+
+       /*
+        * current and flags are updated by multiple threads concurrently.
+        */
+       unsigned long current;          /* progress counter, updated atomically */
+       enum health_flags flags;        /* other flags, updated atomically */
+       enum health_type type;          /* Indicates the nature of the thread. */
+       /* Node of the global TLS state list. */
+       struct cds_list_head node;
+};
 
-/* Health state counters for the kernel thread */
-extern struct health_state health_thread_kernel;
+/* Declare TLS health state. */
+extern DECLARE_URCU_TLS(struct health_state, health_state);
 
 /*
- * Update current counter by 1 to indicate that the thread is in a blocking
- * state cause by a poll().
+ * Update current counter by 1 to indicate that the thread entered or left a
+ * blocking state caused by a poll(). If the counter's value is not an even
+ * number (meaning a code execution flow), an assert() is raised.
  */
-static inline void health_poll_update(struct health_state *state)
+static inline void health_poll_entry(void)
 {
-       assert(state);
+       /* Code MUST be in code execution state which is an even number. */
+       assert(!(uatomic_read(&URCU_TLS(health_state).current)
+                               & HEALTH_POLL_VALUE));
 
-       uatomic_add(&state->current, HEALTH_POLL_VALUE);
+       uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE);
 }
 
 /*
- * Update current counter by 2 which indicates that we are currently running in
- * a thread and NOT blocked at a poll().
+ * Update current counter by 1 indicating the exit of a poll or blocking call.
+ * If the counter's value is not an odd number (a poll execution), an assert()
+ * is raised.
  */
-static inline void health_code_update(struct health_state *state)
+static inline void health_poll_exit(void)
 {
-       assert(state);
+       /* Code MUST be in poll execution state which is an odd number. */
+       assert(uatomic_read(&URCU_TLS(health_state).current)
+                               & HEALTH_POLL_VALUE);
 
-       uatomic_add(&state->current, HEALTH_CODE_VALUE);
+       uatomic_add(&URCU_TLS(health_state).current, HEALTH_POLL_VALUE);
 }
 
 /*
- * Reset health state. A value of zero indicate a bad health state.
+ * Update current counter by 2 indicates progress in execution of a
+ * thread.
  */
-static inline void health_reset(struct health_state *state)
+static inline void health_code_update(void)
 {
-       assert(state);
-
-       uatomic_set(&state->current, 0);
-       uatomic_set(&state->last, 0);
+       uatomic_add(&URCU_TLS(health_state).current, HEALTH_CODE_VALUE);
 }
 
 /*
- * Init health state.
+ * Set health "error" flag.
  */
-static inline void health_init(struct health_state *state)
+static inline void health_error(void)
 {
-       assert(state);
-
-       uatomic_set(&state->last, 0);
-       uatomic_set(&state->current, HEALTH_CODE_VALUE);
+       uatomic_or(&URCU_TLS(health_state).flags, HEALTH_ERROR);
 }
 
-int health_check_state(struct health_state *state);
+int health_check_state(enum health_type type);
+void health_register(enum health_type type);
+void health_unregister(void);
 
 #endif /* _HEALTH_H */
This page took 0.025758 seconds and 5 git commands to generate.