#!/bin/bash
# The script generated by PIPA-TREE is used to collect performance data.
# Please check whether it meets expectations before running.
# ZJU SPAIL(https://github.com/ZJU-SPAIL) reserves all rights.
# Generated at 2025-09-08T20:57:59.810596

# Check if sar and perf are available
if ! command -v sar &> /dev/null; then
echo "sar command not found. Please install sar."
exit 1
fi

if ! command -v perf &> /dev/null; then
echo "perf command not found. Please install perf."
exit 1
fi

WORKSPACE=./pipa-data/


MODE=${1:-counting}

if [ "$MODE" = "counting" ] || [ "$MODE" = "" ]; then
  # Execute counting mode commands
  LC_ALL='C' sar -A -f $WORKSPACE/sar.dat >$WORKSPACE/sar.txt
  sadf -d $WORKSPACE/sar.dat -- -A > $WORKSPACE/sar.csv
elif [ "$MODE" = "profiling" ]; then
  # Execute profiling mode commands
  perf script -i $WORKSPACE/perf.data -I --show-kernel-path --header > $WORKSPACE/perf.script
  perf report -i $WORKSPACE/perf.data -I --header > $WORKSPACE/perf.report
  perf buildid-list -i $WORKSPACE/perf.data > $WORKSPACE/perf.buildid
else
  # Invalid parameter
  echo "Invalid parameter. Usage: $0 [counting|profiling]"
exit 1
fi
