Skip to content

Commit

Permalink
Updated CPU extra info display logic in xtop
Browse files Browse the repository at this point in the history
  • Loading branch information
kala13x committed Mar 19, 2024
1 parent e3caa4f commit 451019c
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions tools/xtop.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 "\
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 <iface>] [-m <seconds>] [-t <type>] [-u <pid>]\n", pName);
printf(" %s [-a <addr>] [-p <port>] [-l <path>] [-d] [-s] [-e]\n", XTOPApp_WhiteSpace(nLength));
printf("Usage: %s [-e <count>] [-i <iface>] [-m <seconds>] [-t <type>]\n", pName);
printf(" %s [-a <addr>] [-p <port>] [-l <path>] [-u <pid>] [-d] [-s]\n", XTOPApp_WhiteSpace(nLength));
printf(" %s [-U <user>] [-P <pass>] [-K <key>] [-c] [-v] [-h]\n\n", XTOPApp_WhiteSpace(nLength));

printf("Options are:\n");
printf(" %s-e%s <count> # Limit extra CPU info to given number\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-i%s <iface> # Interface name to display on top\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-m%s <seconds> # Monitoring interval seconds\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
printf(" %s-t%s <type> # Sort result by selected type%s*%s\n", XSTR_CLR_CYAN, XSTR_FMT_RESET, XSTR_CLR_RED, XSTR_FMT_RESET);
printf(" %s-u%s <pid> # 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);
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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)
{
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 451019c

Please sign in to comment.