Skip to content

Commit c026c23

Browse files
dedekindlenb
authored andcommitted
tools/power turbostat: read from pipes too
Commit '47936f944e78 tools/power turbostat: fix printing on input' make a valid fix, but it completely disabled piped stdin support, which is a valuable use-case. Indeed, if stdin is a pipe, turbostat won't read anything from it, so it becomes impossible to get turbostat output at user-defined moments, instead of the regular intervals. There is no reason why this should works for terminals, but not for pipes. This patch improves the situation. Instead of ignoring pipes, we read data from them but gracefully handle the EOF case. Signed-off-by: Artem Bityutskiy <[email protected]> Signed-off-by: Len Brown <[email protected]>
1 parent d93ea56 commit c026c23

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

tools/power/x86/turbostat/turbostat.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ unsigned int has_hwp_epp; /* IA32_HWP_REQUEST[bits 31:24] */
100100
unsigned int has_hwp_pkg; /* IA32_HWP_REQUEST_PKG */
101101
unsigned int has_misc_feature_control;
102102
unsigned int first_counter_read = 1;
103+
int ignore_stdin;
103104

104105
#define RAPL_PKG (1 << 0)
105106
/* 0x610 MSR_PKG_POWER_LIMIT */
@@ -3013,26 +3014,37 @@ void setup_signal_handler(void)
30133014

30143015
void do_sleep(void)
30153016
{
3016-
struct timeval select_timeout;
3017+
struct timeval tout;
3018+
struct timespec rest;
30173019
fd_set readfds;
30183020
int retval;
30193021

30203022
FD_ZERO(&readfds);
30213023
FD_SET(0, &readfds);
30223024

3023-
if (!isatty(fileno(stdin))) {
3025+
if (ignore_stdin) {
30243026
nanosleep(&interval_ts, NULL);
30253027
return;
30263028
}
30273029

3028-
select_timeout = interval_tv;
3029-
retval = select(1, &readfds, NULL, NULL, &select_timeout);
3030+
tout = interval_tv;
3031+
retval = select(1, &readfds, NULL, NULL, &tout);
30303032

30313033
if (retval == 1) {
30323034
switch (getc(stdin)) {
30333035
case 'q':
30343036
exit_requested = 1;
30353037
break;
3038+
case EOF:
3039+
/*
3040+
* 'stdin' is a pipe closed on the other end. There
3041+
* won't be any further input.
3042+
*/
3043+
ignore_stdin = 1;
3044+
/* Sleep the rest of the time */
3045+
rest.tv_sec = (tout.tv_sec + tout.tv_usec / 1000000);
3046+
rest.tv_nsec = (tout.tv_usec % 1000000) * 1000;
3047+
nanosleep(&rest, NULL);
30363048
}
30373049
/* make sure this manually-invoked interval is at least 1ms long */
30383050
nanosleep(&one_msec, NULL);

0 commit comments

Comments
 (0)