aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorleasid <leasid@yandex-team.com>2024-05-28 13:55:11 +0300
committerleasid <leasid@yandex-team.com>2024-05-28 14:07:48 +0300
commitbbb72f5da0f3bd15c1b8316133b17c8eb221cd2b (patch)
tree73cea68d7c8ed0d8be0049abb10c567168312b36
parent8edaa11f428e96d432517e72ae918e5a647829b5 (diff)
downloadydb-bbb72f5da0f3bd15c1b8316133b17c8eb221cd2b.tar.gz
UTIL: ProcUptime - process uptime
Добавляем uptime для процесса 7dc898ce5f81666a4cd5f912f49a3035dc2c599c
-rw-r--r--util/datetime/process_uptime.cpp46
-rw-r--r--util/datetime/process_uptime.h8
-rw-r--r--util/datetime/process_uptime_ut.cpp17
-rw-r--r--util/datetime/ut/ya.make1
-rw-r--r--util/ya.make1
5 files changed, 73 insertions, 0 deletions
diff --git a/util/datetime/process_uptime.cpp b/util/datetime/process_uptime.cpp
new file mode 100644
index 0000000000..7ddf194d9f
--- /dev/null
+++ b/util/datetime/process_uptime.cpp
@@ -0,0 +1,46 @@
+#include "process_uptime.h"
+#include "systime.h"
+#include "uptime.h"
+
+#if defined(_linux_)
+ #include <util/string/builder.h>
+ #include <util/string/cast.h>
+ #include <util/stream/file.h>
+ #include <util/string/split.h>
+
+ #include <unistd.h>
+#elif defined(_win_)
+ #include <processthreadsapi.h>
+#endif
+
+TDuration ProcessUptime() {
+#if defined(_win_)
+ FILETIME createProcessTime;
+ FILETIME dummy1;
+ FILETIME dummy2;
+ FILETIME dummy3;
+ if (GetProcessTimes(GetCurrentProcess(), &createProcessTime, &dummy1, &dummy2, &dummy3)) {
+ timeval processCreationTimeval{.tv_sec = 0, .tv_usec = 0};
+ FileTimeToTimeval(&createProcessTime, &processCreationTimeval);
+ const TInstant processCreationInstant = TInstant::Seconds(processCreationTimeval.tv_sec) + TDuration::MicroSeconds(processCreationTimeval.tv_usec);
+ return TInstant::Now() - processCreationInstant;
+ }
+ ythrow TSystemError() << "Failed to obtain process starttime";
+#elif defined(_linux_)
+ static const auto statPath = "/proc/self/stat";
+ // /proc/<pid>/stat format: #21 (0-based) item - starttime %llu - The time the process started after system boot
+ TUnbufferedFileInput statFile(statPath);
+ auto statStr = statFile.ReadAll();
+ const auto completeStatsSize = 20; // First two fields skipped to ignore variations of parentheses
+ TVector<TString> stats = StringSplitter(TStringBuf{statStr}.RAfter(')').After(' ')).Split(' ').Take(completeStatsSize);
+ ui64 startTimeTicks = 0;
+ Y_THROW_UNLESS(stats.size() == completeStatsSize, "Broken format of " << statPath);
+ if (!TryFromString<ui64>(stats.back(), startTimeTicks)) {
+ ythrow yexception() << "Failed to extract process starttime value from " << statPath;
+ }
+ auto startTimeSeconds = startTimeTicks / sysconf(_SC_CLK_TCK);
+ return Uptime() - TDuration::Seconds(startTimeSeconds);
+#else
+ ythrow yexception() << "unimplemented";
+#endif
+}
diff --git a/util/datetime/process_uptime.h b/util/datetime/process_uptime.h
new file mode 100644
index 0000000000..7f279f0e33
--- /dev/null
+++ b/util/datetime/process_uptime.h
@@ -0,0 +1,8 @@
+#pragma once
+
+#include "base.h"
+
+/**
+ * Returns process uptime
+ */
+TDuration ProcessUptime();
diff --git a/util/datetime/process_uptime_ut.cpp b/util/datetime/process_uptime_ut.cpp
new file mode 100644
index 0000000000..4ef95e4323
--- /dev/null
+++ b/util/datetime/process_uptime_ut.cpp
@@ -0,0 +1,17 @@
+#include <library/cpp/testing/unittest/registar.h>
+
+#include "base.h"
+#include "process_uptime.h"
+#include "uptime.h"
+
+Y_UNIT_TEST_SUITE(TestProcessUptimeSuite) {
+ Y_UNIT_TEST(TestProcessUptime) {
+ auto t0 = Uptime();
+ auto t1 = ProcessUptime();
+ UNIT_ASSERT(t1 < TDuration::Minutes(30));
+ UNIT_ASSERT(t0 > t1);
+ Sleep(TDuration::MilliSeconds(50)); // typical uptime resolution is 10-16 ms
+ auto t2 = ProcessUptime();
+ UNIT_ASSERT(t2 >= t1);
+ }
+}
diff --git a/util/datetime/ut/ya.make b/util/datetime/ut/ya.make
index 22b9a4fd37..617b9fcddb 100644
--- a/util/datetime/ut/ya.make
+++ b/util/datetime/ut/ya.make
@@ -5,6 +5,7 @@ SRCS(
datetime/cputimer_ut.cpp
datetime/parser_deprecated_ut.cpp
datetime/parser_ut.cpp
+ datetime/process_uptime_ut.cpp
datetime/uptime_ut.cpp
)
diff --git a/util/ya.make b/util/ya.make
index 881520edef..2d9d01c8da 100644
--- a/util/ya.make
+++ b/util/ya.make
@@ -22,6 +22,7 @@ JOIN_SRCS(
datetime/base.cpp
datetime/constants.cpp
datetime/cputimer.cpp
+ datetime/process_uptime.cpp
datetime/systime.cpp
datetime/uptime.cpp
)