1 | /* |
2 | * health.h |
3 | * |
4 | * Copyright (C) 2018 Aerospike, Inc. |
5 | * |
6 | * Portions may be licensed to Aerospike, Inc. under one or more contributor |
7 | * license agreements. |
8 | * |
9 | * This program is free software: you can redistribute it and/or modify it under |
10 | * the terms of the GNU Affero General Public License as published by the Free |
11 | * Software Foundation, either version 3 of the License, or (at your option) any |
12 | * later version. |
13 | * |
14 | * This program is distributed in the hope that it will be useful, but WITHOUT |
15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
16 | * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
17 | * details. |
18 | * |
19 | * You should have received a copy of the GNU Affero General Public License |
20 | * along with this program. If not, see http://www.gnu.org/licenses/ |
21 | */ |
22 | |
23 | #pragma once |
24 | |
25 | //========================================================== |
26 | // Includes. |
27 | // |
28 | |
29 | #include <stdbool.h> |
30 | #include <stdint.h> |
31 | |
32 | #include "dynbuf.h" |
33 | #include "node.h" |
34 | |
35 | |
36 | //========================================================== |
37 | // Typedefs & constants. |
38 | // |
39 | |
40 | #define AS_HEALTH_SAMPLING_MASK 0xF // 1 out of 16 ops (6.25%) |
41 | |
42 | typedef enum { |
43 | AS_HEALTH_LOCAL_DEVICE_READ_LAT, |
44 | AS_HEALTH_LOCAL_TYPE_MAX |
45 | } as_health_local_stat_type; |
46 | |
47 | typedef enum { |
48 | AS_HEALTH_NODE_FABRIC_FDS, |
49 | AS_HEALTH_NODE_PROXIES, |
50 | AS_HEALTH_NODE_ARRIVALS, |
51 | AS_HEALTH_NODE_TYPE_MAX |
52 | } as_health_node_stat_type; |
53 | |
54 | typedef enum { |
55 | AS_HEALTH_NS_REPL_LAT, |
56 | AS_HEALTH_NS_TYPE_MAX |
57 | } as_health_ns_stat_type; |
58 | |
59 | |
60 | //========================================================== |
61 | // Globals. |
62 | // |
63 | |
64 | extern bool g_health_enabled; |
65 | |
66 | extern __thread uint64_t g_device_read_counter; |
67 | extern __thread uint64_t g_replica_write_counter; |
68 | |
69 | |
70 | //========================================================== |
71 | // Public API. |
72 | // |
73 | |
74 | void as_health_get_outliers(cf_dyn_buf* db); |
75 | void as_health_get_stats(cf_dyn_buf* db); |
76 | void as_health_start(); |
77 | |
78 | // Not called directly - called by inline wrappers below. |
79 | void health_add_device_latency(uint32_t ns_id, uint32_t d_id, uint64_t start_us); |
80 | void health_add_node_counter(cf_node node, as_health_node_stat_type stat_type); |
81 | void health_add_ns_latency(cf_node node, uint32_t ns_id, as_health_ns_stat_type stat_type, uint64_t start_us); |
82 | |
83 | static inline bool |
84 | as_health_sample_device_read() |
85 | { |
86 | return g_health_enabled && |
87 | (g_device_read_counter++ & AS_HEALTH_SAMPLING_MASK) == 0; |
88 | } |
89 | |
90 | static inline bool |
91 | as_health_sample_replica_write() |
92 | { |
93 | return g_health_enabled && |
94 | (g_replica_write_counter++ & AS_HEALTH_SAMPLING_MASK) == 0; |
95 | } |
96 | |
97 | static inline void |
98 | as_health_add_device_latency(uint32_t ns_id, uint32_t d_id, uint64_t start_us) |
99 | { |
100 | if (g_health_enabled && start_us != 0) { |
101 | health_add_device_latency(ns_id, d_id, start_us); |
102 | } |
103 | } |
104 | |
105 | static inline void |
106 | as_health_add_node_counter(cf_node node, as_health_node_stat_type type) |
107 | { |
108 | if (g_health_enabled) { |
109 | health_add_node_counter(node, type); |
110 | } |
111 | } |
112 | |
113 | static inline void |
114 | as_health_add_ns_latency(cf_node node, uint32_t ns_id, |
115 | as_health_ns_stat_type type, uint64_t start_us) |
116 | { |
117 | if (g_health_enabled && start_us != 0) { |
118 | health_add_ns_latency(node, ns_id, type, start_us); |
119 | } |
120 | } |
121 | |