1 | // Licensed to the Apache Software Foundation (ASF) under one |
2 | // or more contributor license agreements. See the NOTICE file |
3 | // distributed with this work for additional information |
4 | // regarding copyright ownership. The ASF licenses this file |
5 | // to you under the Apache License, Version 2.0 (the |
6 | // "License"); you may not use this file except in compliance |
7 | // with the License. You may obtain a copy of the License at |
8 | // |
9 | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | // |
11 | // Unless required by applicable law or agreed to in writing, |
12 | // software distributed under the License is distributed on an |
13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | // KIND, either express or implied. See the License for the |
15 | // specific language governing permissions and limitations |
16 | // under the License. |
17 | |
18 | // Date 2014/09/25 17:50:21 |
19 | |
20 | #ifndef BVAR_RECORDER_H |
21 | #define BVAR_RECORDER_H |
22 | |
23 | #include <stdint.h> // int64_t uint64_t |
24 | #include "butil/macros.h" // BAIDU_CASSERT |
25 | #include "butil/logging.h" // LOG |
26 | #include "bvar/detail/combiner.h" // detail::AgentCombiner |
27 | #include "bvar/variable.h" |
28 | #include "bvar/window.h" |
29 | #include "bvar/detail/sampler.h" |
30 | |
31 | namespace bvar { |
32 | |
33 | struct Stat { |
34 | Stat() : sum(0), num(0) {}; |
35 | Stat(int64_t sum2, int64_t num2) : sum(sum2), num(num2) {} |
36 | int64_t sum; |
37 | int64_t num; |
38 | |
39 | int64_t get_average_int() const { |
40 | //num can be changed by sampling thread, use tmp_num |
41 | int64_t tmp_num = num; |
42 | if (tmp_num == 0) { |
43 | return 0; |
44 | } |
45 | return sum / (int64_t)tmp_num; |
46 | } |
47 | double get_average_double() const { |
48 | int64_t tmp_num = num; |
49 | if (tmp_num == 0) { |
50 | return 0.0; |
51 | } |
52 | return (double)sum / (double)tmp_num; |
53 | } |
54 | Stat operator-(const Stat& rhs) const { |
55 | return Stat(sum - rhs.sum, num - rhs.num); |
56 | } |
57 | void operator-=(const Stat& rhs) { |
58 | sum -= rhs.sum; |
59 | num -= rhs.num; |
60 | } |
61 | Stat operator+(const Stat& rhs) const { |
62 | return Stat(sum + rhs.sum, num + rhs.num); |
63 | } |
64 | void operator+=(const Stat& rhs) { |
65 | sum += rhs.sum; |
66 | num += rhs.num; |
67 | } |
68 | }; |
69 | |
70 | inline std::ostream& operator<<(std::ostream& os, const Stat& s) { |
71 | const int64_t v = s.get_average_int(); |
72 | if (v != 0) { |
73 | return os << v; |
74 | } else { |
75 | return os << s.get_average_double(); |
76 | } |
77 | } |
78 | |
79 | // For calculating average of numbers. |
80 | // Example: |
81 | // IntRecorder latency; |
82 | // latency << 1 << 3 << 5; |
83 | // CHECK_EQ(3, latency.average()); |
84 | class IntRecorder : public Variable { |
85 | public: |
86 | // Compressing format: |
87 | // | 20 bits (unsigned) | sign bit | 43 bits | |
88 | // num sum |
89 | const static size_t SUM_BIT_WIDTH=44; |
90 | const static uint64_t MAX_SUM_PER_THREAD = (1ul << SUM_BIT_WIDTH) - 1; |
91 | const static uint64_t MAX_NUM_PER_THREAD = (1ul << (64ul - SUM_BIT_WIDTH)) - 1; |
92 | BAIDU_CASSERT(SUM_BIT_WIDTH > 32 && SUM_BIT_WIDTH < 64, |
93 | SUM_BIT_WIDTH_must_be_between_33_and_63); |
94 | |
95 | struct AddStat { |
96 | void operator()(Stat& s1, const Stat& s2) const { s1 += s2; } |
97 | }; |
98 | struct MinusStat { |
99 | void operator()(Stat& s1, const Stat& s2) const { s1 -= s2; } |
100 | }; |
101 | |
102 | typedef Stat value_type; |
103 | typedef detail::ReducerSampler<IntRecorder, Stat, |
104 | AddStat, MinusStat> sampler_type; |
105 | |
106 | typedef Stat SampleSet; |
107 | |
108 | struct AddToStat { |
109 | void operator()(Stat& lhs, uint64_t rhs) const { |
110 | lhs.sum += _extend_sign_bit(_get_sum(rhs)); |
111 | lhs.num += _get_num(rhs); |
112 | } |
113 | }; |
114 | |
115 | typedef detail::AgentCombiner<Stat, uint64_t, AddToStat> combiner_type; |
116 | typedef combiner_type::Agent agent_type; |
117 | |
118 | IntRecorder() : _sampler(NULL) {} |
119 | |
120 | explicit IntRecorder(const butil::StringPiece& name) : _sampler(NULL) { |
121 | expose(name); |
122 | } |
123 | |
124 | IntRecorder(const butil::StringPiece& prefix, const butil::StringPiece& name) |
125 | : _sampler(NULL) { |
126 | expose_as(prefix, name); |
127 | } |
128 | |
129 | ~IntRecorder() { |
130 | hide(); |
131 | if (_sampler) { |
132 | _sampler->destroy(); |
133 | _sampler = NULL; |
134 | } |
135 | } |
136 | |
137 | // Note: The input type is acutally int. Use int64_t to check overflow. |
138 | IntRecorder& operator<<(int64_t/*note*/ sample); |
139 | |
140 | int64_t average() const { |
141 | return _combiner.combine_agents().get_average_int(); |
142 | } |
143 | |
144 | double average(double) const { |
145 | return _combiner.combine_agents().get_average_double(); |
146 | } |
147 | |
148 | Stat get_value() const { |
149 | return _combiner.combine_agents(); |
150 | } |
151 | |
152 | Stat reset() { |
153 | return _combiner.reset_all_agents(); |
154 | } |
155 | |
156 | AddStat op() const { return AddStat(); } |
157 | MinusStat inv_op() const { return MinusStat(); } |
158 | |
159 | void describe(std::ostream& os, bool /*quote_string*/) const override { |
160 | os << get_value(); |
161 | } |
162 | |
163 | bool valid() const { return _combiner.valid(); } |
164 | |
165 | sampler_type* get_sampler() { |
166 | if (NULL == _sampler) { |
167 | _sampler = new sampler_type(this); |
168 | _sampler->schedule(); |
169 | } |
170 | return _sampler; |
171 | } |
172 | |
173 | // This name is useful for printing overflow log in operator<< since |
174 | // IntRecorder is often used as the source of data and not exposed. |
175 | void set_debug_name(const butil::StringPiece& name) { |
176 | _debug_name.assign(name.data(), name.size()); |
177 | } |
178 | |
179 | private: |
180 | // TODO: The following numeric functions should be independent utils |
181 | static uint64_t _get_sum(const uint64_t n) { |
182 | return (n & MAX_SUM_PER_THREAD); |
183 | } |
184 | |
185 | static uint64_t _get_num(const uint64_t n) { |
186 | return n >> SUM_BIT_WIDTH; |
187 | } |
188 | |
189 | // Fill all the first (64 - SUM_BIT_WIDTH + 1) bits with 1 if the sign bit is 1 |
190 | // to represent a complete 64-bit negative number |
191 | // Check out http://en.wikipedia.org/wiki/Signed_number_representations if |
192 | // you are confused |
193 | static int64_t _extend_sign_bit(const uint64_t sum) { |
194 | return (((1ul << (64ul - SUM_BIT_WIDTH + 1)) - 1) |
195 | * ((1ul << (SUM_BIT_WIDTH - 1) & sum))) |
196 | | (int64_t)sum; |
197 | } |
198 | |
199 | // Convert complement into a |SUM_BIT_WIDTH|-bit unsigned integer |
200 | static uint64_t _get_complement(int64_t n) { |
201 | return n & (MAX_SUM_PER_THREAD); |
202 | } |
203 | |
204 | static uint64_t _compress(const uint64_t num, const uint64_t sum) { |
205 | return (num << SUM_BIT_WIDTH) |
206 | // There is a redundant '1' in the front of sum which was |
207 | // combined with two negative number, so truncation has to be |
208 | // done here |
209 | | (sum & MAX_SUM_PER_THREAD) |
210 | ; |
211 | } |
212 | |
213 | // Check whether the sum of the two integer overflows the range of signed |
214 | // integer with the width of SUM_BIT_WIDTH, which is |
215 | // [-2^(SUM_BIT_WIDTH -1), 2^(SUM_BIT_WIDTH -1) - 1) (eg. [-128, 127) for |
216 | // signed 8-bit integer) |
217 | static bool _will_overflow(const int64_t lhs, const int rhs) { |
218 | return |
219 | // Both integers are positive and the sum is larger than the largest |
220 | // number |
221 | ((lhs > 0) && (rhs > 0) |
222 | && (lhs + rhs > ((int64_t)MAX_SUM_PER_THREAD >> 1))) |
223 | // Or both integers are negative and the sum is less than the lowest |
224 | // number |
225 | || ((lhs < 0) && (rhs < 0) |
226 | && (lhs + rhs < (-((int64_t)MAX_SUM_PER_THREAD >> 1)) - 1)) |
227 | // otherwise the sum cannot overflow iff lhs does not overflow |
228 | // because |sum| < |lhs| |
229 | ; |
230 | } |
231 | |
232 | private: |
233 | combiner_type _combiner; |
234 | sampler_type* _sampler; |
235 | std::string _debug_name; |
236 | }; |
237 | |
238 | inline IntRecorder& IntRecorder::operator<<(int64_t sample) { |
239 | if (BAIDU_UNLIKELY((int64_t)(int)sample != sample)) { |
240 | const char* reason = NULL; |
241 | if (sample > std::numeric_limits<int>::max()) { |
242 | reason = "overflows" ; |
243 | sample = std::numeric_limits<int>::max(); |
244 | } else { |
245 | reason = "underflows" ; |
246 | sample = std::numeric_limits<int>::min(); |
247 | } |
248 | // Truncate to be max or min of int. We're using 44 bits to store the |
249 | // sum thus following aggregations are not likely to be over/underflow. |
250 | if (!name().empty()) { |
251 | LOG(WARNING) << "Input=" << sample << " to `" << name() |
252 | << "\' " << reason; |
253 | } else if (!_debug_name.empty()) { |
254 | LOG(WARNING) << "Input=" << sample << " to `" << _debug_name |
255 | << "\' " << reason; |
256 | } else { |
257 | LOG(WARNING) << "Input=" << sample << " to IntRecorder(" |
258 | << (void*)this << ") " << reason; |
259 | } |
260 | } |
261 | agent_type* agent = _combiner.get_or_create_tls_agent(); |
262 | if (BAIDU_UNLIKELY(!agent)) { |
263 | LOG(FATAL) << "Fail to create agent" ; |
264 | return *this; |
265 | } |
266 | uint64_t n; |
267 | agent->element.load(&n); |
268 | const uint64_t complement = _get_complement(sample); |
269 | uint64_t num; |
270 | uint64_t sum; |
271 | do { |
272 | num = _get_num(n); |
273 | sum = _get_sum(n); |
274 | if (BAIDU_UNLIKELY((num + 1 > MAX_NUM_PER_THREAD) || |
275 | _will_overflow(_extend_sign_bit(sum), sample))) { |
276 | // Although agent->element might have been cleared at this |
277 | // point, it is just OK because the very value is 0 in |
278 | // this case |
279 | agent->combiner->commit_and_clear(agent); |
280 | sum = 0; |
281 | num = 0; |
282 | n = 0; |
283 | } |
284 | } while (!agent->element.compare_exchange_weak( |
285 | n, _compress(num + 1, sum + complement))); |
286 | return *this; |
287 | } |
288 | |
289 | } // namespace bvar |
290 | |
291 | #endif //BVAR_RECORDER_H |
292 | |