Skip to content

Commit e104459

Browse files
committed
Finalized XTOP interactive UI changes and increased version
1 parent 38a7f41 commit e104459

File tree

1 file changed

+48
-18
lines changed

1 file changed

+48
-18
lines changed

tools/xtop.c

Lines changed: 48 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "cli.h"
2323

2424
#define XTOP_VERSION_MAJ 1
25-
#define XTOP_VERSION_MIN 9
25+
#define XTOP_VERSION_MIN 10
2626

2727
#define XTOP_SORT_DISABLE 0
2828
#define XTOP_SORT_BUSY 1
@@ -56,6 +56,9 @@ extern char *optarg;
5656
#define XIFACE_NAME_NARROW_PADDING 12
5757
#define XIFACE_NAME_WIDE_PADDING 15
5858

59+
#define XTOP_ACTIVE_IFACES_RESET 0
60+
#define XTOP_CORE_COUNT_RESET -1
61+
5962
#define XTOP_OPT_ON "["XSTR_CLR_GREEN"on"XSTR_FMT_RESET"] "
6063
#define XTOP_OPT_OFF "["XSTR_CLR_RED"off"XSTR_FMT_RESET"]"
6164

@@ -90,7 +93,9 @@ typedef struct xmon_ctx_ {
9093
char sToken[XSTR_MIN];
9194
char sKey[XSTR_MIN];
9295

96+
int nRealCores;
9397
int nCoreCount;
98+
9499
size_t nIntervalU;
95100
uint16_t nCPUExtraMin;
96101
uint16_t nActiveIfaces;
@@ -129,6 +134,7 @@ void XTOP_InitContext(xtop_ctx_t *pCtx)
129134
pCtx->nIfaceCount = 0;
130135
pCtx->nIntervalU = 0;
131136
pCtx->nCoreCount = -1;
137+
pCtx->nRealCores = -1;
132138
pCtx->nPort = 0;
133139
pCtx->nPID = 0;
134140
}
@@ -191,10 +197,19 @@ void XTOP_DisplayHelp(xtop_ctx_t *pCtx)
191197
const char *pScreenRendering = pCtx->bLineByLine ? "[lines]" : "[frame]";
192198
const char *pSortType = XTOP_GetSortTypeName(pCtx->nSort);
193199

200+
if (pCtx->nCoreCount < 0 && pCtx->nRealCores)
201+
pCtx->nCoreCount = pCtx->nRealCores;
202+
203+
char sSpace[XSTR_TINY] = XSTR_INIT;
204+
size_t nSpaceSize = pCtx->nCoreCount >= 10 ? 2 : 3;
205+
nSpaceSize = pCtx->nCoreCount >= 100 ? 1 : nSpaceSize;
206+
nSpaceSize = pCtx->nCoreCount >= 1000 ? 0 : nSpaceSize;
207+
xstrnfill(sSpace, sizeof(sSpace), nSpaceSize, XSTR_SPACE_CHAR);
208+
194209
// S.K. >> Note: Messages may seem not aligned, but they are aligned because of the formatted arguments
195210
printf("Interactive options are:\n");
196-
printf(" %s%s+%s # Increase CPU core count\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET);
197-
printf(" %s%s-%s # Decrease CPU core count\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET);
211+
printf(" %s%s+%s [%d] %s # Increase CPU core count\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET, pCtx->nCoreCount, sSpace);
212+
printf(" %s%s-%s [%d] %s # Decrease CPU core count\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET, pCtx->nCoreCount, sSpace);
198213
printf(" %s%sa%s %s # Toggle - show CPU sum\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET, pShowCpuSum);
199214
printf(" %s%sc%s %s # Toggle - show all CPU cores\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET, pShowAllCPU);
200215
printf(" %s%si%s %s # Toggle - show all network interfaces\n", XSTR_FMT_BOLD, XSTR_CLR_CYAN, XSTR_FMT_RESET, pShowAllIfaces);
@@ -759,6 +774,7 @@ XSTATUS XTOP_AddCPUInfo(xcli_win_t *pWin, xcpu_info_t *pCore)
759774
XSTATUS XTOP_AddCPUExtra(xtop_ctx_t *pCtx, xcli_win_t *pWin, xcli_bar_t *pBar, xmem_info_t *pMemInfo, xcpu_stats_t *pCPU)
760775
{
761776
XCLIWin_AddAligned(pWin, XTOP_CPU_HEADER, XSTR_BACK_BLUE, XCLI_LEFT);
777+
if (pCtx->nRealCores < 0) pCtx->nRealCores = pCPU->nCoreCount;
762778

763779
if (pCtx->bShowCPUSum)
764780
{
@@ -802,6 +818,7 @@ XSTATUS XTOP_AddCPUExtra(xtop_ctx_t *pCtx, xcli_win_t *pWin, xcli_bar_t *pBar, x
802818

803819
static xbool_t XTOP_IsNarrowInterface(xcli_win_t *pWin)
804820
{
821+
// Below 102 columns we can not fit all interface data without truncation
805822
return (pWin->frame.nColumns < 102) ? XTRUE : XFALSE;
806823
}
807824

@@ -821,6 +838,7 @@ static uint8_t XTOP_GetIfaceSpacePadding(xcli_win_t *pWin, xbool_t bIsHeader)
821838

822839
static uint8_t XTOP_GetAddrSpacePadding(xcli_win_t *pWin, size_t nMaxIPLen)
823840
{
841+
// Calculate space padding for IP and MAC addresses based on window size
824842
uint8_t nSpacePadding = (pWin->frame.nColumns < 112) ? 7 : 8;
825843
nSpacePadding = (pWin->frame.nColumns < 110) ? 6 : nSpacePadding;
826844
nSpacePadding = (pWin->frame.nColumns < 108) ? 5 : nSpacePadding;
@@ -876,8 +894,9 @@ XSTATUS XTOP_AddInterface(xcli_win_t *pWin, xtop_ctx_t *pCtx, size_t nMaxIPLen,
876894

877895
XSTATUS XTOP_IsIfaceValidIP(xnet_iface_t *pIface)
878896
{
879-
if (!xstrused(pIface->sIPAddr) ||
880-
xstrncmp(pIface->sIPAddr, "0.0.0.0", 7))
897+
if (!pIface ||
898+
!xstrused(pIface->sIPAddr) ||
899+
!strncmp(pIface->sIPAddr, "0.0.0.0", 7))
881900
{
882901
return XFALSE;
883902
}
@@ -887,8 +906,9 @@ XSTATUS XTOP_IsIfaceValidIP(xnet_iface_t *pIface)
887906

888907
xbool_t XTOP_HasIfaceValidMac(xnet_iface_t *pIface)
889908
{
890-
if (!xstrused(pIface->sHWAddr) ||
891-
xstrncmp(pIface->sHWAddr, "00:00:00:00:00:00", 17))
909+
if (!pIface ||
910+
!xstrused(pIface->sHWAddr) ||
911+
!strncmp(pIface->sHWAddr, "00:00:00:00:00:00", 17))
892912
{
893913
return XFALSE;
894914
}
@@ -1774,41 +1794,37 @@ static void XTOP_ProcessSTDIN(xtop_ctx_t *pCtx)
17741794

17751795
while (XCLI_GetChar(&c, XTRUE) == XSTDOK)
17761796
{
1797+
xbool_t bValid = XTRUE;
1798+
17771799
if (c == 'c')
17781800
{
17791801
pCtx->bShowAllCPUs = !pCtx->bShowAllCPUs;
1780-
bRedraw = XTRUE;
17811802
bReset = XTRUE;
17821803
}
17831804
else if (c == 'i')
17841805
{
17851806
pCtx->bShowAllIfaces = !pCtx->bShowAllIfaces;
1786-
bRedraw = XTRUE;
17871807
bReset = XTRUE;
17881808
}
17891809
else if (c == 'l')
17901810
{
17911811
pCtx->bLineByLine = !pCtx->bLineByLine;
1792-
bRedraw = XTRUE;
17931812
bReset = XTRUE;
17941813
}
17951814
else if (c == 'x')
17961815
{
17971816
pCtx->bDisplayHeader = !pCtx->bDisplayHeader;
1798-
pCtx->nCoreCount = -1;
1799-
bRedraw = XTRUE;
1817+
pCtx->nCoreCount = XTOP_CORE_COUNT_RESET;
18001818
}
18011819
else if (c == 'a')
18021820
{
18031821
pCtx->bShowCPUSum = !pCtx->bShowCPUSum;
1804-
bRedraw = XTRUE;
18051822
}
18061823
else if (c == 's')
18071824
{
18081825
if (++pCtx->nSort > XTOP_SORT_MAX)
18091826
pCtx->nSort = XTOP_SORT_DISABLE;
18101827

1811-
bRedraw = XTRUE;
18121828
bReset = XTRUE;
18131829
}
18141830
else if (c == 'h')
@@ -1819,26 +1835,40 @@ static void XTOP_ProcessSTDIN(xtop_ctx_t *pCtx)
18191835
else if (c == 'q')
18201836
{
18211837
pCtx->bQuit = XTRUE;
1822-
bRedraw = XTRUE;
18231838
}
18241839
else if (c == '+')
18251840
{
18261841
pCtx->nCoreCount++;
1827-
bRedraw = XTRUE;
1842+
1843+
if (pCtx->nRealCores > 0 &&
1844+
pCtx->nCoreCount > pCtx->nRealCores)
1845+
pCtx->nCoreCount = pCtx->nRealCores;
18281846
}
18291847
else if (c == '-')
18301848
{
18311849
pCtx->nCoreCount--;
1850+
1851+
if (pCtx->nCoreCount < 0)
1852+
pCtx->nCoreCount = 0;
1853+
}
1854+
else
1855+
{
1856+
bValid = XFALSE;
1857+
}
1858+
1859+
if (bValid)
1860+
{
18321861
bRedraw = XTRUE;
18331862
}
18341863
}
18351864

18361865
if (bReset)
18371866
{
1838-
pCtx->nActiveIfaces = 0;
1839-
pCtx->nCoreCount = -1;
1867+
pCtx->nActiveIfaces = XTOP_ACTIVE_IFACES_RESET;
1868+
pCtx->nCoreCount = XTOP_CORE_COUNT_RESET;
18401869
}
18411870

1871+
// Redraw help message only if its enabled and something is changed
18421872
if (bRedraw) pCtx->bRedrawHelp = pCtx->bDisplayHelp;
18431873
}
18441874
#endif

0 commit comments

Comments
 (0)