From 09be95e40463c51ccc2094e4b88bb51d42ac57a4 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 5 Mar 2022 10:23:16 +0800 Subject: [PATCH 1/2] sysinfo for monitor --- include/libs/monitor/monitor.h | 10 +-- include/os/osSysinfo.h | 4 +- source/dnode/mgmt/impl/inc/dndEnv.h | 1 + source/dnode/mgmt/impl/src/dndMgmt.c | 14 ++-- source/dnode/mgmt/impl/src/dndVnodes.c | 7 ++ source/os/src/osSysinfo.c | 108 +++++++++++-------------- 6 files changed, 69 insertions(+), 75 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 0041f3ac5f..19b4ad6dc6 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -92,11 +92,11 @@ typedef struct { 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 - int64_t net_in; - int64_t net_out; + int64_t disk_engine; // Byte + int64_t disk_used; // Byte + int64_t disk_total; // Byte + int64_t net_in; // Byte + int64_t net_out; // Byte float io_read; float io_write; float io_read_disk; diff --git a/include/os/osSysinfo.h b/include/os/osSysinfo.h index e14dba8269..da0a801290 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); -int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars); -int32_t taosGetProcIO(float *readKB, float *writeKB); +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes); +int32_t taosGetProcIOSpeed(float *readKB, float *writeKB, float *readDiskKB, float *writeDiskKB); int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); int32_t taosGetBandSpeed(float *bandSpeedKb); diff --git a/source/dnode/mgmt/impl/inc/dndEnv.h b/source/dnode/mgmt/impl/inc/dndEnv.h index efe54ee1e5..b9acbea02f 100644 --- a/source/dnode/mgmt/impl/inc/dndEnv.h +++ b/source/dnode/mgmt/impl/inc/dndEnv.h @@ -108,6 +108,7 @@ typedef struct { SHashObj *hash; int32_t openVnodes; int32_t totalVnodes; + int32_t masterNum; SRWLatch latch; SQWorkerPool queryPool; SFWorkerPool fetchPool; diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 60cfdc299c..0a3a39e830 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -487,12 +487,10 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { taosGetSysMemory(&pInfo->mem_system); pInfo->mem_total = tsTotalMemoryKB; pInfo->disk_engine = 0; - pInfo->disk_used = tsDataSpace.size.used / (1024 * 1024 * 1024.0); - pInfo->disk_total = tsDataSpace.size.avail / (1024 * 1024 * 1024.0); + pInfo->disk_used = tsDataSpace.size.used; + pInfo->disk_total = tsDataSpace.size.avail; 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; + taosGetProcIOSpeed(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk); pInfo->req_select = 0; pInfo->req_select_rate = 0; pInfo->req_insert = 0; @@ -501,9 +499,9 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { 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->errors = tsNumOfErrorLogs; + pInfo->vnodes_num = pDnode->vmgmt.totalVnodes; + pInfo->masters = pDnode->vmgmt.masterNum; pInfo->has_mnode = dndIsMnode(pDnode); } diff --git a/source/dnode/mgmt/impl/src/dndVnodes.c b/source/dnode/mgmt/impl/src/dndVnodes.c index 1c7bac4b6b..28bc615aba 100644 --- a/source/dnode/mgmt/impl/src/dndVnodes.c +++ b/source/dnode/mgmt/impl/src/dndVnodes.c @@ -17,6 +17,7 @@ #include "dndVnodes.h" #include "dndMgmt.h" #include "dndTransport.h" +#include "sync.h" typedef struct { int32_t vgId; @@ -979,6 +980,8 @@ void dndCleanupVnodes(SDnode *pDnode) { void dndGetVnodeLoads(SDnode *pDnode, SArray *pLoads) { SVnodesMgmt *pMgmt = &pDnode->vmgmt; + int32_t totalVnodes = 0; + int32_t masterNum = 0; taosRLockLatch(&pMgmt->latch); @@ -993,8 +996,12 @@ void dndGetVnodeLoads(SDnode *pDnode, SArray *pLoads) { vnodeGetLoad(pVnode->pImpl, &vload); taosArrayPush(pLoads, &vload); + totalVnodes++; + if (vload.role == TAOS_SYNC_STATE_LEADER) masterNum++; pIter = taosHashIterate(pMgmt->hash, pIter); } taosRUnLockLatch(&pMgmt->latch); + pMgmt->totalVnodes = totalVnodes; + pMgmt->masterNum = masterNum; } diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 12952141a4..73b64b5319 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -124,52 +124,26 @@ int32_t taosGetBandSpeed(float *bandSpeedKb) { return 0; } -int32_t taosReadProcIO(int64_t *readbyte, int64_t *writebyte) { +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { IO_COUNTERS io_counter; if (GetProcessIoCounters(GetCurrentProcess(), &io_counter)) { - if (readbyte) *readbyte = io_counter.ReadTransferCount; - if (writebyte) *writebyte = io_counter.WriteTransferCount; + if (rchars) *rchars = io_counter.ReadTransferCount; + if (wchars) *wchars = io_counter.WriteTransferCount; + if (read_bytes) *read_bytes = 0; + if (write_bytes) *write_bytes = 0; return 0; } return -1; } -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) != 0) { - return -1; - } - - if (lastReadbyte == -1 || lastWritebyte == -1) { - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; - return -1; - } - - *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); - *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); - if (*readKB < 0) *readKB = 0; - if (*writeKB < 0) *writeKB = 0; - - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; - - return 0; -} - void taosGetSystemInfo() { taosGetCpuCores(&tsNumOfCores); taosGetTotalMemory(&tsTotalMemoryKB); - float tmp1, tmp2; + float tmp1, tmp2, tmp3, tmp4; taosGetBandSpeed(&tmp1); taosGetCpuUsage(&tmp1, &tmp2); - taosGetProcIO(&tmp1, &tmp2); + taosGetProcIOSpeed(&tmp1, &tmp2, &tmp3, &tmp4); } void taosKillSystem() { @@ -259,15 +233,11 @@ void taosGetSystemInfo() { tsNumOfCores = sysconf(_SC_NPROCESSORS_ONLN); } -int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { if (rchars) *rchars = 0; if (wchars) *wchars = 0; - return 0; -} - -int32_t taosGetProcIO(float *readKB, float *writeKB) { - *readKB = 0; - *writeKB = 0; + if (read_bytes) *read_bytes = 0; + if (write_bytes) *write_bytes = 0; return 0; } @@ -631,8 +601,7 @@ int32_t taosGetBandSpeed(float *bandSpeedKb) { return 0; } -int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { - // FILE *fp = fopen(tsProcIOFile, "r"); +int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // printf("open file:%s failed", tsProcIOFile); @@ -655,16 +624,22 @@ int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { } else if (strstr(line, "wchar:") != NULL) { sscanf(line, "%s %" PRId64, tmp, wchars); readIndex++; - } else { + } if (strstr(line, "read_bytes:") != NULL) { + sscanf(line, "%s %" PRId64, tmp, read_bytes); + readIndex++; + } else if (strstr(line, "write_bytes::") != NULL) { + sscanf(line, "%s %" PRId64, tmp, write_bytes); + readIndex++; + }else { } - if (readIndex >= 2) break; + if (readIndex >= 4) break; } if (line != NULL) tfree(line); taosCloseFile(&pFile); - if (readIndex < 2) { + if (readIndex < 4) { // printf("read file:%s failed", tsProcIOFile); return -1; } @@ -672,30 +647,43 @@ int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars) { return 0; } -int32_t taosGetProcIO(float *readKB, float *writeKB) { - static int64_t lastReadbyte = -1; - static int64_t lastWritebyte = -1; +int32_t taosGetProcIOSpeed(float *readKB, float *writeKB, float *readDiskKB, float *writeDiskKB) { + static int64_t lastRchar = -1; + static int64_t lastWchar = -1; + static int64_t lastRbyte = -1; + static int64_t lastWbyte = -1; - int64_t curReadbyte = 0; - int64_t curWritebyte = 0; + int64_t curRchar = 0; + int64_t curWchar = 0; + int64_t curRbyte = 0; + int64_t curWbyte = 0; - if (taosReadProcIO(&curReadbyte, &curWritebyte) != 0) { + if (taosReadProcIO(&curRchar, &curWchar, &curRbyte, &curWbyte) != 0) { return -1; } - if (lastReadbyte == -1 || lastWritebyte == -1) { - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; + if (lastRchar == -1 || lastWchar == -1 || lastRbyte == -1 || lastWbyte == -1) { + lastRchar = curRchar; + lastWchar = curWchar; + lastRbyte = curRbyte; + lastWbyte = curWbyte; return -1; } - *readKB = (float)((double)(curReadbyte - lastReadbyte) / 1024); - *writeKB = (float)((double)(curWritebyte - lastWritebyte) / 1024); + *readKB = (curRchar - lastRchar) / 1024.0f; + *writeKB = (curWchar - lastWchar) / 1024.0f; + *readDiskKB = (curRbyte - lastRbyte) / 1024.0f; + *writeDiskKB = (curWbyte - lastWbyte) / 1024.0f; + if (*readKB < 0) *readKB = 0; if (*writeKB < 0) *writeKB = 0; + if (*readDiskKB < 0) *readDiskKB = 0; + if (*writeDiskKB < 0) *writeDiskKB = 0; - lastReadbyte = curReadbyte; - lastWritebyte = curWritebyte; + lastRchar = curRchar; + lastWchar = curWchar; + lastRbyte = curRbyte; + lastWbyte = curWbyte; return 0; } @@ -705,10 +693,10 @@ void taosGetSystemInfo() { taosGetCpuCores(&tsNumOfCores); taosGetTotalMemory(&tsTotalMemoryKB); - float tmp1, tmp2; + float tmp1, tmp2, tmp3, tmp4; taosGetBandSpeed(&tmp1); taosGetCpuUsage(&tmp1, &tmp2); - taosGetProcIO(&tmp1, &tmp2); + taosGetProcIOSpeed(&tmp1, &tmp2, &tmp3, &tmp4); } void taosKillSystem() { From cde33ee27a3b0ad07080fcc6c7fa1a3e133e8896 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 5 Mar 2022 11:56:00 +0800 Subject: [PATCH 2/2] io and band --- include/libs/monitor/monitor.h | 28 ++--- include/os/osSysinfo.h | 8 +- source/dnode/mgmt/impl/src/dndMgmt.c | 6 +- source/os/src/osSysinfo.c | 182 ++++++++++++--------------- 4 files changed, 101 insertions(+), 123 deletions(-) diff --git a/include/libs/monitor/monitor.h b/include/libs/monitor/monitor.h index 19b4ad6dc6..1695edd983 100644 --- a/include/libs/monitor/monitor.h +++ b/include/libs/monitor/monitor.h @@ -86,21 +86,21 @@ typedef struct { typedef struct { float uptime; // day - float cpu_engine; - float cpu_system; + double cpu_engine; + double cpu_system; float cpu_cores; - int64_t mem_engine; // KB - int64_t mem_system; // KB - int64_t mem_total; // KB - int64_t disk_engine; // Byte - int64_t disk_used; // Byte - int64_t disk_total; // Byte - int64_t net_in; // Byte - int64_t net_out; // Byte - float io_read; - float io_write; - float io_read_disk; - float io_write_disk; + int64_t mem_engine; // KB + int64_t mem_system; // KB + int64_t mem_total; // KB + int64_t disk_engine; // Byte + int64_t disk_used; // Byte + int64_t disk_total; // Byte + double net_in; // bytes per second + double net_out; // bytes per second + double io_read; + double io_write; + double io_read_disk; + double 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 da0a801290..9081fa9715 100644 --- a/include/os/osSysinfo.h +++ b/include/os/osSysinfo.h @@ -38,15 +38,15 @@ int32_t taosGetEmail(char *email, int32_t maxLen); int32_t taosGetOsReleaseName(char *releaseName, int32_t maxLen); 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 taosGetCpuUsage(double *cpu_system, double *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); int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes); -int32_t taosGetProcIOSpeed(float *readKB, float *writeKB, float *readDiskKB, float *writeDiskKB); -int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes); -int32_t taosGetBandSpeed(float *bandSpeedKb); +int32_t taosGetIOSpeed(double *readKB, double *writeKB, double *readDiskKB, double *writeDiskKB); +int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes); +int32_t taosGetBandSpeed(double *receive_bytes_per_sec, double *transmit_bytes_per_sec); int32_t taosSystem(const char *cmd); void taosKillSystem(); diff --git a/source/dnode/mgmt/impl/src/dndMgmt.c b/source/dnode/mgmt/impl/src/dndMgmt.c index 0a3a39e830..ff9eb5d884 100644 --- a/source/dnode/mgmt/impl/src/dndMgmt.c +++ b/source/dnode/mgmt/impl/src/dndMgmt.c @@ -488,9 +488,9 @@ static void dndGetMonitorDnodeInfo(SDnode *pDnode, SMonDnodeInfo *pInfo) { pInfo->mem_total = tsTotalMemoryKB; pInfo->disk_engine = 0; pInfo->disk_used = tsDataSpace.size.used; - pInfo->disk_total = tsDataSpace.size.avail; - taosGetCardInfo(NULL, &pInfo->net_in, &pInfo->net_out); - taosGetProcIOSpeed(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk); + pInfo->disk_total = tsDataSpace.size.total; + taosGetBandSpeed(&pInfo->net_in, &pInfo->net_out); + taosGetIOSpeed(&pInfo->io_read, &pInfo->io_write, &pInfo->io_read_disk, &pInfo->io_write_disk); pInfo->req_select = 0; pInfo->req_select_rate = 0; pInfo->req_insert = 0; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index 73b64b5319..8f2820f67a 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -87,7 +87,7 @@ int32_t taosGetCpuCores(float *numOfCores) { return 0; } -int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(double *sysCpuUsage, double *procCpuUsage) { *sysCpuUsage = 0; *procCpuUsage = 0; return 0; @@ -112,15 +112,9 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { } } -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 0; -} - -int32_t taosGetBandSpeed(float *bandSpeedKb) { - *bandSpeedKb = 0; +int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { + *receive_bytes = 0; + *transmit_bytes = 0; return 0; } @@ -140,10 +134,10 @@ void taosGetSystemInfo() { taosGetCpuCores(&tsNumOfCores); taosGetTotalMemory(&tsTotalMemoryKB); - float tmp1, tmp2, tmp3, tmp4; - taosGetBandSpeed(&tmp1); + double tmp1, tmp2, tmp3, tmp4; + taosGetBandSpeed(&tmp1, &tmp2); taosGetCpuUsage(&tmp1, &tmp2); - taosGetProcIOSpeed(&tmp1, &tmp2, &tmp3, &tmp4); + taosGetIOSpeed(&tmp1, &tmp2, &tmp3, &tmp4); } void taosKillSystem() { @@ -241,19 +235,13 @@ int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, in return 0; } -int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - if (bytes) *bytes = 0; - if (rbytes) *rbytes = 0; - if (tbytes) *tbytes = 0; +int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { + *receive_bytes = 0; + *transmit_bytes = 0; return 0; } -int32_t taosGetBandSpeed(float *bandSpeedKb) { - *bandSpeedKb = 0; - return 0; -} - -int32_t taosGetCpuUsage(float *sysCpuUsage, float *procCpuUsage) { +int32_t taosGetCpuUsage(double *sysCpuUsage, double *procCpuUsage) { *sysCpuUsage = 0; *procCpuUsage = 0; return 0; @@ -370,7 +358,6 @@ int32_t taosGetSysMemory(int64_t *usedKB) { } 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); @@ -404,7 +391,6 @@ int32_t taosGetProcMemory(int64_t *usedKB) { } 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); @@ -429,7 +415,6 @@ static int32_t taosGetSysCpuInfo(SysCpuInfo *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); @@ -463,7 +448,7 @@ int32_t taosGetCpuCores(float *numOfCores) { return 0; } -int32_t taosGetCpuUsage(float *cpu_system, float *cpu_engine) { +int32_t taosGetCpuUsage(double *cpu_system, double *cpu_engine) { static uint64_t lastSysUsed = 0; static uint64_t lastSysTotal = 0; static uint64_t lastProcTotal = 0; @@ -492,8 +477,8 @@ int32_t taosGetCpuUsage(float *cpu_system, float *cpu_engine) { return -1; } - *cpu_engine = (float)((double)(curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100); - *cpu_system = (float)((double)(curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100); + *cpu_engine = (curSysUsed - lastSysUsed) / (double)(curSysTotal - lastSysTotal) * 100; + *cpu_system = (curProcTotal - lastProcTotal) / (double)(curSysTotal - lastSysTotal) * 100; lastSysUsed = curSysUsed; lastSysTotal = curSysTotal; @@ -514,14 +499,9 @@ int32_t taosGetDiskSize(char *dataDir, SDiskSize *diskSize) { } } -int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { - if (bytes) *bytes = 0; - // FILE *fp = fopen(tsSysNetFile, "r"); +int32_t taosGetCardInfo(int64_t *receive_bytes, int64_t *transmit_bytes) { TdFilePtr pFile = taosOpenFile(tsSysNetFile, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - // printf("open file:%s failed", tsSysNetFile); - return -1; - } + if (pFile == NULL) return -1; ssize_t _bytes = 0; char *line = NULL; @@ -554,9 +534,8 @@ int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { "%s %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64 " %" PRId64, nouse0, &o_rbytes, &rpackts, &nouse1, &nouse2, &nouse3, &nouse4, &nouse5, &nouse6, &o_tbytes, &tpackets); - if (rbytes) *rbytes = o_rbytes; - if (tbytes) *tbytes = o_tbytes; - if (bytes) *bytes += (o_rbytes + o_tbytes); + *receive_bytes = o_rbytes; + *transmit_bytes = o_tbytes; } if (line != NULL) tfree(line); @@ -565,57 +544,52 @@ int32_t taosGetCardInfo(int64_t *bytes, int64_t *rbytes, int64_t *tbytes) { return 0; } -int32_t taosGetBandSpeed(float *bandSpeedKb) { - static int64_t lastBytes = 0; - static time_t lastTime = 0; - int64_t curBytes = 0; - time_t curTime = time(NULL); +int32_t taosGetBandSpeed(double *receive_bytes_per_sec, double *transmit_bytes_per_sec) { + static int64_t last_receive_bytes = 0; + static int64_t last_transmit_bytes = 0; + static int64_t last_time = 0; + int64_t cur_receive_bytes = 0; + int64_t cur_transmit_bytes = 0; + int64_t cur_time = taosGetTimestampMs(); - if (taosGetCardInfo(&curBytes, NULL, NULL) != 0) { + if (taosGetCardInfo(&cur_receive_bytes, &cur_transmit_bytes) != 0) { return -1; } - if (lastTime == 0 || lastBytes == 0) { - lastTime = curTime; - lastBytes = curBytes; - *bandSpeedKb = 0; + if (last_time == 0 || last_time >= cur_time) { + last_time = cur_time; + last_receive_bytes = cur_receive_bytes; + last_transmit_bytes = cur_transmit_bytes; + *receive_bytes_per_sec = 0; + *transmit_bytes_per_sec = 0; return 0; } - if (lastTime >= curTime || lastBytes > curBytes) { - lastTime = curTime; - lastBytes = curBytes; - *bandSpeedKb = 0; - return 0; - } + *receive_bytes_per_sec = (cur_receive_bytes - last_receive_bytes) / (double)(cur_time - last_time) * 1000; + *transmit_bytes_per_sec = (cur_transmit_bytes - last_transmit_bytes) / (double)(cur_time - last_time) * 1000; - double totalBytes = (double)(curBytes - lastBytes) / 1024 * 8; // Kb - *bandSpeedKb = (float)(totalBytes / (double)(curTime - lastTime)); + last_time = cur_time; + last_transmit_bytes = cur_transmit_bytes; + last_receive_bytes = cur_receive_bytes; - // //printf("bandwidth lastBytes:%ld, lastTime:%ld, curBytes:%ld, curTime:%ld, - // speed:%f", lastBytes, lastTime, curBytes, curTime, *bandSpeed); - - lastTime = curTime; - lastBytes = curBytes; + if (*receive_bytes_per_sec < 0) *receive_bytes_per_sec = 0; + if (*transmit_bytes_per_sec < 0) *transmit_bytes_per_sec = 0; return 0; } int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, int64_t *write_bytes) { TdFilePtr pFile = taosOpenFile(tsProcIOFile, TD_FILE_READ | TD_FILE_STREAM); - if (pFile == NULL) { - // printf("open file:%s failed", tsProcIOFile); - return -1; - } + if (pFile == NULL) return -1; ssize_t _bytes = 0; char *line = NULL; - char tmp[10]; + char tmp[24]; int readIndex = 0; while (!taosEOFFile(pFile)) { _bytes = taosGetLineFile(pFile, &line); - if ((_bytes < 0) || (line == NULL)) { + if (_bytes < 10 || line == NULL) { break; } if (strstr(line, "rchar:") != NULL) { @@ -624,13 +598,13 @@ int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, in } else if (strstr(line, "wchar:") != NULL) { sscanf(line, "%s %" PRId64, tmp, wchars); readIndex++; - } if (strstr(line, "read_bytes:") != NULL) { + } else if (strstr(line, "read_bytes:") != NULL) { // read_bytes sscanf(line, "%s %" PRId64, tmp, read_bytes); readIndex++; - } else if (strstr(line, "write_bytes::") != NULL) { + } else if (strstr(line, "write_bytes:") != NULL) { // write_bytes sscanf(line, "%s %" PRId64, tmp, write_bytes); readIndex++; - }else { + } else { } if (readIndex >= 4) break; @@ -640,50 +614,54 @@ int32_t taosReadProcIO(int64_t *rchars, int64_t *wchars, int64_t *read_bytes, in taosCloseFile(&pFile); if (readIndex < 4) { - // printf("read file:%s failed", tsProcIOFile); return -1; } return 0; } -int32_t taosGetProcIOSpeed(float *readKB, float *writeKB, float *readDiskKB, float *writeDiskKB) { - static int64_t lastRchar = -1; - static int64_t lastWchar = -1; - static int64_t lastRbyte = -1; - static int64_t lastWbyte = -1; +int32_t taosGetIOSpeed(double *rchar_per_sec, double *wchar_per_sec, double *read_bytes_per_sec, + double *write_bytes_per_sec) { + static int64_t last_rchar = -1; + static int64_t last_wchar = -1; + static int64_t last_read_bytes = -1; + static int64_t last_write_bytes = -1; + static int64_t last_time = 0; - int64_t curRchar = 0; - int64_t curWchar = 0; - int64_t curRbyte = 0; - int64_t curWbyte = 0; + int64_t cur_rchar = 0; + int64_t cur_wchar = 0; + int64_t cur_read_bytes = 0; + int64_t cur_write_bytes = 0; + int64_t cur_time = taosGetTimestampMs(); - if (taosReadProcIO(&curRchar, &curWchar, &curRbyte, &curWbyte) != 0) { + if (taosReadProcIO(&cur_rchar, &cur_wchar, &cur_read_bytes, &cur_write_bytes) != 0) { return -1; } - if (lastRchar == -1 || lastWchar == -1 || lastRbyte == -1 || lastWbyte == -1) { - lastRchar = curRchar; - lastWchar = curWchar; - lastRbyte = curRbyte; - lastWbyte = curWbyte; + if (last_time == 0 || last_time >= cur_time) { + last_time = cur_time; + last_rchar = cur_rchar; + last_wchar = cur_wchar; + last_read_bytes = cur_read_bytes; + last_write_bytes = cur_write_bytes; return -1; } - *readKB = (curRchar - lastRchar) / 1024.0f; - *writeKB = (curWchar - lastWchar) / 1024.0f; - *readDiskKB = (curRbyte - lastRbyte) / 1024.0f; - *writeDiskKB = (curWbyte - lastWbyte) / 1024.0f; + *rchar_per_sec = (cur_rchar - last_rchar) / (double)(cur_time - last_time) * 1000; + *wchar_per_sec = (cur_wchar - last_wchar) / (double)(cur_time - last_time) * 1000; + *read_bytes_per_sec = (cur_read_bytes - last_read_bytes) / (double)(cur_time - last_time) * 1000; + *write_bytes_per_sec = (cur_write_bytes - last_write_bytes) / (double)(cur_time - last_time) * 1000; - if (*readKB < 0) *readKB = 0; - if (*writeKB < 0) *writeKB = 0; - if (*readDiskKB < 0) *readDiskKB = 0; - if (*writeDiskKB < 0) *writeDiskKB = 0; + last_time = cur_time; + last_rchar = cur_rchar; + last_wchar = cur_wchar; + last_read_bytes = cur_read_bytes; + last_write_bytes = cur_write_bytes; - lastRchar = curRchar; - lastWchar = curWchar; - lastRbyte = curRbyte; - lastWbyte = curWbyte; + if (*rchar_per_sec < 0) *rchar_per_sec = 0; + if (*wchar_per_sec < 0) *wchar_per_sec = 0; + if (*read_bytes_per_sec < 0) *read_bytes_per_sec = 0; + if (*write_bytes_per_sec < 0) *write_bytes_per_sec = 0; return 0; } @@ -693,10 +671,10 @@ void taosGetSystemInfo() { taosGetCpuCores(&tsNumOfCores); taosGetTotalMemory(&tsTotalMemoryKB); - float tmp1, tmp2, tmp3, tmp4; - taosGetBandSpeed(&tmp1); + double tmp1, tmp2, tmp3, tmp4; + taosGetBandSpeed(&tmp1, &tmp2); taosGetCpuUsage(&tmp1, &tmp2); - taosGetProcIOSpeed(&tmp1, &tmp2, &tmp3, &tmp4); + taosGetIOSpeed(&tmp1, &tmp2, &tmp3, &tmp4); } void taosKillSystem() {