Skip to content

Commit 6f45731

Browse files
committed
Improved vertical responsivity in XTOP
1 parent ab6dd3c commit 6f45731

File tree

5 files changed

+56
-15
lines changed

5 files changed

+56
-15
lines changed

src/data/array.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ void XArray_Delete(xarray_t *pArr, size_t nIndex)
357357
{
358358
xarray_data_t *pData = XArray_Get(pArr, nIndex);
359359
if (pData != NULL) XArray_ClearData(pArr, pData);
360+
361+
size_t i;
362+
for (i = nIndex; i < pArr->nUsed; i++)
363+
{
364+
if ((i + 1) >= pArr->nUsed) break;
365+
pArr->pData[i] = pArr->pData[i+1];
366+
}
367+
368+
pArr->pData[--pArr->nUsed] = NULL;
369+
XArray_Realloc(pArr);
360370
}
361371

362372
xarray_data_t* XArray_Set(xarray_t *pArr, size_t nIndex, xarray_data_t *pNewData)

src/sys/xtop.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ int XTop_GetNetworkStats(xtop_stats_t *pStats, xarray_t *pIfaces)
132132
for (i = 0; i < nUsed; i++)
133133
{
134134
xnet_iface_t *pSrcIface = (xnet_iface_t*)XArray_GetData(&pStats->netIfaces, i);
135-
if (pSrcIface == NULL) continue;
135+
if (pSrcIface == NULL || !pSrcIface->bActive) continue;
136136

137137
xnet_iface_t *pDstIface = (xnet_iface_t*)malloc(sizeof(xnet_iface_t));
138138
if (pDstIface == NULL) continue;
@@ -163,16 +163,23 @@ static void XTop_UpdateNetworkStats(xtop_stats_t *pStats)
163163
XSync_Lock(&pStats->netLock);
164164
xarray_t *pIfaces = &pStats->netIfaces;
165165

166+
unsigned int i;
167+
for (i = 0; i < pIfaces->nUsed; i++)
168+
{
169+
xnet_iface_t *pIface = (xnet_iface_t*)XArray_GetData(pIfaces, i);
170+
pIface->bActive = XFALSE;
171+
}
172+
166173
struct dirent *pEntry = readdir(pDir);
167174
while(pEntry != NULL)
168175
{
169176
/* Found an entry, but ignore . and .. */
170177
if (!strcmp(".", pEntry->d_name) ||
171178
!strcmp("..", pEntry->d_name))
172-
{
173-
pEntry = readdir(pDir);
174-
continue;
175-
}
179+
{
180+
pEntry = readdir(pDir);
181+
continue;
182+
}
176183

177184
char sBuffer[XPROC_BUFFER_SIZE];
178185
char sIfacePath[XPATH_MAX];
@@ -262,6 +269,8 @@ static void XTop_UpdateNetworkStats(xtop_stats_t *pStats)
262269
xstrncpy(netIface.sIPAddr, sizeof(netIface.sIPAddr), XNET_IPADDR_DEFAULT);
263270

264271
memcpy(pIface, &netIface, sizeof(xnet_iface_t));
272+
273+
pIface->bActive = XTRUE;
265274
nHaveIface = XTRUE;
266275
}
267276
}
@@ -273,6 +282,7 @@ static void XTop_UpdateNetworkStats(xtop_stats_t *pStats)
273282
if (pNewIface != NULL)
274283
{
275284
memcpy(pNewIface, &netIface, sizeof(xnet_iface_t));
285+
pNewIface->bActive = XTRUE;
276286

277287
if (XAddr_GetIFCIP(pNewIface->sName, pNewIface->sIPAddr, sizeof(pNewIface->sIPAddr)) <= 0)
278288
xstrncpy(pNewIface->sIPAddr, sizeof(pNewIface->sIPAddr), XNET_IPADDR_DEFAULT);
@@ -284,6 +294,14 @@ static void XTop_UpdateNetworkStats(xtop_stats_t *pStats)
284294
pEntry = readdir(pDir);
285295
}
286296

297+
// Remove unused interfaces
298+
for (i = 0; i < pIfaces->nUsed; i++)
299+
{
300+
xnet_iface_t *pIface = (xnet_iface_t*)XArray_GetData(pIfaces, i);
301+
if (pIface == NULL || pIface->bActive) continue;
302+
XArray_Delete(pIfaces, i--);
303+
}
304+
287305
closedir(pDir);
288306
XSync_Unlock(&pStats->netLock);
289307
}

src/sys/xtop.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ typedef struct XNetIface {
4747
int64_t nBytesSent;
4848
int64_t nBandwidth;
4949
int32_t nType;
50+
xbool_t bActive;
5051

5152
char sName[XNAME_MAX];
5253
char sHWAddr[XADDR_MAX];

src/xver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#define XUTILS_VERSION_MAX 2
1414
#define XUTILS_VERSION_MIN 6
15-
#define XUTILS_BUILD_NUMBER 16
15+
#define XUTILS_BUILD_NUMBER 17
1616

1717
#ifdef __cplusplus
1818
extern "C" {

tools/xtop.c

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
#define XTOP_API_URI "/api/all"
3333
#define XTOP_TOTAL_LEN 5
34-
#define XTOP_CPU_EXTRA_DEFAULT ((uint16_t)-1)
34+
#define XTOP_CPU_EXTRA_MIN 2
3535

3636
#define XTOP_CPU_HEADER " "\
3737
"CPU IDL "\
@@ -78,7 +78,8 @@ typedef struct xtop_args_ {
7878
char sKey[XSTR_MIN];
7979

8080
size_t nIntervalU;
81-
uint16_t nCPUExtraLimit;
81+
uint16_t nCPUExtraMin;
82+
uint16_t nIfaceCount;
8283
uint16_t nPort;
8384
uint8_t nSort;
8485
xpid_t nPID;
@@ -129,7 +130,7 @@ void XTOPApp_DisplayUsage(const char *pName)
129130
printf(" %s [-U <user>] [-P <pass>] [-K <key>] [-c] [-v] [-x] [-h]\n\n", XTOPApp_WhiteSpace(nLength));
130131

131132
printf("Options are:\n");
132-
printf(" %s-e%s <count> # Limit extra CPU info to given number\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
133+
printf(" %s-e%s <count> # Minimum count of extra CPU info\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
133134
printf(" %s-i%s <iface> # Interface name to display on top\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
134135
printf(" %s-m%s <seconds> # Monitoring interval seconds\n", XSTR_CLR_CYAN, XSTR_FMT_RESET);
135136
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);
@@ -193,7 +194,8 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[])
193194
xstrnul(pArgs->sToken);
194195
xstrnul(pArgs->sKey);
195196

196-
pArgs->nCPUExtraLimit = XTOP_CPU_EXTRA_DEFAULT;
197+
pArgs->nCPUExtraMin = XTOP_CPU_EXTRA_MIN;
198+
pArgs->nIfaceCount = 0;
197199
pArgs->nIntervalU = 0;
198200
pArgs->nPort = 0;
199201
pArgs->nPID = 0;
@@ -230,7 +232,7 @@ int XTOPApp_ParseArgs(xtop_args_t *pArgs, int argc, char *argv[])
230232
pArgs->nSort = XTOPApp_GetSortType(optarg);
231233
break;
232234
case 'e':
233-
pArgs->nCPUExtraLimit = atoi(optarg);
235+
pArgs->nCPUExtraMin = atoi(optarg);
234236
break;
235237
case 'm':
236238
pArgs->nIntervalU = atoi(optarg);
@@ -667,9 +669,14 @@ XSTATUS XTOPApp_AddCPUExtra(xcli_win_t *pWin, xtop_args_t *pArgs, xcli_bar_t *pB
667669
XSTATUS nStatus = XTOPApp_AddCPUInfo(pWin, &pCPU->sum);
668670
if (nStatus <= 0) return nStatus;
669671

672+
size_t nOccupiedLines = pWin->lines.nUsed + pArgs->nIfaceCount + 3;
670673
uint16_t i, nCount = pCPU->nCoreCount;
671-
if (pArgs->nCPUExtraLimit != XTOP_CPU_EXTRA_DEFAULT)
672-
nCount = XSTD_MIN(pCPU->nCoreCount, pArgs->nCPUExtraLimit);
674+
675+
while (nOccupiedLines + nCount > pWin->frame.nRows)
676+
{
677+
if (nCount <= pArgs->nCPUExtraMin) break;
678+
nCount--;
679+
}
673680

674681
if ((pArgs->nSort && pCPU->nCoreCount &&
675682
pArgs->nSort != XTOP_SORT_NAME &&
@@ -824,6 +831,7 @@ XSTATUS XTOPApp_AddNetworkInfo(xcli_win_t *pWin, xtop_args_t *pArgs, xarray_t *p
824831
xstrncat(sLine, sizeof(sLine), "%s", sRound);
825832

826833
XWindow_AddAligned(pWin, sLine, XSTR_BACK_BLUE, XCLI_LEFT);
834+
pArgs->nIfaceCount = 0;
827835

828836
if (nTrackID >= 0)
829837
{
@@ -835,7 +843,11 @@ XSTATUS XTOPApp_AddNetworkInfo(xcli_win_t *pWin, xtop_args_t *pArgs, xarray_t *p
835843
{
836844
if (nTrackID >= 0 && i == nTrackID) continue;
837845
xnet_iface_t *pIface = (xnet_iface_t*)XArray_GetData(pIfaces, i);
838-
if (pIface != NULL) XTOPApp_AddInterface(pWin, pArgs, nMaxIPLen, pIface, nLength);
846+
if (pIface != NULL)
847+
{
848+
XTOPApp_AddInterface(pWin, pArgs, nMaxIPLen, pIface, nLength);
849+
pArgs->nIfaceCount++;
850+
}
839851
}
840852

841853
nSpacePadding = XTOPApp_GetIfaceSpacePadding(pWin, XFALSE);
@@ -1659,7 +1671,7 @@ int main(int argc, char *argv[])
16591671
XTOPApp_AddCPULoadBar(&win, &bar, &cpuStats);
16601672
XTOPApp_AddOverallBar(&win, &bar, &memInfo, &cpuStats);
16611673

1662-
if (args.nCPUExtraLimit > 0)
1674+
if (args.nCPUExtraMin > 0)
16631675
{
16641676
XWindow_AddEmptyLine(&win);
16651677
XTOPApp_AddCPUExtra(&win, &args, &bar, &memInfo, &cpuStats);

0 commit comments

Comments
 (0)