Coverage Report

Created: 2025-08-05 06:45

/src/brpc/src/bvar/collector.cpp
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: Mon Dec 14 19:12:30 CST 2015
19
20
#include <map>
21
#include <gflags/gflags.h>
22
#include "butil/memory/singleton_on_pthread_once.h"
23
#include "butil/threading/platform_thread.h"
24
#include "bvar/bvar.h"
25
#include "bvar/collector.h"
26
27
namespace bvar {
28
29
// TODO: Do we need to expose this flag? Dumping thread may dump different
30
// kind of samples, users are unlikely to make good decisions on this value.
31
DEFINE_int32(bvar_collector_max_pending_samples, 1000,
32
             "Destroy unprocessed samples when they're too many");
33
34
DEFINE_int32(bvar_collector_expected_per_second, 1000,
35
             "Expected number of samples to be collected per second");
36
37
// CAUTION: Don't change this value unless you know exactly what it means.
38
static const int64_t COLLECTOR_GRAB_INTERVAL_US = 100000L; // 100ms
39
40
BAIDU_CASSERT(!(COLLECTOR_SAMPLING_BASE & (COLLECTOR_SAMPLING_BASE - 1)),
41
              must_be_power_of_2);
42
43
// Combine two circular linked list into one.
44
struct CombineCollected {
45
0
    void operator()(Collected* & s1, Collected* s2) const {
46
0
        if (s2 == NULL) {
47
0
            return;
48
0
        }
49
0
        if (s1 == NULL) {
50
0
            s1 = s2;
51
0
            return;
52
0
        }
53
0
        s1->InsertBeforeAsList(s2);
54
0
    }
55
};
56
57
// A thread and a special bvar to collect samples submitted.
58
class Collector : public bvar::Reducer<Collected*, CombineCollected> {
59
public:
60
    Collector();
61
    ~Collector();
62
63
0
    int64_t last_active_cpuwide_us() const { return _last_active_cpuwide_us; }
64
65
    void wakeup_grab_thread();
66
67
private:
68
    // The thread for collecting TLS submissions.
69
    void grab_thread();
70
71
    // The thread for calling user's callbacks.
72
    void dump_thread();
73
74
    // Adjust speed_limit if grab_thread collected too many in one round.
75
    void update_speed_limit(CollectorSpeedLimit* speed_limit,
76
                            size_t* last_ngrab, size_t cur_ngrab,
77
                            int64_t interval_us);
78
79
0
    static void* run_grab_thread(void* arg) {
80
0
        butil::PlatformThread::SetName("bvar_collector_grabber");
81
0
        static_cast<Collector*>(arg)->grab_thread();
82
0
        return NULL;
83
0
    }
84
85
0
    static void* run_dump_thread(void* arg) {
86
0
        butil::PlatformThread::SetName("bvar_collector_dumper");
87
0
        static_cast<Collector*>(arg)->dump_thread();
88
0
        return NULL;
89
0
    }
90
91
0
    static int64_t get_pending_count(void* arg) {
92
0
        Collector* d = static_cast<Collector*>(arg);
93
0
        return d->_ngrab - d->_ndump - d->_ndrop;
94
0
    }
95
    
96
private:
97
    // periodically modified by grab_thread, accessed by every submit.
98
    // Make sure that this cacheline does not include frequently modified field.
99
    int64_t _last_active_cpuwide_us;
100
    
101
    bool _created;      // Mark validness of _grab_thread.
102
    bool _stop;         // Set to true in dtor.
103
    pthread_t _grab_thread;     // For joining.
104
    pthread_t _dump_thread;
105
    int64_t _ngrab BAIDU_CACHELINE_ALIGNMENT;
106
    int64_t _ndrop;
107
    int64_t _ndump;
108
    pthread_mutex_t _dump_thread_mutex;
109
    pthread_cond_t _dump_thread_cond;
110
    butil::LinkNode<Collected> _dump_root;
111
    pthread_mutex_t _sleep_mutex;
112
    pthread_cond_t _sleep_cond;
113
};
114
115
Collector::Collector()
116
0
    : _last_active_cpuwide_us(butil::cpuwide_time_us())
117
0
    , _created(false)
118
0
    , _stop(false)
119
0
    , _grab_thread(0)
120
0
    , _dump_thread(0)
121
0
    , _ngrab(0)
122
0
    , _ndrop(0)
123
0
    , _ndump(0) {
124
0
    pthread_mutex_init(&_dump_thread_mutex, NULL);
125
0
    pthread_cond_init(&_dump_thread_cond, NULL);
126
0
    pthread_mutex_init(&_sleep_mutex, NULL);
127
0
    pthread_cond_init(&_sleep_cond, NULL);
128
0
    int rc = pthread_create(&_grab_thread, NULL, run_grab_thread, this);
129
0
    if (rc != 0) {
130
0
        LOG(ERROR) << "Fail to create Collector, " << berror(rc);
131
0
    } else {
132
0
        _created = true;
133
0
    }
134
0
}
135
136
0
Collector::~Collector() {
137
0
    if (_created) {
138
0
        _stop = true;
139
0
        pthread_join(_grab_thread, NULL);
140
0
        _created = false;
141
0
    }
142
0
    pthread_mutex_destroy(&_dump_thread_mutex);
143
0
    pthread_cond_destroy(&_dump_thread_cond);
144
0
    pthread_mutex_destroy(&_sleep_mutex);
145
0
    pthread_cond_destroy(&_sleep_cond);
146
0
}
147
148
template <typename T>
149
0
static T deref_value(void* arg) {
150
0
    return *(T*)arg;
151
0
}
Unexecuted instantiation: collector.cpp:double bvar::deref_value<double>(void*)
Unexecuted instantiation: collector.cpp:long bvar::deref_value<long>(void*)
152
153
// for limiting samples returning NULL in speed_limit()
154
static CollectorSpeedLimit g_null_speed_limit = BVAR_COLLECTOR_SPEED_LIMIT_INITIALIZER;
155
156
0
void Collector::grab_thread() {
157
0
    _last_active_cpuwide_us = butil::cpuwide_time_us();
158
0
    int64_t last_before_update_sl = _last_active_cpuwide_us;
159
160
    // This is the thread for collecting TLS submissions. User's callbacks are
161
    // called inside the separate _dump_thread to prevent a slow callback
162
    // (caused by busy disk generally) from blocking collecting code too long
163
    // that pending requests may explode memory.
164
0
    CHECK_EQ(0, pthread_create(&_dump_thread, NULL, run_dump_thread, this));
165
166
    // vars
167
0
    bvar::PassiveStatus<int64_t> pending_sampled_data(
168
0
        "bvar_collector_pending_samples", get_pending_count, this);
169
0
    double busy_seconds = 0;
170
0
    bvar::PassiveStatus<double> busy_seconds_var(deref_value<double>, &busy_seconds);
171
0
    bvar::PerSecond<bvar::PassiveStatus<double> > busy_seconds_second(
172
0
        "bvar_collector_grab_thread_usage", &busy_seconds_var);
173
174
0
    bvar::PassiveStatus<int64_t> ngrab_var(deref_value<int64_t>, &_ngrab);
175
0
    bvar::PerSecond<bvar::PassiveStatus<int64_t> > ngrab_second(
176
0
        "bvar_collector_grab_second", &ngrab_var);
177
178
    // Maps for calculating speed limit.
179
0
    typedef std::map<CollectorSpeedLimit*, size_t> GrapMap;
180
0
    GrapMap last_ngrab_map;
181
0
    GrapMap ngrab_map;
182
    // Map for group samples by preprocessors.
183
0
    typedef std::map<CollectorPreprocessor*, std::vector<Collected*> >
184
0
        PreprocessorMap;
185
0
    PreprocessorMap prep_map;
186
187
    // The main loop.
188
0
    while (!_stop) {
189
0
        const int64_t abstime = _last_active_cpuwide_us + COLLECTOR_GRAB_INTERVAL_US;
190
191
        // Clear and reuse vectors in prep_map, don't clear prep_map directly.
192
0
        for (PreprocessorMap::iterator it = prep_map.begin(); it != prep_map.end();
193
0
             ++it) {
194
0
            it->second.clear();
195
0
        }
196
197
        // Collect TLS submissions and give them to dump_thread.
198
0
        butil::LinkNode<Collected>* head = this->reset();
199
0
        if (head) {
200
0
            butil::LinkNode<Collected> tmp_root;
201
0
            head->InsertBeforeAsList(&tmp_root);
202
0
            head = NULL;
203
            
204
            // Group samples by preprocessors.
205
0
            for (butil::LinkNode<Collected>* p = tmp_root.next(); p != &tmp_root;) {
206
0
                butil::LinkNode<Collected>* saved_next = p->next();
207
0
                p->RemoveFromList();
208
0
                CollectorPreprocessor* prep = p->value()->preprocessor();
209
0
                prep_map[prep].push_back(p->value());
210
0
                p = saved_next;
211
0
            }
212
            // Iterate prep_map
213
0
            butil::LinkNode<Collected> root;
214
0
            for (PreprocessorMap::iterator it = prep_map.begin();
215
0
                 it != prep_map.end(); ++it) {
216
0
                std::vector<Collected*> & list = it->second;
217
0
                if (it->second.empty()) {
218
                    // don't call preprocessor when there's no samples.
219
0
                    continue;
220
0
                }
221
0
                if (it->first != NULL) {
222
0
                    it->first->process(list);
223
0
                }
224
0
                for (size_t i = 0; i < list.size(); ++i) {
225
0
                    Collected* p = list[i];
226
0
                    CollectorSpeedLimit* speed_limit = p->speed_limit();
227
0
                    if (speed_limit == NULL) {
228
0
                        ++ngrab_map[&g_null_speed_limit];
229
0
                    } else {
230
                        // Add up the samples of certain type.
231
0
                        ++ngrab_map[speed_limit];
232
0
                    }
233
                    // Drop samples if dump_thread is too busy.
234
                    // FIXME: equal probabilities to drop.
235
0
                    ++_ngrab;
236
0
                    if (_ngrab >= _ndrop + _ndump +
237
0
                        FLAGS_bvar_collector_max_pending_samples) {
238
0
                        ++_ndrop;
239
0
                        p->destroy();
240
0
                    } else {
241
0
                        p->InsertBefore(&root);
242
0
                    }
243
0
                }
244
0
            }
245
            // Give the samples to dump_thread
246
0
            if (root.next() != &root) {  // non empty
247
0
                butil::LinkNode<Collected>* head2 = root.next();
248
0
                root.RemoveFromList();
249
0
                BAIDU_SCOPED_LOCK(_dump_thread_mutex);
250
0
                head2->InsertBeforeAsList(&_dump_root);
251
0
                pthread_cond_signal(&_dump_thread_cond);
252
0
            }
253
0
        }
254
0
        int64_t now = butil::cpuwide_time_us();
255
0
        int64_t interval = now - last_before_update_sl;
256
0
        last_before_update_sl = now;
257
0
        for (GrapMap::iterator it = ngrab_map.begin();
258
0
             it != ngrab_map.end(); ++it) {
259
0
            update_speed_limit(it->first, &last_ngrab_map[it->first],
260
0
                               it->second, interval);
261
0
        }
262
        
263
0
        now = butil::cpuwide_time_us();
264
        // calcuate thread usage.
265
0
        busy_seconds += (now - _last_active_cpuwide_us) / 1000000.0;
266
0
        _last_active_cpuwide_us = now;
267
268
        // sleep for the next round.
269
0
        if (!_stop && abstime > now) {
270
0
            timespec abstimespec = butil::microseconds_from_now(abstime - now);
271
0
            pthread_mutex_lock(&_sleep_mutex);
272
0
            pthread_cond_timedwait(&_sleep_cond, &_sleep_mutex, &abstimespec);
273
0
            pthread_mutex_unlock(&_sleep_mutex);
274
0
        }
275
0
        _last_active_cpuwide_us = butil::cpuwide_time_us();
276
0
    }
277
    // make sure _stop is true, we may have other reasons to quit above loop
278
0
    {
279
0
        BAIDU_SCOPED_LOCK(_dump_thread_mutex);
280
0
        _stop = true; 
281
0
        pthread_cond_signal(&_dump_thread_cond);
282
0
    }
283
0
    CHECK_EQ(0, pthread_join(_dump_thread, NULL));
284
0
}
285
286
0
void Collector::wakeup_grab_thread() {
287
0
    pthread_mutex_lock(&_sleep_mutex);
288
0
    pthread_cond_signal(&_sleep_cond);
289
0
    pthread_mutex_unlock(&_sleep_mutex);
290
0
}
291
292
// Adjust speed_limit to match collected samples per second
293
void Collector::update_speed_limit(CollectorSpeedLimit* sl,
294
                                   size_t* last_ngrab, size_t cur_ngrab,
295
0
                                   int64_t interval_us) {
296
    // FIXME: May become too large at startup.
297
0
    const size_t round_ngrab = cur_ngrab - *last_ngrab;
298
0
    if (round_ngrab == 0) {
299
0
        return;
300
0
    }
301
0
    *last_ngrab = cur_ngrab;
302
0
    if (interval_us < 0) {
303
0
        interval_us = 0;
304
0
    }
305
0
    size_t new_sampling_range = 0;
306
0
    const size_t old_sampling_range = sl->sampling_range;
307
0
    if (!sl->ever_grabbed) {
308
0
        if (sl->first_sample_real_us) {
309
0
            interval_us = butil::gettimeofday_us() - sl->first_sample_real_us;
310
0
            if (interval_us < 0) {
311
0
                interval_us = 0;
312
0
            }
313
0
        } else {
314
            // Rare. the timestamp is still not set or visible yet. Just
315
            // use the default interval which may make the calculated
316
            // sampling_range larger.
317
0
        }
318
0
        new_sampling_range = FLAGS_bvar_collector_expected_per_second
319
0
            * interval_us * COLLECTOR_SAMPLING_BASE / (1000000L * round_ngrab);
320
0
    } else {
321
        // NOTE: the multiplications are unlikely to overflow.
322
0
        new_sampling_range = FLAGS_bvar_collector_expected_per_second
323
0
            * interval_us * old_sampling_range / (1000000L * round_ngrab);
324
        // Don't grow or shrink too fast.
325
0
        if (interval_us < 1000000L) {
326
0
            new_sampling_range =
327
0
                (new_sampling_range * interval_us +
328
0
                 old_sampling_range * (1000000L - interval_us)) / 1000000L;
329
0
        }
330
0
    }
331
    // Make sure new value is sane.
332
0
    if (new_sampling_range == 0) {
333
0
        new_sampling_range = 1;
334
0
    } else if (new_sampling_range > COLLECTOR_SAMPLING_BASE) {
335
0
        new_sampling_range = COLLECTOR_SAMPLING_BASE;
336
0
    }
337
338
    // NOTE: don't update unmodified fields in sl to avoid meaningless
339
    // flushing of the cacheline. 
340
0
    if (new_sampling_range != old_sampling_range) {
341
0
        sl->sampling_range = new_sampling_range;
342
0
    }
343
0
    if (!sl->ever_grabbed) {
344
0
        sl->ever_grabbed = true;
345
0
    }
346
0
}
347
348
0
size_t is_collectable_before_first_time_grabbed(CollectorSpeedLimit* sl) {
349
0
    if (!sl->ever_grabbed) {
350
0
        int before_add = sl->count_before_grabbed.fetch_add(
351
0
            1, butil::memory_order_relaxed);
352
0
        if (before_add == 0) {
353
0
            sl->first_sample_real_us = butil::gettimeofday_us();
354
0
        } else if (before_add >= FLAGS_bvar_collector_expected_per_second) {
355
0
            butil::get_leaky_singleton<Collector>()->wakeup_grab_thread();
356
0
        }
357
0
    }
358
0
    return sl->sampling_range;
359
0
}
360
361
// Call user's callbacks in this thread.
362
0
void Collector::dump_thread() {
363
0
    int64_t last_ns = butil::cpuwide_time_ns();
364
365
    // vars
366
0
    double busy_seconds = 0;
367
0
    bvar::PassiveStatus<double> busy_seconds_var(deref_value<double>, &busy_seconds);
368
0
    bvar::PerSecond<bvar::PassiveStatus<double> > busy_seconds_second(
369
0
        "bvar_collector_dump_thread_usage", &busy_seconds_var);
370
371
0
    bvar::PassiveStatus<int64_t> ndumped_var(deref_value<int64_t>, &_ndump);
372
0
    bvar::PerSecond<bvar::PassiveStatus<int64_t> > ndumped_second(
373
0
        "bvar::collector_dump_second", &ndumped_var);
374
375
0
    butil::LinkNode<Collected> root;
376
0
    size_t round = 0;
377
378
    // The main loop
379
0
    while (!_stop) {
380
0
        ++round;
381
        // Get new samples set by grab_thread.
382
0
        butil::LinkNode<Collected>* newhead = NULL;
383
0
        {
384
0
            BAIDU_SCOPED_LOCK(_dump_thread_mutex);
385
0
            while (!_stop && _dump_root.next() == &_dump_root) {
386
0
                const int64_t now_ns = butil::cpuwide_time_ns();
387
0
                busy_seconds += (now_ns - last_ns) / 1000000000.0;
388
0
                pthread_cond_wait(&_dump_thread_cond, &_dump_thread_mutex);
389
0
                last_ns = butil::cpuwide_time_ns();
390
0
            }
391
0
            if (_stop) {
392
0
                break;
393
0
            }
394
0
            newhead = _dump_root.next();
395
0
            _dump_root.RemoveFromList();
396
0
        }
397
0
        CHECK(newhead != &_dump_root);
398
0
        newhead->InsertBeforeAsList(&root);
399
400
        // Call callbacks.
401
0
        for (butil::LinkNode<Collected>* p = root.next(); !_stop && p != &root;) {
402
            // We remove p from the list, save next first.
403
0
            butil::LinkNode<Collected>* saved_next = p->next();
404
0
            p->RemoveFromList();
405
0
            Collected* s = p->value();
406
0
            s->dump_and_destroy(round);
407
0
            ++_ndump;
408
0
            p = saved_next;
409
0
        }
410
0
    }
411
0
}
412
413
0
void Collected::submit(int64_t cpuwide_us) {
414
0
    Collector* d = butil::get_leaky_singleton<Collector>();
415
    // Destroy the sample in-place if the grab_thread did not run for twice
416
    // of the normal interval. This also applies to the situation that
417
    // grab_thread aborts due to severe errors.
418
    // Collector::_last_active_cpuwide_us is periodically modified by grab_thread,
419
    // cache bouncing is tolerable.
420
0
    if (cpuwide_us < d->last_active_cpuwide_us() + COLLECTOR_GRAB_INTERVAL_US * 2) {
421
0
        *d << this;
422
0
    } else {
423
0
        destroy();
424
0
    }
425
0
}
426
427
0
static double get_sampling_ratio(void* arg) {
428
0
    return ((const CollectorSpeedLimit*)arg)->sampling_range /
429
0
        (double)COLLECTOR_SAMPLING_BASE;
430
0
}
431
432
DisplaySamplingRatio::DisplaySamplingRatio(const char* name,
433
                                           const CollectorSpeedLimit* sl)
434
0
    : _var(name, get_sampling_ratio, (void*)sl) {
435
0
}
436
437
}  // namespace bvar