top: refactor 'scale_tics' function for better scaling

This patch refactors the 'scale_tics' function to more
closely parallel uptime shown on the first line of the
summary area. The old logic has been preserved through
the header file's new  #define SCALE_FORMER provision.

However, the former logic was actually a big disaster.
These are some potential problems with that old logic:

1. With respect to our time fields top no longer deals
solely with cpu time. So, the old limits of '68 weeks'
could possibly be insufficient to reflect those times.

2. Given the widths of top's new time fields, the code
never got beyond scaling to hours. For example, with a
ridiculously large span of 19 years, the scaled result
would then be shown as '167832h'. We never reached the
days ('6993d') or even the weeks ('999w') equivalents.

3. Similarly, with that 'TIME+' field and a large tics
value, results would then appear as 'MMMMMM:SS' rather
than the more meaningful 'HH:MM:SS' or days and hours.

So henceforth we will adopt these scaling conventions:

  MMM:SS.hh ... minutes:seconds.hundredths
  MMM:SS ...... minutes:seconds
  HH,MM ....... hours,minutes
  D+H ......... days+hours (with 'd' & 'h' suffixes)
  D ........... days (with 'd' suffix)
  W+D ......... weeks+days (with 'w' & 'd' suffixes)
  W ........... weeks (with 'w' suffix)

Note that, unlike our former scaling logic, that 'MMM'
portion won't be allowed to grow unconditionally. It's
limited (arbitrarily?) to 360 total minutes (6 hours).
Additionally, the 'HH' guy will be limited to 96 hours
(4 days) while that 'D' limit was set at 14 (2 weeks).

Whenever a limit is hit, scaling will advance a level.

Reference(s):
. Feb, 2022 - added 'ELAPSED'
commit 9348d3b008
. Feb, 2022 - added 'STARTED'
commit 7647e96b0a

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2022-03-03 00:00:00 -06:00 committed by Craig Small
parent 9348d3b008
commit 71eb90c1b2
2 changed files with 93 additions and 28 deletions

113
top/top.c
View File

@ -1630,45 +1630,52 @@ end_justifies:
} // end: scale_pcnt
#define TICS_AS_FULL 0
#define TICS_AS_SECS 1
#define TICS_AS_MINS 2
#define TICS_AS_SECS 0
#define TICS_AS_MINS 1
#define TICS_AS_HOUR 2
/*
* Do some scaling stuff.
* Format 'tics' to fit 'width', then justify it. */
static const char *scale_tics (TIC_t tics, int width, int justr, int abrv) {
* Try to format 'tics' to reach 'target' while also
* fitting in 'width', then justify it. */
static const char *scale_tics (TIC_t tics, int width, int justr, int target) {
#ifdef CASEUP_SUFIX
#define HH "%uH" // nls_maybe
#define DD "%uD"
#define WW "%uW"
#define HH "%luH" // nls_maybe
#define DD "%luD"
#define WW "%luW"
#else
#define HH "%uh" // nls_maybe
#define DD "%ud"
#define WW "%uw"
#define HH "%luh" // nls_maybe
#define DD "%lud"
#define WW "%luw"
#endif
static char buf[SMLBUFSIZ];
unsigned long nt; // narrow time, for speed on 32-bit
unsigned cc; // centiseconds
unsigned nn; // multi-purpose whatever
TIC_t nt; // for speed on 64-bit
#ifdef SCALE_FORMER
unsigned long cc; // centiseconds
unsigned long nn; // multi-purpose whatever
#else
unsigned long cent, secs, mins, hour, days, week;
#endif
buf[0] = '\0';
nt = (tics * 100ull) / Hertz; // up to 68 weeks of cpu time
nt = (tics * 100ull) / Hertz; // lots of room for any time
if (Rc.zero_suppress && 0 >= nt)
goto end_justifies;
#ifdef SCALE_FORMER
cc = nt % 100; // centiseconds past second
nt /= 100; // total seconds
nn = nt % 60; // seconds past the minute
nt /= 60; // total minutes
if (abrv < TICS_AS_SECS
&& (width >= snprintf(buf, sizeof(buf), "%lu:%02u.%02u", nt, nn, cc)))
if (target < TICS_AS_MINS
&& (width >= snprintf(buf, sizeof(buf), "%llu:%02lu.%02lu", nt, nn, cc)))
goto end_justifies;
if (abrv < TICS_AS_MINS
&& (width >= snprintf(buf, sizeof(buf), "%lu:%02u", nt, nn)))
if (target < TICS_AS_HOUR
&& (width >= snprintf(buf, sizeof(buf), "%llu:%02lu", nt, nn)))
goto end_justifies;
nn = nt % 60; // minutes past the hour
nt /= 60; // total hours
if (width >= snprintf(buf, sizeof(buf), "%lu,%02u", nt, nn))
if (width >= snprintf(buf, sizeof(buf), "%llu,%02lu", nt, nn))
goto end_justifies;
nn = nt; // now also hours
if (width >= snprintf(buf, sizeof(buf), HH, nn))
@ -1679,9 +1686,63 @@ static const char *scale_tics (TIC_t tics, int width, int justr, int abrv) {
nn /= 7; // now weeks
if (width >= snprintf(buf, sizeof(buf), WW, nn))
goto end_justifies;
#else
#define mmLIMIT 360 // arbitrary 6 hours
#define hhLIMIT 96 // arbitrary 4 days
#define ddLIMIT 14 // arbitrary 2 weeks
cent = (nt % 100); // cent past secs
secs = (nt /= 100); // total secs
mins = (nt /= 60); // total mins
hour = (nt /= 60); // total hour
days = (nt /= 24); // totat days
week = (nt / 7); // total week
switch (target) {
case TICS_AS_SECS:
if (mins < mmLIMIT + 1) {
if (width >= snprintf(buf, sizeof(buf), "%lu:%02lu.%02lu", mins, secs % 60, cent))
goto end_justifies;
}
case TICS_AS_MINS: // fall through
if (mins < mmLIMIT + 1) {
if (width >= snprintf(buf, sizeof(buf), "%lu:%02lu", mins, secs % 60))
goto end_justifies;
}
case TICS_AS_HOUR: // fall through
if (hour < hhLIMIT + 1) {
if (width >= snprintf(buf, sizeof(buf), "%lu,%02lu", hour, mins % 60))
goto end_justifies;
}
default: // fall through
if (days < ddLIMIT + 1) {
if (width >= snprintf(buf, sizeof(buf), DD "+" HH, days, hour % 24))
goto end_justifies;
#ifdef SCALE_POSTFX
if (width >= snprintf(buf, sizeof(buf), DD "+%lu", days, hour % 24))
goto end_justifies;
#endif
if (width >= snprintf(buf, sizeof(buf), DD, days))
goto end_justifies;
}
if (width >= snprintf(buf, sizeof(buf), WW "+" DD, week, days % 7))
goto end_justifies;
#ifdef SCALE_POSTFX
if (width >= snprintf(buf, sizeof(buf), WW "+%lu", week, days % 7))
goto end_justifies;
#endif
if (width >= snprintf(buf, sizeof(buf), WW, week))
goto end_justifies;
break;
}
#undef mmLIMIT
#undef hhLIMIT
#undef ddLIMIT
#endif
// well shoot, this outta' fit...
snprintf(buf, sizeof(buf), "?");
end_justifies:
return justify_pad(buf, width, justr);
#undef HH
@ -6243,22 +6304,22 @@ static const char *task_show (const WIN_t *q, int idx) {
case EU_FLG: // PIDS_FLAGS
cp = make_str(hex_make(rSv(EU_FLG, ul_int), 1), W, Js, AUTOX_NO);
break;
/* ull_int, scale_tics (try centiseconds) */
/* ull_int, scale_tics (try 'minutes:seconds.hundredths') */
case EU_TM2: // PIDS_TICS_ALL
case EU_TME: // PIDS_TICS_ALL
{ TIC_t t;
if (CHKw(q, Show_CTIMES)) t = rSv(eu_TICS_ALL_C, ull_int);
else t = rSv(i, ull_int);
cp = scale_tics(t, W, Jn, TICS_AS_FULL);
cp = scale_tics(t, W, Jn, TICS_AS_SECS);
}
break;
/* ull_int, scale_tics (try seconds) */
/* ull_int, scale_tics (try 'minutes:seconds') */
case EU_TM3: // PIDS_TICS_BEGAN
cp = scale_tics(rSv(EU_TM3, ull_int), W, Jn, TICS_AS_SECS);
cp = scale_tics(rSv(EU_TM3, ull_int), W, Jn, TICS_AS_MINS);
break;
/* real, scale_tics (try minutes) */
/* real, scale_tics (try 'hour,minutes') */
case EU_TM4: // PIDS_TIME_ELAPSED
cp = scale_tics(rSv(EU_TM4, real) * Hertz, W, Jn, TICS_AS_MINS);
cp = scale_tics(rSv(EU_TM4, real) * Hertz, W, Jn, TICS_AS_HOUR);
break;
/* str, make_str (all AUTOX yes) */
case EU_LXC: // PIDS_LXCNAME

View File

@ -49,6 +49,8 @@
//#define RCFILE_NOERR /* rcfile errs silently default, vs. fatal */
//#define RECALL_FIXED /* don't reorder saved strings if recalled */
//#define RMAN_IGNORED /* don't consider auto right margin glitch */
//#define SCALE_FORMER /* scale_tics() guy shouldn't mimic uptime */
//#define SCALE_POSTFX /* scale_tics() try without a 'h,d' suffix */
//#define SCROLLVAR_NO /* disable intra-column horizontal scrolls */
//#define SCROLLV_BY_1 /* when scrolling left/right do not move 8 */
//#define STRINGCASENO /* case insenstive compare/locate versions */
@ -569,7 +571,9 @@ typedef struct WIN_t {
#if defined(MEMGRAPH_OLD)
# warning 'MEMGRAPH_OLD' will make the man document Section 2c. misleading
#endif
#if defined(SCALE_FORMER) && defined(SCALE_POSTFX)
# warning 'SCALE_POSTFX' is ignored when 'SCALE_FORMER' is active
#endif
/*###### Some Prototypes (ha!) #########################################*/
@ -624,7 +628,7 @@ typedef struct WIN_t {
//atic const char *scale_mem (int target, float num, int width, int justr);
//atic const char *scale_num (float num, int width, int justr);
//atic const char *scale_pcnt (float num, int width, int justr, int xtra);
//atic const char *scale_tics (TIC_t tics, int width, int justr, int abrv);
//atic const char *scale_tics (TIC_t tics, int width, int justr, int target);
/*------ Fields Management support -------------------------------------*/
/*atic struct Fieldstab[] = { ... } */
//atic void adj_geometry (void);