osSysinfo

This commit is contained in:
xsren 2024-09-12 19:56:49 +08:00
parent a7da6a7d51
commit 82d83b2e6b
8 changed files with 112 additions and 54 deletions

View File

@ -130,6 +130,12 @@ extern int64_t tsRandErrScope;
extern threadlocal bool tsEnableRandErr;
#define TAOS_UNUSED(expr) (void)(expr)
#define TAOS_SKIP_ERROR(expr) \
{ \
int32_t _code = terrno; \
(void)(expr); \
terrno = _code; \
}
#ifdef __cplusplus
}

View File

@ -39,15 +39,17 @@ 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);
void taosGetCpuCores(float *numOfCores, bool physical);
void taosGetCpuUsage(double *cpu_system, double *cpu_engine);
int32_t taosGetCpuCores(float *numOfCores, bool physical);
int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine);
int32_t taosGetCpuInstructions(char* sse42, char* avx, char* avx2, char* fma, char* avx512);
int32_t taosGetTotalMemory(int64_t *totalKB);
int32_t taosGetProcMemory(int64_t *usedKB);
int32_t taosGetSysMemory(int64_t *usedKB);
int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize);
void taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes);
void taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes);
int32_t taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes);
void taosSetDefaultProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes);
int32_t taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes);
void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes);
void taosKillSystem();
int32_t taosGetSystemUUID(char *uid, int32_t uidlen);

View File

@ -156,6 +156,7 @@ int32_t taosGetErrSize();
#define TSDB_CODE_OUT_OF_BUFFER TAOS_DEF_ERROR_CODE(0, 0x0137)
#define TSDB_CODE_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x0138)
#define TSDB_CODE_SOCKET_ERROR TAOS_DEF_ERROR_CODE(0, 0x0139)
#define TSDB_CODE_UNSUPPORT_OS TAOS_DEF_ERROR_CODE(0, 0x013A)
//client
#define TSDB_CODE_TSC_INVALID_OPERATION TAOS_DEF_ERROR_CODE(0, 0x0200)

View File

@ -55,14 +55,36 @@ 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, false);
(void)taosGetProcMemory(&pInfo->mem_engine);
(void)taosGetSysMemory(&pInfo->mem_system);
int32_t code = 0;
code = taosGetCpuUsage(&pInfo->cpu_system, &pInfo->cpu_engine);
if (code != 0) {
dError("failed to get cpu usage since %s", tstrerror(code));
}
code = taosGetCpuCores(&pInfo->cpu_cores, false);
if (code != 0) {
dError("failed to get cpu cores since %s", tstrerror(code));
}
code = taosGetProcMemory(&pInfo->mem_engine);
if (code != 0) {
dError("failed to get proc memory since %s", tstrerror(code));
}
code = taosGetSysMemory(&pInfo->mem_system);
if (code != 0) {
dError("failed to get sys memory since %s", tstrerror(code));
}
pInfo->mem_total = tsTotalMemoryKB;
pInfo->disk_engine = 0;
pInfo->disk_used = tsDataSpace.size.used;
pInfo->disk_total = tsDataSpace.size.total;
taosGetCardInfoDelta(&pInfo->net_in, &pInfo->net_out);
taosGetProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk);
code = taosGetCardInfoDelta(&pInfo->net_in, &pInfo->net_out);
if (code != 0) {
dError("failed to get card info since %s", tstrerror(code));
taosSetDefaultCardInfoDelta(&pInfo->net_in, &pInfo->net_out);
}
code = taosGetProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk);
if (code != 0) {
dError("failed to get proc io delta since %s", tstrerror(code));
taosSetDefaultProcIODelta(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk);
}
return;
}

View File

@ -323,8 +323,8 @@ void taosRemoveOldFiles(const char *dirname, int32_t keepDays) {
}
}
(void)taosCloseDir(&pDir);
(void)rmdir(dirname);
TAOS_UNUSED(taosCloseDir(&pDir));
TAOS_UNUSED(rmdir(dirname));
}
int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) {

View File

@ -190,10 +190,10 @@ int64_t taosCopyFile(const char *from, const char *to) {
_err:
if (pFileFrom != NULL) TAOS_UNUSED(taosCloseFile(&pFileFrom));
if (pFileTo != NULL) TAOS_UNUSED(taosCloseFile(&pFileTo));
if (pFileFrom != NULL) TAOS_SKIP_ERROR(taosCloseFile(&pFileFrom));
if (pFileTo != NULL) TAOS_SKIP_ERROR(taosCloseFile(&pFileTo));
/* coverity[+retval] */
TAOS_UNUSED(taosRemoveFile(to));
TAOS_SKIP_ERROR(taosRemoveFile(to));
terrno = code;
return -1;
@ -1485,14 +1485,14 @@ int32_t taosCompressFile(char *srcFileName, char *destFileName) {
cmp_end:
if (fd >= 0) {
(void)close(fd);
TAOS_SKIP_ERROR(close(fd));
}
if (pSrcFile) {
TAOS_UNUSED(taosCloseFile(&pSrcFile));
TAOS_SKIP_ERROR(taosCloseFile(&pSrcFile));
}
if (dstFp) {
TAOS_UNUSED(gzclose(dstFp));
TAOS_SKIP_ERROR(gzclose(dstFp));
}
taosMemoryFree(data);

View File

@ -135,9 +135,14 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) {
str++;
char *revisedCharset = taosCharsetReplace(str);
tstrncpy(outCharset, revisedCharset, TD_CHARSET_LEN);
taosMemoryFree(revisedCharset);
if (NULL == revisedCharset) {
(void)strcpy(outCharset, "UTF-8");
} else {
tstrncpy(outCharset, revisedCharset, TD_CHARSET_LEN);
taosMemoryFree(revisedCharset);
}
// printf("charset not configured, set to system default:%s", outCharset);
} else {
strcpy(outCharset, "UTF-8");

View File

@ -179,10 +179,8 @@ static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) {
char line[1024];
ssize_t bytes = taosGetsFile(pFile, sizeof(line), line);
if (bytes < 0) {
code = terrno;
(void)taosCloseFile(&pFile);
terrno = code;
return code;
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
return terrno;
}
char cpu[10] = {0};
@ -230,9 +228,7 @@ static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
char line[1024] = {0};
ssize_t bytes = taosGetsFile(pFile, sizeof(line), line);
if (bytes < 0) {
code = terrno;
(void)taosCloseFile(&pFile);
terrno = code;
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
return code;
}
@ -250,7 +246,7 @@ static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) {
}
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
#endif
return 0;
@ -427,7 +423,7 @@ int32_t taosGetOsReleaseName(char *releaseName, char* sName, char* ver, int32_t
if (++cnt >= 3) break;
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
return code;
#endif
}
@ -497,13 +493,13 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
if (strncmp(line, "processor", 9) == 0) coreCount += 1;
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
if (code != 0 && (done & 1) == 0) {
TdFilePtr pFile1 = taosOpenFile("/proc/device-tree/model", TD_FILE_READ | TD_FILE_STREAM);
if (pFile1 != NULL) {
ssize_t bytes = taosGetsFile(pFile1, maxLen, cpuModel);
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
if (bytes > 0) {
code = 0;
done |= 1;
@ -535,9 +531,9 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) {
// Returns the container's CPU quota if successful, otherwise returns the physical CPU cores
static int32_t taosCntrGetCpuCores(float *numOfCores) {
#ifdef WINDOWS
return -1;
return TSDB_CODE_UNSUPPORT_OS;
#elif defined(_TD_DARWIN_64)
return -1;
return TSDB_CODE_UNSUPPORT_OS;
#else
TdFilePtr pFile = NULL;
if (!(pFile = taosOpenFile(tsCpuQuotaFile, TD_FILE_READ | TD_FILE_STREAM))) {
@ -545,11 +541,11 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
}
char qline[32] = {0};
if (taosGetsFile(pFile, sizeof(qline), qline) <= 0) {
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
goto _sys;
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
float quota = taosStr2Float(qline, NULL);
if (quota < 0) {
goto _sys;
@ -561,11 +557,11 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
char pline[32] = {0};
if (taosGetsFile(pFile, sizeof(pline), pline) <= 0) {
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
goto _sys;
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
float period = taosStr2Float(pline, NULL);
float quotaCores = quota / period;
@ -575,10 +571,16 @@ static int32_t taosCntrGetCpuCores(float *numOfCores) {
} else {
*numOfCores = sysCores;
}
if(*numOfCores <= 0) {
return TAOS_SYSTEM_ERROR(errno);
}
goto _end;
_sys:
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
if(*numOfCores <= 0) {
return TAOS_SYSTEM_ERROR(errno);
}
_end:
return 0;
@ -586,26 +588,35 @@ _end:
#endif
}
void taosGetCpuCores(float *numOfCores, bool physical) {
int32_t taosGetCpuCores(float *numOfCores, bool physical) {
#ifdef WINDOWS
SYSTEM_INFO info;
GetSystemInfo(&info);
*numOfCores = info.dwNumberOfProcessors;
return;
return 0;
#elif defined(_TD_DARWIN_64)
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
if(*numOfCores <= 0) {
return TAOS_SYSTEM_ERROR(errno);
}
return;
#else
if (physical) {
*numOfCores = sysconf(_SC_NPROCESSORS_ONLN);
if(*numOfCores <= 0) {
return TAOS_SYSTEM_ERROR(errno);
}
} else {
(void)taosCntrGetCpuCores(numOfCores);
int code= taosCntrGetCpuCores(numOfCores);
if(code != 0) {
return code;
}
}
return;
return 0;
#endif
}
void taosGetCpuUsage(double *cpu_system, double *cpu_engine) {
int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine) {
static int64_t lastSysUsed = -1;
static int64_t lastSysTotal = -1;
static int64_t lastProcTotal = -1;
@ -639,6 +650,7 @@ void taosGetCpuUsage(double *cpu_system, double *cpu_engine) {
lastSysTotal = curSysTotal;
lastProcTotal = curProcTotal;
}
return 0;
}
#define __cpuid_fix(level, a, b, c, d) \
@ -860,7 +872,7 @@ int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int
if (readIndex >= 4) break;
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
if (readIndex < 4) {
return -1;
@ -870,7 +882,7 @@ int32_t taosGetProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int
#endif
}
void taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
int32_t taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
static int64_t last_rchars = -1;
static int64_t last_wchars = -1;
static int64_t last_read_bytes = -1;
@ -879,7 +891,8 @@ void taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, i
static int64_t cur_wchars = 0;
static int64_t cur_read_bytes = 0;
static int64_t cur_write_bytes = 0;
if (taosGetProcIO(&cur_rchars, &cur_wchars, &cur_read_bytes, &cur_write_bytes) == 0) {
int32_t code = taosGetProcIO(&cur_rchars, &cur_wchars, &cur_read_bytes, &cur_write_bytes);
if (code == 0) {
if(last_rchars >=0 && last_wchars >=0 && last_read_bytes >=0 && last_write_bytes >= 0){
*rchars = cur_rchars - last_rchars;
*wchars = cur_wchars - last_wchars;
@ -897,11 +910,15 @@ void taosGetProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, i
last_read_bytes = cur_read_bytes;
last_write_bytes = cur_write_bytes;
} else {
*rchars = 0;
*wchars = 0;
*read_bytes = 0;
*write_bytes = 0;
return code;
}
return 0;
}
void taosSetDefaultProcIODelta(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) {
*rchars = 0;
*wchars = 0;
*read_bytes = 0;
*write_bytes = 0;
}
int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
@ -953,18 +970,19 @@ int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) {
*transmit_bytes += o_tbytes;
}
(void)taosCloseFile(&pFile);
TAOS_SKIP_ERROR(taosCloseFile(&pFile));
return 0;
#endif
}
void taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
int32_t taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
static int64_t last_receive_bytes = -1;
static int64_t last_transmit_bytes = -1;
int64_t cur_receive_bytes = 0;
int64_t cur_transmit_bytes = 0;
if (taosGetCardInfo(&cur_receive_bytes, &cur_transmit_bytes) == 0) {
int32_t code = taosGetCardInfo(&cur_receive_bytes, &cur_transmit_bytes);
if (code == 0) {
if(last_receive_bytes >= 0 && last_transmit_bytes >= 0){
*receive_bytes = cur_receive_bytes - last_receive_bytes;
*transmit_bytes = cur_transmit_bytes - last_transmit_bytes;
@ -977,9 +995,13 @@ void taosGetCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
last_receive_bytes = cur_receive_bytes;
last_transmit_bytes = cur_transmit_bytes;
} else {
*receive_bytes = 0;
*transmit_bytes = 0;
return code;
}
return 0;
}
void taosSetDefaultCardInfoDelta(int64_t *receive_bytes, int64_t *transmit_bytes) {
*receive_bytes = 0;
*transmit_bytes = 0;
}
void taosKillSystem() {