From 6cfff92b21d0d38e7e9e155402f1b40e62a7f522 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 17:11:24 +0800 Subject: [PATCH 1/5] get tfs monitor info --- include/libs/tfs/tfs.h | 9 +++++++++ source/dnode/mgmt/impl/inc/dndEnv.h | 5 ++--- source/dnode/mgmt/impl/src/dndEnv.c | 9 ++++++++- source/dnode/mgmt/impl/src/dndMgmt.c | 15 ++++++++++----- source/libs/tfs/CMakeLists.txt | 2 +- source/libs/tfs/src/tfs.c | 21 +++++++++++++++++++++ 6 files changed, 51 insertions(+), 10 deletions(-) diff --git a/include/libs/tfs/tfs.h b/include/libs/tfs/tfs.h index b6d10c81bf..1b41da33bb 100644 --- a/include/libs/tfs/tfs.h +++ b/include/libs/tfs/tfs.h @@ -17,6 +17,7 @@ #define _TD_TFS_H_ #include "tdef.h" +#include "monitor.h" #ifdef __cplusplus extern "C" { @@ -237,6 +238,14 @@ const STfsFile *tfsReaddir(STfsDir *pDir); */ void tfsClosedir(STfsDir *pDir); +/** + * @brief Get disk info of tfs. + * + * @param pTfs The fs object. + * @param pInfo The info object. + */ +int32_t tfsGetMonitorInfo(STfs *pTfs, SMonDiskInfo *pInfo); + #ifdef __cplusplus } #endif diff --git a/source/dnode/mgmt/impl/inc/dndEnv.h b/source/dnode/mgmt/impl/inc/dndEnv.h index 724ba30155..efe54ee1e5 100644 --- a/source/dnode/mgmt/impl/inc/dndEnv.h +++ b/source/dnode/mgmt/impl/inc/dndEnv.h @@ -31,7 +31,7 @@ typedef struct { SDnode *pDnode; STaosQueue *queue; union { - SQWorkerPool pool; + SQWorkerPool pool; SWWorkerPool mpool; }; } SDnodeWorker; @@ -137,8 +137,7 @@ typedef struct SDnode { SStartupReq startup; } SDnode; - -int32_t dndGetDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo); +int32_t dndGetMonitorDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/impl/src/dndEnv.c b/source/dnode/mgmt/impl/src/dndEnv.c index ae08d6387c..1cbdf0c1dc 100644 --- a/source/dnode/mgmt/impl/src/dndEnv.c +++ b/source/dnode/mgmt/impl/src/dndEnv.c @@ -324,4 +324,11 @@ void dndCleanup() { dInfo("dnode env is cleaned up"); } -int32_t dndGetDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo) { return 0; } \ No newline at end of file +int32_t dndGetMonitorDiskInfo(SDnode *pDnode, SMonDiskInfo *pInfo) { + tstrncpy(pInfo->logdir.name, tsLogDir, sizeof(pInfo->logdir.name)); + pInfo->logdir.size = tsLogSpace.size; + tstrncpy(pInfo->tempdir.name, tsTempDir, sizeof(pInfo->tempdir.name)); + pInfo->tempdir.size = tsTempSpace.size; + + return tfsGetMonitorInfo(pDnode->pTfs, pInfo); +} \ No newline at end of file diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 268ed7e362..70f5060ead 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -474,13 +474,13 @@ void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) { rpcSendResponse(&rpcRsp); } -static int32_t dndGetBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { +static int32_t dndGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { pInfo->dnode_id = dndGetDnodeId(pDnode); tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); return 0; } -static int32_t dndGetDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } +static int32_t dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } static void dndSendMonitorReport(SDnode *pDnode) { if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; @@ -490,7 +490,7 @@ static void dndSendMonitorReport(SDnode *pDnode) { if (pMonitor == NULL) return; SMonBasicInfo basicInfo = {0}; - if (dndGetBasicInfo(pDnode, &basicInfo) == 0) { + if (dndGetMonitorBasicInfo(pDnode, &basicInfo) == 0) { monSetBasicInfo(pMonitor, &basicInfo); } @@ -504,15 +504,20 @@ static void dndSendMonitorReport(SDnode *pDnode) { } SMonDnodeInfo dnodeInfo = {0}; - if (dndGetDnodeInfo(pDnode, &dnodeInfo) == 0) { + if (dndGetMonitorDnodeInfo(pDnode, &dnodeInfo) == 0) { monSetDnodeInfo(pMonitor, &dnodeInfo); } SMonDiskInfo diskInfo = {0}; - if (dndGetDiskInfo(pDnode, &diskInfo) == 0) { + if (dndGetMonitorDiskInfo(pDnode, &diskInfo) == 0) { monSetDiskInfo(pMonitor, &diskInfo); } + taosArrayDestroy(clusterInfo.dnodes); + taosArrayDestroy(clusterInfo.mnodes); + taosArrayDestroy(vgroupInfo.vgroups); + taosArrayDestroy(diskInfo.datadirs); + monSendReport(pMonitor); monCleanupMonitorInfo(pMonitor); } diff --git a/source/libs/tfs/CMakeLists.txt b/source/libs/tfs/CMakeLists.txt index 607ccd4c48..97e02fc8a9 100644 --- a/source/libs/tfs/CMakeLists.txt +++ b/source/libs/tfs/CMakeLists.txt @@ -6,7 +6,7 @@ target_include_directories( PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) -target_link_libraries(tfs os util common) +target_link_libraries(tfs os util common monitor) if(${BUILD_TEST}) add_subdirectory(test) diff --git a/source/libs/tfs/src/tfs.c b/source/libs/tfs/src/tfs.c index f686703643..944e67c863 100644 --- a/source/libs/tfs/src/tfs.c +++ b/source/libs/tfs/src/tfs.c @@ -544,3 +544,24 @@ static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter) { return pDisk; } + +int32_t tfsGetMonitorInfo(STfs *pTfs, SMonDiskInfo *pInfo) { + pInfo->datadirs = taosArrayInit(32, sizeof(SMonDiskDesc)); + if (pInfo->datadirs == NULL) return -1; + + tfsUpdateSize(pTfs); + + tfsLock(pTfs); + for (int32_t level = 0; level < pTfs->nlevel; level++) { + STfsTier *pTier = &pTfs->tiers[level]; + for (int32_t disk = 0; disk < pTier->ndisk; ++disk) { + STfsDisk *pDisk = pTier->disks[disk]; + SMonDiskDesc dinfo = {0}; + dinfo.size = pDisk->size; + dinfo.level = pDisk->level; + tstrncpy(dinfo.name, pDisk->path, sizeof(dinfo.name)); + taosArrayPush(pInfo->datadirs, &dinfo); + } + } + tfsUnLock(pTfs); +} \ No newline at end of file From c018e91859645c280a8f12d7784a0ba87abd8885 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 17:30:33 +0800 Subject: [PATCH 2/5] minor changes --- include/os/osSysinfo.h | 6 +- source/dnode/mnode/impl/src/mndTelem.c | 4 +- source/os/src/osSysinfo.c | 110 ++++++++++++------------- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index bf9d7662e6..7ad9c57a3c 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -34,9 +34,9 @@ typedef struct { } SDiskSpace; void taosGetSystemInfo(); -bool taosGetEmail(char *email, int32_t maxLen); -bool taosGetOsReleaseName(char *releaseName, int32_t maxLen); -bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores); +int32_t taosGetEmail(char *email, int32_t maxLen); +int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen); +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores); int32_t taosGetCpuCores(); bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); bool taosGetTotalSysMemoryKB(uint64_t *kb); diff --git a/source/dnode/mnode/impl/src/mndTelem.c b/source/dnode/mnode/impl/src/mndTelem.c index 1cd7593846..da2c52ec8f 100644 --- a/source/dnode/mnode/impl/src/mndTelem.c +++ b/source/dnode/mnode/impl/src/mndTelem.c @@ -52,12 +52,12 @@ static char* mndBuildTelemetryReport(SMnode* pMnode) { tjsonAddStringToObject(pJson, "instanceId", clusterName); tjsonAddDoubleToObject(pJson, "reportVersion", 1); - if (taosGetOsReleaseName(tmp, sizeof(tmp))) { + if (taosGetOsReleaseName(tmp, sizeof(tmp)) == 0) { tjsonAddStringToObject(pJson, "os", tmp); } int32_t numOfCores = 0; - if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores)) { + if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) { tjsonAddStringToObject(pJson, "cpuModel", tmp); tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores); } else { diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 3e761c6d91..d3f56e5901 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -80,7 +80,6 @@ bool taosGetProcMemory(float *memoryUsedMB) { return true; } - int32_t taosGetCpuCores() { SYSTEM_INFO info; GetSystemInfo(&info); @@ -106,7 +105,7 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { diskSize->used = (int64_t)(i64TotalBytes - i64FreeBytes); return 0; } else { - //printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } @@ -173,12 +172,12 @@ void taosGetSystemInfo() { } void taosKillSystem() { - //printf("function taosKillSystem, exit!"); + // printf("function taosKillSystem, exit!"); exit(0); } int taosSystem(const char *cmd) { - //printf("taosSystem not support"); + // printf("taosSystem not support"); return -1; } @@ -241,9 +240,8 @@ char *taosGetCmdlineByPID(int pid) { return ""; } #include #include - void taosKillSystem() { - //printf("function taosKillSystem, exit!"); + // printf("function taosKillSystem, exit!"); exit(0); } @@ -300,7 +298,7 @@ bool taosGetSysMemory(float *memoryUsedMB) { } int taosSystem(const char *cmd) { - //printf("un support funtion"); + // printf("un support funtion"); return -1; } @@ -309,7 +307,7 @@ void taosSetCoreDump() {} int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { struct statvfs info; if (statvfs(dataDir, &info)) { - //printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); + // printf("failed to get disk size, dataDir:%s errno:%s", tsDataDir, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } else { @@ -404,12 +402,12 @@ bool taosGetProcMemory(float *memoryUsedMB) { // FILE *fp = fopen(tsProcMemFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsProcMemFile); + // printf("open file:%s failed", tsProcMemFile); return false; } ssize_t _bytes = 0; - char * line = NULL; + char *line = NULL; while (!taosEOFFile(pFile)) { _bytes = taosGetLineFile(pFile, &line); if ((_bytes < 0) || (line == NULL)) { @@ -421,7 +419,7 @@ bool taosGetProcMemory(float *memoryUsedMB) { } if (line == NULL) { - //printf("read file:%s failed", tsProcMemFile); + // printf("read file:%s failed", tsProcMemFile); taosCloseFile(&pFile); return false; } @@ -431,7 +429,7 @@ bool taosGetProcMemory(float *memoryUsedMB) { sscanf(line, "%s %" PRId64, tmp, &memKB); *memoryUsedMB = (float)((double)memKB / 1024); - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; } @@ -440,14 +438,14 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { // FILE *fp = fopen(tsSysCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsSysCpuFile); + // printf("open file:%s failed", tsSysCpuFile); return false; } - char * line = NULL; + char *line = NULL; ssize_t _bytes = taosGetLineFile(pFile, &line); if ((_bytes < 0) || (line == NULL)) { - //printf("read file:%s failed", tsSysCpuFile); + // printf("read file:%s failed", tsSysCpuFile); taosCloseFile(&pFile); return false; } @@ -456,7 +454,7 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { sscanf(line, "%s %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64, cpu, &cpuInfo->user, &cpuInfo->nice, &cpuInfo->system, &cpuInfo->idle); - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; } @@ -465,14 +463,14 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { // FILE *fp = fopen(tsProcCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsProcCpuFile); + // printf("open file:%s failed", tsProcCpuFile); return false; } - char * line = NULL; + char *line = NULL; ssize_t _bytes = taosGetLineFile(pFile, &line); if ((_bytes < 0) || (line == NULL)) { - //printf("read file:%s failed", tsProcCpuFile); + // printf("read file:%s failed", tsProcCpuFile); taosCloseFile(&pFile); return false; } @@ -486,12 +484,11 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { } } - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; } - int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { @@ -550,12 +547,12 @@ bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { // FILE *fp = fopen(tsSysNetFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsSysNetFile); + // printf("open file:%s failed", tsSysNetFile); return false; } ssize_t _bytes = 0; - char * line = NULL; + char *line = NULL; while (!taosEOFFile(pFile)) { int64_t o_rbytes = 0; @@ -590,7 +587,7 @@ bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { *bytes += (o_rbytes + o_tbytes); } - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); return true; @@ -636,12 +633,12 @@ bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { // FILE *fp = fopen(tsProcIOFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - //printf("open file:%s failed", tsProcIOFile); + // printf("open file:%s failed", tsProcIOFile); return false; } ssize_t _bytes = 0; - char * line = NULL; + char *line = NULL; char tmp[10]; int readIndex = 0; @@ -662,11 +659,11 @@ bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { if (readIndex >= 2) break; } - if(line != NULL) tfree(line); + if (line != NULL) tfree(line); taosCloseFile(&pFile); if (readIndex < 2) { - //printf("read file:%s failed", tsProcIOFile); + // printf("read file:%s failed", tsProcIOFile); return false; } @@ -713,12 +710,11 @@ void taosGetSystemInfo() { taosGetBandSpeed(&tmp1); taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); - } void taosKillSystem() { // SIGINT - //printf("taosd will shut down soon"); + // printf("taosd will shut down soon"); kill(tsProcId, 2); } @@ -727,22 +723,22 @@ int taosSystem(const char *cmd) { int res; char buf[1024]; if (cmd == NULL) { - //printf("taosSystem cmd is NULL!"); + // printf("taosSystem cmd is NULL!"); return -1; } if ((fp = popen(cmd, "r")) == NULL) { - //printf("popen cmd:%s error: %s", cmd, strerror(errno)); + // printf("popen cmd:%s error: %s", cmd, strerror(errno)); return -1; } else { while (fgets(buf, sizeof(buf), fp)) { - //printf("popen result:%s", buf); + // printf("popen result:%s", buf); } if ((res = pclose(fp)) == -1) { - //printf("close popen file pointer fp error!"); + // printf("close popen file pointer fp error!"); } else { - //printf("popen res is :%d", res); + // printf("popen res is :%d", res); } return res; @@ -757,14 +753,14 @@ void taosSetCoreDump(bool enable) { struct rlimit rlim_new; if (getrlimit(RLIMIT_CORE, &rlim) == 0) { #ifndef _ALPINE - //printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); + // printf("the old unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); #else - //printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); + // printf("the old unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); #endif rlim_new.rlim_cur = RLIM_INFINITY; rlim_new.rlim_max = RLIM_INFINITY; if (setrlimit(RLIMIT_CORE, &rlim_new) != 0) { - //printf("set unlimited fail, error: %s", strerror(errno)); + // printf("set unlimited fail, error: %s", strerror(errno)); rlim_new.rlim_cur = rlim.rlim_max; rlim_new.rlim_max = rlim.rlim_max; (void)setrlimit(RLIMIT_CORE, &rlim_new); @@ -773,9 +769,9 @@ void taosSetCoreDump(bool enable) { if (getrlimit(RLIMIT_CORE, &rlim) == 0) { #ifndef _ALPINE - //printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); + // printf("the new unlimited para: rlim_cur=%" PRIu64 ", rlim_max=%" PRIu64, rlim.rlim_cur, rlim.rlim_max); #else - //printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); + // printf("the new unlimited para: rlim_cur=%llu, rlim_max=%llu", rlim.rlim_cur, rlim.rlim_max); #endif } @@ -801,10 +797,10 @@ void taosSetCoreDump(bool enable) { old_len = sizeof(old_usespid); if (syscall(SYS__sysctl, &args) == -1) { - //printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); + // printf("_sysctl(kern_core_uses_pid) set fail: %s", strerror(errno)); } - //printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); + // printf("The old core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); old_usespid = 0; old_len = 0; @@ -817,10 +813,10 @@ void taosSetCoreDump(bool enable) { old_len = sizeof(old_usespid); if (syscall(SYS__sysctl, &args) == -1) { - //printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); + // printf("_sysctl(kern_core_uses_pid) get fail: %s", strerror(errno)); } - //printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); + // printf("The new core_uses_pid[%" PRIu64 "]: %d", old_len, old_usespid); #endif } @@ -885,7 +881,7 @@ SysNameInfo taosGetSysNameInfo() { return info; } -bool taosGetEmail(char *email, int32_t maxLen) { +int32_t taosGetEmail(char *email, int32_t maxLen) { const char *filepath = "/usr/local/taos/email"; TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); @@ -893,17 +889,17 @@ bool taosGetEmail(char *email, int32_t maxLen) { if (taosReadFile(pFile, (void *)email, maxLen) < 0) { taosCloseFile(&pFile); - return false; + return -1; } taosCloseFile(&pFile); - return true; + return 0; } -bool taosGetOsReleaseName(char *releaseName, int32_t maxLen) { - char *line = NULL; - size_t size = 0; - bool ret = false; +int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { + char *line = NULL; + size_t size = 0; + int32_t code = -1; TdFilePtr pFile = taosOpenFile("/etc/os-release", TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) return false; @@ -917,21 +913,21 @@ bool taosGetOsReleaseName(char *releaseName, int32_t maxLen) { line[size - 2] = 0; } tstrncpy(releaseName, p, maxLen); - ret = true; + code = 0; break; } } if (line != NULL) free(line); taosCloseFile(&pFile); - return ret; + return code; } -bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { char *line = NULL; size_t size = 0; int32_t done = 0; - bool ret = false; + int32_t code = -1; TdFilePtr pFile = taosOpenFile("/proc/cpuinfo", TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) return false; @@ -941,7 +937,7 @@ bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { if (((done & 1) == 0) && strncmp(line, "model name", 10) == 0) { const char *v = strchr(line, ':') + 2; tstrncpy(cpuModel, v, maxLen); - ret = true; + code = 0; done |= 1; } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { const char *v = strchr(line, ':') + 2; @@ -953,7 +949,7 @@ bool taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { if (line != NULL) free(line); taosCloseFile(&pFile); - return ret; + return code; } bool taosGetTotalSysMemoryKB(uint64_t *kb) { From c890b0b5c1987ffbec0186c5c72ba4a0db27d100 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 18:10:56 +0800 Subject: [PATCH 3/5] sysinfo in monitor --- include/libs/monitor/monitor.h | 6 +- include/os/osEnv.h | 6 +- include/os/osSysinfo.h | 12 +- source/common/src/tglobal.c | 10 +- source/dnode/mgmt/impl/src/dndMgmt.c | 43 ++++-- source/dnode/mnode/impl/src/mndTelem.c | 11 +- source/os/src/osEnv.c | 11 +- source/os/src/osSysinfo.c | 183 +++++++++++-------------- 8 files changed, 140 insertions(+), 142 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index b3b6091b95..487b2bfe88 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -88,9 +88,9 @@ typedef struct { float cpu_engine; float cpu_system; float cpu_cores; - float mem_engine; // MB - float mem_system; // MB - float mem_total; // MB + int64_t mem_engine; // KB + int64_t mem_system; // KB + int64_t mem_total; // KB float disk_engine; // GB float disk_used; // GB float disk_total; // GB diff --git a/include/os/osEnv.h b/include/os/osEnv.h index 4ac073f6c2..1426ba87f6 100644 --- a/include/os/osEnv.h +++ b/include/os/osEnv.h @@ -28,11 +28,11 @@ extern char tsCharset[]; extern char tsLocale[]; extern int8_t tsDaylight; extern bool tsEnableCoreFile; -extern int64_t tsPageSize; +extern int64_t tsPageSizeKB; extern int64_t tsOpenMax; extern int64_t tsStreamMax; -extern int32_t tsNumOfCores; -extern int32_t tsTotalMemoryMB; +extern float tsNumOfCores; +extern int64_t tsTotalMemoryKB; extern char configDir[]; extern char tsDataDir[]; diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 7ad9c57a3c..df5ed28a99 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -36,12 +36,12 @@ typedef struct { void taosGetSystemInfo(); int32_t taosGetEmail(char *email, int32_t maxLen); int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen); -int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores); -int32_t taosGetCpuCores(); -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage); -bool taosGetTotalSysMemoryKB(uint64_t *kb); -bool taosGetProcMemory(float *memoryUsedMB); // -bool taosGetSysMemory(float *memoryUsedMB); // +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores); +int32_t taosGetCpuCores(float *numOfCores); +int32_t taosGetCpuUsage(float *cpu_system, float *cpu_engine); +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); bool taosReadProcIO(int64_t *rchars, int64_t *wchars); bool taosGetProcIO(float *readKB, float *writeKB); diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index 5998b7c9ce..93b66e3369 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -279,11 +279,11 @@ static int32_t taosAddSystemCfg(SConfig *pCfg) { if (cfgAddLocale(pCfg, "locale", tsLocale) != 0) return -1; if (cfgAddCharset(pCfg, "charset", tsCharset) != 0) return -1; if (cfgAddBool(pCfg, "enableCoreFile", 1, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "numOfCores", tsNumOfCores, 1, 100000, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "pageSize(KB)", tsPageSize, 0, INT64_MAX, 1) != 0) return -1; + if (cfgAddFloat(pCfg, "numOfCores", tsNumOfCores, 0, 100000, 1) != 0) return -1; if (cfgAddInt64(pCfg, "openMax", tsOpenMax, 0, INT64_MAX, 1) != 0) return -1; if (cfgAddInt64(pCfg, "streamMax", tsStreamMax, 0, INT64_MAX, 1) != 0) return -1; - if (cfgAddInt32(pCfg, "totalMemory(MB)", tsTotalMemoryMB, 0, INT32_MAX, 1) != 0) return -1; + if (cfgAddInt32(pCfg, "pageSize(KB)", tsPageSizeKB, 0, INT64_MAX, 1) != 0) return -1; + if (cfgAddInt64(pCfg, "totalMemory(KB)", tsTotalMemoryKB, 0, INT64_MAX, 1) != 0) return -1; if (cfgAddString(pCfg, "os sysname", info.sysname, 1) != 0) return -1; if (cfgAddString(pCfg, "os nodename", info.nodename, 1) != 0) return -1; if (cfgAddString(pCfg, "os release", info.release, 1) != 0) return -1; @@ -404,10 +404,6 @@ static void taosSetSystemCfg(SConfig *pCfg) { const char *charset = cfgGetItem(pCfg, "charset")->str; taosSetSystemLocale(locale, charset); - if (tsNumOfCores <= 1) { - tsNumOfCores = 2; - } - bool enableCore = cfgGetItem(pCfg, "enableCoreFile")->bval; taosSetConsoleEcho(enableCore); diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 70f5060ead..704847894e 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -474,13 +474,40 @@ void dndProcessStartupReq(SDnode *pDnode, SRpcMsg *pReq) { rpcSendResponse(&rpcRsp); } -static int32_t dndGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { +static void dndGetMonitorBasicInfo(SDnode *pDnode, SMonBasicInfo *pInfo) { pInfo->dnode_id = dndGetDnodeId(pDnode); tstrncpy(pInfo->dnode_ep, tsLocalEp, TSDB_EP_LEN); - return 0; } -static int32_t dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { return 0; } +static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { + pInfo->uptime = (taosGetTimestampMs() - pDnode->dmgmt.rebootTime) / (86400000.0f); + taosGetCpuUsage(&pInfo->cpu_engine, &pInfo->cpu_system); + pInfo->cpu_cores = tsNumOfCores; + taosGetProcMemory(&pInfo->mem_engine); + taosGetSysMemory(&pInfo->mem_system); + pInfo->mem_total = tsTotalMemoryKB; + pInfo->disk_engine = 4.1; + pInfo->disk_used = 4.2; + pInfo->disk_total = 4.3; + pInfo->net_in = 5.1; + pInfo->net_out = 5.2; + pInfo->io_read = 6.1; + pInfo->io_write = 6.2; + pInfo->io_read_disk = 7.1; + pInfo->io_write_disk = 7.2; + pInfo->req_select = 8; + pInfo->req_select_rate = 8.1; + pInfo->req_insert = 9; + pInfo->req_insert_success = 10; + pInfo->req_insert_rate = 10.1; + pInfo->req_insert_batch = 11; + pInfo->req_insert_batch_success = 12; + pInfo->req_insert_batch_rate = 12.3; + pInfo->errors = 4; + pInfo->vnodes_num = 5; + pInfo->masters = 6; + pInfo->has_mnode = 1; +} static void dndSendMonitorReport(SDnode *pDnode) { if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; @@ -490,9 +517,8 @@ static void dndSendMonitorReport(SDnode *pDnode) { if (pMonitor == NULL) return; SMonBasicInfo basicInfo = {0}; - if (dndGetMonitorBasicInfo(pDnode, &basicInfo) == 0) { - monSetBasicInfo(pMonitor, &basicInfo); - } + dndGetMonitorBasicInfo(pDnode, &basicInfo); + monSetBasicInfo(pMonitor, &basicInfo); SMonClusterInfo clusterInfo = {0}; SMonVgroupInfo vgroupInfo = {0}; @@ -504,9 +530,8 @@ static void dndSendMonitorReport(SDnode *pDnode) { } SMonDnodeInfo dnodeInfo = {0}; - if (dndGetMonitorDnodeInfo(pDnode, &dnodeInfo) == 0) { - monSetDnodeInfo(pMonitor, &dnodeInfo); - } + dndGetMonitorDnodeInfo(pDnode, &dnodeInfo); + monSetDnodeInfo(pMonitor, &dnodeInfo); SMonDiskInfo diskInfo = {0}; if (dndGetMonitorDiskInfo(pDnode, &diskInfo) == 0) { diff --git a/source/dnode/mnode/impl/src/mndTelem.c b/source/dnode/mnode/impl/src/mndTelem.c index da2c52ec8f..968319e7b4 100644 --- a/source/dnode/mnode/impl/src/mndTelem.c +++ b/source/dnode/mnode/impl/src/mndTelem.c @@ -56,19 +56,16 @@ static char* mndBuildTelemetryReport(SMnode* pMnode) { tjsonAddStringToObject(pJson, "os", tmp); } - int32_t numOfCores = 0; + float numOfCores = 0; if (taosGetCpuInfo(tmp, sizeof(tmp), &numOfCores) == 0) { tjsonAddStringToObject(pJson, "cpuModel", tmp); tjsonAddDoubleToObject(pJson, "numOfCpu", numOfCores); } else { - tjsonAddDoubleToObject(pJson, "numOfCpu", taosGetCpuCores()); + tjsonAddDoubleToObject(pJson, "numOfCpu", tsNumOfCores); } - uint64_t memoryKB = 0; - if (taosGetTotalSysMemoryKB(&memoryKB)) { - snprintf(tmp, sizeof(tmp), "%" PRIu64 " kB", memoryKB); - tjsonAddStringToObject(pJson, "memory", tmp); - } + snprintf(tmp, sizeof(tmp), "%" PRId64 " kB", tsTotalMemoryKB); + tjsonAddStringToObject(pJson, "memory", tmp); tjsonAddStringToObject(pJson, "version", version); tjsonAddStringToObject(pJson, "buildInfo", buildinfo); diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index 4c368fe895..0b2fe904b3 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -31,11 +31,11 @@ char tsLocale[TD_LOCALE_LEN] = {0}; char tsCharset[TD_CHARSET_LEN] = {0}; int8_t tsDaylight = 0; bool tsEnableCoreFile = 0; -int64_t tsPageSize = 0; +int64_t tsPageSizeKB = 0; int64_t tsOpenMax = 0; int64_t tsStreamMax = 0; -int32_t tsNumOfCores = 0; -int32_t tsTotalMemoryMB = 0; +float tsNumOfCores = 0; +int64_t tsTotalMemoryKB = 0; void osInit() { srand(taosSafeRand()); @@ -44,6 +44,11 @@ void osInit() { taosSetSystemTimezone(tsTimezone, tsTimezone, &tsDaylight); taosGetSystemInfo(); + // deadlock in query + if (tsNumOfCores < 2) { + tsNumOfCores = 2; + } + #if defined(_TD_WINDOWS_64) || defined(_TD_WINDOWS_32) taosWinSocketInit(); diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index d3f56e5901..bf036354e9 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -39,32 +39,32 @@ #include #pragma warning(pop) -static int32_t taosGetTotalMemory() { +int32_t taosGetTotalMemory(int64_t *totalKB) { MEMORYSTATUSEX memsStat; memsStat.dwLength = sizeof(memsStat); if (!GlobalMemoryStatusEx(&memsStat)) { - return 0; + return -1; } - float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); - return (int32_t)nMemTotal; + *totalKB = memsStat.ullTotalPhys / 1024; + return 0; } -bool taosGetSysMemory(float *memoryUsedMB) { +int32_t taosGetSysMemory(int64_t *usedKB) { MEMORYSTATUSEX memsStat; memsStat.dwLength = sizeof(memsStat); if (!GlobalMemoryStatusEx(&memsStat)) { - return false; + return -1; } - float nMemFree = memsStat.ullAvailPhys / (1024.0f * 1024.0f); - float nMemTotal = memsStat.ullTotalPhys / (1024.0f * 1024.0f); + int64_t nMemFree = memsStat.ullAvailPhys / 1024; + int64_t nMemTotal = memsStat.ullTotalPhys / 1024.0; - *memoryUsedMB = nMemTotal - nMemFree; - return true; + *usedKB = nMemTotal - nMemFree; + return 0; } -bool taosGetProcMemory(float *memoryUsedMB) { +int32_t taosGetProcMemory(int64_t *usedKB) { unsigned bytes_used = 0; #if defined(_WIN64) && defined(_MSC_VER) @@ -76,20 +76,21 @@ bool taosGetProcMemory(float *memoryUsedMB) { } #endif - *memoryUsedMB = (float)bytes_used / 1024 / 1024; - return true; + *usedKB = bytes_used / 1024; + return 0; } -int32_t taosGetCpuCores() { +int32_t taosGetCpuCores(float *numOfCores) { SYSTEM_INFO info; GetSystemInfo(&info); - return (int32_t)info.dwNumberOfProcessors; + *numOfCores = info.dwNumberOfProcessors; + return 0; } -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { *sysCpuUsage = 0; *procCpuUsage = 0; - return true; + return 0; } int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { @@ -162,8 +163,8 @@ bool taosGetProcIO(float *readKB, float *writeKB) { } void taosGetSystemInfo() { - tsNumOfCores = taosGetCpuCores(); - tsTotalMemoryMB = taosGetTotalMemory(); + taosGetCpuCores(&tsNumOfCores); + taosGetTotalMemory(&tsTotalMemoryKB); float tmp1, tmp2; taosGetBandSpeed(&tmp1); @@ -245,16 +246,17 @@ void taosKillSystem() { exit(0); } -int32_t taosGetCpuCores() { return sysconf(_SC_NPROCESSORS_ONLN); } +int32_t taosGetCpuCores(float *numOfCores) { + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); + return 0; +} void taosGetSystemInfo() { - // taosGetProcInfos(); - - tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); long physical_pages = sysconf(_SC_PHYS_PAGES); long page_size = sysconf(_SC_PAGESIZE); - tsTotalMemoryMB = physical_pages * page_size / (1024 * 1024); - tsPageSize = page_size; + tsTotalMemoryKB = physical_pages * page_size / 1024; + tsPageSizeKB = page_size / 1024; + tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); } bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { @@ -281,20 +283,20 @@ bool taosGetBandSpeed(float *bandSpeedKb) { return true; } -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { *sysCpuUsage = 0; *procCpuUsage = 0; - return true; + return 0; } -bool taosGetProcMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; - return true; +int32_t taosGetProcMemory(int64_t *usedKB) { + *usedKB = 0; + return 0; } -bool taosGetSysMemory(float *memoryUsedMB) { - *memoryUsedMB = 0; - return true; +int32_t taosGetSysMemory(int64_t *usedKB) { + *usedKB = 0; + return 0; } int taosSystem(const char *cmd) { @@ -375,35 +377,34 @@ static char tsSysCpuFile[] = "/proc/stat"; static char tsProcCpuFile[25] = {0}; static char tsProcMemFile[25] = {0}; static char tsProcIOFile[25] = {0}; -static float tsPageSizeKB = 0; static void taosGetProcInfos() { - tsPageSize = sysconf(_SC_PAGESIZE); + tsPageSizeKB = sysconf(_SC_PAGESIZE) / 1024; tsOpenMax = sysconf(_SC_OPEN_MAX); tsStreamMax = sysconf(_SC_STREAM_MAX); - tsProcId = (pid_t)syscall(SYS_gettid); - tsPageSizeKB = (float)(sysconf(_SC_PAGESIZE)) / 1024; - snprintf(tsProcMemFile, 25, "/proc/%d/status", tsProcId); - snprintf(tsProcCpuFile, 25, "/proc/%d/stat", tsProcId); - snprintf(tsProcIOFile, 25, "/proc/%d/io", tsProcId); + snprintf(tsProcMemFile, sizeof(tsProcMemFile), "/proc/%d/status", tsProcId); + snprintf(tsProcCpuFile, sizeof(tsProcCpuFile), "/proc/%d/stat", tsProcId); + snprintf(tsProcIOFile, sizeof(tsProcIOFile), "/proc/%d/io", tsProcId); } -static int32_t taosGetTotalMemory() { return (int32_t)((float)sysconf(_SC_PHYS_PAGES) * tsPageSizeKB / 1024); } - -bool taosGetSysMemory(float *memoryUsedMB) { - float memoryAvailMB = (float)sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB / 1024; - *memoryUsedMB = (float)tsTotalMemoryMB - memoryAvailMB; - return true; +int32_t taosGetTotalMemory(int64_t *totalKB) { + *totalKB = (int64_t)(sysconf(_SC_PHYS_PAGES) * tsPageSizeKB); + return 0; } -bool taosGetProcMemory(float *memoryUsedMB) { +int32_t taosGetSysMemory(int64_t *usedKB) { + *usedKB = sysconf(_SC_AVPHYS_PAGES) * tsPageSizeKB; + return 0; +} + +int32_t taosGetProcMemory(int64_t *usedKB) { // FILE *fp = fopen(tsProcMemFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcMemFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcMemFile); - return false; + return -1; } ssize_t _bytes = 0; @@ -421,25 +422,23 @@ bool taosGetProcMemory(float *memoryUsedMB) { if (line == NULL) { // printf("read file:%s failed", tsProcMemFile); taosCloseFile(&pFile); - return false; + return -1; } - int64_t memKB = 0; - char tmp[10]; - sscanf(line, "%s %" PRId64, tmp, &memKB); - *memoryUsedMB = (float)((double)memKB / 1024); + char tmp[10]; + sscanf(line, "%s %" PRId64, tmp, usedKB); if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { +static int32_t taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { // FILE *fp = fopen(tsSysCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsSysCpuFile); - return false; + return -1; } char *line = NULL; @@ -447,7 +446,7 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { if ((_bytes < 0) || (line == NULL)) { // printf("read file:%s failed", tsSysCpuFile); taosCloseFile(&pFile); - return false; + return -1; } char cpu[10] = {0}; @@ -456,15 +455,15 @@ static bool taosGetSysCpuInfo(SysCpuInfo *cpuInfo) { if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { +static int32_t taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { // FILE *fp = fopen(tsProcCpuFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcCpuFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcCpuFile); - return false; + return -1; } char *line = NULL; @@ -472,7 +471,7 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { if ((_bytes < 0) || (line == NULL)) { // printf("read file:%s failed", tsProcCpuFile); taosCloseFile(&pFile); - return false; + return -1; } for (int i = 0, blank = 0; line[i] != 0; ++i) { @@ -486,23 +485,26 @@ static bool taosGetProcCpuInfo(ProcCpuInfo *cpuInfo) { if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -int32_t taosGetCpuCores() { return (int32_t)sysconf(_SC_NPROCESSORS_ONLN); } +int32_t taosGetCpuCores(float *numOfCores) { + *numOfCores = sysconf(_SC_NPROCESSORS_ONLN); + return 0; +} -bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(float *cpu_system, float *cpu_engine) { static uint64_t lastSysUsed = 0; static uint64_t lastSysTotal = 0; static uint64_t lastProcTotal = 0; SysCpuInfo sysCpu; ProcCpuInfo procCpu; - if (!taosGetSysCpuInfo(&sysCpu)) { - return false; + if (taosGetSysCpuInfo(&sysCpu) != 0) { + return -1; } - if (!taosGetProcCpuInfo(&procCpu)) { - return false; + if (taosGetProcCpuInfo(&procCpu) != 0) { + return -1; } uint64_t curSysUsed = sysCpu.user + sysCpu.nice + sysCpu.system; @@ -513,21 +515,21 @@ bool taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { lastSysUsed = curSysUsed > 1 ? curSysUsed : 1; lastSysTotal = curSysTotal > 1 ? curSysTotal : 1; lastProcTotal = curProcTotal > 1 ? curProcTotal : 1; - return false; + return -1; } if (curSysTotal == lastSysTotal) { - return false; + return -1; } - *sysCpuUsage = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); - *procCpuUsage = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); + *cpu_engine = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); + *cpu_system = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); lastSysUsed = curSysUsed; lastSysTotal = curSysTotal; lastProcTotal = curProcTotal; - return true; + return 0; } int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { @@ -700,13 +702,10 @@ bool taosGetProcIO(float *readKB, float *writeKB) { void taosGetSystemInfo() { taosGetProcInfos(); - - tsNumOfCores = taosGetCpuCores(); - tsTotalMemoryMB = taosGetTotalMemory(); + taosGetCpuCores(&tsNumOfCores); + taosGetTotalMemory(&tsTotalMemoryKB); float tmp1, tmp2; - taosGetSysMemory(&tmp1); - taosGetProcMemory(&tmp2); taosGetBandSpeed(&tmp1); taosGetCpuUsage(&tmp1, &tmp2); taosGetProcIO(&tmp1, &tmp2); @@ -923,7 +922,7 @@ int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen) { return code; } -int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { +int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { char *line = NULL; size_t size = 0; int32_t done = 0; @@ -941,7 +940,7 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { done |= 1; } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { const char *v = strchr(line, ':') + 2; - *numOfCores = atoi(v); + *numOfCores = atof(v); done |= 2; } } @@ -952,28 +951,4 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, int32_t *numOfCores) { return code; } -bool taosGetTotalSysMemoryKB(uint64_t *kb) { - char *line = NULL; - size_t size = 0; - bool ret = false; - - TdFilePtr pFile = taosOpenFile("/proc/meminfo", TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) return false; - - while ((size = taosGetLineFile(pFile, &line)) != -1) { - line[size - 1] = '\0'; - if (strncmp(line, "MemTotal", 8) == 0) { - const char *p = strchr(line, ':') + 1; - while (*p == ' ') p++; - ret = true; - *kb = atoll(p); - break; - } - } - - if (line != NULL) free(line); - taosCloseFile(&pFile); - return ret; -} - #endif From 3fbb78a951fd383b6a4882f915393ba867544565 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 18:55:16 +0800 Subject: [PATCH 4/5] io --- include/os/osSysinfo.h | 4 +-- source/dnode/mgmt/impl/src/dndMgmt.c | 7 ++--- source/os/src/osSysinfo.c | 42 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index df5ed28a99..3c68615c64 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -43,8 +43,8 @@ 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); -bool taosReadProcIO(int64_t *rchars, int64_t *wchars); -bool taosGetProcIO(float *readKB, float *writeKB); +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars); +int32_t taosGetProcIO(float *readKB, float *writeKB); bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); bool taosGetBandSpeed(float *bandSpeedKb); diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 704847894e..6a743fcf48 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -491,10 +491,9 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { pInfo->disk_total = 4.3; pInfo->net_in = 5.1; pInfo->net_out = 5.2; - pInfo->io_read = 6.1; - pInfo->io_write = 6.2; - pInfo->io_read_disk = 7.1; - pInfo->io_write_disk = 7.2; + taosGetProcIO(&pInfo->io_read, &pInfo->io_write); + pInfo->io_read_disk = 0; + pInfo->io_write_disk = 0; pInfo->req_select = 8; pInfo->req_select_rate = 8.1; pInfo->req_insert = 9; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index bf036354e9..3683943c61 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -124,31 +124,31 @@ bool taosGetBandSpeed(float *bandSpeedKb) { return true; } -bool taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { +int32_t taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { IO_COUNTERS io_counter; if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { if (readbyte) *readbyte = io_counter.ReadTransferCount; if (writebyte) *writebyte = io_counter.WriteTransferCount; - return true; + return 0; } - return false; + return -1; } -bool taosGetProcIO(float *readKB, float *writeKB) { +int32_t taosGetProcIO(float *readKB, float *writeKB) { static int64_t lastReadbyte = -1; static int64_t lastWritebyte = -1; int64_t curReadbyte = 0; int64_t curWritebyte = 0; - if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { - return false; + if (taosReadProcIO(&curReadbyte, &curWritebyte) != 0) { + return -1; } if (lastReadbyte == -1 || lastWritebyte == -1) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return false; + return -1; } *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); @@ -159,7 +159,7 @@ bool taosGetProcIO(float *readKB, float *writeKB) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return true; + return 0; } void taosGetSystemInfo() { @@ -259,16 +259,16 @@ void taosGetSystemInfo() { tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); } -bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { if (rchars) *rchars = 0; if (wchars) *wchars = 0; - return true; + return 0; } -bool taosGetProcIO(float *readKB, float *writeKB) { +int32_t taosGetProcIO(float *readKB, float *writeKB) { *readKB = 0; *writeKB = 0; - return true; + return 0; } bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { @@ -631,12 +631,12 @@ bool taosGetBandSpeed(float *bandSpeedKb) { return true; } -bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { // FILE *fp = fopen(tsProcIOFile, "r"); TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcIOFile); - return false; + return -1; } ssize_t _bytes = 0; @@ -666,27 +666,27 @@ bool taosReadProcIO(int64_t *rchars, int64_t *wchars) { if (readIndex < 2) { // printf("read file:%s failed", tsProcIOFile); - return false; + return -1; } - return true; + return 0; } -bool taosGetProcIO(float *readKB, float *writeKB) { +int32_t taosGetProcIO(float *readKB, float *writeKB) { static int64_t lastReadbyte = -1; static int64_t lastWritebyte = -1; int64_t curReadbyte = 0; int64_t curWritebyte = 0; - if (!taosReadProcIO(&curReadbyte, &curWritebyte)) { - return false; + if (taosReadProcIO(&curReadbyte, &curWritebyte) != 0) { + return -1; } if (lastReadbyte == -1 || lastWritebyte == -1) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return false; + return -1; } *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); @@ -697,7 +697,7 @@ bool taosGetProcIO(float *readKB, float *writeKB) { lastReadbyte = curReadbyte; lastWritebyte = curWritebyte; - return true; + return 0; } void taosGetSystemInfo() { From 2bf46ddc54a1282620b4ad725f2a94a68c95008c Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 3 Mar 2022 19:12:08 +0800 Subject: [PATCH 5/5] monitor --- include/libs/monitor/monitor.h | 12 ++++----- include/os/osSysinfo.h | 4 +-- source/dnode/mgmt/impl/inc/dndMnode.h | 1 + source/dnode/mgmt/impl/src/dndMgmt.c | 33 +++++++++++------------ source/dnode/mgmt/impl/src/dndMnode.c | 7 +++++ source/os/src/osSysinfo.c | 38 +++++++++++++-------------- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 487b2bfe88..00cb7c3dbc 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -94,12 +94,12 @@ typedef struct { float disk_engine; // GB float disk_used; // GB float disk_total; // GB - float net_in; // Kb/s - float net_out; // Kb/s - float io_read; // Mb/s - float io_write; // Mb/s - float io_read_disk; // Mb/s - float io_write_disk; // Mb/s + int64_t net_in; + int64_t net_out; + float io_read; + float io_write; + float io_read_disk; + float io_write_disk; int32_t req_select; float req_select_rate; int32_t req_insert; diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index 3c68615c64..e14dba8269 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -45,8 +45,8 @@ int32_t taosGetSysMemory(int64_t *usedKB); int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize); int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars); int32_t taosGetProcIO(float *readKB, float *writeKB); -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); -bool taosGetBandSpeed(float *bandSpeedKb); +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); +int32_t taosGetBandSpeed(float *bandSpeedKb); int32_t taosSystem(const char *cmd); void taosKillSystem(); diff --git a/source/dnode/mgmt/impl/inc/dndMnode.h b/source/dnode/mgmt/impl/inc/dndMnode.h index 0f03bb3832..0aee3b4b43 100644 --- a/source/dnode/mgmt/impl/inc/dndMnode.h +++ b/source/dnode/mgmt/impl/inc/dndMnode.h @@ -34,6 +34,7 @@ int32_t dndProcessDropMnodeReq(SDnode *pDnode, SRpcMsg *pRpcMsg); int32_t dndGetMnodeMonitorInfo(SDnode *pDnode, SMonClusterInfo *pClusterInfo, SMonVgroupInfo *pVgroupInfo, SMonGrantInfo *pGrantInfo); +int8_t dndIsMnode(SDnode *pDnode); #ifdef __cplusplus } diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 6a743fcf48..60cfdc299c 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -486,26 +486,25 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { taosGetProcMemory(&pInfo->mem_engine); taosGetSysMemory(&pInfo->mem_system); pInfo->mem_total = tsTotalMemoryKB; - pInfo->disk_engine = 4.1; - pInfo->disk_used = 4.2; - pInfo->disk_total = 4.3; - pInfo->net_in = 5.1; - pInfo->net_out = 5.2; + pInfo->disk_engine = 0; + pInfo->disk_used = tsDataSpace.size.used / (1024 * 1024 * 1024.0); + pInfo->disk_total = tsDataSpace.size.avail / (1024 * 1024 * 1024.0); + taosGetCardInfo(NULL, &pInfo->net_in, &pInfo->net_out); taosGetProcIO(&pInfo->io_read, &pInfo->io_write); pInfo->io_read_disk = 0; pInfo->io_write_disk = 0; - pInfo->req_select = 8; - pInfo->req_select_rate = 8.1; - pInfo->req_insert = 9; - pInfo->req_insert_success = 10; - pInfo->req_insert_rate = 10.1; - pInfo->req_insert_batch = 11; - pInfo->req_insert_batch_success = 12; - pInfo->req_insert_batch_rate = 12.3; - pInfo->errors = 4; - pInfo->vnodes_num = 5; - pInfo->masters = 6; - pInfo->has_mnode = 1; + pInfo->req_select = 0; + pInfo->req_select_rate = 0; + pInfo->req_insert = 0; + pInfo->req_insert_success = 0; + pInfo->req_insert_rate = 0; + pInfo->req_insert_batch = 0; + pInfo->req_insert_batch_success = 0; + pInfo->req_insert_batch_rate = 0; + pInfo->errors = 0; + pInfo->vnodes_num = 0; + pInfo->masters = 0; + pInfo->has_mnode = dndIsMnode(pDnode); } static void dndSendMonitorReport(SDnode *pDnode) { diff --git a/source/dnode/mgmt/impl/src/dndMnode.c b/source/dnode/mgmt/impl/src/dndMnode.c index 6cb117867f..47e74b5c57 100644 --- a/source/dnode/mgmt/impl/src/dndMnode.c +++ b/source/dnode/mgmt/impl/src/dndMnode.c @@ -639,4 +639,11 @@ int32_t dndGetMnodeMonitorInfo(SDnode *pDnode, SMonClusterInfo *pClusterInfo, SM int32_t code = mndGetMonitorInfo(pMnode, pClusterInfo, pVgroupInfo, pGrantInfo); dndReleaseMnode(pDnode, pMnode); return code; +} + +int8_t dndIsMnode(SDnode *pDnode) { + SMnode *pMnode = dndAcquireMnode(pDnode); + if (pMnode == NULL) return 0; + dndReleaseMnode(pDnode, pMnode); + return 1; } \ No newline at end of file diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 3683943c61..c674aeaca2 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -112,16 +112,16 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { } } -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { if (bytes) *bytes = 0; if (rbytes) *rbytes = 0; if (tbytes) *tbytes = 0; - return true; + return 0; } -bool taosGetBandSpeed(float *bandSpeedKb) { +int32_t taosGetBandSpeed(float *bandSpeedKb) { *bandSpeedKb = 0; - return true; + return 0; } int32_t taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { @@ -271,16 +271,16 @@ int32_t taosGetProcIO(float *readKB, float *writeKB) { return 0; } -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { if (bytes) *bytes = 0; if (rbytes) *rbytes = 0; if (tbytes) *tbytes = 0; - return true; + return 0; } -bool taosGetBandSpeed(float *bandSpeedKb) { +int32_t taosGetBandSpeed(float *bandSpeedKb) { *bandSpeedKb = 0; - return true; + return 0; } int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { @@ -544,13 +544,13 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { } } -bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - *bytes = 0; +int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { + if (bytes) *bytes = 0; // FILE *fp = fopen(tsSysNetFile, "r"); TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsSysNetFile); - return false; + return -1; } ssize_t _bytes = 0; @@ -586,37 +586,37 @@ bool taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets); if (rbytes) *rbytes = o_rbytes; if (tbytes) *tbytes = o_tbytes; - *bytes += (o_rbytes + o_tbytes); + if (bytes) *bytes += (o_rbytes + o_tbytes); } if (line != NULL) tfree(line); taosCloseFile(&pFile); - return true; + return 0; } -bool taosGetBandSpeed(float *bandSpeedKb) { +int32_t taosGetBandSpeed(float *bandSpeedKb) { static int64_t lastBytes = 0; static time_t lastTime = 0; int64_t curBytes = 0; time_t curTime = time(NULL); - if (!taosGetCardInfo(&curBytes, NULL, NULL)) { - return false; + if (taosGetCardInfo(&curBytes, NULL, NULL) != 0) { + return -1; } if (lastTime == 0 || lastBytes == 0) { lastTime = curTime; lastBytes = curBytes; *bandSpeedKb = 0; - return true; + return 0; } if (lastTime >= curTime || lastBytes > curBytes) { lastTime = curTime; lastBytes = curBytes; *bandSpeedKb = 0; - return true; + return 0; } double totalBytes = (double)(curBytes - lastBytes) / 1024 * 8; // Kb @@ -628,7 +628,7 @@ bool taosGetBandSpeed(float *bandSpeedKb) { lastTime = curTime; lastBytes = curBytes; - return true; + return 0; } int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) {