Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue=#196 use CLOCK_MONOTONIC in clock_gettime #197

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/sofa/pbrpc/ptime.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_REALTIME, &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 };
Expand Down
4 changes: 2 additions & 2 deletions src/sofa/pbrpc/rpc_client_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Expand Down
4 changes: 2 additions & 2 deletions src/sofa/pbrpc/rpc_controller_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_this<RpcControl
{
int64 timeout = Timeout();
_expiration = timeout <= 0 ? ptime_infin()
: ptime_now() + time_duration_milliseconds(timeout);
: ptime_now(false) + time_duration_milliseconds(timeout);
}

const PTime& Expiration() const
Expand Down Expand Up @@ -333,7 +333,7 @@ class RpcControllerImpl : public sofa::pbrpc::enable_shared_from_this<RpcControl
_is_request_sent = true;
_local_endpoint = local_endpoint;
_sent_bytes = sent_bytes;
_request_sent_time = ptime_now();
_request_sent_time = ptime_now(false);
}


Expand Down
4 changes: 2 additions & 2 deletions src/sofa/pbrpc/rpc_request.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void RpcRequest::CallMethod(
google::protobuf::Message* request,
google::protobuf::Message* response)
{
PTime time_now = ptime_now();
PTime time_now = ptime_now(false);
const RpcControllerImplPtr& cntl = controller->impl();

RpcServerStreamPtr stream = cntl->RpcServerStream().lock();
Expand Down Expand Up @@ -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 =
Expand Down
7 changes: 4 additions & 3 deletions src/sofa/pbrpc/rpc_server_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -204,7 +205,7 @@ void RpcServerImpl::Stop()

PTime RpcServerImpl::GetStartTime()
{
return _start_time;
return _real_start_time;
}

RpcServerOptions RpcServerImpl::GetOptions()
Expand Down Expand Up @@ -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<FastLock> _(_stream_set_lock);
_stream_set.insert(stream);
Expand Down
1 change: 1 addition & 0 deletions src/sofa/pbrpc/rpc_server_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ class RpcServerImpl : public sofa::pbrpc::enable_shared_from_this<RpcServerImpl>
MutexLock _start_stop_lock;

PTime _start_time;
PTime _real_start_time;
int64 _ticks_per_second;
int64 _last_maintain_ticks;
int64 _last_restart_listen_ticks;
Expand Down
2 changes: 1 addition & 1 deletion src/sofa/pbrpc/rpc_server_message_stream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
2 changes: 1 addition & 1 deletion src/sofa/pbrpc/rpc_timeout_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class RpcTimeoutManager : public sofa::pbrpc::enable_shared_from_this<RpcTimeout
RpcTimeoutManager(IOService& io_service)
: _io_service(io_service)
, _is_running(false)
, _epoch_time(ptime_now())
, _epoch_time(ptime_now(false))
, _next_id(1u)
{}

Expand Down
2 changes: 1 addition & 1 deletion src/sofa/pbrpc/timeout_manager_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace pbrpc {

TimeoutManagerImpl::TimeoutManagerImpl()
: _is_running(false)
, _epoch_time(ptime_now())
, _epoch_time(ptime_now(false))
, _last_ticks(0)
, _rectify_ticks(time_duration_milliseconds(kTimerGranularity).ticks())
, _next_id(1)
Expand Down
2 changes: 1 addition & 1 deletion src/sofa/pbrpc/timer_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TimerWorker : public sofa::pbrpc::enable_shared_from_this<TimerWorker>
{
if (_is_running)
{
PTime now = ptime_now();
PTime now = ptime_now(false);

if (ec != boost::asio::error::operation_aborted && _work_routine)
{
Expand Down