Skip to content

Commit c97a737

Browse files
committed
Use monotonic time instead of wall time
1 parent f614c81 commit c97a737

34 files changed

Lines changed: 154 additions & 155 deletions

example/auto_concurrency_limiter/client.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,13 @@ struct TestCaseContext {
119119
, stage_index(0)
120120
, test_case(tc)
121121
, next_stage_sec(test_case.qps_stage_list(0).duration_sec() +
122-
butil::gettimeofday_s()) {
122+
butil::cpuwide_time_s()) {
123123
DisplayStage(test_case.qps_stage_list(stage_index));
124124
Update();
125125
}
126126

127127
bool Update() {
128-
if (butil::gettimeofday_s() >= next_stage_sec) {
128+
if (butil::cpuwide_time_s() >= next_stage_sec) {
129129
++stage_index;
130130
if (stage_index < test_case.qps_stage_list_size()) {
131131
next_stage_sec += test_case.qps_stage_list(stage_index).duration_sec();
@@ -144,7 +144,7 @@ struct TestCaseContext {
144144
} else if (qps_stage.type() == test::SMOOTH) {
145145
qps = lower_bound + (upper_bound - lower_bound) /
146146
double(qps_stage.duration_sec()) * (qps_stage.duration_sec() - next_stage_sec
147-
+ butil::gettimeofday_s());
147+
+ butil::cpuwide_time_s());
148148
}
149149
interval_us.store(1.0 / qps * 1000000, butil::memory_order_relaxed);
150150
return true;

example/auto_concurrency_limiter/server.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class EchoServiceImpl : public test::EchoService {
9696
void SetTestCase(const test::TestCase& test_case) {
9797
_test_case = test_case;
9898
_next_stage_start = _test_case.latency_stage_list(0).duration_sec() +
99-
butil::gettimeofday_s();
99+
butil::cpuwide_time_s();
100100
_stage_index = 0;
101101
_running_case = false;
102102
DisplayStage(_test_case.latency_stage_list(_stage_index));
@@ -137,7 +137,7 @@ class EchoServiceImpl : public test::EchoService {
137137

138138
void ComputeLatency() {
139139
if (_stage_index < _test_case.latency_stage_list_size() &&
140-
butil::gettimeofday_s() > _next_stage_start) {
140+
butil::cpuwide_time_s() > _next_stage_start) {
141141
++_stage_index;
142142
if (_stage_index < _test_case.latency_stage_list_size()) {
143143
_next_stage_start += _test_case.latency_stage_list(_stage_index).duration_sec();
@@ -167,7 +167,7 @@ class EchoServiceImpl : public test::EchoService {
167167
int latency = lower_bound + (upper_bound - lower_bound) /
168168
double(latency_stage.duration_sec()) *
169169
(latency_stage.duration_sec() - _next_stage_start +
170-
butil::gettimeofday_s());
170+
butil::cpuwide_time_s());
171171
_latency.store(latency, butil::memory_order_relaxed);
172172
} else {
173173
LOG(FATAL) << "Wrong Type:" << latency_stage.type();

example/dynamic_partition_echo_c++/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class EchoServiceImpl : public example::EchoService {
6969
}
7070
}
7171
if (FLAGS_spin) {
72-
int64_t end_time = butil::gettimeofday_us() + (int64_t)delay;
73-
while (butil::gettimeofday_us() < end_time) {}
72+
int64_t end_time = butil::cpuwide_time_us() + (int64_t)delay;
73+
while (butil::cpuwide_time_us() < end_time) {}
7474
} else {
7575
bthread_usleep((int64_t)delay);
7676
}

example/multi_threaded_echo_fns_c++/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ class EchoServiceImpl : public example::EchoService {
7070
}
7171
}
7272
if (FLAGS_spin) {
73-
int64_t end_time = butil::gettimeofday_us() + (int64_t)delay;
74-
while (butil::gettimeofday_us() < end_time) {}
73+
int64_t end_time = butil::cpuwide_time_us() + (int64_t)delay;
74+
while (butil::cpuwide_time_us() < end_time) {}
7575
} else {
7676
bthread_usleep((int64_t)delay);
7777
}

example/partition_echo_c++/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ class EchoServiceImpl : public example::EchoService {
6969
}
7070
}
7171
if (FLAGS_spin) {
72-
int64_t end_time = butil::gettimeofday_us() + (int64_t)delay;
73-
while (butil::gettimeofday_us() < end_time) {}
72+
int64_t end_time = butil::cpuwide_time_us() + (int64_t)delay;
73+
while (butil::cpuwide_time_us() < end_time) {}
7474
} else {
7575
bthread_usleep((int64_t)delay);
7676
}

example/rdma_performance/client.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class PerformanceTest {
176176
}
177177
--closure->test->_iterations;
178178
uint64_t last = g_last_time.load(butil::memory_order_relaxed);
179-
uint64_t now = butil::gettimeofday_us();
179+
uint64_t now = butil::cpuwide_time_us();
180180
if (now > last && now - last > 100000) {
181181
if (g_last_time.exchange(now, butil::memory_order_relaxed) == last) {
182182
g_client_cpu_recorder <<
@@ -192,7 +192,7 @@ class PerformanceTest {
192192

193193
static void* RunTest(void* arg) {
194194
PerformanceTest* test = (PerformanceTest*)arg;
195-
test->_start_time = butil::gettimeofday_us();
195+
test->_start_time = butil::cpuwide_time_us();
196196
test->_iterations = FLAGS_test_iterations;
197197

198198
for (int i = 0; i < FLAGS_queue_depth; ++i) {
@@ -235,7 +235,7 @@ void Test(int thread_num, int attachment_size) {
235235
}
236236
tests.push_back(t);
237237
}
238-
uint64_t start_time = butil::gettimeofday_us();
238+
uint64_t start_time = butil::cpuwide_time_us();
239239
bthread_t tid[thread_num];
240240
if (FLAGS_expected_qps > 0) {
241241
bthread_t tid;
@@ -250,7 +250,7 @@ void Test(int thread_num, int attachment_size) {
250250
bthread_usleep(10000);
251251
}
252252
}
253-
uint64_t end_time = butil::gettimeofday_us();
253+
uint64_t end_time = butil::cpuwide_time_us();
254254
double throughput = g_total_bytes / 1.048576 / (end_time - start_time);
255255
if (FLAGS_test_iterations == 0) {
256256
std::cout << "Avg-Latency: " << g_latency_recorder.latency(10)

example/selective_echo_c++/server.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ class EchoServiceImpl : public example::EchoService {
6868
}
6969
}
7070
if (FLAGS_spin) {
71-
int64_t end_time = butil::gettimeofday_us() + (int64_t)delay;
72-
while (butil::gettimeofday_us() < end_time) {}
71+
int64_t end_time = butil::cpuwide_time_us() + (int64_t)delay;
72+
while (butil::cpuwide_time_us() < end_time) {}
7373
} else {
7474
bthread_usleep((int64_t)delay);
7575
}

src/brpc/cluster_recover_policy.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool DefaultClusterRecoverPolicy::StopRecoverIfNecessary() {
5151
if (!_recovering) {
5252
return false;
5353
}
54-
int64_t now_ms = butil::gettimeofday_ms();
54+
int64_t now_ms = butil::cpuwide_time_ms();
5555
std::unique_lock<butil::Mutex> mu(_mutex);
5656
if (_last_usable_change_time_ms != 0 && _last_usable != 0 &&
5757
(now_ms - _last_usable_change_time_ms > _hold_seconds * 1000)) {
@@ -92,7 +92,7 @@ bool DefaultClusterRecoverPolicy::DoReject(const std::vector<ServerId>& server_l
9292
if (!_recovering) {
9393
return false;
9494
}
95-
int64_t now_ms = butil::gettimeofday_ms();
95+
int64_t now_ms = butil::cpuwide_time_ms();
9696
uint64_t usable = GetUsableServerCount(now_ms, server_list);
9797
if (_last_usable != usable) {
9898
std::unique_lock<butil::Mutex> mu(_mutex);

src/brpc/details/health_check.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void* HealthCheckManager::AppCheck(void* arg) {
9999
done->cntl.Reset();
100100
done->cntl.http_request().uri() = done->hc_option.health_check_path;
101101
ControllerPrivateAccessor(&done->cntl).set_health_check_call();
102-
done->last_check_time_ms = butil::gettimeofday_ms();
102+
done->last_check_time_ms = butil::cpuwide_time_ms();
103103
done->channel.CallMethod(NULL, &done->cntl, NULL, NULL, done);
104104
return NULL;
105105
}
@@ -126,7 +126,7 @@ void OnAppHealthCheckDone::Run() {
126126
<< ", " << cntl.ErrorText();
127127

128128
int64_t sleep_time_ms =
129-
last_check_time_ms + interval_s * 1000 - butil::gettimeofday_ms();
129+
last_check_time_ms + interval_s * 1000 - butil::cpuwide_time_ms();
130130
if (sleep_time_ms > 0) {
131131
// TODO(zhujiashun): we need to handle the case when timer fails
132132
// and bthread_usleep returns immediately. In most situations,

src/brpc/global.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,13 +238,13 @@ static void* GlobalUpdate(void*) {
238238
}
239239

240240
std::vector<SocketId> conns;
241-
const int64_t start_time_us = butil::gettimeofday_us();
241+
const int64_t start_time_us = butil::cpuwide_time_us();
242242
const int WARN_NOSLEEP_THRESHOLD = 2;
243243
int64_t last_time_us = start_time_us;
244244
int consecutive_nosleep = 0;
245245
int64_t last_return_free_memory_time = start_time_us;
246246
while (1) {
247-
const int64_t sleep_us = 1000000L + last_time_us - butil::gettimeofday_us();
247+
const int64_t sleep_us = 1000000L + last_time_us - butil::cpuwide_time_us();
248248
if (sleep_us > 0) {
249249
if (bthread_usleep(sleep_us) < 0) {
250250
PLOG_IF(FATAL, errno != ESTOP) << "Fail to sleep";
@@ -257,7 +257,7 @@ static void* GlobalUpdate(void*) {
257257
LOG(WARNING) << __FUNCTION__ << " is too busy!";
258258
}
259259
}
260-
last_time_us = butil::gettimeofday_us();
260+
last_time_us = butil::cpuwide_time_us();
261261

262262
TrackMe();
263263

0 commit comments

Comments
 (0)