Commit 47936f94 authored by Artem Bityutskiy's avatar Artem Bityutskiy Committed by Len Brown

tools/power turbostat: fix printing on input

The recent patch that implements table printing on a keypress introduced a
regression - turbostat prints the table almost continuously if it is run from a
daemon program.

The problem is also easy to reproduce like this:

echo | turbostat

The reason is that we cannot assume that stdin is always a TTY. It can be many
things.

This patch adds fixes the problem by limiting the new keypress functionality to
TTYs only. If stdin is not a TTY, we just sleep for the full interval time.

While on it, clean-up 'do_sleep()' to return no value, as callers do not expect
that anyway.
Signed-off-by: default avatarArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Signed-off-by: default avatarLen Brown <len.brown@intel.com>
parent b9ad8ee0
...@@ -49,6 +49,7 @@ char *proc_stat = "/proc/stat"; ...@@ -49,6 +49,7 @@ char *proc_stat = "/proc/stat";
FILE *outf; FILE *outf;
int *fd_percpu; int *fd_percpu;
struct timeval interval_tv = {5, 0}; struct timeval interval_tv = {5, 0};
struct timespec interval_ts = {5, 0};
struct timespec one_msec = {0, 1000000}; struct timespec one_msec = {0, 1000000};
unsigned int debug; unsigned int debug;
unsigned int quiet; unsigned int quiet;
...@@ -2635,7 +2636,7 @@ void setup_signal_handler(void) ...@@ -2635,7 +2636,7 @@ void setup_signal_handler(void)
err(1, "sigaction SIGUSR1"); err(1, "sigaction SIGUSR1");
} }
int do_sleep(void) void do_sleep(void)
{ {
struct timeval select_timeout; struct timeval select_timeout;
fd_set readfds; fd_set readfds;
...@@ -2644,12 +2645,15 @@ int do_sleep(void) ...@@ -2644,12 +2645,15 @@ int do_sleep(void)
FD_ZERO(&readfds); FD_ZERO(&readfds);
FD_SET(0, &readfds); FD_SET(0, &readfds);
select_timeout = interval_tv; if (!isatty(fileno(stdin))) {
nanosleep(&interval_ts, NULL);
return;
}
select_timeout = interval_tv;
retval = select(1, &readfds, NULL, NULL, &select_timeout); retval = select(1, &readfds, NULL, NULL, &select_timeout);
if (retval == 1) { if (retval == 1) {
switch (getc(stdin)) { switch (getc(stdin)) {
case 'q': case 'q':
exit_requested = 1; exit_requested = 1;
...@@ -2658,9 +2662,8 @@ int do_sleep(void) ...@@ -2658,9 +2662,8 @@ int do_sleep(void)
/* make sure this manually-invoked interval is at least 1ms long */ /* make sure this manually-invoked interval is at least 1ms long */
nanosleep(&one_msec, NULL); nanosleep(&one_msec, NULL);
} }
return retval;
} }
void turbostat_loop() void turbostat_loop()
{ {
int retval; int retval;
...@@ -5134,8 +5137,9 @@ void cmdline(int argc, char **argv) ...@@ -5134,8 +5137,9 @@ void cmdline(int argc, char **argv)
exit(2); exit(2);
} }
interval_tv.tv_sec = interval; interval_tv.tv_sec = interval_ts.tv_sec = interval;
interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000; interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
} }
break; break;
case 'J': case 'J':
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment