[PATCH v3 5/9] perf stat: implement control commands handling

From: Alexey Budankov
Date: Fri May 08 2020 - 06:50:46 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 | 48 +++++++++++++++++++++++++++++----------
1 file changed, 36 insertions(+), 12 deletions(-)

diff --git a/tools/perf/builtin-stat.c b/tools/perf/builtin-stat.c
index 73404b723592..abea82a1ba24 100644
--- a/tools/perf/builtin-stat.c
+++ b/tools/perf/builtin-stat.c
@@ -460,8 +460,9 @@ static int handle_events(pid_t pid, struct perf_stat_config *config)
{
pid_t child = 0;
bool res, stop = false;
- struct timespec time_to_sleep;
- int sleep_time, status = 0, times = config->times;
+ int time_to_sleep, sleep_time, status = 0, times = config->times;
+ enum evlist_ctl_cmd cmd = EVLIST_CTL_CMD_UNSUPPORTED;
+ struct timespec time_start, time_stop, time_diff;

if (config->interval)
sleep_time = config->interval;
@@ -470,22 +471,45 @@ static int handle_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)
child = waitpid(pid, &status, WNOHANG);
if (child || stop || done)
break;
- nanosleep(&time_to_sleep, NULL);
- if (pid == -1)
- stop = !is_target_alive(&target, evsel_list->core.threads);
- if (config->timeout) {
- stop = !stop ? true : stop;
- } else {
- res = print_interval_and_stop(config, &times);
- stop = !stop ? res : stop;
+ clock_gettime(CLOCK_MONOTONIC, &time_start);
+ if (!(evlist__poll(evsel_list, time_to_sleep) > 0)) { /* poll timeout or EINTR */
+ if (pid == -1)
+ stop = !is_target_alive(&target, evsel_list->core.threads);
+ if (config->timeout) {
+ stop = !stop ? true : stop;
+ } else {
+ res = print_interval_and_stop(config, &times);
+ stop = !stop ? res : stop;
+ }
+ time_to_sleep = sleep_time;
+ } else { /* fd revent */
+ if (evlist__ctlfd_process(evsel_list, &cmd) > 0) {
+ switch (cmd) {
+ case EVLIST_CTL_CMD_ENABLE:
+ pr_info(EVLIST_ENABLED_MSG);
+ stop = print_interval_and_stop(config, &times);
+ break;
+ case EVLIST_CTL_CMD_DISABLE:
+ stop = print_interval_and_stop(config, &times);
+ pr_info(EVLIST_DISABLED_MSG);
+ break;
+ case EVLIST_CTL_CMD_ACK:
+ case EVLIST_CTL_CMD_UNSUPPORTED:
+ default:
+ break;
+ }
+ }
+ 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);

--
2.24.1