From 7e33fc47c642aceea8ad53e86a6797239b91f5f9 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Thu, 9 Mar 2023 00:00:00 -0600 Subject: [PATCH] top: avoid '%Cpu' distortion resulting from keystrokes Like a line from the movie Cool Hand Luke: "what we've got here is failure to communicate"; well that was me! Finally, I got a handle on the issue referenced below. At first, it seemed inappropriate to try comparing cpu percentages as reported by different top versions. And even more so when they are invoked many seconds apart. As it turns out, this issue had nothing to do with the specific processor. Nor did it involve two versions of top running simultaneously using the same delay value. Rather, it concerns keyboard input and several changes which were made last year in commits referenced below. They were prompted by development of the 'Ctrl' bottom window feature. Initially, if transitioning from a big window to a small window portions of the former window remained visible until the next refresh. A solution to that led to a flaw when resizing top. Fixing that then created a race condition with full screen replacement. The net effect of all those changes was to distort the cpu percentage value for the processor on which top is dispatched to service user input. It arose because the new frame was begun immediately, yielding few 'ticks'. [ when fewer ticks are accumulated the potential for ] [ distortion increases. As an example, hold some key ] [ then watch cpu percentages (works best in graphs). ] [ while any version of top will show distortions for ] [ the above experiment, a v4.0.0 top will be greater ] [ and %cpu is distorted even for a single keystroke. ] So, to restore proper 3.3.17 keystroke behavior, we'll revert parts of the first 3 commits shown below. Plus, the 4th commit will be entirely reversed as redundant. Reference(s): https://gitlab.com/procps-ng/procps/-/issues/274 . Sep, 2022 - avoid potential 'BREAK_screen' race commit 3e5016c2898d7129d2cdae7b1def8fe85d41619f . Sep, 2022 - fix improper sigwinch behavior commit 9d9993708b1854f7396b978a6d54d6c508e1fd08 . May, 2022 - made more responsive to kdb input commit 3ea1bc779fb7ca5bf065a5ca620ec5b5823bc61c . turn bottom window off with additional key commit 3f068a66c84347a42fd27d8a809cea2656043b37 Signed-off-by: Jim Warner --- src/top/top.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/src/top/top.c b/src/top/top.c index ad862654..94c15937 100644 --- a/src/top/top.c +++ b/src/top/top.c @@ -2144,6 +2144,7 @@ static void adj_geometry (void) { Graph_mems->style = Curwin->rc.graph_mems; fflush(stdout); + Frames_signal = BREAK_off; } // end: adj_geometry @@ -2477,7 +2478,6 @@ static void fields_utility (void) { spewFI signify_that: - Frames_signal = BREAK_off; putp(Cap_clr_scr); adj_geometry(); @@ -3306,7 +3306,6 @@ static int insp_view_choice (struct pids_stack *p) { int key, curlin = 0, curcol = 0; signify_that: - Frames_signal = BREAK_off; putp(Cap_clr_scr); adj_geometry(); @@ -3429,7 +3428,6 @@ static void inspection_utility (int pid) { // must re-hide cursor since the prompt for a pid made it huge putp((Cursor_state = Cap_curs_hide)); signify_that: - Frames_signal = BREAK_off; putp(Cap_clr_scr); adj_geometry(); @@ -4613,7 +4611,6 @@ static void wins_colors (void) { wins_clrhlp(w, 1); putp((Cursor_state = Cap_curs_huge)); signify_that: - Frames_signal = BREAK_off; putp(Cap_clr_scr); adj_geometry(); @@ -5268,6 +5265,8 @@ static void bot_item_toggle (int what, const char *head, char sep) { if (Bot_what == what) { BOT_TOSS; } else { + // accommodate transition from larger to smaller window + Bot_rsvd = 0; switch (what) { case BOT_ITEM_NS: for (i = 0; i < MAXTBL(ns_tab); i++) @@ -5363,7 +5362,6 @@ static void help_view (void) { putp((Cursor_state = Cap_curs_huge)); signify_that: - Frames_signal = BREAK_off; putp(Cap_clr_scr); adj_geometry(); @@ -5388,7 +5386,6 @@ signify_that: case '?': case 'h': case 'H': do { signify_this: - Frames_signal = BREAK_off; putp(Cap_clr_scr); adj_geometry(); show_special(1, fmtmk(N_unq(WINDOWS_help_fmt) @@ -5582,8 +5579,6 @@ static void keys_global (int ch) { Winstk[i].begtask = Winstk[i].focus_pid = 0; // force an extra procs refresh to avoid %cpu distortions... Pseudo_row = PROC_XTRA; - // signal that we just corrupted entire screen - Frames_signal = BREAK_screen; break; case 'I': if (Cpu_cnt > 1) { @@ -7360,7 +7355,6 @@ int main (int argc, char *argv[]) { if (0 < Loops) --Loops; if (!Loops) bye_bye(NULL); - if (Frames_signal) { Frames_signal = BREAK_off; zap_fieldstab(); continue; } ts.tv_sec = Rc.delay_time; ts.tv_nsec = (Rc.delay_time - (int)Rc.delay_time) * 1000000000;