Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 47 additions & 54 deletions Runner/suites/Kernel/FunctionalArea/baseport/CPUFreq_Validation/run.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#!/bin/sh

. "$(pwd)/init_env"
# CPUFreq Validator: Parallel, Colorized
/var/Runner/init_env
TESTNAME="CPUFreq_Validation"
. "$TOOLS/functestlib.sh"

Expand All @@ -11,120 +9,115 @@ log_info "----------------------------------------------------------------------
log_info "-------------------Starting $TESTNAME Testcase----------------------------"
log_info "=== CPUFreq Frequency Walker with Validation ==="

# Color codes
GREEN="\e[32m"
RED="\e[31m"
YELLOW="\e[33m"
BLUE="\e[34m"
NC="\e[0m" # No Color
# Color codes (ANSI escape sequences)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can leave it for now, but in the long run I would discourage from using colour output in test scripts.

GREEN="\033[32m"
RED="\033[31m"
YELLOW="\033[33m"
BLUE="\033[34m"
NC="\033[0m"

NUM_CPUS=$(nproc)
echo -e "${YELLOW}Detected $NUM_CPUS CPU cores.${NC}"
printf "${YELLOW}Detected %s CPU cores.${NC}\n" "$NUM_CPUS"

overall_pass=true
declare -A core_status
overall_pass=0
status_dir="/tmp/cpufreq_status.$$"
mkdir -p "$status_dir"

validate_cpu_core() {
local cpu=$1
local core_id=$2
cpu=$1
core_id=$2
status_file="$status_dir/core_$core_id"

echo -e "${BLUE}Processing $cpu...${NC}"
printf "${BLUE}Processing %s...${NC}\n" "$cpu"

if [ ! -d "$cpu/cpufreq" ]; then
echo -e "${BLUE}[SKIP]${NC} $cpu does not support cpufreq."
core_status["$core_id"]="skip"
printf "${BLUE}[SKIP]${NC} %s does not support cpufreq.\n" "$cpu"
echo "skip" > "$status_file"
return
fi

available_freqs=$(cat "$cpu/cpufreq/scaling_available_frequencies" 2>/dev/null)

if [ -z "$available_freqs" ]; then
echo -e "${YELLOW}[INFO]${NC} No available frequencies for $cpu. Skipping..."
core_status["$core_id"]="skip"
printf "${YELLOW}[INFO]${NC} No available frequencies for %s. Skipping...\n" "$cpu"
echo "skip" > "$status_file"
return
fi

# Set governor to userspace
if echo "userspace" | tee "$cpu/cpufreq/scaling_governor" > /dev/null; then
echo -e "${YELLOW}[INFO]${NC} Set governor to userspace."
printf "${YELLOW}[INFO]${NC} Set governor to userspace.\n"
else
echo -e "${RED}[ERROR]${NC} Cannot set userspace governor for $cpu."
core_status["$core_id"]="fail"
printf "${RED}[ERROR]${NC} Cannot set userspace governor for %s.\n" "$cpu"
echo "fail" > "$status_file"
return
fi

core_status["$core_id"]="pass" # Assume pass unless a failure happens
echo "pass" > "$status_file"

for freq in $available_freqs; do
log_info "Setting $cpu to frequency $freq kHz..."
if echo $freq | tee "$cpu/cpufreq/scaling_setspeed" > /dev/null; then
if echo "$freq" | tee "$cpu/cpufreq/scaling_setspeed" > /dev/null; then
sleep 0.2
actual_freq=$(cat "$cpu/cpufreq/scaling_cur_freq")
if [ "$actual_freq" == "$freq" ]; then
echo -e "${GREEN}[PASS]${NC} $cpu set to $freq kHz."
if [ "$actual_freq" = "$freq" ]; then
printf "${GREEN}[PASS]${NC} %s set to %s kHz.\n" "$cpu" "$freq"
else
echo -e "${RED}[FAIL]${NC} Tried to set $cpu to $freq kHz, but current is $actual_freq kHz."
core_status["$core_id"]="fail"
printf "${RED}[FAIL]${NC} Tried to set %s to %s kHz, but current is %s kHz.\n" "$cpu" "$freq" "$actual_freq"
echo "fail" > "$status_file"
fi
else
echo -e "${RED}[ERROR]${NC} Failed to set $cpu to $freq kHz."
core_status["$core_id"]="fail"
printf "${RED}[ERROR]${NC} Failed to set %s to %s kHz.\n" "$cpu" "$freq"
echo "fail" > "$status_file"
fi
done

# Restore governor
echo "Restoring $cpu governor to 'ondemand'..."
echo "ondemand" | sudo tee "$cpu/cpufreq/scaling_governor" > /dev/null
}

# Launch validation per CPU in parallel
cpu_index=0
for cpu in /sys/devices/system/cpu/cpu[0-9]*; do
validate_cpu_core "$cpu" "$cpu_index" &
((cpu_index++))
cpu_index=$((cpu_index + 1))
done

# Wait for all background jobs to finish
wait

# Summary
log_info ""
log_info "=== Per-Core Test Summary ==="
for idx in "${!core_status[@]}"; do
status=${core_status[$idx]}
for status_file in "$status_dir"/core_*; do
idx=$(basename "$status_file" | cut -d_ -f2)
status=$(cat "$status_file")
case "$status" in
pass)
echo -e "CPU$idx: ${GREEN}[PASS]${NC}"
printf "CPU%s: ${GREEN}[PASS]${NC}\n" "$idx"
;;
fail)
echo -e "CPU$idx: ${RED}[FAIL]${NC}"
overall_pass=false
printf "CPU%s: ${RED}[FAIL]${NC}\n" "$idx"
overall_pass=1
;;
skip)
echo -e "CPU$idx: ${BLUE}[SKIPPED]${NC}"
printf "CPU%s: ${BLUE}[SKIPPED]${NC}\n" "$idx"
;;
*)
echo -e "CPU$idx: ${RED}[UNKNOWN STATUS]${NC}"
overall_pass=false
printf "CPU%s: ${RED}[UNKNOWN STATUS]${NC}\n" "$idx"
overall_pass=1
;;
esac
done

# Overall result
log_info ""
log_info "=== Overall CPUFreq Validation Result ==="
if $overall_pass; then
echo -e "${GREEN}[OVERALL PASS]${NC} All CPUs validated successfully."
log_pass "$TESTNAME : Test Passed"
echo "$TESTNAME : Test Passed" > $test_path/$TESTNAME.res
if [ "$overall_pass" -eq 0 ]; then
printf "${GREEN}[OVERALL PASS]${NC} All CPUs validated successfully.\n"
log_pass "$TESTNAME : Test Passed"
echo "$TESTNAME PASS" > "$test_path/$TESTNAME.res"
rm -r "$status_dir"
exit 0
else
echo -e "${RED}[OVERALL FAIL]${NC} Some CPUs failed frequency validation."
log_fail "$TESTNAME : Test Failed"
echo "$TESTNAME : Test Failed" > $test_path/$TESTNAME.res
printf "${RED}[OVERALL FAIL]${NC} Some CPUs failed frequency validation.\n"
log_fail "$TESTNAME : Test Failed"
echo "$TESTNAME FAIL" > "$test_path/$TESTNAME.res"
rm -r "$status_dir"
exit 1
fi

log_info "-------------------Completed $TESTNAME Testcase----------------------------"
fi