top: restore the former behavior after stderr redirect

When top originally responded to the potential libnuma
stderr write, the library was consistently called with
each refresh cycle. That, in turn, guaranteed that any
warning message would be seen at program end by virtue
of: 1) having been issued before the 2nd refresh cycle
and; 2) benefiting from inherited /dev/null buffering.

A later efficiency refactor meant the numa library may
not always be called with every refresh cycle. Rather,
it was only called if top was in one of two numa views
(the '2' or '3' toggles). That, in turn, resulted in a
loss of any warning message at program end unless numa
mode had been preserved in the rcfile. In other words,
if top was started normally then a single cycle stderr
redirect would have long passed by the time the '2' or
'3' toggle was activated. The warning message actually
was spewed but quickly lost to the full screen refresh
which follows all keyboard interactions with the user.

This commit simply moves the restoration of our stderr
redirect to program end (instead of that first display
refresh). Now, any libnuma stderr warning message will
appear as the concluding output line upon quitting top
without regard to when any numa mode view was invoked.

And since this technique might be useful in some other
context (as an example of how to 'buffer' stderr) it's
been generalized with its own #define. But to maximize
its usefulness, the original redirect should be issued
much earlier in pgm startup than top has chosen to do.

Reference(s):
. original libnuma stderr response (msg seen)
commit 35dc6dcc49
. numa refractoring for efficiency (msg lost)
commit f12c0d5c6e

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2014-02-20 00:00:00 -06:00 committed by Craig Small
parent dc072aced7
commit bcf4f5a830
2 changed files with 14 additions and 12 deletions

View File

@ -216,11 +216,13 @@ static int Autox_array [P_MAXPFLGS],
/* Support for NUMA Node display, node expansion/targeting and
run-time dynamic linking with libnuma.so treated as a plugin */
#ifndef OFF_STDERROR
static int Stderr_save = -1;
#endif
static int Numa_node_tot;
static int Numa_node_sel = -1;
#ifndef NUMA_DISABLE
static void *Libnuma_handle;
static int Stderr_save = -1;
#if defined(PRETEND_NUMA) || defined(PRETEND8CPUS)
static int Numa_max_node(void) { return 3; }
#ifndef OFF_NUMASKIP
@ -377,6 +379,15 @@ static void at_eoj (void) {
#endif
}
fflush(stdout);
#ifndef OFF_STDERROR
/* we gotta reverse the stderr redirect which was employed during start up
and needed because the two libnuma 'weak' functions were useless to us! */
if (-1 < Stderr_save) {
dup2(Stderr_save, fileno(stderr));
close(Stderr_save);
Stderr_save = -1; // we'll be ending soon anyway but what the heck
}
#endif
} // end: at_eoj
@ -4070,7 +4081,7 @@ static void wins_stage_2 (void) {
// fill in missing Fieldstab members and build each window's columnhdr
zap_fieldstab();
#ifndef NUMA_DISABLE
#ifndef OFF_STDERROR
/* there's a chance that damn libnuma may spew to stderr so we gotta
make sure he does not corrupt poor ol' top's first output screen!
Yes, he provides some overridable 'weak' functions to change such
@ -5564,16 +5575,6 @@ static void frame_make (void) {
the normal non-interactive output optimization... */
if (!Cap_can_goto) PSU_CLREOS(0);
#ifndef NUMA_DISABLE
/* we gotta reverse the stderr redirect which was employed in wins_stage_2
and needed because the two libnuma 'weak' functions were useless to us! */
if (-1 < Stderr_save) {
dup2(Stderr_save, fileno(stderr));
close(Stderr_save);
Stderr_save = -1;
}
#endif
/* lastly, check auto-sized width needs for the next iteration */
if (AUTOX_MODE && Autox_found)
widths_resize();

View File

@ -43,6 +43,7 @@
//#define OFF_HST_HASH /* use BOTH qsort+bsrch vs. hashing scheme */
//#define OFF_NUMASKIP /* do NOT skip numa nodes if discontinuous */
//#define OFF_SCROLLBK /* disable tty emulators scrollback buffer */
//#define OFF_STDERROR /* disable our stderr buffering (redirect) */
//#define OFF_STDIOLBF /* disable our own stdout _IOFBF override */
//#define PRETEND2_5_X /* pretend we're linux 2.5.x (for IO-wait) */
//#define PRETEND8CPUS /* pretend we're smp with 8 ticsers (sic) */