#!/bin/bash
app_name=$1
if [[ -z ${app_name} ]]; then
  echo "[ERROR] Input app_name is null"
  exit 1
else
  echo "[INFO] app_name is ${app_name}"
fi
pid_num=""
while [[ -z ${pid_num} ]]; do
  pid_num=$(ps -ef | grep ${app_name} | grep -v grep | grep -v perf | awk -F " " '{print $2}')
  echo "[INFO] Finding ${app_name}"
  sleep 1
done

echo "[INFO] ${app_name} pid is ${pid_num}"
echo "[INFO] Start to perf ${app_name}"
if [[ ! -d ${app_name} ]]; then
  mkdir -p ${app_name}
fi
perf record -e inst_retired:u,cache-misses:u -o ${app_name}/perf.data -p ${pid_num} >>${app_name}/perf.log 2>&1
echo "[INFO] End to perf ${app_name}!!! result is ${app_name}/perf.data"
