From 451019cb6389d47c36ef98e8c3def5714b30326e Mon Sep 17 00:00:00 2001 From: Sandro Kalatozishvili Date: Wed, 20 Mar 2024 02:10:10 +0400 Subject: [PATCH] Updated CPU extra info display logic in xtop --- tools/xtop.c | 57 ++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/tools/xtop.c b/tools/xtop.c index 37342f2..e983659 100644 --- a/tools/xtop.c +++ b/tools/xtop.c @@ -20,17 +20,18 @@ #include "xfs.h" #include "api.h" -#define XTOP_VERSION_MAJ 1 -#define XTOP_VERSION_MIN 1 +#define XTOP_VERSION_MAJ 1 +#define XTOP_VERSION_MIN 2 -#define XTOP_SORT_DISABLE 0 -#define XTOP_SORT_BUSY 1 -#define XTOP_SORT_FREE 2 -#define XTOP_SORT_NAME 3 -#define XTOP_SORT_LEN 4 +#define XTOP_SORT_DISABLE 0 +#define XTOP_SORT_BUSY 1 +#define XTOP_SORT_FREE 2 +#define XTOP_SORT_NAME 3 +#define XTOP_SORT_LEN 4 -#define XTOP_API_URI "/api/all" -#define XTOP_TOTAL_LEN 5 +#define XTOP_API_URI "/api/all" +#define XTOP_TOTAL_LEN 5 +#define XTOP_CPU_EXTRA_DEFAULT ((uint16_t)-1) #define XTOP_CPU_HEADER " "\ "CPU IDL "\ @@ -63,7 +64,6 @@ typedef enum { typedef struct xtop_args_ { xtop_stats_t *pStats; - xbool_t bExcludeCPU; xbool_t bDaemon; xbool_t bServer; xbool_t bClient; @@ -77,6 +77,7 @@ typedef struct xtop_args_ { char sKey[XSTR_MIN]; size_t nIntervalU; + uint16_t nCPUExtraLimit; uint16_t nPort; uint8_t nSort; xpid_t nPID; @@ -122,16 +123,16 @@ void XTOPApp_DisplayUsage(const char *pName) XSTR_FMT_BOLD, XSTR_FMT_RESET, XSTR_CLR_RED, XSTR_FMT_RESET, XSTR_CLR_YELLOW, XSTR_FMT_RESET, XSTR_FMT_DIM, XSTR_FMT_RESET, XSTR_FMT_BOLD, XSTR_FMT_RESET); - printf("Usage: %s [-i ] [-m ] [-t ] [-u ]\n", pName); - printf(" %s [-a ] [-p ] [-l ] [-d] [-s] [-e]\n", XTOPApp_WhiteSpace(nLength)); + printf("Usage: %s [-e ] [-i ] [-m ] [-t ]\n", pName); + printf(" %s [-a ] [-p ] [-l ] [-u ] [-d] [-s]\n", XTOPApp_WhiteSpace(nLength)); printf(" %s [-U ] [-P ] [-K ] [-c] [-v] [-h]\n\n", XTOPApp_WhiteSpace(nLength)); printf("Options are:\n"); + printf(" %s-e%s # Limit extra CPU info to given number\n", XSTR_CLR_CYAN, XSTR_FMT_RESET); printf(" %s-i%s # Interface name to display on top\n", XSTR_CLR_CYAN, XSTR_FMT_RESET); printf(" %s-m%s # Monitoring interval seconds\n", XSTR_CLR_CYAN, XSTR_FMT_RESET); printf(" %s-t%s # Sort result by selected type%s*%s\n", XSTR_CLR_CYAN, XSTR_FMT_RESET, XSTR_CLR_RED, XSTR_FMT_RESET); printf(" %s-u%s # Track process CPU and memory usage\n", XSTR_CLR_CYAN, XSTR_FMT_RESET); - printf(" %s-e%s # Exclude additional CPU info\n", XSTR_CLR_CYAN, XSTR_FMT_RESET); printf(" %s-h%s # Print version and usage\n\n", XSTR_CLR_CYAN, XSTR_FMT_RESET); printf("%sXTOP has a REST API server and client mode to send%s\n", XSTR_FMT_DIM, XSTR_FMT_RESET); @@ -172,7 +173,6 @@ uint8_t XTOPApp_GetSortType(const char *pArg) int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[]) { - pArgs->bExcludeCPU = XFALSE; pArgs->bDaemon = XFALSE; pArgs->bServer = XFALSE; pArgs->bClient = XFALSE; @@ -186,6 +186,7 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[]) xstrnul(pArgs->sToken); xstrnul(pArgs->sKey); + pArgs->nCPUExtraLimit = XTOP_CPU_EXTRA_DEFAULT; pArgs->nIntervalU = 0; pArgs->nPort = 0; pArgs->nPID = 0; @@ -196,7 +197,7 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[]) xbool_t bVerbose = XFALSE; int nChar = 0; - while ((nChar = getopt(argc, argv, "a:i:K:U:P:l:m:p:t:u:c1:d1:s1:e1:d1:v1:h1")) != -1) + while ((nChar = getopt(argc, argv, "a:e:i:K:U:P:l:m:p:t:u:c1:d1:s1:d1:v1:h1")) != -1) { switch (nChar) { @@ -221,6 +222,9 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[]) case 't': pArgs->nSort = XTOPApp_GetSortType(optarg); break; + case 'e': + pArgs->nCPUExtraLimit = atoi(optarg); + break; case 'm': pArgs->nIntervalU = atoi(optarg); break; @@ -230,9 +234,6 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[]) case 'u': pArgs->nPID = atoi(optarg); break; - case 'e': - pArgs->bExcludeCPU = XTRUE; - break; case 'c': pArgs->bClient = XTRUE; break; @@ -311,9 +312,9 @@ int XTOPApp_CompareCPUs(const void *pData1, const void *pData2, void *pContext) xcpu_info_t *pInfo1 = (xcpu_info_t*)((xarray_data_t*)pData1)->pData; xcpu_info_t *pInfo2 = (xcpu_info_t*)((xarray_data_t*)pData2)->pData; - return (pArgs->nSort == XTOP_SORT_BUSY) ? - (int)pInfo1->nIdleTime - (int)pInfo2->nIdleTime: - (int)pInfo2->nIdleTime - (int)pInfo1->nIdleTime; + return (pArgs->nSort == XTOP_SORT_FREE) ? + (int)pInfo2->nIdleTime - (int)pInfo1->nIdleTime: + (int)pInfo1->nIdleTime - (int)pInfo2->nIdleTime; } int XTOPApp_CompareIFaces(const void *pData1, const void *pData2, void *pContext) @@ -656,13 +657,17 @@ XSTATUS XTOPApp_AddCPUExtra(xcli_wind_t *pWin, xtop_args_t *pArgs, xcli_bar_t *p XSTATUS nStatus = XTOPApp_AddCPUInfo(pWin, &pCPU->sum); if (nStatus <= 0) return nStatus; - if (pArgs->nSort && pCPU->nCoreCount && + uint16_t i, nCount = pCPU->nCoreCount; + if (pArgs->nCPUExtraLimit != XTOP_CPU_EXTRA_DEFAULT) + nCount = XSTD_MIN(pCPU->nCoreCount, pArgs->nCPUExtraLimit); + + if ((pArgs->nSort && pCPU->nCoreCount && pArgs->nSort != XTOP_SORT_NAME && - pArgs->nSort != XTOP_SORT_LEN) + pArgs->nSort != XTOP_SORT_LEN) || + nCount != pCPU->nCoreCount) XArray_Sort(&pCPU->cores, XTOPApp_CompareCPUs, pArgs); - uint16_t i; - for (i = 0; i < pCPU->nCoreCount; i++) + for (i = 0; i < nCount; i++) { xcpu_info_t *pCore = (xcpu_info_t*)XArray_GetData(&pCPU->cores, i); if (pCore != NULL) nStatus = XTOPApp_AddCPUInfo(pWin, pCore); @@ -1564,7 +1569,7 @@ int main(int argc, char *argv[]) XTOPApp_AddCPULoadBar(&win, &bar, &cpuStats); XTOPApp_AddOverallBar(&win, &bar, &memInfo, &cpuStats); - if (!args.bExcludeCPU) + if (args.nCPUExtraLimit > 0) { XWindow_AddEmptyLine(&win); XTOPApp_AddCPUExtra(&win, &args, &bar, &memInfo, &cpuStats);