From 3cfa49066ac061c7bbc978afada8c35f91133ee9 Mon Sep 17 00:00:00 2001 From: kailixu Date: Sun, 23 Jul 2023 23:25:58 +0800 Subject: [PATCH] chore: uptime logic optimize --- include/common/tglobal.h | 1 + include/os/os.h | 1 + include/os/osSysinfo.h | 5 +++++ source/dnode/mgmt/exe/dmMain.c | 1 + source/dnode/mgmt/mgmt_dnode/src/dmWorker.c | 18 +++++++++++++++--- source/os/src/osSysinfo.c | 14 ++++++++++++++ 6 files changed, 37 insertions(+), 3 deletions(-) diff --git a/include/common/tglobal.h b/include/common/tglobal.h index b0a1aa6117..4f2ed2b065 100644 --- a/include/common/tglobal.h +++ b/include/common/tglobal.h @@ -90,6 +90,7 @@ extern bool tsMndSkipGrant; // dnode extern int64_t tsDndStart; +extern int64_t tsDndStartOsUptime; extern int64_t tsDndUpTime; // monitor diff --git a/include/os/os.h b/include/os/os.h index 309a977ff6..31f546e032 100644 --- a/include/os/os.h +++ b/include/os/os.h @@ -41,6 +41,7 @@ extern "C" { #include #include #include +#include #include #include #include diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index b5309178ae..a8110ee06a 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -18,6 +18,10 @@ #include "os.h" +// #include /* for _syscallX macros/related stuff */ +// #include /* for struct sysinfo */ + + #ifdef __cplusplus extern "C" { #endif @@ -35,6 +39,7 @@ typedef struct { bool taosCheckSystemIsLittleEnd(); void taosGetSystemInfo(); +int64_t taosGetOsUptime(); int32_t taosGetEmail(char *email, int32_t maxLen); int32_t taosGetOsReleaseName(char *releaseName, char* sName, char* ver, int32_t maxLen); int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores); diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index 242dd8a706..3c08714218 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -374,6 +374,7 @@ int mainWindows(int argc, char **argv) { dInfo("start to init service"); dmSetSignalHandle(); tsDndStart = taosGetTimestampMs(); + tsDndStartOsUptime = taosGetOsUptime(); int32_t code = dmRun(); dInfo("shutting down the service"); diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c b/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c index 55fdd0f46a..5854450fc7 100644 --- a/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c +++ b/source/dnode/mgmt/mgmt_dnode/src/dmWorker.c @@ -24,6 +24,10 @@ static void *dmStatusThreadFp(void *param) { const static int16_t TRIM_FREQ = 30; int32_t trimCount = 0; + int32_t upTimeCount = 0; + int64_t upTime = 0; + int64_t thrdTime = 0; + while (1) { taosMsleep(200); if (pMgmt->pData->dropped || pMgmt->pData->stopped) break; @@ -39,10 +43,18 @@ static void *dmStatusThreadFp(void *param) { if (trimCount == 0) { taosMemoryTrim(0); } - cost = taosGetTimestampMs() - curTime; + + if ((upTimeCount = (++upTimeCount & 7)) == 0) { + upTime = (taosGetOsUptime() - tsDndStartOsUptime) * 1000; + } } - tsDndUpTime += 200; - if (cost > 0) tsDndUpTime += cost; // TODO: use /proc/uptime to replace the upTime calculation for linux + + thrdTime += 200; + cost = taosGetTimestampMs() - curTime; + if (cost > 0) thrdTime += cost; + tsDndUpTime = upTime > thrdTime ? upTime : thrdTime; + printf("upTime:%" PRIi64 " thrdTime:%" PRIi64 " tsDndUpTime:%" PRIi64 " delta:%" PRIi64 "\n", upTime, thrdTime, + tsDndUpTime, upTime - thrdTime); } return NULL; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 6f87f6b75b..1f631c7388 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -961,6 +961,20 @@ char *taosGetCmdlineByPID(int pid) { #endif } +int64_t taosGetOsUptime() { +#ifdef WINDOWS + return 0; +// #else +// #elif defined(_TD_DARWIN_64) +// return 0; +#else + struct sysinfo info; + if (0 == sysinfo(&info)) { + return info.uptime; + }; +#endif +} + void taosSetCoreDump(bool enable) { if (!enable) return; #ifdef WINDOWS -- GitLab