David's patch (https://lkml.org/lkml/2013/5/9/125 ) add a live mode for
perf kvm stat, but his patch can't set the refresh frequency
dynamically.

This patch accepts user's input and changes the value of
kvm->display_time variable.

For example, when the live mode is running, you press any key, it will
prints:

Mapped keys:
     [d]     set refresh frequency
     [q]     quit

Enter selection, or unmapped key to continue:
Enter refresh frequency(seconds): 2

Then, the live mode will refresh the screen every 2 seconds.

Signed-off-by: Runzhen Wang <runz...@linux.vnet.ibm.com>
Signed-off-by: David Ahern <dsah...@gmail.com>
Cc: Arnaldo Carvalho de Melo <a...@ghostprotocols.net>
Cc: Ingo Molnar <mi...@kernel.org>
Cc: Frederic Weisbecker <fweis...@gmail.com>
Cc: Peter Zijlstra <pet...@infradead.org>
Cc: Jiri Olsa <jo...@redhat.com>
Cc: Namhyung Kim <namhy...@kernel.org>
Cc: Xiao Guangrong <xiaoguangr...@linux.vnet.ibm.com>
---
 tools/perf/builtin-kvm.c |   42 ++++++++++++++++++++++++++++++++++++++----
 tools/perf/util/top.c    |    4 ++++
 2 files changed, 42 insertions(+), 4 deletions(-)

diff --git a/tools/perf/builtin-kvm.c b/tools/perf/builtin-kvm.c
index 7392538..3e3f6e2 100644
--- a/tools/perf/builtin-kvm.c
+++ b/tools/perf/builtin-kvm.c
@@ -111,7 +111,7 @@ struct perf_kvm_stat {
        struct rb_root result;
 
        int timerfd;
-       unsigned int display_time;
+       int display_time;
        bool live;
 };
 
@@ -1021,17 +1021,51 @@ static int fd_set_nonblock(int fd)
        return 0;
 }
 
+static void perf_kvm_live__print_mapped_keys(void)
+{
+       fprintf(stdout, "\nMapped keys:\n");
+       fprintf(stdout, "\t[d]     set refresh frequency\n");
+       fprintf(stdout, "\t[q]     quit\n");
+}
+
 static
-int perf_kvm__handle_stdin(struct termios *tc_now, struct termios *tc_save)
+int perf_kvm__handle_stdin(struct perf_kvm_stat *kvm,
+                          struct termios *tc_now,
+                          struct termios *tc_save)
 {
        int c;
+       struct pollfd stdin_poll = { .fd = 0, .events = POLLIN};
 
        tcsetattr(0, TCSANOW, tc_now);
        c = getc(stdin);
        tcsetattr(0, TCSAFLUSH, tc_save);
 
+       if (c == -1)
+               return 0;
        if (c == 'q')
                return 1;
+       perf_kvm_live__print_mapped_keys();
+       fprintf(stdout, "\nEnter selection, or unmapped key to continue: ");
+       fflush(stdout);
+
+       tcsetattr(0, TCSANOW, tc_now);
+       poll(&stdin_poll, 1, -1);
+       c = getc(stdin);
+       tcsetattr(0, TCSAFLUSH, tc_save);
+
+       switch (c) {
+       case 'q':
+               return 1;
+       case 'd':
+               prompt_integer(&kvm->display_time,
+                              "Enter refresh frequency(seconds)");
+               if (kvm->display_time < 1)
+                       kvm->display_time = 1;
+               if (perf_kvm__timerfd_set(kvm) < 0)
+                       return 1;
+       default:
+               break;
+       }
 
        return 0;
 }
@@ -1108,7 +1142,7 @@ static int kvm_events_live_report(struct perf_kvm_stat 
*kvm)
                        goto out;
 
                if (pollfds[nr_stdin].revents & POLLIN)
-                       done = perf_kvm__handle_stdin(&tc, &save);
+                       done = perf_kvm__handle_stdin(kvm, &tc, &save);
 
                if (!rc && !done)
                        err = poll(pollfds, nr_fds, 100);
@@ -1349,7 +1383,7 @@ static int kvm_events_live(struct perf_kvm_stat *kvm,
                        "be more verbose (show counter open errors, etc)"),
                OPT_BOOLEAN('a', "all-cpus", &kvm->opts.target.system_wide,
                        "system-wide collection from all CPUs"),
-               OPT_UINTEGER('d', "display", &kvm->display_time,
+               OPT_INTEGER('d', "display", &kvm->display_time,
                        "time in seconds between display updates"),
                OPT_STRING(0, "event", &kvm->report_event, "report event",
                        "event for reporting: vmexit, mmio, ioport"),
diff --git a/tools/perf/util/top.c b/tools/perf/util/top.c
index 1a2eaec..10bff6d 100644
--- a/tools/perf/util/top.c
+++ b/tools/perf/util/top.c
@@ -121,8 +121,12 @@ void prompt_integer(int *target, const char *msg)
        char *buf = malloc(0), *p;
        size_t dummy = 0;
        int tmp;
+       struct pollfd stdin_poll = { .fd = 0, .events = POLLIN};
 
        fprintf(stdout, "\n%s: ", msg);
+       fflush(stdout);
+       poll(&stdin_poll, 1, -1);
+
        if (getline(&buf, &dummy, stdin) < 0)
                return;
 
-- 
1.7.10.1

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Reply via email to