#!/bin/bash

source /OSM/script/os_load_base.sh
GProcess=/OSM/bin/perf
FLAG=0
SH_NAME=$(basename $0)
function loopRun()
{
    local counter=0
    while [ $counter -le 5 ]
    do
        ((counter++))
        if [ $counter -gt 3 ];then
            sleep 1
        fi
        
        "$@"
        local cmdRet=$?
        [ ${cmdRet} -eq 0 ] && return 0
        OS_echo "[$SH_NAME] error=${cmdRet}, Failed to run: $*." 
    done

    return 1
}
function restartProcess()
{
	local RETRY_TIMES=0
	
	#新增判断 如果文件大小为0，则拷贝基线进程过来。解决小补丁大小超过1M限制方案。
	if [ -f "/OSM/bin/perf" ]; then
		FILE_SIZE=`du -sb /OSM/bin/perf | awk '{print $1}'`
		while [ $? -ne 0 -a $RETRY_TIMES -lt 3 ]
		do
			((RETRY_TIMES++))
			OS_echo "OMM_PERF: check perf size, times($RETRY_TIMES)."
			sleep 0.1
			FILE_SIZE=`du -sb /OSM/bin/perf | awk '{print $1}'`
		done
		if [ $FILE_SIZE -eq 0 ]; then
			loopRun install /OSM/bin/perf_origin /OSM/bin/perf
			if [ $? -ne 0 ]; then
				return 1
			fi
			loopRun chmod 550 /OSM/bin/perf
			if [ $? -ne 0 ]; then
				return 1
			fi
			OS_echo "OMM_PERF: The perf size is $FILE_SIZE."
			return 0
		fi
		OS_echo "OMM_PERF: The perf size is $FILE_SIZE."
	fi
	
	if [ $FLAG -eq 1 ]; then 
		chmod 775 /OSM/bin/perf
		mv /OSM/bin/perf /OSM/bin/perf.tgz
		tar zxf /OSM/bin/perf.tgz -C /OSM/bin
		chmod 550 /OSM/bin/perf
		rm -f /OSM/bin/perf.tgz
	fi

    #判定该进程是否已经被加载，是否还在运行
    red=$(ps -ww -eo pid,cmd | grep $GProcess | grep -v grep | awk '{print $1}')
    OS_echo "OMM_PERF: oldPid: $red"
    if [ "$red" = "" ]; then
        #进程不在位，只有demon不在位的情况，同时如果不在位资源利用率也不会太高，此处不考虑，直接返回OK
        return 0
    else
        OS_echo "OMM_PERF: perf will be killed."
        daemon -s /OSM/bin/perf
		if [ $? -ne 0 ]; then
			OS_echo "stop perf faild."
			daemon -s /OSM/bin/perf
			if [ $? -ne 0 ]; then
			    OS_echo "stop perf faild again."
                #停进程失败了。直接return 不exit.不影响打补丁，只影响PERF补丁功能未带上。
			    return 1
			fi
		fi
		daemon /OSM/bin/perf "/OSM/bin/cgroup_manage.sh -g omm_perf"
        rednew=$(ps -ww -eo pid,cmd | grep $GProcess | grep -v grep | awk '{print $1}')
		RETRY_TIMES=0
		while [ -z "$rednew" -a $RETRY_TIMES -lt 5 ] || [ $red -eq $rednew -a $RETRY_TIMES -lt 5 ]
		do
			((RETRY_TIMES++))
			OS_echo "OMM_PERF: check perf status, times($RETRY_TIMES)."
			sleep 0.2
			rednew=$(ps -ww -eo pid,cmd | grep $GProcess | grep -v grep | awk '{print $1}')
		done
		
		OS_echo "OMM_PERF: newPid: $rednew"
        if [ "$rednew" = "" ] || [ $red -eq $rednew ]; then
			OS_echo "OMM_PERF: the old pid is $red, new pid is $rednew."
            return 1
		fi
		
        return 0
    fi

    return 0
}

function PATCH_START()
{
    return 
}
function PATCH_END()
{
	FLAG=1
    restartProcess
    return
}
function PATCH_ROLLSTART()
{
    return
}
function PATCH_ROLLEND()
{
	red=$(file /OSM/bin/perf | awk '{print $2}')
	if [ "$red" = "ELF" ]; then
	FLAG=0
	OS_echo "OMM_PERF: perf FLAG 0."
	else
	FLAG=1
	OS_echo "OMM_PERF: perf FLAG 1."
	fi
	restartProcess
	return
}
#start：打补丁前需要执行的操作，脚本的执行时间点在替换文件前
#end：打补丁后需要执行的操作，脚本的执行时间点在替换文件后
#rollstart：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之前。
#rollend：回滚补丁时需要执行的操作，脚本的执行时间点在文件被恢复之后。
#PATCH_CLOSE_SWITCH:先读取性能统计开关的状态，将其值保存到switch中,并关闭性能统计开关
#PATCH_OPEN_SWITCH:如果用户之前是打开状态，将其开关打开
OS_echo "OMM_PERF: command is ($1)."
L_CMD=$1
case $L_CMD in
        "start") 
                PATCH_START
                ;;
        "end") 
                PATCH_END
                ;;
        "rollstart") 
                PATCH_ROLLSTART
                ;;
        "rollend") 
                PATCH_ROLLEND
                ;;
        *)
                OS_echo "unsupported option."
esac
exit 0

