diff options
author | Vlad Yaroslavlev <vladon@vladon.com> | 2022-02-10 16:46:25 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:25 +0300 |
commit | 344ea37b4a345701ab0e67de2266a1c1bd7baf2d (patch) | |
tree | 1a2c5ffcf89eb53ecd79dbc9bc0a195c27404d0c /util/datetime/uptime.cpp | |
parent | 706b83ed7de5a473436620367af31fc0ceecde07 (diff) | |
download | ydb-344ea37b4a345701ab0e67de2266a1c1bd7baf2d.tar.gz |
Restoring authorship annotation for Vlad Yaroslavlev <vladon@vladon.com>. Commit 2 of 2.
Diffstat (limited to 'util/datetime/uptime.cpp')
-rw-r--r-- | util/datetime/uptime.cpp | 104 |
1 files changed, 52 insertions, 52 deletions
diff --git a/util/datetime/uptime.cpp b/util/datetime/uptime.cpp index 322a229000..12476a94bf 100644 --- a/util/datetime/uptime.cpp +++ b/util/datetime/uptime.cpp @@ -1,56 +1,56 @@ -#include "uptime.h" - -#if defined(_win_) +#include "uptime.h" + +#if defined(_win_) #include <util/system/winint.h> -#elif defined(_linux_) +#elif defined(_linux_) #include <util/stream/file.h> #include <util/string/cast.h> -#elif defined(_darwin_) +#elif defined(_darwin_) #include <sys/sysctl.h> -#endif - -#if defined(_darwin_) -namespace { - TInstant GetBootTime() { - struct timeval timeSinceBoot; - size_t len = sizeof(timeSinceBoot); - int request[2] = {CTL_KERN, KERN_BOOTTIME}; - if (sysctl(request, 2, &timeSinceBoot, &len, nullptr, 0) < 0) { - ythrow yexception() << "cannot get kern.boottime from sysctl"; - } - return TInstant::MicroSeconds(timeSinceBoot.tv_sec * 1'000'000 + timeSinceBoot.tv_usec); - } - - TDuration GetDarwinUptime() { - TInstant beforeNow; - TInstant afterNow; - TInstant now; - - // avoid race when NTP changes machine time between getting Now() and uptime - afterNow = GetBootTime(); - do { - beforeNow = afterNow; - now = Now(); - afterNow = GetBootTime(); - } while (afterNow != beforeNow); - - return now - beforeNow; - } -} -#endif // _darwin_ - -TDuration Uptime() { -#if defined(_win_) - return TDuration::MilliSeconds(GetTickCount64()); -#elif defined(_linux_) - TUnbufferedFileInput in("/proc/uptime"); - TString uptimeStr = in.ReadLine(); - double up, idle; - if (sscanf(uptimeStr.data(), "%lf %lf", &up, &idle) < 2) { - ythrow yexception() << "cannot read values from /proc/uptime"; - } - return TDuration::MilliSeconds(up * 1000.0); -#elif defined(_darwin_) - return GetDarwinUptime(); -#endif -} +#endif + +#if defined(_darwin_) +namespace { + TInstant GetBootTime() { + struct timeval timeSinceBoot; + size_t len = sizeof(timeSinceBoot); + int request[2] = {CTL_KERN, KERN_BOOTTIME}; + if (sysctl(request, 2, &timeSinceBoot, &len, nullptr, 0) < 0) { + ythrow yexception() << "cannot get kern.boottime from sysctl"; + } + return TInstant::MicroSeconds(timeSinceBoot.tv_sec * 1'000'000 + timeSinceBoot.tv_usec); + } + + TDuration GetDarwinUptime() { + TInstant beforeNow; + TInstant afterNow; + TInstant now; + + // avoid race when NTP changes machine time between getting Now() and uptime + afterNow = GetBootTime(); + do { + beforeNow = afterNow; + now = Now(); + afterNow = GetBootTime(); + } while (afterNow != beforeNow); + + return now - beforeNow; + } +} +#endif // _darwin_ + +TDuration Uptime() { +#if defined(_win_) + return TDuration::MilliSeconds(GetTickCount64()); +#elif defined(_linux_) + TUnbufferedFileInput in("/proc/uptime"); + TString uptimeStr = in.ReadLine(); + double up, idle; + if (sscanf(uptimeStr.data(), "%lf %lf", &up, &idle) < 2) { + ythrow yexception() << "cannot read values from /proc/uptime"; + } + return TDuration::MilliSeconds(up * 1000.0); +#elif defined(_darwin_) + return GetDarwinUptime(); +#endif +} |