[PATCH v4 06/10] perf stat: implement control commands handling

From: Alexey Budankov
Date: Mon May 25 2020 - 10:21:22 EST



Implement handling of 'enable' and 'disable' control commands
coming from control file descriptor.

Signed-off-by: Alexey Budankov <alexey.budankov@xxxxxxxxxxxxxxx>
---
tools/perf/builtin-stat.c | 45 +++++++++++++++++++++++++++++++++------
1 file changed, 39 insertions(+), 6 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 70b48a279b75..c06c2b701a15 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -439,6 +439,31 @@ static bool process_timeout(bool timeout, unsigned int interval, int *times)
return print_interval(interval, times);
}

+static bool process_evlist(struct evlist *evlist, unsigned int interval, int *times)
+{
+ bool stop = false;
+ enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
+
+ if (evlist__ctlfd_process(evlist, &cmd) > 0) {
+ switch (cmd) {
+ case EVLIST_CTL_CMD_ENABLE:
+ pr_info(EVLIST_ENABLED_MSG);
+ stop = print_interval(interval, times);
+ break;
+ case EVLIST_CTL_CMD_DISABLE:
+ stop = print_interval(interval, times);
+ pr_info(EVLIST_DISABLED_MSG);
+ break;
+ case EVLIST_CTL_CMD_ACK:
+ case EVLIST_CTL_CMD_UNSUPPORTED:
+ default:
+ break;
+ }
+ }
+
+ return stop;
+}
+
static void enable_counters(void)
{
if (stat_config.initial_delay < 0) {
@@ -518,8 +543,8 @@ static int dispatch_events(pid_t pid, struct perf_stat_config *config)
{
pid_t child = 0;
bool stop = false;
- struct timespec time_to_sleep;
- int sleep_time, status = 0;
+ int time_to_sleep, sleep_time, status = 0;
+ struct timespec time_start, time_stop, time_diff;
int times = config->times;

if (config->interval)
@@ -529,8 +554,7 @@ static int dispatch_events(pid_t pid, struct perf_stat_config *config)
else
sleep_time = 1000;

- time_to_sleep.tv_sec = sleep_time / MSEC_PER_SEC;
- time_to_sleep.tv_nsec = (sleep_time % MSEC_PER_SEC) * NSEC_PER_MSEC;
+ time_to_sleep = sleep_time;

do {
if (pid != -1) {
@@ -543,8 +567,17 @@ static int dispatch_events(pid_t pid, struct perf_stat_config *config)
if (child || stop || done)
break;

- nanosleep(&time_to_sleep, NULL);
- stop = process_timeout(config->timeout, config->interval, &times);
+ clock_gettime(CLOCK_MONOTONIC, &time_start);
+ if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
+ stop = process_timeout(config->timeout, config->interval, &times);
+ time_to_sleep = sleep_time;
+ } else { /* fd revent */
+ stop = process_evlist(evsel_list, config->interval, &times);
+ clock_gettime(CLOCK_MONOTONIC, &time_stop);
+ diff_timespec(&time_diff, &time_stop, &time_start);
+ time_to_sleep -= time_diff.tv_sec * MSEC_PER_SEC +
+ time_diff.tv_nsec / NSEC_PER_MSEC;
+ }
} while (1);

return status;
--
2.24.1