[PATCH 1/3] perf top: Allow running without stdin

From: Igor Lubashev
Date: Tue Oct 22 2019 - 21:56:01 EST


Allow perf top --stdio to run without access to stdin.
This lets perf top to run in a batch mode until interrupted.

The following now works as expected:

$ perf top < /dev/null

Signed-off-by: Igor Lubashev <ilubashe@xxxxxxxxxx>
---
tools/perf/builtin-top.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-top.c b/tools/perf/builtin-top.c
index d96f24c8770d..fbc0dc135b8a 100644
--- a/tools/perf/builtin-top.c
+++ b/tools/perf/builtin-top.c
@@ -665,6 +665,7 @@ static void display_setup_sig(void)
static void *display_thread(void *arg)
{
struct pollfd stdin_poll = { .fd = 0, .events = POLLIN };
+ nfds_t nfds = 1;
struct termios save;
struct perf_top *top = arg;
int delay_msecs, c;
@@ -684,7 +685,8 @@ static void *display_thread(void *arg)
delay_msecs = top->delay_secs * MSEC_PER_SEC;
set_term_quiet_input(&save);
/* trash return*/
- getc(stdin);
+ if (getc(stdin) == EOF)
+ nfds = 0;

while (!done) {
perf_top__print_sym_table(top);
@@ -692,7 +694,7 @@ static void *display_thread(void *arg)
* Either timeout expired or we got an EINTR due to SIGWINCH,
* refresh screen in both cases.
*/
- switch (poll(&stdin_poll, 1, delay_msecs)) {
+ switch (poll(&stdin_poll, nfds, delay_msecs)) {
case 0:
continue;
case -1:
@@ -701,6 +703,10 @@ static void *display_thread(void *arg)
__fallthrough;
default:
c = getc(stdin);
+ if (c == EOF) {
+ nfds = 0;
+ continue;
+ }
tcsetattr(0, TCSAFLUSH, &save);

if (perf_top__handle_keypress(top, c))
--
2.7.4