diff options
author | Håvard Pettersen <havardpe@oath.com> | 2019-11-18 14:29:21 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@oath.com> | 2019-11-19 12:55:35 +0000 |
commit | 0e99acfd33930fc5180ac390718f4b20f2d4d7e2 (patch) | |
tree | 4e219f48f8900a84f46b3ee075ecc49624cdd678 /fnet/src/examples | |
parent | 957a7cc70ba85568618fb2b5282d38f009c688ea (diff) |
use chrono instead of fastos time
Diffstat (limited to 'fnet/src/examples')
-rw-r--r-- | fnet/src/examples/frt/rpc/rpc_proxy.cpp | 15 | ||||
-rw-r--r-- | fnet/src/examples/timeout/timeout.cpp | 20 |
2 files changed, 22 insertions, 13 deletions
diff --git a/fnet/src/examples/frt/rpc/rpc_proxy.cpp b/fnet/src/examples/frt/rpc/rpc_proxy.cpp index 1f1a3a80f2f..a61e2d37197 100644 --- a/fnet/src/examples/frt/rpc/rpc_proxy.cpp +++ b/fnet/src/examples/frt/rpc/rpc_proxy.cpp @@ -2,6 +2,7 @@ #include <vespa/fnet/frt/frt.h> #include <vespa/fastos/app.h> +#include <chrono> #include <vespa/log/log.h> LOG_SETUP("rpc_proxy"); @@ -83,13 +84,17 @@ ReqDone::RequestDone(FRT_RPCRequest *req) const char * RPCProxy::GetPrefix(FRT_RPCRequest *req) { - FastOS_Time t; tm currTime; tm *currTimePt; - t.SetNow(); - time_t secs = t.Secs(); - currTimePt = localtime_r(&secs, &currTime); + using clock = std::chrono::system_clock; + auto now = clock::now(); + auto since = now.time_since_epoch(); + auto secs = std::chrono::duration_cast<std::chrono::seconds>(since); + auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(since - secs); + time_t my_time = clock::to_time_t(now); + + currTimePt = localtime_r(&my_time, &currTime); assert(currTimePt == &currTime); (void) currTimePt; @@ -107,7 +112,7 @@ RPCProxy::GetPrefix(FRT_RPCRequest *req) currTime.tm_hour, currTime.tm_min, currTime.tm_sec, - (int)(t.GetMicroSeconds() / 1000), + int(ms.count()), GetSession(req)->id, rid); diff --git a/fnet/src/examples/timeout/timeout.cpp b/fnet/src/examples/timeout/timeout.cpp index bba58faca55..1d6ecc11909 100644 --- a/fnet/src/examples/timeout/timeout.cpp +++ b/fnet/src/examples/timeout/timeout.cpp @@ -2,6 +2,7 @@ #include <vespa/fnet/fnet.h> #include <vespa/fastos/app.h> +#include <chrono> #include <vespa/log/log.h> LOG_SETUP("timeout"); @@ -42,8 +43,11 @@ public: int MyApp::Main() { - double ms; - FastOS_Time t; + using clock = std::chrono::steady_clock; + using ms_double = std::chrono::duration<double,std::milli>; + + ms_double ms; + clock::time_point t; FNET_PacketQueue queue; FastOS_ThreadPool pool(65000); FNET_Transport transport; @@ -57,28 +61,28 @@ MyApp::Main() FNET_Context context; fprintf(stderr, "scheduling timeout in 2 seconds...\n"); - t.SetNow(); + t = clock::now(); timeout.Schedule(2.0); // timeout in 2 seconds FastOS_Thread::Sleep(1000); timeout.Unschedule(); // cancel timeout - ms = t.MilliSecsToNow(); + ms = (clock::now() - t); if (queue.GetPacketCnt_NoLock() == 0) fprintf(stderr, "timeout canceled; no timeout packet delivered\n"); - fprintf(stderr, "time since timeout was scheduled: %f ms\n", ms); + fprintf(stderr, "time since timeout was scheduled: %f ms\n", ms.count()); fprintf(stderr, "scheduling timeout in 2 seconds...\n"); - t.SetNow(); + t = clock::now(); timeout.Schedule(2.0); // timeout in 2 seconds packet = queue.DequeuePacket(&context); // wait for timeout - ms = t.MilliSecsToNow(); + ms = (clock::now() - t); if (packet->IsTimeoutCMD()) fprintf(stderr, "got timeout packet\n"); - fprintf(stderr, "time since timeout was scheduled: %f ms\n", ms); + fprintf(stderr, "time since timeout was scheduled: %f ms\n", ms.count()); transport.ShutDown(true); pool.Close(); |