Skip to contentSkip to conversation

perf tools: Allow to enable/disable events via control pipe

PATCH Superseded Jiri Olsa 1 participant 5 messages Dec 16, 2020 perf events

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Jiri Olsa

author reviewer
#0 lore raw

v3 cover perf tools: Allow to enable/disable events via control pipe

hi,
adding support to enable/disable specific events via control file via following commands:

    # echo 'enable sched:sched_process_fork' > control
    # echo 'disabled sched:sched_process_fork' > control

v3 changes:
- use ' ' instead of '-' in syntax and add command argument processing [Arnaldo]
- add options to evlist [Arnaldo]
- add man page changes

v2 changes:
- added acks
- change list to evlist [Arnaldo]
- add evlist-verbose command [Arnaldo]
- add '' to enale-/disable- error message

The code is available in here:
git://git.kernel.org/pub/scm/linux/kernel/git/jolsa/perf.git
perf/control

thanks,
jirka

---
Jiri Olsa (4):
perf tools: Reformat record's control fd man text perf tools: Add evlist__disable_evsel/evlist__enable_evsel perf tools: Allow to enable/disable events via control file perf tools: Add evlist control command

 tools/perf/Documentation/perf-record.txt |  23 +++++++++++----
 tools/perf/builtin-record.c              |   9 ++----
 tools/perf/builtin-stat.c                |   3 +-
 tools/perf/util/evlist.c                 | 173 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
 tools/perf/util/evlist.h                 |   4 +++
 tools/perf/util/evsel_fprintf.c          |   2 ++
 tools/perf/util/python-ext-sources       |   1 +
 tools/perf/util/setup.py                 |   2 +-
 8 files changed, 197 insertions(+), 20 deletions(-)
author reviewer
#1 lore raw

PATCH v3 1/4 perf tools: Reformat record's control fd man text

Adding available control commands in separate paragraph, so it's more readable and easier to add new commands.

---
 tools/perf/Documentation/perf-record.txt | 16 +++++++++++-----
 1 file changed, 11 insertions(+), 5 deletions(-)
tools/perf/Documentation/perf-record.txt +11 -5
index 768888b9326a..f896c4b791e1 100644
@@ -634,11 +634,17 @@ endif::HAVE_LIBPFM[]
 --control=fifo:ctl-fifo[,ack-fifo]::
 --control=fd:ctl-fd[,ack-fd]::
 ctl-fifo / ack-fifo are opened and used as ctl-fd / ack-fd as follows.
-Listen on ctl-fd descriptor for command to control measurement ('enable': enable events,
-'disable': disable events, 'snapshot': AUX area tracing snapshot). Measurements can be
-started with events disabled using --delay=-1 option. Optionally send control command
-completion ('ack\n') to ack-fd descriptor to synchronize with the controlling process.
-Example of bash shell script to enable and disable events during measurements:
+Listen on ctl-fd descriptor for command to control measurement.
+
+Available commands:
+  'enable'  : enable events
+  'disable' : disable events
+  'snapshot': AUX area tracing snapshot).
+
+Measurements can be started with events disabled using --delay=-1 option. Optionally
+send control command completion ('ack\n') to ack-fd descriptor to synchronize with the
+controlling process.  Example of bash shell script to enable and disable events during
+measurements:
 
  #!/bin/bash
 
Signature · 1 line
2.26.2
author reviewer
#2 lore raw

PATCH v3 2/4 perf tools: Add evlist__disable_evsel/evlist__enable_evsel

In reply to Jiri Olsa

Adding interface to enable/disable single event in the evlist based on its name. It will be used later in new control enable/disable interface.

Keeping the evlist::enabled true when one or more events are enabled so the toggle can work properly and toggle evlist to disabled state.

---
 tools/perf/util/evlist.c | 69 ++++++++++++++++++++++++++++++++++++++--
 tools/perf/util/evlist.h |  2 ++
 2 files changed, 68 insertions(+), 3 deletions(-)
tools/perf/util/evlist.c +66 -3
index 493819173a8e..70aff26612a9 100644
@@ -370,7 +370,30 @@ bool evsel__cpu_iter_skip(struct evsel *ev, int cpu)
 	return true;
 }
 
-void evlist__disable(struct evlist *evlist)
+static int evsel__strcmp(struct evsel *pos, char *evsel_name)
+{
+	if (!evsel_name)
+		return 0;
+	if (evsel__is_dummy_event(pos))
+		return 1;
+	return strcmp(pos->name, evsel_name);
+}
+
+static int evlist__is_enabled(struct evlist *evlist)
+{
+	struct evsel *pos;
+
+	evlist__for_each_entry(evlist, pos) {
+		if (!evsel__is_group_leader(pos) || !pos->core.fd)
+			continue;
+		/* If at least one event is enabled, evlist is enabled. */
+		if (!pos->disabled)
+			return true;
+	}
+	return false;
+}
+
+static void __evlist__disable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
 	struct affinity affinity;
@@ -386,6 +409,8 @@ void evlist__disable(struct evlist *evlist)
 			affinity__set(&affinity, cpu);
 
 			evlist__for_each_entry(evlist, pos) {
+				if (evsel__strcmp(pos, evsel_name))
+					continue;
 				if (evsel__cpu_iter_skip(pos, cpu))
 					continue;
 				if (pos->disabled || !evsel__is_group_leader(pos) || !pos->core.fd)
@@ -403,15 +428,34 @@ void evlist__disable(struct evlist *evlist)
 
 	affinity__cleanup(&affinity);
 	evlist__for_each_entry(evlist, pos) {
+		if (evsel__strcmp(pos, evsel_name))
+			continue;
 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
 			continue;
 		pos->disabled = true;
 	}
 
-	evlist->enabled = false;
+	/*
+	 * If we disabled only single event, we need to check
+	 * the enabled state of the evlist manually.
+	 */
+	if (evsel_name)
+		evlist->enabled = evlist__is_enabled(evlist);
+	else
+		evlist->enabled = false;
+}
+
+void evlist__disable(struct evlist *evlist)
+{
+	__evlist__disable(evlist, NULL);
+}
+
+void evlist__disable_evsel(struct evlist *evlist, char *evsel_name)
+{
+	__evlist__disable(evlist, evsel_name);
 }
 
-void evlist__enable(struct evlist *evlist)
+static void __evlist__enable(struct evlist *evlist, char *evsel_name)
 {
 	struct evsel *pos;
 	struct affinity affinity;
@@ -424,6 +468,8 @@ void evlist__enable(struct evlist *evlist)
 		affinity__set(&affinity, cpu);
 
 		evlist__for_each_entry(evlist, pos) {
+			if (evsel__strcmp(pos, evsel_name))
+				continue;
 			if (evsel__cpu_iter_skip(pos, cpu))
 				continue;
 			if (!evsel__is_group_leader(pos) || !pos->core.fd)
@@ -433,14 +479,31 @@ void evlist__enable(struct evlist *evlist)
 	}
 	affinity__cleanup(&affinity);
 	evlist__for_each_entry(evlist, pos) {
+		if (evsel__strcmp(pos, evsel_name))
+			continue;
 		if (!evsel__is_group_leader(pos) || !pos->core.fd)
 			continue;
 		pos->disabled = false;
 	}
 
+	/*
+	 * Even single event sets the 'enabled' for evlist,
+	 * so the toggle can work properly and toggle to
+	 * 'disabled' state.
+	 */
 	evlist->enabled = true;
 }
 
+void evlist__enable(struct evlist *evlist)
+{
+	__evlist__enable(evlist, NULL);
+}
+
+void evlist__enable_evsel(struct evlist *evlist, char *evsel_name)
+{
+	__evlist__enable(evlist, evsel_name);
+}
+
 void evlist__toggle_enable(struct evlist *evlist)
 {
 	(evlist->enabled ? evlist__disable : evlist__enable)(evlist);
tools/perf/util/evlist.h +2 -0
index 9b0c795736bb..1aae75895dea 100644
@@ -186,6 +186,8 @@ size_t evlist__mmap_size(unsigned long pages);
 void evlist__disable(struct evlist *evlist);
 void evlist__enable(struct evlist *evlist);
 void evlist__toggle_enable(struct evlist *evlist);
+void evlist__disable_evsel(struct evlist *evlist, char *evsel_name);
+void evlist__enable_evsel(struct evlist *evlist, char *evsel_name);
 
 int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx);
 
Signature · 1 line
2.26.2
author reviewer
#3 lore raw

PATCH v3 3/4 perf tools: Allow to enable/disable events via control file

In reply to Jiri Olsa

Adding new control events to enable/disable specific event. The interface string for control file are:

'enable <EVENT NAME>'
'disable <EVENT NAME>'

when received the command, perf will scan the current evlist for <EVENT NAME> and if found it's enabled/disabled.

Example session:

terminal 1:

    # mkfifo control ack perf.pipe
    # perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:*' -o - > perf.pipe
    Events disabled

terminal 2:

    # cat perf.pipe | perf --no-pager script -i -

terminal 3:

    # echo 'enable sched:sched_process_fork' > control

terminal 1:

    # mkfifo control ack perf.pipe
    # perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:*' -o - > perf.pipe
    ...
    event sched:sched_process_fork enabled

terminal 2:

    # cat perf.pipe | perf --no-pager script -i -
    bash 33349 [034] 149587.674295: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34056
    bash 33349 [034] 149588.239521: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34057

terminal 3:

    # echo 'enable sched:sched_wakeup_new' > control

terminal 1:

    # mkfifo control ack perf.pipe
    # perf record --control=fifo:control,ack -D -1 --no-buffering -e 'sched:*' -o - > perf.pipe
    ...
    event sched:sched_wakeup_new enabled

terminal 2:

    # cat perf.pipe | perf --no-pager script -i -
    ...
    bash 33349 [034] 149632.228023: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34059
    bash 33349 [034] 149632.228050:   sched:sched_wakeup_new: bash:34059 [120] success=1 CPU:036
    bash 33349 [034] 149633.950005: sched:sched_process_fork: comm=bash pid=33349 child_comm=bash child_pid=34060
    bash 33349 [034] 149633.950030:   sched:sched_wakeup_new: bash:34060 [120] success=1 CPU:036
---
 tools/perf/Documentation/perf-record.txt |  8 +--
 tools/perf/builtin-record.c              |  8 +--
 tools/perf/builtin-stat.c                |  2 -
 tools/perf/util/evlist.c                 | 63 ++++++++++++++++++++++--
 4 files changed, 67 insertions(+), 14 deletions(-)
tools/perf/Documentation/perf-record.txt +5 -3
index f896c4b791e1..55d2d335b8a8 100644
@@ -637,9 +637,11 @@ ctl-fifo / ack-fifo are opened and used as ctl-fd / ack-fd as follows.
 Listen on ctl-fd descriptor for command to control measurement.
 
 Available commands:
-  'enable'  : enable events
-  'disable' : disable events
-  'snapshot': AUX area tracing snapshot).
+  'enable'       : enable events
+  'disable'      : disable events
+  'enable name'  : enable event 'name'
+  'disable name' : disable event 'name'
+  'snapshot'     : AUX area tracing snapshot).
 
 Measurements can be started with events disabled using --delay=-1 option. Optionally
 send control command completion ('ack\n') to ack-fd descriptor to synchronize with the
tools/perf/builtin-record.c +2 -6
index d832c108a1ca..e9874832dc27 100644
@@ -1937,18 +1937,14 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 
 		if (evlist__ctlfd_process(rec->evlist, &cmd) > 0) {
 			switch (cmd) {
-			case EVLIST_CTL_CMD_ENABLE:
-				pr_info(EVLIST_ENABLED_MSG);
-				break;
-			case EVLIST_CTL_CMD_DISABLE:
-				pr_info(EVLIST_DISABLED_MSG);
-				break;
 			case EVLIST_CTL_CMD_SNAPSHOT:
 				hit_auxtrace_snapshot_trigger(rec);
 				evlist__ctlfd_ack(rec->evlist);
 				break;
 			case EVLIST_CTL_CMD_ACK:
 			case EVLIST_CTL_CMD_UNSUPPORTED:
+			case EVLIST_CTL_CMD_ENABLE:
+			case EVLIST_CTL_CMD_DISABLE:
 			default:
 				break;
 			}
tools/perf/builtin-stat.c +0 -2
index 89c32692f40c..4c43c5e14fd4 100644
@@ -578,14 +578,12 @@ static void process_evlist(struct evlist *evlist, unsigned int interval)
 	if (evlist__ctlfd_process(evlist, &cmd) > 0) {
 		switch (cmd) {
 		case EVLIST_CTL_CMD_ENABLE:
-			pr_info(EVLIST_ENABLED_MSG);
 			if (interval)
 				process_interval();
 			break;
 		case EVLIST_CTL_CMD_DISABLE:
 			if (interval)
 				process_interval();
-			pr_info(EVLIST_DISABLED_MSG);
 			break;
 		case EVLIST_CTL_CMD_SNAPSHOT:
 		case EVLIST_CTL_CMD_ACK:
tools/perf/util/evlist.c +60 -3

76 lines not on this page · raw message

Signature · 1 line
2.26.2
author reviewer
#4 lore raw

PATCH v3 4/4 perf tools: Add evlist control command

In reply to Jiri Olsa

Adding new evlist control event to display all evlist events. When it is received, perf will scan and print current evlist into perf record terminal.

The interface string for control file is:
evlist [-v|-g|-F]

The syntax follows perf evlist command:

  -F  Show just the sample frequency used for each event.
  -v  Show all fields.
  -g  Show event group information.

Example session:

terminal 1:

    # mkfifo control ack
    # perf record --control=fifo:control,ack -e '{cycles,instructions}'

terminal 2:

    # echo evlist > control

terminal 1:

    # perf record --control=fifo:control,ack -e '{cycles,instructions}'
    ...
    cycles
    instructions
    dummy:HG

terminal 2:

    # echo 'evlist -v' > control
  terminal 1:
    ...
    cycles: size: 120, { sample_period, sample_freq }: 4000, sample_type:            \
    IP|TID|TIME|ID|CPU|PERIOD, read_format: ID, disabled: 1, inherit: 1, freq: 1,    \
    sample_id_all: 1, exclude_guest: 1
    instructions: size: 120, config: 0x1, { sample_period, sample_freq }: 4000,      \
    sample_type: IP|TID|TIME|ID|CPU|PERIOD, read_format: ID, inherit: 1, freq: 1,    \
    sample_id_all: 1, exclude_guest: 1
    dummy:HG: type: 1, size: 120, config: 0x9, { sample_period, sample_freq }: 4000, \
    sample_type: IP|TID|TIME|ID|CPU|PERIOD, read_format: ID, inherit: 1, mmap: 1,    \
    comm: 1, freq: 1, task: 1, sample_id_all: 1, mmap2: 1, comm_exec: 1, ksymbol: 1, \
     bpf_event: 1

terminal 2:

    # echo 'evlist -g' > control

terminal 1:

    ...
    {cycles,instructions}
    dummy:HG

terminal 2:

    # echo 'evlist -F' > control

terminal 1:

    ...
    cycles: sample_freq=4000
    instructions: sample_freq=4000
    dummy:HG: sample_freq=4000

This new evlist command is handy to get real event names when wildcards are used.

Adding evsel_fprintf.c object to python/perf.so build, because it's now evlist.c dependency.

Adding PYTHON_PERF define for python/perf.so compilation, so we can use it to compile in only evsel__fprintf from evsel_fprintf.c object.

---
 tools/perf/Documentation/perf-record.txt | 15 ++++++---
 tools/perf/builtin-record.c              |  1 +
 tools/perf/builtin-stat.c                |  1 +
 tools/perf/util/evlist.c                 | 41 ++++++++++++++++++++++++
 tools/perf/util/evlist.h                 |  2 ++
 tools/perf/util/evsel_fprintf.c          |  2 ++
 tools/perf/util/python-ext-sources       |  1 +
 tools/perf/util/setup.py                 |  2 +-
 8 files changed, 59 insertions(+), 6 deletions(-)
tools/perf/Documentation/perf-record.txt +10 -5
index 55d2d335b8a8..ab3c00030baf 100644
@@ -637,11 +637,16 @@ ctl-fifo / ack-fifo are opened and used as ctl-fd / ack-fd as follows.
 Listen on ctl-fd descriptor for command to control measurement.
 
 Available commands:
-  'enable'       : enable events
-  'disable'      : disable events
-  'enable name'  : enable event 'name'
-  'disable name' : disable event 'name'
-  'snapshot'     : AUX area tracing snapshot).
+  'enable'           : enable events
+  'disable'          : disable events
+  'enable name'      : enable event 'name'
+  'disable name'     : disable event 'name'
+  'snapshot'         : AUX area tracing snapshot).
+
+  'evlist [-v|-g|-F] : display all events
+                       -F  Show just the sample frequency used for each event.
+                       -v  Show all fields.
+                       -g  Show event group information.
 
 Measurements can be started with events disabled using --delay=-1 option. Optionally
 send control command completion ('ack\n') to ack-fd descriptor to synchronize with the
tools/perf/builtin-record.c +1 -0
index e9874832dc27..d4be9f923a17 100644
@@ -1945,6 +1945,7 @@ static int __cmd_record(struct record *rec, int argc, const char **argv)
 			case EVLIST_CTL_CMD_UNSUPPORTED:
 			case EVLIST_CTL_CMD_ENABLE:
 			case EVLIST_CTL_CMD_DISABLE:
+			case EVLIST_CTL_CMD_EVLIST:
 			default:
 				break;
 			}
tools/perf/builtin-stat.c +1 -0
index 4c43c5e14fd4..b0180c62c4da 100644
@@ -588,6 +588,7 @@ static void process_evlist(struct evlist *evlist, unsigned int interval)
 		case EVLIST_CTL_CMD_SNAPSHOT:
 		case EVLIST_CTL_CMD_ACK:
 		case EVLIST_CTL_CMD_UNSUPPORTED:
+		case EVLIST_CTL_CMD_EVLIST:
 		default:
 			break;
 		}
tools/perf/util/evlist.c +41 -0

65 lines not on this page · raw message

tools/perf/util/evlist.h +2 -0
index 1aae75895dea..e79c64d81d21 100644
@@ -330,6 +330,7 @@ struct evsel *evlist__reset_weak_group(struct evlist *evlist, struct evsel *evse
 #define EVLIST_CTL_CMD_DISABLE_TAG "disable"
 #define EVLIST_CTL_CMD_ACK_TAG     "ack\n"
 #define EVLIST_CTL_CMD_SNAPSHOT_TAG "snapshot"
+#define EVLIST_CTL_CMD_EVLIST_TAG "evlist"
 
 #define EVLIST_CTL_CMD_MAX_LEN 64
 
@@ -339,6 +340,7 @@ enum evlist_ctl_cmd {
 	EVLIST_CTL_CMD_DISABLE,
 	EVLIST_CTL_CMD_ACK,
 	EVLIST_CTL_CMD_SNAPSHOT,
+	EVLIST_CTL_CMD_EVLIST,
 };
 
 int evlist__parse_control(const char *str, int *ctl_fd, int *ctl_fd_ack, bool *ctl_fd_close);
tools/perf/util/evsel_fprintf.c +2 -0
index fb498a723a00..bfedd7b23521 100644
@@ -100,6 +100,7 @@ int evsel__fprintf(struct evsel *evsel, struct perf_attr_details *details, FILE
 	return ++printed;
 }
 
+#ifndef PYTHON_PERF
 int sample__fprintf_callchain(struct perf_sample *sample, int left_alignment,
 			      unsigned int print_opts, struct callchain_cursor *cursor,
 			      struct strlist *bt_stop_list, FILE *fp)
@@ -239,3 +240,4 @@ int sample__fprintf_sym(struct perf_sample *sample, struct addr_location *al,
 
 	return printed;
 }
+#endif /* PYTHON_PERF */
tools/perf/util/python-ext-sources +1 -0
index a9d9c142eb7c..71b753523fac 100644
@@ -10,6 +10,7 @@ util/python.c
 util/cap.c
 util/evlist.c
 util/evsel.c
+util/evsel_fprintf.c
 util/perf_event_attr_fprintf.c
 util/cpumap.c
 util/memswap.c
tools/perf/util/setup.py +1 -1
index c5e3e9a68162..483f05004e68 100644
@@ -43,7 +43,7 @@ class install_lib(_install_lib):
 
 cflags = getenv('CFLAGS', '').split()
 # switch off several checks (need to be at the end of cflags list)
-cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls' ]
+cflags += ['-fno-strict-aliasing', '-Wno-write-strings', '-Wno-unused-parameter', '-Wno-redundant-decls', '-DPYTHON_PERF' ]
 if not cc_is_clang:
     cflags += ['-Wno-cast-function-type' ]
 
Signature · 1 line
2.26.2