liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /bin/sh
#
# Copyright (c) International Business Machines  Corp., 2001
# Author: Nageswara R Sastry <nasastry@in.ibm.com>
#
# 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
#
 
export TCID="Power_Management03"
export TST_TOTAL=4
 
. test.sh
. pm_include.sh
 
check_cpufreq_sysfs_files() {
   total_cpus=$(tst_ncpus)
   (( total_cpus-=1 ))
   RC=0
 
   for cpu in $(seq 0 "${total_cpus}" )
   do
       cpufiles=$(find /sys/devices/system/cpu/cpu"${cpu}"/cpufreq/ \
           -name "*" -type f -perm /400)
       for files in ${cpufiles}
       do
           cat ${files} >/dev/null 2>&1
           if [ $? -ne 0 ] ; then
               echo "${0}: FAIL: cat ${files}"
               RC=1
           fi
       done
   done
   if [ ${RC} -eq 0 ] ; then
       echo "${0}: PASS: Checking cpu freq sysfs files"
   fi
   return $RC
}
 
change_govr() {
   available_govr=$(get_supporting_govr)
 
   total_cpus=$(tst_ncpus)
   (( total_cpus-=1 ))
   RC=0
 
   for cpu in $(seq 0 "${total_cpus}" )
   do
       for govr in ${available_govr}
       do
           echo ${govr} > \
   /sys/devices/system/cpu/cpu${cpu}/cpufreq/scaling_governor
           if [ "$?" -ne "0" ] ; then
               echo "${0}: FAIL: Unable to set" \
                   "governor -- ${govr} for cpu${cpu}"
               RC=1
           fi
       done
   done
   if [ ${RC} -eq 0 ] ; then
       echo "${0}: PASS: Changing cpu governors"
   fi
   return $RC
}
 
change_freq() {
   available_freq=$(get_supporting_freq)
   available_govr=$(get_supporting_govr)
   RC=0
 
   total_cpus=$(tst_ncpus)
   (( total_cpus-=1 ))
 
   if ( echo ${available_govr} | grep -i "userspace" \
       >/dev/null 2>&1 ); then
       for cpu in $(seq 0 "${total_cpus}" )
       do
           echo userspace > \
   /sys/devices/system/cpu/cpu${cpu}/cpufreq/scaling_governor
           if [ $? -ne 0 ] ; then
               RC=1
           fi
       done
       if [ ${RC} -ne 1 ] ; then
           for cpu in $(seq 0 "${total_cpus}" )
           do
               for freq in ${available_freq}
               do
                   echo ${freq} > \
   /sys/devices/system/cpu/cpu${cpu}/cpufreq/scaling_setspeed
                   if [ "$?" -ne "0" ] ; then
                       echo "${0}: FAIL: Unable" \
                           "to set frequency -- ${freq} for cpu${cpu}"
                       RC=1
                   fi
               done
           done
       fi
   fi
   if [ ${RC} -eq 0 ] ; then
       echo "${0}: PASS: Changing cpu frequencies"
   fi
   return $RC
}
 
pwkm_load_unload() {
   RC=0
   loaded_governor=`cat \
       /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`
   for module in `modprobe -l | grep cpufreq_ | \
       cut -f8 -d"/" | cut -f1 -d"."`
   do
       #echo -n "Loading $module ... "
       if [ $module != "cpufreq_$loaded_governor" ]; then
           modprobe $module >/dev/null
           if [ $? -ne 0 ] ; then
               echo "${0}: FAIL: Loading of module $module" \
                   "or check whether you compiled as module or not"
               RC=1
           fi
       fi
   done
   for module in `modprobe -l | grep cpufreq_ | \
       cut -f8 -d"/" | cut -f1 -d"."`
       do
       #echo -n "Unloading $module ... "
       if [ $module != "cpufreq_$loaded_governor" ]; then
           modprobe -r $module >/dev/null
           if [ $? -ne 0 ] ; then
               echo "${0}: FAIL: Loading of module $module" \
                   "or check whether you compiled as module or not"
               RC=1
           fi
       fi
   done
   return $RC
}
 
# Checking test environment
check_kervel_arch
 
# Checking cpufreq sysfs interface files
if [ ! -d /sys/devices/system/cpu/cpu0/cpufreq ] ; then
   tst_brkm TCONF "Required kernel configuration for CPU_FREQ NOT set"
fi
 
if check_cpufreq_sysfs_files ; then
   tst_resm TPASS "CPUFREQ sysfs tests"
else
   tst_resm TFAIL "CPUFREQ sysfs tests"
fi
 
# Changing governors
if change_govr ; then
   tst_resm TPASS "Changing governors"
else
   tst_resm TFAIL "Changing governors"
fi
 
# Changing frequencies
if change_freq ; then
   tst_resm TPASS "Changing frequncies"
else
    tst_resm TFAIL "Changing frequncies"
fi
 
# Loading and Unloading governor related kernel modules
if pwkm_load_unload ; then
   tst_resm TPASS "Loading and Unloading of governor kernel" \
       "modules"
else
   tst_resm TFAIL "Loading and Unloading of governor kernel" \
       "modules got failed"
fi
 
tst_exit