#!/bin/bash

# Copyright (C) 2023 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

if [ -z ${ANDROID_BUILD_TOP} ]
then
  echo "Source envsetup.sh first"
  exit 1
fi

cd $(dirname "$0")/..

SUMMARY_FILE=AFDO_SUMMARY.txt

echo "Top functions from sampling profiles" > ${SUMMARY_FILE}
echo "Auto-generated by scripts/afdo_summary.sh" >> ${SUMMARY_FILE}
echo "=========================================" >> ${SUMMARY_FILE}
echo >> ${SUMMARY_FILE}

for f in sampling/*.afdo;
do
  echo ${f} >> ${SUMMARY_FILE}
  echo "====" >> ${SUMMARY_FILE}
  llvm-profdata show --sample --topn=16 ${f} | grep "Total sample" -A16 | tail -n+2 | tr -s ' ' | cut -d' ' -f6 | llvm-cxxfilt >> ${SUMMARY_FILE}
  echo >> ${SUMMARY_FILE}
  echo >> ${SUMMARY_FILE}
done
