#! /bin/bash
set -e

ROOTPATH="/sys/fs/cgroup/memory"
CGROUP_TYPE=":memory:"

usage() { echo "Usage: $0 [-n <second>] [-p <pid>]" 1>&2; exit 1; }

while getopts ":n:p:" o; do
    case "${o}" in
        n)
            n=${OPTARG}
            ;;
        p)
            p=${OPTARG}
            ;;
        *)
            usage
            ;;
    esac
done
shift $((OPTIND-1))


check_pid()
{
    if [ -f "$TARGET_GROUP" ]; then
        GROUP_STRING=`cat $TARGET_GROUP`
    else
        echo "pid $1 not exist"
        exit 1
    fi

}

get_once_bilistat_arr()
{
    if [ -z "${FULL_PATH}" ] ; then
        array=(${GROUP_STRING})
        for VAR in ${array[@]}
        do
            if [[ $VAR == *$CGROUP_TYPE* ]]; then
                TASK_PATH=${VAR#*$CGROUP_TYPE}
                FULL_PATH=${ROOTPATH}${TASK_PATH}
                echo $FULL_PATH
            fi
        done  
    fi    

    if [ -d "$FULL_PATH" ]; then
        cd $FULL_PATH
        BILI_STAT=`cat memory.bili_stat`
        cd - > /tmp/lwy.txt  
    else
        echo "directory $FULL_PATH not exist"
        exit 1
    fi   

    unset array
    while read -r line; do
        array+=("$line")
    done <<< "$BILI_STAT"

}

display_history_bilistat()
{
    DIRECT_RECLAIM=${array[0]}
    MEMCG_RECLAIM=${array[1]}
    DIRECT_COMPACT=${array[2]}
    ASYNC_RECLAIM=${array[3]}
    echo "type 0-2 2-4 4-8 8-16 16-32 32-64 46-128 128-256 256-512 512-1024 1024-2048 2048-4096 4096-8192 >=8192 total_ms total_count" > /tmp/lwy.txt
    echo "direct_reclaim $DIRECT_RECLAIM" >> /tmp/lwy.txt
    echo "memcg_reclaim $MEMCG_RECLAIM" >> /tmp/lwy.txt
    echo "direct_compact $DIRECT_COMPACT" >> /tmp/lwy.txt
    echo "async_reclaim $ASYNC_RECLAIM" >> /tmp/lwy.txt
    cat /tmp/lwy.txt | column -t
    
}

compare_history_bilistat()
{
    get_once_bilistat_arr
    BILI_ARR0=(${BILI_STAT})
    display_history_bilistat
    #echo "$BILI_ARR0"

    sleep ${n}
    echo "-------------------------after ${n}s-----------------------------"

    get_once_bilistat_arr
    BILI_ARR1=(${BILI_STAT})
    display_history_bilistat
    #echo "BILI_ARR1 $BILI_ARR1"
    #echo "array ${array[*]}"
    echo "----------------------------diff---------------------------------"

    for ((i=0; i<49; i++))
    do
        if [ "${BILI_ARR0[$i]}" ==  "${BILI_ARR1[$i]}" ]; then
            DIFF[$i]="0"
        else
            #echo "i $i ${BILI_ARR1[$i]} ${BILI_ARR0[$i]}"
            DIFF[$i]=$((${BILI_ARR1[$i]}-${BILI_ARR0[$i]}))
            #echo "DIFF  ${DIFF[$i]}"
        fi

        INDEX=$(($i%16))
        if [ $i -lt 16 ]; then
            LINE0[$i]=${DIFF[$i]}
            else
                if [ $i -lt 32 ]; then
                    LINE1[$INDEX]=${DIFF[$i]}
                   # echo "${LINE1[` expr $i % 16 `]}"??
                    else
                        if [ $i -lt 48 ]; then
                            LINE2[$INDEX]=${DIFF[$i]}
                            else
                            LINE3[$INDEX]=${DIFF[$i]}
                        fi
                fi
        fi 
    done

    echo "type 0-2 2-4 4-8 8-16 16-32 32-64 46-128 128-256 256-512 512-1024 1024-2048 2048-4096 4096-8192 >=8192 total_ms total_count" > /tmp/lwy.txt
    echo "direct_reclaim ${LINE0[*]}" >> /tmp/lwy.txt
    echo "memcg_reclaim ${LINE1[*]}" >> /tmp/lwy.txt
    echo "direct_compact ${LINE2[*]}" >> /tmp/lwy.txt
    echo "async_reclaim ${LINE3[*]}" >> /tmp/lwy.txt 
    cat /tmp/lwy.txt | column -t
}

end()
{
    if [ -f /tmp/lwy.txt ]; then
        rm /tmp/lwy.txt
    fi
    exit 0 
}


if [ -z "${n}" ] && [ -z "${p}" ]; then
    FULL_PATH=$ROOTPATH
    get_once_bilistat_arr
    display_history_bilistat
    end
fi

if [ -z "${n}" ] && [ ! -z "${p}" ]; then
    TARGET_GROUP="/proc/${p}/cgroup"
    check_pid
    get_once_bilistat_arr
    display_history_bilistat
    end
fi

if [ ! -z "${n}" ] && [ -z "${p}" ]; then
    FULL_PATH=$ROOTPATH
    compare_history_bilistat 
    end

fi

if [ ! -z "${n}" ] && [ ! -z "${p}" ]; then
    TARGET_GROUP="/proc/${p}/cgroup"
    check_pid
    compare_history_bilistat 
    end

fi

