#!/bin/bash
########################################################################################################
#Program: This shell script is used to change the CPUs' cpufreq governor
#	from powersave to performance under the kernel v4.1.
#Author: Sky.Zhang
#Date: 12/15/2017
#Version: 0.1
########################################################################################################
#Change List
#	1. And pcc_cpufreq modules to the method insertModules.
#Author: Sky.Zhang
#Date: 06/12/2018
#Version: 0.2
########################################################################################################
#Change List
#	1. Delete cpufreq_ondemand modules to the method insertModules.
#	2. Add the logic judgement of status the CPU governor driver.
#Authro: Sky.Zhang
#Date: 06/20/2018
#Version: 0.3
########################################################################################################
#Change: List
#	1. Fix logic and grammmar erros.
#	2. And new judgement logic of driver intel_pstate.
#	3. And new judgement logic to judge the status of rpm tools .
#	4. Create log file to log the success or error log.
#	5. Change the  judgement logic of CPU governor driver module.
#Author: Sky.Zhang
#Date: 07/11/2018
#Versioin: 0.4
########################################################################################################
#Change List
#	1. Adjust the logic of install cpu governor management tools(cpufrequtils or kernel-tools).
#	2. Fix the bug of pcc-freq driver(Only for HP servers) couldn't be loaded for all threads.
#	3. Fix logic and grammar erros.
#Author: Sky.Zhang
#Date: 12/17/2018
#Version: 0.5
########################################################################################################

#Global arguments
cpuCount=`cat /proc/cpuinfo | grep -c processor`
perfMode=performance
logFile=/root/setCPUPerf.log

function usage() {
	echo -e "$0 ondemand|performance|powersave"
	echo -e "exapmle:"
	echo -e	"\t$0		Change CPU governor to ondemand."
	echo -e "\t$0 ondemand	Change CPU governor to ondemand."
	echo -e "\t$0 performance	Change CPU governor mode to performance."
	echo -e "\t$0 powersave	Change CPU governor mode to powersave."
	exit 
}

while getopts "h" arg
do
	case $arg in
		h)
		usage;;
	esac
done


#Check tools is installed or not.
function installTools() {
	isInstalled=`rpm -qa | grep cpufrequtils`
	if [ "${isInstalled}" != "" ]; then
		yum remove cpufrequtils* -y
	fi

	isInstalled=`rpm -qa | grep kernel-tools`
	if [ "${isInstalled}" == "" ]; then
		yum install kernel-tools -y
	fi
	
	isInstalled=`rpm -qa | grep  "kernel-tools"`
	
	if [ -z "${isInstalled}" ]; then
		echo -e "Something wrong with tools install with error code 0x01." | tee -a ${logFile}
		exit 1
	fi
}


#Change CPUs'governer.
function changeGoverner() {
	for((i=0;i<$cpuCount;i++))
	do
		$1 -c $i $2 -g ${perfMode}
	done
}

#Check CPUs' governer.
function checkGoverner() {
	for((i=0;i<$cpuCount;i++))
	do
		echo -e "CPU $i:`$1 -c $i $2 -p`"
	done
}

#Insert module
function insertAndCheckModules() {	
	driver=""
	modprobe acpi-cpufreq
        modprobe pcc_cpufreq

	driver=`cpupower frequency-info -d | grep -v analyzing`
        vendor=`dmidecode -t 1 |grep "Manufacturer"| awk '{print $NF}'`
 
       if [ "${vendor}" == "HP" ] && [ "${driver}" == "pcc-cpufreq"  ]; then
                count=0
                while [ ${count} -ne ${cpuCount} ]
                do
                        sleep 5s
                        modprobe -r pcc-cpufreq
                        sleep 5s
                        modprobe pcc-cpufreq
                        sleep 5s
                        count=`ls -ld /sys/devices/system/cpu/cpu[0-9]*/cpufreq | wc -l`
                done
        fi	
		
	if [ "${driver}" = "" ];then
		echo -e "There is no suitable driver for CPU, Please Check BIOS setting first." | tee -a ${logFile}
		echo -e "The error code is 0x02"
		exit 2
	fi
}

#The shell start from here.
#checkKernelVersion

if [ "$1" != "" ]; then
	if [ "$1" != "ondemand" ] && [ "$1" != "performance" ] && [ "$1" != "powersave" ]; then
		echo -e "The argument is wrong, it should be 'ondemand' or 'performance', and the error code is 0x03." | tee -a ${logFile}
		exit 3
	fi
	perfMode=$1
fi


rm -rf ${logFile}

installTools
insertAndCheckModules

changeGoverner "cpupower" "frequency-set"
checkGoverner "cpupower" "frequency-info"
