#!/bin/bash

GREEN='\033[0;32m'
NC='\033[0m'

mcu_end=$(cat /proc/bootloader_log  | grep "SPL$" | awk '{print $1}' | tr -d ',' | awk '{printf $1/1000000}')
spl_end=$(cat /proc/bootloader_log  | grep "end phase" | awk '{print $1}' | tr -d ',' | awk '{printf $1/1000000}')
optee_end=$(cat /proc/bootloader_log  | grep board_init_f | awk '{print $1}' | tr -d ',' | awk '{printf $1/1000000}')
uboot_end=$(cat /proc/bootloader_log  | grep start_kernel | awk '{print $1}' | tr -d ',' | awk '{printf $1/1000000}')
kernel_end=$(dmesg | grep "Freeing unused kernel memory" | awk '{print $2}' | tr -d ']')
#[ -z "${kernel_end}" ] && kernel_end=$(grep "Freeing unused kernel memory" /log/kernel/message* /log/kernel/archive/SuperSoC_Kernel-*$(cat /log/reset_count.txt)* | awk -F '[' '{print $2}' | awk '{print $1}' | tr -d ']')
[ -z "${kernel_end}" ] && echo "WARNING: can not get kernel boottime, please reboot and test!" && exit 1
initramfs_end=$(dmesg  | grep EXT4 | grep -m 1 "mounted filesystem" | awk '{print $2}' | tr -d ']')
systemd_start=$(systemd-analyze time | head -n 1 | awk '{print $4}' | tr -d 's')
echo "${systemd_start}" | grep -q m  && systemd_start=$(echo "${systemd_start}" | awk '{print $1/1000}')
initramfs_to_systemd=$(echo "${systemd_start} ${initramfs_end}" | awk '{print $1-$2}')
appinit_end=$(systemd-analyze critical-chain hobot-appinit.service | awk '/hobot-appinit.service/{print $NF}' | tr -d '@s')
echo "${appinit_end}" | grep -q m && appinit_end=$(echo "${appinit_end}" | awk '{print $1/1000}')

if [ -e /proc/lantin_profiling/vm_info ]; then
    lantin_duration=$(cat /proc/lantin_profiling/vm_info | grep lantinhv_boot_time | awk -F '[: ]' '{print $2/1000000000}')
    boottime=$(echo "${uboot_end} ${lantin_duration} ${initramfs_end}" | awk '{print $1+$2+$3}')
else
    boottime=$(echo "${uboot_end} ${initramfs_end}" | awk '{print $1+$2}')
fi
boottime_to_appinit=$(echo "${boottime} ${initramfs_to_systemd} ${appinit_end}" | awk '{print $1+$2+$3}')
hsm_sbl_bist_mcu=${mcu_end}
spl=$(echo "${spl_end} ${mcu_end}" | awk '{print $1-$2}')
bl31_optee=$(echo "${optee_end} ${spl_end}" | awk '{print $1-$2}')
uboot=$(echo "${uboot_end} ${optee_end}" | awk '{print $1-$2}')
kernel=${kernel_end}
initramfs=$(echo "${initramfs_end} ${kernel_end}" | awk '{print $1-$2}')
systemd_to_appinit=$(echo "${initramfs_to_systemd} ${appinit_end}" | awk '{print $1+$2}')


echo -en "${GREEN}boottime: ${boottime} s${NC}\n"
echo -en "${GREEN}boottime to appinit: ${boottime_to_appinit} s${NC}\n"
echo "    hsm sbl bist mcu: ${hsm_sbl_bist_mcu} s"
echo "    spl: ${spl} s"
echo "    bl31 optee: ${bl31_optee} s"
echo "    uboot: ${uboot} s"
if [ -e /proc/lantin_profiling/vm_info ]; then
    echo "    lantin: ${lantin_duration} s"
fi
echo "    kernel: ${kernel} s"
echo "    initramfs: ${initramfs} s"
echo "    systemd ~ appinit: ${systemd_to_appinit} s"
echo ""

echo -en "${GREEN}systemd service time:${NC}\n"
systemd-analyze critical-chain hobot-mount.service | grep hobot-mount.service | grep -v @
systemd-analyze critical-chain hobot-loadko.service | grep hobot-loadko.service | grep -v @
systemd-analyze critical-chain hobot-defaultip.service | grep hobot-defaultip.service | grep -v @
systemd-analyze critical-chain hobot-otatool.service | grep hobot-otatool.service | grep -v @
systemd-analyze critical-chain hobot-appinit.service | grep -e @ -e + | grep -v "The time"
echo ""

echo -en "${GREEN}spl to uboot bootstage time:${NC}\n"
cat /proc/bootloader_log | grep "Timer summary in microseconds" -A 150 | grep -v "Starting kernel"

dmesg  | grep initcall | grep -q usecs && \
echo -en "${GREEN}kernel initcall time:${NC}\n" && \
dmesg | grep " initcall " | grep -v " calling " | tr '+' ' ' | awk '{print $1$2"   "$(NF-1)" "$NF"\t"$4}' | sort -nrk 2 | head -n 50

echo ""
exit 0
