Coverage Report

Created: 2024-09-08 06:43

/src/brpc/src/bvar/recorder.h
Line
Count
Source (jump to first uncovered line)
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
0
    Stat() : sum(0), num(0) {}
35
0
    Stat(int64_t sum2, int64_t num2) : sum(sum2), num(num2) {}
36
    int64_t sum;
37
    int64_t num;
38
        
39
0
    int64_t get_average_int() const {
40
        //num can be changed by sampling thread, use tmp_num
41
0
        int64_t tmp_num = num;
42
0
        if (tmp_num == 0) {
43
0
            return 0;
44
0
        }
45
0
        return sum / (int64_t)tmp_num;
46
0
    }
47
0
    double get_average_double() const {
48
0
        int64_t tmp_num = num;
49
0
        if (tmp_num == 0) {
50
0
            return 0.0;
51
0
        }
52
0
        return (double)sum / (double)tmp_num;
53
0
    }
54
0
    Stat operator-(const Stat& rhs) const {
55
0
        return Stat(sum - rhs.sum, num - rhs.num);
56
0
    }
57
0
    void operator-=(const Stat& rhs) {
58
0
        sum -= rhs.sum;
59
0
        num -= rhs.num;
60
0
    }
61
0
    Stat operator+(const Stat& rhs) const {
62
0
        return Stat(sum + rhs.sum, num + rhs.num);
63
0
    }
64
0
    void operator+=(const Stat& rhs) {
65
0
        sum += rhs.sum;
66
0
        num += rhs.num;
67
0
    }
68
};
69
70
0
inline std::ostream& operator<<(std::ostream& os, const Stat& s) {
71
0
    const int64_t v = s.get_average_int();
72
0
    if (v != 0) {
73
0
        return os << v;
74
0
    } else {
75
0
        return os << s.get_average_double();
76
0
    }
77
0
}
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
0
        void operator()(Stat& s1, const Stat& s2) const { s1 += s2; }
97
    };
98
    struct MinusStat {
99
0
        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
0
        void operator()(Stat& lhs, uint64_t rhs) const {
110
0
            lhs.sum += _extend_sign_bit(_get_sum(rhs));
111
0
            lhs.num += _get_num(rhs);
112
0
        }
113
    };
114
    
115
    typedef detail::AgentCombiner<Stat, uint64_t, AddToStat> combiner_type;
116
    typedef combiner_type::Agent agent_type;
117
118
0
    IntRecorder() : _sampler(NULL) {}
119
120
0
    explicit IntRecorder(const butil::StringPiece& name) : _sampler(NULL) {
121
0
        expose(name);
122
0
    }
123
124
    IntRecorder(const butil::StringPiece& prefix, const butil::StringPiece& name)
125
0
        : _sampler(NULL) {
126
0
        expose_as(prefix, name);
127
0
    }
128
129
0
    ~IntRecorder() {
130
0
        hide();
131
0
        if (_sampler) {
132
0
            _sampler->destroy();
133
0
            _sampler = NULL;
134
0
        }
135
0
    }
136
137
    // Note: The input type is acutally int. Use int64_t to check overflow.
138
    IntRecorder& operator<<(int64_t/*note*/ sample);
139
140
0
    int64_t average() const {
141
0
        return _combiner.combine_agents().get_average_int();
142
0
    }
143
144
0
    double average(double) const {
145
0
        return _combiner.combine_agents().get_average_double();
146
0
    }
147
148
0
    Stat get_value() const {
149
0
        return _combiner.combine_agents();
150
0
    }
151
    
152
0
    Stat reset() {
153
0
        return _combiner.reset_all_agents();
154
0
    }
155
156
0
    AddStat op() const { return AddStat(); }
157
0
    MinusStat inv_op() const { return MinusStat(); }
158
    
159
0
    void describe(std::ostream& os, bool /*quote_string*/) const override {
160
0
        os << get_value();
161
0
    }
162
163
0
    bool valid() const { return _combiner.valid(); }
164
    
165
0
    sampler_type* get_sampler() {
166
0
        if (NULL == _sampler) {
167
0
            _sampler = new sampler_type(this);
168
0
            _sampler->schedule();
169
0
        }
170
0
        return _sampler;
171
0
    }
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
0
    void set_debug_name(const butil::StringPiece& name) {
176
0
        _debug_name.assign(name.data(), name.size());
177
0
    }
178
    
179
private:
180
    // TODO: The following numeric functions should be independent utils
181
0
    static uint64_t _get_sum(const uint64_t n) {
182
0
        return (n & MAX_SUM_PER_THREAD);
183
0
    }
184
185
0
    static uint64_t _get_num(const uint64_t n) {
186
0
        return n >> SUM_BIT_WIDTH;
187
0
    }
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
0
    static int64_t _extend_sign_bit(const uint64_t sum) {
194
0
        return (((1ul << (64ul - SUM_BIT_WIDTH + 1)) - 1) 
195
0
               * ((1ul << (SUM_BIT_WIDTH - 1) & sum)))
196
0
               | (int64_t)sum;
197
0
    }
198
199
    // Convert complement into a |SUM_BIT_WIDTH|-bit unsigned integer
200
0
    static uint64_t _get_complement(int64_t n) {
201
0
        return n & (MAX_SUM_PER_THREAD);
202
0
    }
203
204
0
    static uint64_t _compress(const uint64_t num, const uint64_t sum) {
205
0
        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
0
               | (sum & MAX_SUM_PER_THREAD)
210
0
               ;
211
0
    }
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
0
    static bool _will_overflow(const int64_t lhs, const int rhs) {
218
0
        return 
219
            // Both integers are positive and the sum is larger than the largest
220
            // number
221
0
            ((lhs > 0) && (rhs > 0) 
222
0
                && (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
0
            || ((lhs < 0) && (rhs < 0) 
226
0
                    && (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
0
            ;
230
0
    }
231
232
private:
233
    combiner_type           _combiner;
234
    sampler_type*           _sampler;
235
    std::string             _debug_name;
236
};
237
238
0
inline IntRecorder& IntRecorder::operator<<(int64_t sample) {
239
0
    if (BAIDU_UNLIKELY((int64_t)(int)sample != sample)) {
240
0
        const char* reason = NULL;
241
0
        if (sample > std::numeric_limits<int>::max()) {
242
0
            reason = "overflows";
243
0
            sample = std::numeric_limits<int>::max();
244
0
        } else {
245
0
            reason = "underflows";
246
0
            sample = std::numeric_limits<int>::min();
247
0
        }
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
0
        if (!name().empty()) {
251
0
            LOG(WARNING) << "Input=" << sample << " to `" << name()
252
0
                       << "\' " << reason;
253
0
        } else if (!_debug_name.empty()) {
254
0
            LOG(WARNING) << "Input=" << sample << " to `" << _debug_name
255
0
                       << "\' " << reason;
256
0
        } else {
257
0
            LOG(WARNING) << "Input=" << sample << " to IntRecorder("
258
0
                       << (void*)this << ") " << reason;
259
0
        }
260
0
    }
261
0
    agent_type* agent = _combiner.get_or_create_tls_agent();
262
0
    if (BAIDU_UNLIKELY(!agent)) {
263
0
        LOG(FATAL) << "Fail to create agent";
264
0
        return *this;
265
0
    }
266
0
    uint64_t n;
267
0
    agent->element.load(&n);
268
0
    const uint64_t complement = _get_complement(sample);
269
0
    uint64_t num;
270
0
    uint64_t sum;
271
0
    do {
272
0
        num = _get_num(n);
273
0
        sum = _get_sum(n);
274
0
        if (BAIDU_UNLIKELY((num + 1 > MAX_NUM_PER_THREAD) ||
275
0
                           _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
0
            agent->combiner->commit_and_clear(agent);
280
0
            sum = 0;
281
0
            num = 0;
282
0
            n = 0;
283
0
        }
284
0
    } while (!agent->element.compare_exchange_weak(
285
0
                 n, _compress(num + 1, sum + complement)));
286
0
    return *this;
287
0
}
288
289
}  // namespace bvar
290
291
#endif  //BVAR_RECORDER_H