Skip to content

Commit

Permalink
tools/power turbostat: end current interval upon newline input
Browse files Browse the repository at this point in the history
In turbostat interval mode, a newline typed on standard input
will now conclude the current interval.  Data will immediately
be collected and printed for that interval, and the next interval
will be started.

This is similar to the recently added SIGUSR1 feature.
But that is for use by programs, while this is for interactive use.

Signed-off-by: Len Brown <[email protected]>
  • Loading branch information
lenb committed Jun 1, 2018
1 parent 0721196 commit b9ad8ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
7 changes: 7 additions & 0 deletions tools/power/x86/turbostat/turbostat.8
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,13 @@ CPU PRF_CTRL

.fi

.SH INPUT

For interval-mode, turbostat will immediately end the current interval
when it sees a newline on standard input.
turbostat will then start the next interval.
Control-C will be send a SIGINT to turbostat,
which will immediately abort the program with no further processing.
.SH SIGNALS

SIGINT will interrupt interval-mode.
Expand Down
43 changes: 37 additions & 6 deletions tools/power/x86/turbostat/turbostat.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/resource.h>
#include <fcntl.h>
#include <signal.h>
Expand All @@ -47,7 +48,8 @@
char *proc_stat = "/proc/stat";
FILE *outf;
int *fd_percpu;
struct timespec interval_ts = {5, 0};
struct timeval interval_tv = {5, 0};
struct timespec one_msec = {0, 1000000};
unsigned int debug;
unsigned int quiet;
unsigned int shown;
Expand Down Expand Up @@ -478,7 +480,7 @@ void help(void)
"--cpu cpu-set limit output to summary plus cpu-set:\n"
" {core | package | j,k,l..m,n-p }\n"
"--quiet skip decoding system configuration header\n"
"--interval sec Override default 5-second measurement interval\n"
"--interval sec.subsec Override default 5-second measurement interval\n"
"--help print this help message\n"
"--list list column headers only\n"
"--out file create or truncate \"file\" for all output\n"
Expand Down Expand Up @@ -2615,6 +2617,8 @@ static void signal_handler (int signal)
fprintf(stderr, "SIGUSR1\n");
break;
}
/* make sure this manually-invoked interval is at least 1ms long */
nanosleep(&one_msec, NULL);
}

void setup_signal_handler(void)
Expand All @@ -2630,6 +2634,33 @@ void setup_signal_handler(void)
if (sigaction(SIGUSR1, &sa, NULL) < 0)
err(1, "sigaction SIGUSR1");
}

int do_sleep(void)
{
struct timeval select_timeout;
fd_set readfds;
int retval;

FD_ZERO(&readfds);
FD_SET(0, &readfds);

select_timeout = interval_tv;

retval = select(1, &readfds, NULL, NULL, &select_timeout);

if (retval == 1) {

switch (getc(stdin)) {
case 'q':
exit_requested = 1;
break;
}
/* make sure this manually-invoked interval is at least 1ms long */
nanosleep(&one_msec, NULL);
}

return retval;
}
void turbostat_loop()
{
int retval;
Expand Down Expand Up @@ -2659,7 +2690,7 @@ void turbostat_loop()
re_initialize();
goto restart;
}
nanosleep(&interval_ts, NULL);
do_sleep();
if (snapshot_proc_sysfs_files())
goto restart;
retval = for_all_cpus(get_counters, ODD_COUNTERS);
Expand All @@ -2680,7 +2711,7 @@ void turbostat_loop()
flush_output_stdout();
if (exit_requested)
break;
nanosleep(&interval_ts, NULL);
do_sleep();
if (snapshot_proc_sysfs_files())
goto restart;
retval = for_all_cpus(get_counters, EVEN_COUNTERS);
Expand Down Expand Up @@ -5103,8 +5134,8 @@ void cmdline(int argc, char **argv)
exit(2);
}

interval_ts.tv_sec = interval;
interval_ts.tv_nsec = (interval - interval_ts.tv_sec) * 1000000000;
interval_tv.tv_sec = interval;
interval_tv.tv_usec = (interval - interval_tv.tv_sec) * 1000000;
}
break;
case 'J':
Expand Down

0 comments on commit b9ad8ee

Please sign in to comment.