diff mbox series

[V2,2/2] tools/perf/tests: perf all metricgroups test fails when perf_event access is restricted

Message ID 20230804050047.94240-2-atrajeev@linux.vnet.ibm.com (mailing list archive)
State Handled Elsewhere
Headers show
Series [V2,1/2] tools/perf/tests: perf all metrics test fails when perf_event access is restricted | expand

Commit Message

Athira Rajeev Aug. 4, 2023, 5 a.m. UTC
Perf all metricgroups test fails as below when perf_event access
is restricted.

    ./perf test -v "perf all metricgroups test"
    Testing Memory_BW
    Error:
    Access to performance monitoring and observability operations is limited.
    Enforced MAC policy settings (SELinux) can limit access to performance
    access to performance monitoring and observability operations for processes
    without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.

    test child finished with -1
    ---- end ----
    perf all metricgroups test: FAILED!

Fix the testcase to skip those metric events which needs perf_event access
explicitly. The exit code of the testcase is based on return code of
the perf stat command ( enabled by set -e option ). Hence save the
exit status in a variable and use that to decide success or fail for the
testcase.

Signed-off-by: Athira Rajeev <atrajeev@linux.vnet.ibm.com>
---
Changelog:
v1 -> v2:
 Changed the condition to use "echo" and "grep" so it works on
 Posix shell as well.

 tools/perf/tests/shell/stat_all_metricgroups.sh | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

Comments

Disha Goel Aug. 7, 2023, 2:44 p.m. UTC | #1
On 04/08/23 10:30 am, Athira Rajeev wrote:
> Perf all metricgroups test fails as below when perf_event access
> is restricted.
>
>      ./perf test -v "perf all metricgroups test"
>      Testing Memory_BW
>      Error:
>      Access to performance monitoring and observability operations is limited.
>      Enforced MAC policy settings (SELinux) can limit access to performance
>      access to performance monitoring and observability operations for processes
>      without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.
>
>      test child finished with -1
>      ---- end ----
>      perf all metricgroups test: FAILED!
>
> Fix the testcase to skip those metric events which needs perf_event access
> explicitly. The exit code of the testcase is based on return code of
> the perf stat command ( enabled by set -e option ). Hence save the
> exit status in a variable and use that to decide success or fail for the
> testcase.
>
> Signed-off-by: Athira Rajeev<atrajeev@linux.vnet.ibm.com>

With this patch applied(on power) perf metricgroups test works correctly when perf_event access is restricted.

  # ./perf test "perf all metricgroups test"
  96: perf all metricgroups test                                      : Ok

Tested-by: Disha Goel<disgoel@linux.ibm.com>

> ---
> Changelog:
> v1 -> v2:
>   Changed the condition to use "echo" and "grep" so it works on
>   Posix shell as well.
>
>   tools/perf/tests/shell/stat_all_metricgroups.sh | 14 +++++++++++---
>   1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/tools/perf/tests/shell/stat_all_metricgroups.sh b/tools/perf/tests/shell/stat_all_metricgroups.sh
> index cb35e488809a..eaa5e1172294 100755
> --- a/tools/perf/tests/shell/stat_all_metricgroups.sh
> +++ b/tools/perf/tests/shell/stat_all_metricgroups.sh
> @@ -2,11 +2,19 @@
>   # perf all metricgroups test
>   # SPDX-License-Identifier: GPL-2.0
>
> -set -e
> -
>   for m in $(perf list --raw-dump metricgroups); do
>     echo "Testing $m"
> -  perf stat -M "$m" -a true
> +  result=$(perf stat -M "$m" -a true 2>&1)
> +  rc=$?
> +  # Skip if there is no access to perf_events monitoring
> +  # Otherwise exit based on the return code of perf comamnd.
> +  if echo "$result" | grep -q "Access to performance monitoring and observability operations is limited";
> +  then
> +      continue
> +  else
> +      [ $rc -ne 0 ] && exit $rc
> +  fi
> +
>   done
>
>   exit 0
Arnaldo Carvalho de Melo Aug. 7, 2023, 9:19 p.m. UTC | #2
Em Mon, Aug 07, 2023 at 08:14:39PM +0530, Disha Goel escreveu:
> On 04/08/23 10:30 am, Athira Rajeev wrote:
> > Perf all metricgroups test fails as below when perf_event access
> > is restricted.
> > 
> >      ./perf test -v "perf all metricgroups test"
> >      Testing Memory_BW
> >      Error:
> >      Access to performance monitoring and observability operations is limited.
> >      Enforced MAC policy settings (SELinux) can limit access to performance
> >      access to performance monitoring and observability operations for processes
> >      without CAP_PERFMON, CAP_SYS_PTRACE or CAP_SYS_ADMIN Linux capability.
> > 
> >      test child finished with -1
> >      ---- end ----
> >      perf all metricgroups test: FAILED!
> > 
> > Fix the testcase to skip those metric events which needs perf_event access
> > explicitly. The exit code of the testcase is based on return code of
> > the perf stat command ( enabled by set -e option ). Hence save the
> > exit status in a variable and use that to decide success or fail for the
> > testcase.

I wonder if we shouldn't somehow check if the credentials needed to
performing a test shouldn't be checked before trying it. This way we
would check if the check that the tool or the kernel is doing is the
appropriate one.

I.e. the kernel refusal for doing something may be an error.

- Arnaldo

> > Signed-off-by: Athira Rajeev<atrajeev@linux.vnet.ibm.com>
> 
> With this patch applied(on power) perf metricgroups test works correctly when perf_event access is restricted.
> 
>  # ./perf test "perf all metricgroups test"
>  96: perf all metricgroups test                                      : Ok
> 
> Tested-by: Disha Goel<disgoel@linux.ibm.com>
> 
> > ---
> > Changelog:
> > v1 -> v2:
> >   Changed the condition to use "echo" and "grep" so it works on
> >   Posix shell as well.
> > 
> >   tools/perf/tests/shell/stat_all_metricgroups.sh | 14 +++++++++++---
> >   1 file changed, 11 insertions(+), 3 deletions(-)
> > 
> > diff --git a/tools/perf/tests/shell/stat_all_metricgroups.sh b/tools/perf/tests/shell/stat_all_metricgroups.sh
> > index cb35e488809a..eaa5e1172294 100755
> > --- a/tools/perf/tests/shell/stat_all_metricgroups.sh
> > +++ b/tools/perf/tests/shell/stat_all_metricgroups.sh
> > @@ -2,11 +2,19 @@
> >   # perf all metricgroups test
> >   # SPDX-License-Identifier: GPL-2.0
> > 
> > -set -e
> > -
> >   for m in $(perf list --raw-dump metricgroups); do
> >     echo "Testing $m"
> > -  perf stat -M "$m" -a true
> > +  result=$(perf stat -M "$m" -a true 2>&1)
> > +  rc=$?
> > +  # Skip if there is no access to perf_events monitoring
> > +  # Otherwise exit based on the return code of perf comamnd.
> > +  if echo "$result" | grep -q "Access to performance monitoring and observability operations is limited";
> > +  then
> > +      continue
> > +  else
> > +      [ $rc -ne 0 ] && exit $rc
> > +  fi
> > +
> >   done
> > 
> >   exit 0
diff mbox series

Patch

diff --git a/tools/perf/tests/shell/stat_all_metricgroups.sh b/tools/perf/tests/shell/stat_all_metricgroups.sh
index cb35e488809a..eaa5e1172294 100755
--- a/tools/perf/tests/shell/stat_all_metricgroups.sh
+++ b/tools/perf/tests/shell/stat_all_metricgroups.sh
@@ -2,11 +2,19 @@ 
 # perf all metricgroups test
 # SPDX-License-Identifier: GPL-2.0
 
-set -e
-
 for m in $(perf list --raw-dump metricgroups); do
   echo "Testing $m"
-  perf stat -M "$m" -a true
+  result=$(perf stat -M "$m" -a true 2>&1)
+  rc=$?
+  # Skip if there is no access to perf_events monitoring
+  # Otherwise exit based on the return code of perf comamnd.
+  if echo "$result" | grep -q "Access to performance monitoring and observability operations is limited";
+  then
+      continue
+  else
+      [ $rc -ne 0 ] && exit $rc
+  fi
+
 done
 
 exit 0