From 66f0a69752d3910dc29a3b925acc2cd3a72e6c76 Mon Sep 17 00:00:00 2001 From: kailixu Date: Tue, 29 Aug 2023 14:31:47 +0800 Subject: [PATCH 1/4] feat: get cpu cores from quota --- source/os/src/osSysinfo.c | 53 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 5f73251e3b..053fa25fa3 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -124,6 +124,8 @@ static char tsSysCpuFile[] = "/proc/stat"; static char tsProcCpuFile[25] = {0}; static char tsProcMemFile[25] = {0}; static char tsProcIOFile[25] = {0}; +static char tsCpuCfsPeroid[] = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"; +static char tsCpuCfsQuota[] = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"; static void taosGetProcIOnfos() { tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024; @@ -493,6 +495,53 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { #endif } +static int32_t taosCntrGetCpuCores(float *numOfCores) { +#ifdef WINDOWS + return -1; +#elif defined(_TD_DARWIN_64) + return -1; +#else + TdFilePtr pFile = NULL; + if (!(pFile = taosOpenFile(tsCpuCfsQuota, TD_FILE_READ | TD_FILE_STREAM))) { + goto _sys; + } + char qline[32] = {0}; + if (taosGetsFile(pFile, sizeof(qline), qline) < 0) { + taosCloseFile(&pFile); + goto _sys; + } + taosCloseFile(&pFile); + float quota = taosStr2Float(qline, NULL); + if (quota < 0) { + goto _sys; + } + + if (!(pFile = taosOpenFile(tsCpuCfsPeroid, TD_FILE_READ | TD_FILE_STREAM))) { + goto _sys; + } + char pline[32] = {0}; + if (taosGetsFile(pFile, sizeof(pline), pline) < 0) { + taosCloseFile(&pFile); + goto _sys; + } + taosCloseFile(&pFile); + + float peroid = taosStr2Float(pline, NULL); + float quotaCores = quota / peroid; + float sysCores = sysconf(_SC_NPROCESSORS_ONLN); + if (quotaCores < sysCores && quotaCores > 0) { + *numOfCores = quotaCores; + } else { + *numOfCores = sysCores; + } + goto _end; +_sys: + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); +_end: + return 0; +#endif +} + int32_t taosGetCpuCores(float *numOfCores) { #ifdef WINDOWS SYSTEM_INFO info; @@ -502,8 +551,12 @@ int32_t taosGetCpuCores(float *numOfCores) { #elif defined(_TD_DARWIN_64) *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); return 0; +#else +#if 1 + taosCntrGetCpuCores(numOfCores); #else *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); +#endif return 0; #endif } From f09429f08925e3fc0a54881b17bdc91e1d630f5e Mon Sep 17 00:00:00 2001 From: kailixu Date: Tue, 29 Aug 2023 14:37:22 +0800 Subject: [PATCH 2/4] feat: get cpu cores from quota --- source/os/src/osSysinfo.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 053fa25fa3..7a6897ed6d 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -121,11 +121,11 @@ LONG WINAPI exceptionHandler(LPEXCEPTION_POINTERS exception); static pid_t tsProcId; static char tsSysNetFile[] = "/proc/net/dev"; static char tsSysCpuFile[] = "/proc/stat"; +static char tsCpuPeroidFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"; +static char tsCpuQuotaFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"; static char tsProcCpuFile[25] = {0}; static char tsProcMemFile[25] = {0}; static char tsProcIOFile[25] = {0}; -static char tsCpuCfsPeroid[] = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"; -static char tsCpuCfsQuota[] = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"; static void taosGetProcIOnfos() { tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024; @@ -502,7 +502,7 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) { return -1; #else TdFilePtr pFile = NULL; - if (!(pFile = taosOpenFile(tsCpuCfsQuota, TD_FILE_READ | TD_FILE_STREAM))) { + if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) { goto _sys; } char qline[32] = {0}; @@ -516,7 +516,7 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) { goto _sys; } - if (!(pFile = taosOpenFile(tsCpuCfsPeroid, TD_FILE_READ | TD_FILE_STREAM))) { + if (!(pFile = taosOpenFile(tsCpuPeroidFile, TD_FILE_READ | TD_FILE_STREAM))) { goto _sys; } char pline[32] = {0}; From eb7d2d495ad63f2064995fee89c44dfc3a48e8f2 Mon Sep 17 00:00:00 2001 From: kailixu Date: Wed, 30 Aug 2023 15:21:03 +0800 Subject: [PATCH 3/4] chore: supports obtaining physical/virtual CPU cores --- include/os/osSysinfo.h | 2 +- source/dnode/mgmt/node_util/src/dmUtil.c | 2 +- source/libs/function/src/tudf.c | 3 ++- source/os/src/osSysinfo.c | 29 ++++++++++++------------ 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index a6a3655a55..29b6f07dca 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -39,7 +39,7 @@ 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); -int32_t taosGetCpuCores(float *numOfCores); +int32_t taosGetCpuCores(float *numOfCores, bool physical); void taosGetCpuUsage(double *cpu_system, double *cpu_engine); int32_t taosGetCpuInstructions(char* sse42, char* avx, char* avx2, char* fma); int32_t taosGetTotalMemory(int64_t *totalKB); diff --git a/source/dnode/mgmt/node_util/src/dmUtil.c b/source/dnode/mgmt/node_util/src/dmUtil.c index 648a9ab9ce..bab7d068f3 100644 --- a/source/dnode/mgmt/node_util/src/dmUtil.c +++ b/source/dnode/mgmt/node_util/src/dmUtil.c @@ -56,7 +56,7 @@ void *dmSetMgmtHandle(SArray *pArray, tmsg_t msgType, void *nodeMsgFp, bool need void dmGetMonitorSystemInfo(SMonSysInfo *pInfo) { taosGetCpuUsage(&pInfo->cpu_system, &pInfo->cpu_engine); - taosGetCpuCores(&pInfo->cpu_cores); + taosGetCpuCores(&pInfo->cpu_cores, false); taosGetProcMemory(&pInfo->mem_engine); taosGetSysMemory(&pInfo->mem_system); pInfo->mem_total = tsTotalMemoryKB; diff --git a/source/libs/function/src/tudf.c b/source/libs/function/src/tudf.c index 5b9f44c812..7e344866a5 100644 --- a/source/libs/function/src/tudf.c +++ b/source/libs/function/src/tudf.c @@ -121,7 +121,8 @@ static int32_t udfSpawnUdfd(SUdfdData *pData) { snprintf(dnodeIdEnvItem, 32, "%s=%d", "DNODE_ID", pData->dnodeId); float numCpuCores = 4; - taosGetCpuCores(&numCpuCores); + taosGetCpuCores(&numCpuCores, false); + numCpuCores = TMAX(numCpuCores, 2); snprintf(thrdPoolSizeEnvItem, 32, "%s=%d", "UV_THREADPOOL_SIZE", (int)numCpuCores * 2); char pathTaosdLdLib[512] = {0}; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 7a6897ed6d..89b1916f23 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -236,7 +236,7 @@ bool taosCheckSystemIsLittleEnd() { void taosGetSystemInfo() { #ifdef WINDOWS - taosGetCpuCores(&tsNumOfCores); + taosGetCpuCores(&tsNumOfCores, false); taosGetTotalMemory(&tsTotalMemoryKB); taosGetCpuUsage(NULL, NULL); #elif defined(_TD_DARWIN_64) @@ -247,7 +247,7 @@ void taosGetSystemInfo() { tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); #else taosGetProcIOnfos(); - taosGetCpuCores(&tsNumOfCores); + taosGetCpuCores(&tsNumOfCores, false); taosGetTotalMemory(&tsTotalMemoryKB); taosGetCpuUsage(NULL, NULL); taosGetCpuInstructions(&tsSSE42Enable, &tsAVXEnable, &tsAVX2Enable, &tsFMAEnable); @@ -495,6 +495,7 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { #endif } +// Returns the container's CPU quota if successful, otherwise returns the physical CPU cores static int32_t taosCntrGetCpuCores(float *numOfCores) { #ifdef WINDOWS return -1; @@ -503,26 +504,26 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) { #else TdFilePtr pFile = NULL; if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) { - goto _sys; + goto _physical; } char qline[32] = {0}; if (taosGetsFile(pFile, sizeof(qline), qline) < 0) { taosCloseFile(&pFile); - goto _sys; + goto _physical; } taosCloseFile(&pFile); float quota = taosStr2Float(qline, NULL); if (quota < 0) { - goto _sys; + goto _physical; } if (!(pFile = taosOpenFile(tsCpuPeroidFile, TD_FILE_READ | TD_FILE_STREAM))) { - goto _sys; + goto _physical; } char pline[32] = {0}; if (taosGetsFile(pFile, sizeof(pline), pline) < 0) { taosCloseFile(&pFile); - goto _sys; + goto _physical; } taosCloseFile(&pFile); @@ -535,14 +536,14 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) { *numOfCores = sysCores; } goto _end; -_sys: +_physical: *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); _end: return 0; #endif } -int32_t taosGetCpuCores(float *numOfCores) { +int32_t taosGetCpuCores(float *numOfCores, bool physical) { #ifdef WINDOWS SYSTEM_INFO info; GetSystemInfo(&info); @@ -552,11 +553,11 @@ int32_t taosGetCpuCores(float *numOfCores) { *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); return 0; #else -#if 1 - taosCntrGetCpuCores(numOfCores); -#else - *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); -#endif + if (physical) { + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); + } else { + taosCntrGetCpuCores(numOfCores); + } return 0; #endif } From d5b0d39c401f425da1a07617d9575535a627e044 Mon Sep 17 00:00:00 2001 From: kailixu Date: Thu, 31 Aug 2023 08:59:09 +0800 Subject: [PATCH 4/4] fix: typo when get cpu cores --- source/os/src/osSysinfo.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 89b1916f23..562328a198 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -121,7 +121,7 @@ LONG WINAPI exceptionHandler(LPEXCEPTION_POINTERS exception); static pid_t tsProcId; static char tsSysNetFile[] = "/proc/net/dev"; static char tsSysCpuFile[] = "/proc/stat"; -static char tsCpuPeroidFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"; +static char tsCpuPeriodFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_period_us"; static char tsCpuQuotaFile[] = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us"; static char tsProcCpuFile[25] = {0}; static char tsProcMemFile[25] = {0}; @@ -504,31 +504,31 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) { #else TdFilePtr pFile = NULL; if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) { - goto _physical; + goto _sys; } char qline[32] = {0}; if (taosGetsFile(pFile, sizeof(qline), qline) < 0) { taosCloseFile(&pFile); - goto _physical; + goto _sys; } taosCloseFile(&pFile); float quota = taosStr2Float(qline, NULL); if (quota < 0) { - goto _physical; + goto _sys; } - if (!(pFile = taosOpenFile(tsCpuPeroidFile, TD_FILE_READ | TD_FILE_STREAM))) { - goto _physical; + if (!(pFile = taosOpenFile(tsCpuPeriodFile, TD_FILE_READ | TD_FILE_STREAM))) { + goto _sys; } char pline[32] = {0}; if (taosGetsFile(pFile, sizeof(pline), pline) < 0) { taosCloseFile(&pFile); - goto _physical; + goto _sys; } taosCloseFile(&pFile); - float peroid = taosStr2Float(pline, NULL); - float quotaCores = quota / peroid; + float period = taosStr2Float(pline, NULL); + float quotaCores = quota / period; float sysCores = sysconf(_SC_NPROCESSORS_ONLN); if (quotaCores < sysCores && quotaCores > 0) { *numOfCores = quotaCores; @@ -536,7 +536,7 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) { *numOfCores = sysCores; } goto _end; -_physical: +_sys: *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); _end: return 0;