[4/8] perf test: Wait for a new thread when testing --per-thread record

Message ID 20221020172643.3458767-5-namhyung@kernel.org
State New
Headers
Series perf test: Improve perf record tests (v2) |

Commit Message

Namhyung Kim Oct. 20, 2022, 5:26 p.m. UTC
  Just running the target program is not enough to test multi-thread
target because it'd be racy perf vs target startup.  I used the
initial delay but it cannot guarantee for perf to see the thread.

Instead, use wait_for_threads helper from shell/lib/waiting.sh to make
sure it starts the sibling thread first.  Then perf record can use -p
option to profile the target process.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/tests/shell/record.sh | 27 +++++++++++++++++++++++++++
 1 file changed, 27 insertions(+)
  

Comments

Ian Rogers Oct. 20, 2022, 11:57 p.m. UTC | #1
On Thu, Oct 20, 2022 at 10:26 AM Namhyung Kim <namhyung@kernel.org> wrote:
>
> Just running the target program is not enough to test multi-thread
> target because it'd be racy perf vs target startup.  I used the
> initial delay but it cannot guarantee for perf to see the thread.
>
> Instead, use wait_for_threads helper from shell/lib/waiting.sh to make
> sure it starts the sibling thread first.  Then perf record can use -p
> option to profile the target process.
>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Acked-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
>  tools/perf/tests/shell/record.sh | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
>
> diff --git a/tools/perf/tests/shell/record.sh b/tools/perf/tests/shell/record.sh
> index 952981481239..d1640d1daf2e 100755
> --- a/tools/perf/tests/shell/record.sh
> +++ b/tools/perf/tests/shell/record.sh
> @@ -4,6 +4,9 @@
>
>  set -e
>
> +shelldir=$(dirname "$0")
> +. "${shelldir}"/lib/waiting.sh
> +
>  err=0
>  perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
>  testprog=$(mktemp /tmp/__perf_test.prog.XXXXXX)
> @@ -96,6 +99,30 @@ test_per_thread() {
>      err=1
>      return
>    fi
> +
> +  # run the test program in background (forever)
> +  ${testprog} 1 &
> +  TESTPID=$!
> +
> +  rm -f "${perfdata}"
> +
> +  wait_for_threads ${TESTPID} 2
> +  perf record -p "${TESTPID}" --per-thread -o "${perfdata}" sleep 1 2> /dev/null
> +  kill ${TESTPID}
> +
> +  if [ ! -e "${perfdata}" ]
> +  then
> +    echo "Per-thread record [Failed record -p]"
> +    err=1
> +    return
> +  fi
> +  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
> +  then
> +    echo "Per-thread record [Failed -p missing output]"
> +    err=1
> +    return
> +  fi
> +
>    echo "Basic --per-thread mode test [Success]"
>  }
>
> --
> 2.38.0.135.g90850a2211-goog
>
  
Adrian Hunter Oct. 21, 2022, 8:45 a.m. UTC | #2
On 20/10/22 20:26, Namhyung Kim wrote:
> Just running the target program is not enough to test multi-thread
> target because it'd be racy perf vs target startup.  I used the
> initial delay but it cannot guarantee for perf to see the thread.
> 
> Instead, use wait_for_threads helper from shell/lib/waiting.sh to make
> sure it starts the sibling thread first.  Then perf record can use -p
> option to profile the target process.
> 
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Reviewed-by: Adrian Hunter <adrian.hunter@intel.com>

> ---
>  tools/perf/tests/shell/record.sh | 27 +++++++++++++++++++++++++++
>  1 file changed, 27 insertions(+)
> 
> diff --git a/tools/perf/tests/shell/record.sh b/tools/perf/tests/shell/record.sh
> index 952981481239..d1640d1daf2e 100755
> --- a/tools/perf/tests/shell/record.sh
> +++ b/tools/perf/tests/shell/record.sh
> @@ -4,6 +4,9 @@
>  
>  set -e
>  
> +shelldir=$(dirname "$0")
> +. "${shelldir}"/lib/waiting.sh
> +
>  err=0
>  perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
>  testprog=$(mktemp /tmp/__perf_test.prog.XXXXXX)
> @@ -96,6 +99,30 @@ test_per_thread() {
>      err=1
>      return
>    fi
> +
> +  # run the test program in background (forever)
> +  ${testprog} 1 &
> +  TESTPID=$!
> +
> +  rm -f "${perfdata}"
> +
> +  wait_for_threads ${TESTPID} 2
> +  perf record -p "${TESTPID}" --per-thread -o "${perfdata}" sleep 1 2> /dev/null
> +  kill ${TESTPID}
> +
> +  if [ ! -e "${perfdata}" ]
> +  then
> +    echo "Per-thread record [Failed record -p]"
> +    err=1
> +    return
> +  fi
> +  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
> +  then
> +    echo "Per-thread record [Failed -p missing output]"
> +    err=1
> +    return
> +  fi
> +
>    echo "Basic --per-thread mode test [Success]"
>  }
>
  

Patch

diff --git a/tools/perf/tests/shell/record.sh b/tools/perf/tests/shell/record.sh
index 952981481239..d1640d1daf2e 100755
--- a/tools/perf/tests/shell/record.sh
+++ b/tools/perf/tests/shell/record.sh
@@ -4,6 +4,9 @@ 
 
 set -e
 
+shelldir=$(dirname "$0")
+. "${shelldir}"/lib/waiting.sh
+
 err=0
 perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
 testprog=$(mktemp /tmp/__perf_test.prog.XXXXXX)
@@ -96,6 +99,30 @@  test_per_thread() {
     err=1
     return
   fi
+
+  # run the test program in background (forever)
+  ${testprog} 1 &
+  TESTPID=$!
+
+  rm -f "${perfdata}"
+
+  wait_for_threads ${TESTPID} 2
+  perf record -p "${TESTPID}" --per-thread -o "${perfdata}" sleep 1 2> /dev/null
+  kill ${TESTPID}
+
+  if [ ! -e "${perfdata}" ]
+  then
+    echo "Per-thread record [Failed record -p]"
+    err=1
+    return
+  fi
+  if ! perf report -i "${perfdata}" -q | grep -q "${testsym}"
+  then
+    echo "Per-thread record [Failed -p missing output]"
+    err=1
+    return
+  fi
+
   echo "Basic --per-thread mode test [Success]"
 }