chore: supports obtaining physical/virtual CPU cores

This commit is contained in:
kailixu 2023-08-30 15:21:03 +08:00
parent d65bf47a36
commit eb7d2d495a
4 changed files with 19 additions and 17 deletions

View File

@ -39,7 +39,7 @@ int64_t taosGetOsUptime();
int32_t taosGetEmail(char *email, int32_t maxLen); int32_t taosGetEmail(char *email, int32_t maxLen);
int32_t taosGetOsReleaseName(char *releaseName, char* sName, char* ver, 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 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); void taosGetCpuUsage(double *cpu_system, double *cpu_engine);
int32_t taosGetCpuInstructions(char* sse42, char* avx, char* avx2, char* fma); int32_t taosGetCpuInstructions(char* sse42, char* avx, char* avx2, char* fma);
int32_t taosGetTotalMemory(int64_t *totalKB); int32_t taosGetTotalMemory(int64_t *totalKB);

View File

@ -56,7 +56,7 @@ void *dmSetMgmtHandle(SArray *pArray, tmsg_t msgType, void *nodeMsgFp, bool need
void dmGetMonitorSystemInfo(SMonSysInfo *pInfo) { void dmGetMonitorSystemInfo(SMonSysInfo *pInfo) {
taosGetCpuUsage(&pInfo->cpu_system, &pInfo->cpu_engine); taosGetCpuUsage(&pInfo->cpu_system, &pInfo->cpu_engine);
taosGetCpuCores(&pInfo->cpu_cores); taosGetCpuCores(&pInfo->cpu_cores, false);
taosGetProcMemory(&pInfo->mem_engine); taosGetProcMemory(&pInfo->mem_engine);
taosGetSysMemory(&pInfo->mem_system); taosGetSysMemory(&pInfo->mem_system);
pInfo->mem_total = tsTotalMemoryKB; pInfo->mem_total = tsTotalMemoryKB;

View File

@ -121,7 +121,8 @@ static int32_t udfSpawnUdfd(SUdfdData *pData) {
snprintf(dnodeIdEnvItem, 32, "%s=%d", "DNODE_ID", pData->dnodeId); snprintf(dnodeIdEnvItem, 32, "%s=%d", "DNODE_ID", pData->dnodeId);
float numCpuCores = 4; float numCpuCores = 4;
taosGetCpuCores(&numCpuCores); taosGetCpuCores(&numCpuCores, false);
numCpuCores = TMAX(numCpuCores, 2);
snprintf(thrdPoolSizeEnvItem, 32, "%s=%d", "UV_THREADPOOL_SIZE", (int)numCpuCores * 2); snprintf(thrdPoolSizeEnvItem, 32, "%s=%d", "UV_THREADPOOL_SIZE", (int)numCpuCores * 2);
char pathTaosdLdLib[512] = {0}; char pathTaosdLdLib[512] = {0};

View File

@ -236,7 +236,7 @@ bool taosCheckSystemIsLittleEnd() {
void taosGetSystemInfo() { void taosGetSystemInfo() {
#ifdef WINDOWS #ifdef WINDOWS
taosGetCpuCores(&tsNumOfCores); taosGetCpuCores(&tsNumOfCores, false);
taosGetTotalMemory(&tsTotalMemoryKB); taosGetTotalMemory(&tsTotalMemoryKB);
taosGetCpuUsage(NULL, NULL); taosGetCpuUsage(NULL, NULL);
#elif defined(_TD_DARWIN_64) #elif defined(_TD_DARWIN_64)
@ -247,7 +247,7 @@ void taosGetSystemInfo() {
tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN);
#else #else
taosGetProcIOnfos(); taosGetProcIOnfos();
taosGetCpuCores(&tsNumOfCores); taosGetCpuCores(&tsNumOfCores, false);
taosGetTotalMemory(&tsTotalMemoryKB); taosGetTotalMemory(&tsTotalMemoryKB);
taosGetCpuUsage(NULL, NULL); taosGetCpuUsage(NULL, NULL);
taosGetCpuInstructions(&tsSSE42Enable, &tsAVXEnable, &tsAVX2Enable, &tsFMAEnable); taosGetCpuInstructions(&tsSSE42Enable, &tsAVXEnable, &tsAVX2Enable, &tsFMAEnable);
@ -495,6 +495,7 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
#endif #endif
} }
// Returns the container's CPU quota if successful, otherwise returns the physical CPU cores
static int32_t taosCntrGetCpuCores(float *numOfCores) { static int32_t taosCntrGetCpuCores(float *numOfCores) {
#ifdef WINDOWS #ifdef WINDOWS
return -1; return -1;
@ -503,26 +504,26 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
#else #else
TdFilePtr pFile = NULL; TdFilePtr pFile = NULL;
if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) { if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) {
goto _sys; goto _physical;
} }
char qline[32] = {0}; char qline[32] = {0};
if (taosGetsFile(pFile, sizeof(qline), qline) < 0) { if (taosGetsFile(pFile, sizeof(qline), qline) < 0) {
taosCloseFile(&pFile); taosCloseFile(&pFile);
goto _sys; goto _physical;
} }
taosCloseFile(&pFile); taosCloseFile(&pFile);
float quota = taosStr2Float(qline, NULL); float quota = taosStr2Float(qline, NULL);
if (quota < 0) { if (quota < 0) {
goto _sys; goto _physical;
} }
if (!(pFile = taosOpenFile(tsCpuPeroidFile, TD_FILE_READ | TD_FILE_STREAM))) { if (!(pFile = taosOpenFile(tsCpuPeroidFile, TD_FILE_READ | TD_FILE_STREAM))) {
goto _sys; goto _physical;
} }
char pline[32] = {0}; char pline[32] = {0};
if (taosGetsFile(pFile, sizeof(pline), pline) < 0) { if (taosGetsFile(pFile, sizeof(pline), pline) < 0) {
taosCloseFile(&pFile); taosCloseFile(&pFile);
goto _sys; goto _physical;
} }
taosCloseFile(&pFile); taosCloseFile(&pFile);
@ -535,14 +536,14 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
*numOfCores = sysCores; *numOfCores = sysCores;
} }
goto _end; goto _end;
_sys: _physical:
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN); *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
_end: _end:
return 0; return 0;
#endif #endif
} }
int32_t taosGetCpuCores(float *numOfCores) { int32_t taosGetCpuCores(float *numOfCores, bool physical) {
#ifdef WINDOWS #ifdef WINDOWS
SYSTEM_INFO info; SYSTEM_INFO info;
GetSystemInfo(&info); GetSystemInfo(&info);
@ -552,11 +553,11 @@ int32_t taosGetCpuCores(float *numOfCores) {
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN); *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
return 0; return 0;
#else #else
#if 1 if (physical) {
taosCntrGetCpuCores(numOfCores);
#else
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN); *numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
#endif } else {
taosCntrGetCpuCores(numOfCores);
}
return 0; return 0;
#endif #endif
} }