/src/fluent-bit/lib/cmetrics/src/cmt_metric_histogram.c
Line | Count | Source |
1 | | /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | |
3 | | /* CMetrics |
4 | | * ======== |
5 | | * Copyright 2021-2022 The CMetrics Authors |
6 | | * |
7 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
8 | | * you may not use this file except in compliance with the License. |
9 | | * You may obtain a copy of the License at |
10 | | * |
11 | | * http://www.apache.org/licenses/LICENSE-2.0 |
12 | | * |
13 | | * Unless required by applicable law or agreed to in writing, software |
14 | | * distributed under the License is distributed on an "AS IS" BASIS, |
15 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
16 | | * See the License for the specific language governing permissions and |
17 | | * limitations under the License. |
18 | | */ |
19 | | |
20 | | #include <cmetrics/cmetrics.h> |
21 | | #include <cmetrics/cmt_metric.h> |
22 | | #include <cmetrics/cmt_math.h> |
23 | | #include <cmetrics/cmt_atomic.h> |
24 | | |
25 | | static inline int metric_hist_exchange(struct cmt_metric *metric, |
26 | | uint64_t timestamp, |
27 | | int bucket_id, |
28 | | uint64_t new, uint64_t old) |
29 | 16 | { |
30 | 16 | int result; |
31 | | |
32 | 16 | result = cmt_atomic_compare_exchange(&metric->hist_buckets[bucket_id], |
33 | 16 | old, new); |
34 | 16 | if (result == 0) { |
35 | 0 | return 0; |
36 | 0 | } |
37 | | |
38 | 16 | cmt_atomic_store(&metric->timestamp, timestamp); |
39 | 16 | return 1; |
40 | 16 | } |
41 | | |
42 | | static inline int metric_hist_count_exchange(struct cmt_metric *metric, |
43 | | uint64_t timestamp, |
44 | | uint64_t new, uint64_t old) |
45 | 2 | { |
46 | 2 | int result; |
47 | | |
48 | 2 | result = cmt_atomic_compare_exchange(&metric->hist_count, old, new); |
49 | 2 | if (result == 0) { |
50 | 0 | return 0; |
51 | 0 | } |
52 | | |
53 | 2 | cmt_atomic_store(&metric->timestamp, timestamp); |
54 | 2 | return 1; |
55 | 2 | } |
56 | | |
57 | | static inline int metric_sum_exchange(struct cmt_metric *metric, |
58 | | uint64_t timestamp, |
59 | | double new_value, double old_value) |
60 | 2 | { |
61 | 2 | uint64_t tmp_new; |
62 | 2 | uint64_t tmp_old; |
63 | 2 | int result; |
64 | | |
65 | 2 | tmp_new = cmt_math_d64_to_uint64(new_value); |
66 | 2 | tmp_old = cmt_math_d64_to_uint64(old_value); |
67 | | |
68 | 2 | result = cmt_atomic_compare_exchange(&metric->hist_sum, tmp_old, tmp_new); |
69 | | |
70 | 2 | if (result == 0) { |
71 | 0 | return 0; |
72 | 0 | } |
73 | | |
74 | 2 | cmt_atomic_store(&metric->timestamp, timestamp); |
75 | 2 | return 1; |
76 | 2 | } |
77 | | |
78 | | void cmt_metric_hist_inc(struct cmt_metric *metric, uint64_t timestamp, |
79 | | int bucket_id) |
80 | 16 | { |
81 | 16 | int result; |
82 | 16 | uint64_t old; |
83 | 16 | uint64_t new; |
84 | | |
85 | 16 | do { |
86 | 16 | old = cmt_atomic_load(&metric->hist_buckets[bucket_id]); |
87 | 16 | new = old + 1; |
88 | 16 | result = metric_hist_exchange(metric, timestamp, bucket_id, new, old); |
89 | 16 | } |
90 | 16 | while (result == 0); |
91 | 16 | } |
92 | | |
93 | | void cmt_metric_hist_count_inc(struct cmt_metric *metric, uint64_t timestamp) |
94 | 2 | { |
95 | 2 | int result; |
96 | 2 | uint64_t old; |
97 | 2 | uint64_t new; |
98 | | |
99 | 2 | do { |
100 | 2 | old = cmt_atomic_load(&metric->hist_count); |
101 | 2 | new = old + 1; |
102 | | |
103 | 2 | result = metric_hist_count_exchange(metric, timestamp, new, old); |
104 | 2 | } |
105 | 2 | while (result == 0); |
106 | 2 | } |
107 | | |
108 | | void cmt_metric_hist_count_set(struct cmt_metric *metric, uint64_t timestamp, |
109 | | uint64_t count) |
110 | 0 | { |
111 | 0 | int result; |
112 | 0 | uint64_t old; |
113 | 0 | uint64_t new; |
114 | |
|
115 | 0 | do { |
116 | 0 | old = cmt_atomic_load(&metric->hist_count); |
117 | 0 | new = count; |
118 | |
|
119 | 0 | result = metric_hist_count_exchange(metric, timestamp, new, old); |
120 | 0 | } |
121 | 0 | while (result == 0); |
122 | 0 | } |
123 | | |
124 | | void cmt_metric_hist_sum_add(struct cmt_metric *metric, uint64_t timestamp, |
125 | | double val) |
126 | 2 | { |
127 | 2 | double old; |
128 | 2 | double new; |
129 | 2 | int result; |
130 | | |
131 | 2 | do { |
132 | 2 | old = cmt_metric_hist_get_sum_value(metric); |
133 | 2 | new = old + val; |
134 | 2 | result = metric_sum_exchange(metric, timestamp, new, old); |
135 | 2 | } |
136 | 2 | while (0 == result); |
137 | 2 | } |
138 | | |
139 | | void cmt_metric_hist_sum_set(struct cmt_metric *metric, uint64_t timestamp, |
140 | | double val) |
141 | 0 | { |
142 | 0 | double old; |
143 | 0 | double new; |
144 | 0 | int result; |
145 | |
|
146 | 0 | do { |
147 | 0 | old = cmt_metric_hist_get_sum_value(metric); |
148 | 0 | new = val; |
149 | 0 | result = metric_sum_exchange(metric, timestamp, new, old); |
150 | 0 | } |
151 | 0 | while (0 == result); |
152 | 0 | } |
153 | | |
154 | | void cmt_metric_hist_set(struct cmt_metric *metric, uint64_t timestamp, |
155 | | int bucket_id, double val) |
156 | 0 | { |
157 | 0 | int result; |
158 | 0 | uint64_t old; |
159 | 0 | uint64_t new; |
160 | |
|
161 | 0 | do { |
162 | 0 | old = cmt_atomic_load(&metric->hist_buckets[bucket_id]); |
163 | 0 | new = val; |
164 | |
|
165 | 0 | result = metric_hist_exchange(metric, timestamp, bucket_id, new, old); |
166 | 0 | } |
167 | 0 | while (result == 0); |
168 | 0 | } |
169 | | |
170 | | uint64_t cmt_metric_hist_get_value(struct cmt_metric *metric, int bucket_id) |
171 | 0 | { |
172 | 0 | uint64_t val; |
173 | |
|
174 | 0 | val = cmt_atomic_load(&metric->hist_buckets[bucket_id]); |
175 | 0 | return val; |
176 | 0 | } |
177 | | |
178 | | uint64_t cmt_metric_hist_get_count_value(struct cmt_metric *metric) |
179 | 0 | { |
180 | 0 | uint64_t val; |
181 | |
|
182 | 0 | val = cmt_atomic_load(&metric->hist_count); |
183 | 0 | return val; |
184 | 0 | } |
185 | | |
186 | | double cmt_metric_hist_get_sum_value(struct cmt_metric *metric) |
187 | 2 | { |
188 | 2 | uint64_t val; |
189 | | |
190 | 2 | val = cmt_atomic_load(&metric->hist_sum); |
191 | 2 | return cmt_math_uint64_to_d64(val); |
192 | 2 | } |