From 6d1a5649f5a224b3547bbbd56f9efe3794b5e5c5 Mon Sep 17 00:00:00 2001 From: yangce Date: Wed, 7 Jun 2017 15:26:39 +0800 Subject: [PATCH] issue=#196 use CLOCK_MONOTONIC in clock_gettime --- src/sofa/pbrpc/ptime.h | 11 +++++++++-- src/sofa/pbrpc/rpc_client_impl.cc | 4 ++-- src/sofa/pbrpc/rpc_controller_impl.h | 4 ++-- src/sofa/pbrpc/rpc_request.cc | 4 ++-- src/sofa/pbrpc/rpc_server_impl.cc | 7 ++++--- src/sofa/pbrpc/rpc_server_impl.h | 1 + src/sofa/pbrpc/rpc_server_message_stream.h | 2 +- src/sofa/pbrpc/rpc_timeout_manager.h | 2 +- src/sofa/pbrpc/timeout_manager_impl.cc | 2 +- src/sofa/pbrpc/timer_worker.h | 2 +- 10 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/sofa/pbrpc/ptime.h b/src/sofa/pbrpc/ptime.h index 5999ddc..133b152 100644 --- a/src/sofa/pbrpc/ptime.h +++ b/src/sofa/pbrpc/ptime.h @@ -18,11 +18,18 @@ typedef boost::posix_time::ptime PTime; typedef boost::posix_time::time_duration TimeDuration; inline TimeDuration time_duration_microseconds(int64_t); -inline PTime ptime_now() +inline PTime ptime_now(bool use_real_time) { #ifdef __linux__ struct timespec ts = { 0, 0 }; - clock_gettime(CLOCK_MONOTONIC, &ts); + if (use_real_time) + { + clock_gettime(CLOCK_REALTIME, &ts); + } + else + { + clock_gettime(CLOCK_MONOTONIC, &ts); + } time_t microsec = ts.tv_sec * 1000000 + ts.tv_nsec / 1000; #else struct timeval tv = { 0, 0 }; diff --git a/src/sofa/pbrpc/rpc_client_impl.cc b/src/sofa/pbrpc/rpc_client_impl.cc index 9612246..9f67206 100644 --- a/src/sofa/pbrpc/rpc_client_impl.cc +++ b/src/sofa/pbrpc/rpc_client_impl.cc @@ -17,7 +17,7 @@ RpcClientImpl::RpcClientImpl(const RpcClientOptions& options) : _options(options) , _is_running(false) , _next_request_id(0) - , _epoch_time(ptime_now()) + , _epoch_time(ptime_now(false)) , _ticks_per_second(time_duration_seconds(1).ticks()) , _last_maintain_ticks(0) , _last_print_connection_ticks(0) @@ -378,7 +378,7 @@ RpcClientStreamPtr RpcClientImpl::FindOrCreateStream(const RpcEndpoint& remote_e stream.reset(new RpcClientStream(_work_thread_group->io_service(), remote_endpoint)); stream->set_flow_controller(_flow_controller); stream->set_max_pending_buffer_size(_max_pending_buffer_size); - stream->reset_ticks((ptime_now() - _epoch_time).ticks(), true); + stream->reset_ticks((ptime_now(false) - _epoch_time).ticks(), true); stream->set_connect_timeout(_options.connect_timeout); stream->set_closed_stream_callback( boost::bind(&RpcClientImpl::OnClosed, shared_from_this(), _1)); diff --git a/src/sofa/pbrpc/rpc_controller_impl.h b/src/sofa/pbrpc/rpc_controller_impl.h index 36e9799..2dd22c9 100644 --- a/src/sofa/pbrpc/rpc_controller_impl.h +++ b/src/sofa/pbrpc/rpc_controller_impl.h @@ -290,7 +290,7 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_thisimpl(); RpcServerStreamPtr stream = cntl->RpcServerStream().lock(); @@ -76,7 +76,7 @@ void RpcRequest::OnCallMethodDone( google::protobuf::Message* request, google::protobuf::Message* response) { - PTime time_now = ptime_now(); + PTime time_now = ptime_now(false); const RpcControllerImplPtr& cntl = controller->impl(); cntl->SetFinishProcessTime(time_now); int64 process_time_us = diff --git a/src/sofa/pbrpc/rpc_server_impl.cc b/src/sofa/pbrpc/rpc_server_impl.cc index 604706f..5e49311 100644 --- a/src/sofa/pbrpc/rpc_server_impl.cc +++ b/src/sofa/pbrpc/rpc_server_impl.cc @@ -21,7 +21,8 @@ RpcServerImpl::RpcServerImpl(const RpcServerOptions& options, : _options(options) , _event_handler(handler) , _is_running(false) - , _start_time(ptime_now()) + , _start_time(ptime_now(false)) + , _real_start_time(ptime_now(true)) , _ticks_per_second(time_duration_seconds(1).ticks()) , _last_maintain_ticks(0) , _last_restart_listen_ticks(0) @@ -204,7 +205,7 @@ void RpcServerImpl::Stop() PTime RpcServerImpl::GetStartTime() { - return _start_time; + return _real_start_time; } RpcServerOptions RpcServerImpl::GetOptions() @@ -403,7 +404,7 @@ void RpcServerImpl::OnAccepted(const RpcServerStreamPtr& stream) } stream->set_max_pending_buffer_size(_max_pending_buffer_size); - stream->reset_ticks((ptime_now() - _start_time).ticks(), true); + stream->reset_ticks((ptime_now(false) - _start_time).ticks(), true); ScopedLocker _(_stream_set_lock); _stream_set.insert(stream); diff --git a/src/sofa/pbrpc/rpc_server_impl.h b/src/sofa/pbrpc/rpc_server_impl.h index 006d76b..46fa56d 100644 --- a/src/sofa/pbrpc/rpc_server_impl.h +++ b/src/sofa/pbrpc/rpc_server_impl.h @@ -100,6 +100,7 @@ class RpcServerImpl : public sofa::pbrpc::enable_shared_from_this MutexLock _start_stop_lock; PTime _start_time; + PTime _real_start_time; int64 _ticks_per_second; int64 _last_maintain_ticks; int64 _last_restart_listen_ticks; diff --git a/src/sofa/pbrpc/rpc_server_message_stream.h b/src/sofa/pbrpc/rpc_server_message_stream.h index 798c2e5..68b5efb 100644 --- a/src/sofa/pbrpc/rpc_server_message_stream.h +++ b/src/sofa/pbrpc/rpc_server_message_stream.h @@ -637,7 +637,7 @@ class RpcServerMessageStream : public RpcByteStream RpcRequestPtr request = _current_rpc_request_parser->GetRequest(); request->SetLocalEndpoint(_local_endpoint); request->SetRemoteEndpoint(_remote_endpoint); - request->SetReceivedTime(ptime_now()); + request->SetReceivedTime(ptime_now(false)); received_messages->push_back(request); reset_receiving_env(); } diff --git a/src/sofa/pbrpc/rpc_timeout_manager.h b/src/sofa/pbrpc/rpc_timeout_manager.h index ca9209b..9cdeacc 100644 --- a/src/sofa/pbrpc/rpc_timeout_manager.h +++ b/src/sofa/pbrpc/rpc_timeout_manager.h @@ -27,7 +27,7 @@ class RpcTimeoutManager : public sofa::pbrpc::enable_shared_from_this { if (_is_running) { - PTime now = ptime_now(); + PTime now = ptime_now(false); if (ec != boost::asio::error::operation_aborted && _work_routine) {