diff mbox

[pm-qa,04/14] add script functions

Message ID 1311684424-8057-5-git-send-email-daniel.lezcano@linaro.org
State Accepted
Headers show

Commit Message

Daniel Lezcano July 26, 2011, 12:46 p.m. UTC
Add a common directory for all the functions we will use later in the test scripts.

Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>
---
 include/functions.sh |  251 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 251 insertions(+), 0 deletions(-)
 create mode 100644 include/functions.sh

\ No newline at end of file
diff mbox

Patch

diff --git a/include/functions.sh b/include/functions.sh
new file mode 100644
index 0000000..67c356a
--- /dev/null
+++ b/include/functions.sh
@@ -0,0 +1,251 @@ 
+#!/bin/bash
+#
+# PM-QA validation test suite for the power management on ARM
+#
+# Copyright (C) 2011, Linaro Limited.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+#
+# Contributors:
+#     Daniel Lezcano <daniel.lezcano@linaro.org> (IBM Corporation)
+#       - initial API and implementation
+#
+
+CPU_PATH="/sys/devices/system/cpu"
+TEST_NAME=$(basename ${0%.sh})
+PREFIX=$TEST_NAME
+BLOCK=$(basename $(pwd))
+
+setprefix_cpu() {
+    PREFIX=$BLOCK:$TEST_NAME/$1
+}
+
+log_begin() {
+    printf "%-75s" "$PREFIX $@ ... "
+}
+
+log_end() {
+    printf "$*\n"
+}
+
+log_skip() {
+    log_begin "$@"
+    log_end "SKIP"
+}
+
+for_each_cpu() {
+
+    local func=$1
+    shift 1
+
+    cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
+
+    for cpu in $cpus; do
+	setprefix_cpu $cpu
+	$func $cpu $@
+    done
+
+    return 0
+}
+
+for_each_governor() {
+
+    local cpu=$1
+    local func=$2
+    local dirpath=$CPU_PATH/$cpu/cpufreq
+    local governors=$(cat $dirpath/scaling_available_governors)
+    shift 2
+
+    for governor in $governors; do
+	$func $cpu $governor $@
+    done
+
+    return 0
+}
+
+for_each_frequency() {
+
+    local cpu=$1
+    local func=$2
+    local dirpath=$CPU_PATH/$cpu/cpufreq
+    local frequencies=$(cat $dirpath/scaling_available_frequencies)
+    shift 2
+
+    for frequency in $frequencies; do
+	$func $cpu $frequency $@
+    done
+
+    return 0
+}
+
+set_governor() {
+
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
+    local newgov=$2
+
+    echo $newgov > $dirpath
+}
+
+get_governor() {
+
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_governor
+
+    cat $dirpath
+}
+
+wait_latency() {
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq
+    local latency=$(cat $dirpath/cpuinfo_transition_latency)
+    local nrfreq=$(cat $dirpath/scaling_available_frequencies | wc -w)
+
+    nrfreq=$((nrfreq + 1))
+    ../utils/nanosleep $(($nrfreq * $latency))
+}
+
+frequnit() {
+    local freq=$1
+    local ghz=$(echo "scale=1;($freq / 1000000)" | bc -l)
+    local mhz=$(echo "scale=1;($freq / 1000)" | bc -l)
+
+    res=$(echo "($ghz > 1.0)" | bc -l)
+    if [ "$res" = "1" ]; then
+	echo $ghz GHz
+	return 0
+    fi
+
+    res=$(echo "($mhz > 1.0)" | bc -l)
+    if [ "$res" = "1" ];then
+	echo $mhz MHz
+	return 0
+    fi
+
+    echo $freq KHz
+}
+
+set_frequency() {
+
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq
+    local newfreq=$2
+    local setfreqpath=$dirpath/scaling_setspeed
+
+    echo $newfreq > $setfreqpath
+    wait_latency $cpu
+}
+
+get_frequency() {
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_cur_freq
+    cat $dirpath
+}
+
+get_max_frequency() {
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_max_freq
+    cat $dirpath
+}
+
+get_min_frequency() {
+    local cpu=$1
+    local dirpath=$CPU_PATH/$cpu/cpufreq/scaling_min_freq
+    cat $dirpath
+}
+
+check() {
+
+    local descr=$1
+    local func=$2
+    shift 2;
+
+    log_begin "checking $descr"
+
+    $func $@
+    if [ $? != 0 ]; then
+	log_end "FAIL"
+	return 1
+    fi
+
+    log_end "PASS"
+
+    return 0
+}
+
+check_cpufreq_files() {
+
+    local dirpath=$CPU_PATH/$1/cpufreq
+    shift 1
+
+    for i in $@; do
+	check "$i exists" "test -f" $dirpath/$i  || return 1
+    done
+
+    return 0
+}
+
+save_governors() {
+
+    governors_backup=
+    local index=0
+
+    for i in $(ls $CPU_PATH | grep "cpu[0-9].*"); do
+	governors_backup[$index]=$(cat $CPU_PATH/$i/cpufreq/scaling_governor)
+	index=$((index + 1))
+    done
+}
+
+restore_governors() {
+
+    local index=0
+    local oldgov=
+
+    for i in $(ls $CPU_PATH | grep "cpu[0-9].*"); do
+	oldgov=${governors_backup[$index]}
+	echo $oldgov > $CPU_PATH/$i/cpufreq/scaling_governor
+	index=$((index + 1))
+    done
+}
+
+save_frequencies() {
+
+    frequencies_backup=
+    local index=0
+    local cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
+    local cpu=
+
+    for cpu in $cpus; do
+	frequencies_backup[$index]=$(cat $CPU_PATH/$cpu/cpufreq/scaling_cur_freq)
+	index=$((index + 1))
+    done
+}
+
+restore_frequencies() {
+
+    local index=0
+    local oldfreq=
+    local cpus=$(ls $CPU_PATH | grep "cpu[0-9].*")
+
+    for cpu in $cpus; do
+	oldfreq=${frequencies_backup[$index]}
+	echo $oldfreq > $CPU_PATH/$cpu/cpufreq/scaling_setspeed
+	index=$((index + 1))
+    done
+}
+
+sigtrap() {
+    exit 255
+}