1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) 2014 Baidu, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "info_thread.h"
namespace brpc {
InfoThread::InfoThread()
: _stop(false)
, _tid(0) {
pthread_mutex_init(&_mutex, NULL);
pthread_cond_init(&_cond, NULL);
}
InfoThread::~InfoThread() {
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_cond);
}
void InfoThread::run() {
int64_t i = 0;
int64_t last_sent_count = 0;
int64_t last_succ_count = 0;
int64_t last_error_count = 0;
int64_t start_time = butil::gettimeofday_us();
while (!_stop) {
int64_t end_time = 0;
while (!_stop &&
(end_time = butil::gettimeofday_us()) < start_time + 1000000L) {
BAIDU_SCOPED_LOCK(_mutex);
if (!_stop) {
timespec ts = butil::microseconds_to_timespec(end_time);
pthread_cond_timedwait(&_cond, &_mutex, &ts);
}
}
start_time = butil::gettimeofday_us();
char buf[64];
const time_t tm_s = start_time / 1000000L;
struct tm lt;
strftime(buf, sizeof(buf), "%Y/%m/%d-%H:%M:%S",
localtime_r(&tm_s, <));
const int64_t cur_sent_count = _options.sent_count->get_value();
const int64_t cur_succ_count = _options.latency_recorder->count();
const int64_t cur_error_count = _options.error_count->get_value();
printf("%s\tsent:%-10dsuccess:%-10derror:%-6dtotal_error:%-10lldtotal_sent:%-10lld\n",
buf,
(int)(cur_sent_count - last_sent_count),
(int)(cur_succ_count - last_succ_count),
(int)(cur_error_count - last_error_count),
(long long)cur_error_count,
(long long)cur_sent_count);
last_sent_count = cur_sent_count;
last_succ_count = cur_succ_count;
last_error_count = cur_error_count;
if (_stop || ++i % 10 == 0) {
printf("[Latency]\n"
" avg %10lld us\n"
" 50%% %10lld us\n"
" 70%% %10lld us\n"
" 90%% %10lld us\n"
" 95%% %10lld us\n"
" 97%% %10lld us\n"
" 99%% %10lld us\n"
" 99.9%% %10lld us\n"
" 99.99%% %10lld us\n"
" max %10lld us\n",
(long long)_options.latency_recorder->latency(),
(long long)_options.latency_recorder->latency_percentile(0.5),
(long long)_options.latency_recorder->latency_percentile(0.7),
(long long)_options.latency_recorder->latency_percentile(0.9),
(long long)_options.latency_recorder->latency_percentile(0.95),
(long long)_options.latency_recorder->latency_percentile(0.97),
(long long)_options.latency_recorder->latency_percentile(0.99),
(long long)_options.latency_recorder->latency_percentile(0.999),
(long long)_options.latency_recorder->latency_percentile(0.9999),
(long long)_options.latency_recorder->max_latency());
}
}
}
static void* run_info_thread(void* arg) {
((InfoThread*)arg)->run();
return NULL;
}
bool InfoThread::start(const InfoThreadOptions& options) {
if (options.latency_recorder == NULL ||
options.error_count == NULL ||
options.sent_count == NULL) {
LOG(ERROR) << "Some required options are NULL";
return false;
}
_options = options;
_stop = false;
if (pthread_create(&_tid, NULL, run_info_thread, this) != 0) {
LOG(ERROR) << "Fail to create info_thread";
return false;
}
return true;
}
void InfoThread::stop() {
{
BAIDU_SCOPED_LOCK(_mutex);
if (_stop) {
return;
}
_stop = true;
pthread_cond_signal(&_cond);
}
pthread_join(_tid, NULL);
}
} // namespace brpc