Skip to contentSkip to conversation

perf tools: Basic bash completion support

PATCH In review Frederic Weisbecker 3 participants 13 messages Aug 7, 2012 perf events

lore
Share & tools
lore
#0 lore raw

cover perf tools: Basic bash completion support

Hey,

Basic bash completion support. Only support perf subcommands and most -e basic event descriptor (no grouping).

I just have a small issue with tracepoints because of their ":" in the middle. It auto completes as long as we haven't yet reached the semicolon. Otherwise we need to add a double quote in the beginning of the expression. I'm quite a newbie in bash completion though, so I might find a subtelty later to solve this.

Frederic Weisbecker (2):
perf tools: Initial bash completion support
perf tools: Support for events bash completion

 tools/perf/Makefile            |    1 +
 tools/perf/bash_completion     |   24 ++++++++++++++
 tools/perf/builtin-list.c      |   14 ++++---
 tools/perf/perf.c              |   69 ++++++++++++++++++++++-----------------
 tools/perf/util/parse-events.c |   70 +++++++++++++++++++++++++---------------
 tools/perf/util/parse-events.h |    7 ++--
 6 files changed, 120 insertions(+), 65 deletions(-)
 create mode 100644 tools/perf/bash_completion
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 1/2 perf tools: Initial bash completion support

This implements bash completion for perf subcommands such as record, report, script, probe, etc...

Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/Makefile        |    1 +
 tools/perf/bash_completion |   20 +++++++++++++
 tools/perf/perf.c          |   69 +++++++++++++++++++++++++-------------------
 3 files changed, 60 insertions(+), 30 deletions(-)
 create mode 100644 tools/perf/bash_completion
tools/perf/Makefile +1 -0
index 35655c3..4000d72 100644
@@ -951,6 +951,7 @@ install: all
 	$(INSTALL) scripts/python/Perf-Trace-Util/lib/Perf/Trace/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'
 	$(INSTALL) scripts/python/*.py -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'
 	$(INSTALL) scripts/python/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/bin'
+	$(INSTALL) -m 755 bash_completion /etc/bash_completion.d/perf
 
 install-python_ext:
 	$(PYTHON_WORD) util/setup.py --quiet install --root='/$(DESTDIR_SQ)'
tools/perf/bash_completion new file +20 -0
new file mode 100644
index 0000000..3547703
@@ -0,0 +1,20 @@
+# perf completion
+
+have perf &&
+_perf()
+{
+	local cur
+
+	COMPREPLY=()
+	_get_comp_words_by_ref cur
+
+	# List perf subcommands
+	if [ $COMP_CWORD -eq 1 ]; then
+		cmds=$(perf --list-cmds)
+		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+	# Fall down to list regular files
+	else
+		_filedir
+	fi
+} &&
+complete -F _perf perf
tools/perf/perf.c +39 -30

93 lines not on this page · raw message

Signature · 1 line
1.7.5.4
#2 lore raw

Re: [PATCH 1/2] perf tools: Initial bash completion support

Quoted 2 lines · Frederic Weisbecker

This implements bash completion for perf subcommands such as record, report, script, probe, etc...

Love it!

Quoted 20 lines
Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
  tools/perf/Makefile        |    1 +
  tools/perf/bash_completion |   20 +++++++++++++
  tools/perf/perf.c          |   69 +++++++++++++++++++++++++-------------------
  3 files changed, 60 insertions(+), 30 deletions(-)

create mode 100644 tools/perf/bash_completion

tools/perf/Makefile +0 -0
index 35655c3..4000d72 100644

tools/perf/Makefile install: all · line 954

Quoted 4 lines
tools/perf/Makefile +1 -0
@@ -951,6 +951,7 @@ install: all
  	$(INSTALL) scripts/python/Perf-Trace-Util/lib/Perf/Trace/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'
  	$(INSTALL) scripts/python/*.py -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'
  	$(INSTALL) scripts/python/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/bin'
+	$(INSTALL) -m 755 bash_completion /etc/bash_completion.d/perf

$(DESTDIR_SQ) is need in front of the destination.

David

#3 lore raw

Re: [PATCH 1/2] perf tools: Initial bash completion support

Quoted 21 lines · David Ahern
Quoted 2 lines · Frederic Weisbecker

This implements bash completion for perf subcommands such as record, report, script, probe, etc...

Love it!

Quoted 15 lines
Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/Makefile        |    1 +
 tools/perf/bash_completion |   20 +++++++++++++
 tools/perf/perf.c          |   69 +++++++++++++++++++++++++-------------------
 3 files changed, 60 insertions(+), 30 deletions(-)
 create mode 100644 tools/perf/bash_completion
Quoted 11 lines

tools/perf/Makefile install: all · line 954

Quoted 9 lines
tools/perf/Makefile +1 -0
index 35655c3..4000d72 100644
@@ -951,6 +951,7 @@ install: all
 	$(INSTALL) scripts/python/Perf-Trace-Util/lib/Perf/Trace/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/Perf-Trace-Util/lib/Perf/Trace'
 	$(INSTALL) scripts/python/*.py -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'
 	$(INSTALL) scripts/python/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/bin'
+	$(INSTALL) -m 755 bash_completion /etc/bash_completion.d/perf

$(DESTDIR_SQ) is need in front of the destination.

Right. Fixing this.

Thanks.

Quoted 2 lines

David

#4 lore raw

PATCH 2/2 perf tools: Support for events bash completion

In reply to Frederic Weisbecker

Add basic bash completion for the -e option in record, top and stat subcommands. Only hardware, software and tracepoint events are supported.

Breakpoints, raw events and events grouping completion need more thinking.

Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/bash_completion     |    6 +++-
 tools/perf/builtin-list.c      |   14 ++++---
 tools/perf/util/parse-events.c |   70 +++++++++++++++++++++++++---------------
 tools/perf/util/parse-events.h |    7 ++--
 4 files changed, 61 insertions(+), 36 deletions(-)
tools/perf/bash_completion +5 -1
index 3547703..25f4d99 100644
@@ -6,12 +6,16 @@ _perf()
 	local cur
 
 	COMPREPLY=()
-	_get_comp_words_by_ref cur
+	_get_comp_words_by_ref cur prev
 
 	# List perf subcommands
 	if [ $COMP_CWORD -eq 1 ]; then
 		cmds=$(perf --list-cmds)
 		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+	# List possible events for -e option
+	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
+		cmds=$(perf list --raw-dump)
+		COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )
 	# Fall down to list regular files
 	else
 		_filedir
tools/perf/builtin-list.c +8 -6
index 6313b6e..bdcff81 100644
@@ -19,15 +19,15 @@ int cmd_list(int argc, const char **argv, const char *prefix __used)
 	setup_pager();
 
 	if (argc == 1)
-		print_events(NULL);
+		print_events(NULL, false);
 	else {
 		int i;
 
 		for (i = 1; i < argc; ++i) {
-			if (i > 1)
+			if (i > 2)
 				putchar('\n');
 			if (strncmp(argv[i], "tracepoint", 10) == 0)
-				print_tracepoint_events(NULL, NULL);
+				print_tracepoint_events(NULL, NULL, false);
 			else if (strcmp(argv[i], "hw") == 0 ||
 				 strcmp(argv[i], "hardware") == 0)
 				print_events_type(PERF_TYPE_HARDWARE);
@@ -36,13 +36,15 @@ int cmd_list(int argc, const char **argv, const char *prefix __used)
 				print_events_type(PERF_TYPE_SOFTWARE);
 			else if (strcmp(argv[i], "cache") == 0 ||
 				 strcmp(argv[i], "hwcache") == 0)
-				print_hwcache_events(NULL);
+				print_hwcache_events(NULL, false);
+			else if (strcmp(argv[i], "--raw-dump") == 0)
+				print_events(NULL, true);
 			else {
 				char *sep = strchr(argv[i], ':'), *s;
 				int sep_idx;
 
 				if (sep == NULL) {
-					print_events(argv[i]);
+					print_events(argv[i], false);
 					continue;
 				}
 				sep_idx = sep - argv[i];
@@ -51,7 +53,7 @@ int cmd_list(int argc, const char **argv, const char *prefix __used)
 					return -1;
 
 				s[sep_idx] = '\0';
-				print_tracepoint_events(s, s + sep_idx + 1);
+				print_tracepoint_events(s, s + sep_idx + 1, false);
 				free(s);
 			}
 		}
tools/perf/util/parse-events.c +44 -26

124 lines not on this page · raw message

tools/perf/util/parse-events.h +4 -3
index ee9c218..6fd9307 100644
@@ -96,10 +96,11 @@ void parse_events_update_lists(struct list_head *list_event,
 void parse_events_error(void *data, void *scanner, char const *msg);
 int parse_events__test(void);
 
-void print_events(const char *event_glob);
+void print_events(const char *event_glob, bool name_only);
 void print_events_type(u8 type);
-void print_tracepoint_events(const char *subsys_glob, const char *event_glob);
-int print_hwcache_events(const char *event_glob);
+void print_tracepoint_events(const char *subsys_glob, const char *event_glob,
+			     bool name_only);
+int print_hwcache_events(const char *event_glob, bool name_only);
 extern int is_valid_tracepoint(const char *event_string);
 
 extern int valid_debugfs_mount(const char *debugfs);
Signature · 1 line
1.7.5.4
#5 lore raw

Re: [PATCH 2/2] perf tools: Support for events bash completion

Quoted 33 lines · Frederic Weisbecker

Add basic bash completion for the -e option in record, top and stat subcommands. Only hardware, software and tracepoint events are supported.

Breakpoints, raw events and events grouping completion need more thinking.

Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
  tools/perf/bash_completion     |    6 +++-
  tools/perf/builtin-list.c      |   14 ++++---
  tools/perf/util/parse-events.c |   70 +++++++++++++++++++++++++---------------
  tools/perf/util/parse-events.h |    7 ++--
  4 files changed, 61 insertions(+), 36 deletions(-)
tools/perf/bash_completion +1 -1
index 3547703..25f4d99 100644
@@ -6,12 +6,16 @@ _perf()
  	local cur
 
  	COMPREPLY=()
-	_get_comp_words_by_ref cur
+	_get_comp_words_by_ref cur prev
 
  	# List perf subcommands

tools/perf/bash_completion _perf() · line 21

Quoted 10 lines
tools/perf/bash_completion +4 -0
  	if [ $COMP_CWORD -eq 1 ]; then
  		cmds=$(perf --list-cmds)
  		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+	# List possible events for -e option
+	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
+		cmds=$(perf list --raw-dump)
+		COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )
  	# Fall down to list regular files
  	else
  		_filedir

Any reason to show a file list except for -i and -o options? e.g.,

tools/perf/bash_completion +1 -1
index 25f4d99..be97349 100644
@@ -17,7 +17,7 @@ _perf()
        cmds=$(perf list --raw-dump)
        COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )
    # Fall down to list regular files
-   else
+   elif [[ $prev == "-o" || $prev == "-i" ]]; then
        _filedir
    fi
 } &&
#6 lore raw

Re: [PATCH 2/2] perf tools: Support for events bash completion

Quoted 33 lines · David Ahern
Quoted 32 lines · Frederic Weisbecker

Add basic bash completion for the -e option in record, top and stat subcommands. Only hardware, software and tracepoint events are supported.

Breakpoints, raw events and events grouping completion need more thinking.

Cc David Ahern <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/bash_completion     |    6 +++-
 tools/perf/builtin-list.c      |   14 ++++---
 tools/perf/util/parse-events.c |   70 +++++++++++++++++++++++++---------------
 tools/perf/util/parse-events.h |    7 ++--
 4 files changed, 61 insertions(+), 36 deletions(-)
tools/perf/bash_completion +1 -1
index 3547703..25f4d99 100644
@@ -6,12 +6,16 @@ _perf()
 	local cur
 
 	COMPREPLY=()
-	_get_comp_words_by_ref cur
+	_get_comp_words_by_ref cur prev
 
Quoted 13 lines

tools/perf/bash_completion _perf() · line 21

Quoted 11 lines
tools/perf/bash_completion +4 -0
 	# List perf subcommands
 	if [ $COMP_CWORD -eq 1 ]; then
 		cmds=$(perf --list-cmds)
 		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
+	# List possible events for -e option
+	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
+		cmds=$(perf list --raw-dump)
+		COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )
 	# Fall down to list regular files
 	else
 		_filedir

Any reason to show a file list except for -i and -o options? e.g.,

Yeah, for example with perf record when you pass a command to launch and profile.

In any case I think it's a better idea to keep this as a default. Not breaking the pre-existing default completion in the guarantee that the new completion is going to be more useful than a burden.

Quoted 15 lines
tools/perf/bash_completion +1 -1
index 25f4d99..be97349 100644
@@ -17,7 +17,7 @@ _perf()
        cmds=$(perf list --raw-dump)
        COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )
    # Fall down to list regular files
-   else
+   elif [[ $prev == "-o" || $prev == "-i" ]]; then
        _filedir
    fi
 } &&
#7 lore raw

Re: [PATCH 2/2] perf tools: Support for events bash completion

In reply to David Ahern
Quoted 5 lines

Review

Quoted 5 lines
  		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
(quoted diff) +4 -0
+	# List possible events for -e option
+	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
+		cmds=$(perf list --raw-dump)
+		COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )

Surely $cur should be quoted here...

Alan

#8 lore raw

Re: [PATCH 2/2] perf tools: Support for events bash completion

Quoted 8 lines · Alan Cox
Quoted 5 lines

Review

Quoted 5 lines
  		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
(quoted diff) +4 -0
+	# List possible events for -e option
+	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
+		cmds=$(perf list --raw-dump)
+		COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )

Surely $cur should be quoted here...

Right, fixing that too.

thanks.

Quoted 1 line

Alan

#9 lore raw
In reply to Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker

Hey,

Basic bash completion support. Only support perf subcommands and most -e basic event descriptor (no grouping).

I just have a small issue with tracepoints because of their ":" in the middle. It auto completes as long as we haven't yet reached the semicolon. Otherwise we need to add a double quote in the beginning of the expression. I'm quite a newbie in bash completion though, so I might find a subtelty later to solve this.

Tips: for testing, you need to "make install" and update the bash completion scripts:

	# make install
	$ . /etc/bash_completion
#10 lore raw
Quoted 12 lines · Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker

Hey,

Basic bash completion support. Only support perf subcommands and most -e basic event descriptor (no grouping).

I just have a small issue with tracepoints because of their ":" in the middle. It auto completes as long as we haven't yet reached the semicolon. Otherwise we need to add a double quote in the beginning of the expression. I'm quite a newbie in bash completion though, so I might find a subtelty later to solve this.

Quoted 6 lines

Tips: for testing, you need to "make install" and update the bash completion scripts:

	# make install
	$ . /etc/bash_completion

ANd you need to make sure the PATH hits the updated binary and not the default other wise you end up with:

/tmp/pbuild/perf recUnknown option: --list-cmds

Usage: perf [--version] [--help] COMMAND [ARGS]
Unknown option: --list-cmds

It's calling /usr/bin/perf with --list-cmds, versus the perf command I am running (/tmp/pbuild/perf). Any way to teach the completion to use the perf binary that the user is running?

David

#11 lore raw
Quoted 23 lines · David Ahern
Quoted 12 lines · Frederic Weisbecker
Quoted 10 lines · Frederic Weisbecker

Hey,

Basic bash completion support. Only support perf subcommands and most -e basic event descriptor (no grouping).

I just have a small issue with tracepoints because of their ":" in the middle. It auto completes as long as we haven't yet reached the semicolon. Otherwise we need to add a double quote in the beginning of the expression. I'm quite a newbie in bash completion though, so I might find a subtelty later to solve this.

Quoted 6 lines

Tips: for testing, you need to "make install" and update the bash completion scripts:

	# make install
	$ . /etc/bash_completion

ANd you need to make sure the PATH hits the updated binary and not the default other wise you end up with:

Quoted 8 lines

/tmp/pbuild/perf recUnknown option: --list-cmds

Usage: perf [--version] [--help] COMMAND [ARGS]
Unknown option: --list-cmds

It's calling /usr/bin/perf with --list-cmds, versus the perf command I am running (/tmp/pbuild/perf). Any way to teach the completion to use the perf binary that the user is running?

Ah good point.

Does the below work for you? I'll respin with that change.

tools/perf/bash_completion +5 -3
index 25f4d99..cba72a9 100644
@@ -3,18 +3,20 @@
 have perf &&
 _perf()
 {
-	local cur
+	local cur cmd
 
 	COMPREPLY=()
 	_get_comp_words_by_ref cur prev
 
+	cmd=${COMP_WORDS[0]}
+
 	# List perf subcommands
 	if [ $COMP_CWORD -eq 1 ]; then
-		cmds=$(perf --list-cmds)
+		cmds=$($cmd --list-cmds)
 		COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) )
 	# List possible events for -e option
 	elif [[ $prev == "-e" && "${COMP_WORDS[1]}" == @(record|stat|top) ]]; then
-		cmds=$(perf list --raw-dump)
+		cmds=$($cmd list --raw-dump)
 		COMPREPLY=( $( compgen -W '$cmds' -- $cur ) )
 	# Fall down to list regular files
 	else
#12 lore raw
Quoted 1 line · Frederic Weisbecker

Does the below work for you? I'll respin with that change.

It does.

David