feat: get cpu cores from quota

This commit is contained in:
kailixu 2023-08-29 14:31:47 +08:00
parent ff6de8ebe2
commit 66f0a69752
1 changed files with 53 additions and 0 deletions

View File

@ -124,6 +124,8 @@ static char tsSysCpuFile[] = "/proc/stat";
static char tsProcCpuFile[25] = {0}; static char tsProcCpuFile[25] = {0};
static char tsProcMemFile[25] = {0}; static char tsProcMemFile[25] = {0};
static char tsProcIOFile[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() { static void taosGetProcIOnfos() {
tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024; tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024;
@ -493,6 +495,53 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
#endif #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) { int32_t taosGetCpuCores(float *numOfCores) {
#ifdef WINDOWS #ifdef WINDOWS
SYSTEM_INFO info; SYSTEM_INFO info;
@ -502,8 +551,12 @@ int32_t taosGetCpuCores(float *numOfCores) {
#elif defined(_TD_DARWIN_64) #elif defined(_TD_DARWIN_64)
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN); *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
return 0; return 0;
#else
#if 1
taosCntrGetCpuCores(numOfCores);
#else #else
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN); *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
#endif
return 0; return 0;
#endif #endif
} }