blob: 007d0a5a84bb9ce5d06dea3833557531012f1205 [file] [log] [blame]
Patrick Bellasif2eac512015-11-27 16:35:57 +00001#!__DEVLIB_SHELL__
2
3CMD=$1
4shift
5
6BUSYBOX=${BUSYBOX:-__DEVLIB_BUSYBOX__}
Patrick Bellasi28739392016-04-22 11:43:49 +01007FIND=${FIND:-$BUSYBOX find}
Patrick Bellasif2eac512015-11-27 16:35:57 +00008GREP=${GREP:-$BUSYBOX grep}
9SED=${SED:-$BUSYBOX sed}
Patrick Bellasi83c13122016-08-26 16:12:23 +010010CAT=${CAT:-$BUSYBOX cat}
Patrick Bellasi21d18f82016-08-26 18:13:47 +010011AWK=${AWK:-$BUSYBOX awk}
Brendan Jackman0dc65bd2016-11-25 16:01:20 +000012PS=${PS:-$BUSYBOX ps}
Patrick Bellasif2eac512015-11-27 16:35:57 +000013
Patrick Bellasicf761312015-11-27 16:38:30 +000014################################################################################
15# CPUFrequency Utility Functions
16################################################################################
17
18cpufreq_set_all_frequencies() {
19 FREQ=$1
20 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
21 echo $FREQ > $CPU/cpufreq/scaling_cur_freq
22 done
23}
24
Patrick Bellasi51b7f012015-11-27 16:40:58 +000025cpufreq_get_all_frequencies() {
26 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq | \
27 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_cur_freq:| |'
28}
29
Patrick Bellasicf761312015-11-27 16:38:30 +000030cpufreq_set_all_governors() {
31 GOV=$1
32 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
33 echo $GOV > $CPU/cpufreq/scaling_governor
34 done
35}
36
Patrick Bellasi51b7f012015-11-27 16:40:58 +000037cpufreq_get_all_governors() {
38 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | \
39 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_governor:| |'
40}
41
Patrick Bellasicf761312015-11-27 16:38:30 +000042cpufreq_trace_all_frequencies() {
Patrick Bellasi83c13122016-08-26 16:12:23 +010043 FREQS=$($CAT /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)
Patrick Bellasicf761312015-11-27 16:38:30 +000044 CPU=0; for F in $FREQS; do
Michele Di Giorgiod7f30922016-10-17 08:34:24 +010045 echo "cpu_frequency_devlib: state=$F cpu_id=$CPU" > /sys/kernel/debug/tracing/trace_marker
Patrick Bellasicf761312015-11-27 16:38:30 +000046 CPU=$((CPU + 1))
47 done
48}
Patrick Bellasif2eac512015-11-27 16:35:57 +000049
50################################################################################
Brendan Jackman6cdae6b2016-11-01 18:21:49 +000051# CPUIdle Utility Functions
52################################################################################
53
54cpuidle_wake_all_cpus() {
55 CPU_PATHS=/sys/devices/system/cpu/cpu[0-9]*
56 MASK=0x1; for F in $CPU_PATHS; do
Brendan Jackmane276abf2017-04-20 15:38:46 +010057 $BUSYBOX taskset $MASK true &
Brendan Jackman6cdae6b2016-11-01 18:21:49 +000058 MASK=$($BUSYBOX printf '0x%x' $((MASK * 2)))
59 done
60}
61
62################################################################################
Patrick Bellasi082a82c2016-01-26 15:38:44 +000063# FTrace Utility Functions
64################################################################################
65
66ftrace_get_function_stats() {
67 for CPU in $(ls /sys/kernel/debug/tracing/trace_stat | sed 's/function//'); do
68 REPLACE_STRING="s/ Function/\n Function (CPU$CPU)/"
Patrick Bellasi83c13122016-08-26 16:12:23 +010069 $CAT /sys/kernel/debug/tracing/trace_stat/function$CPU \
Patrick Bellasi082a82c2016-01-26 15:38:44 +000070 | sed "$REPLACE_STRING"
71 done
72}
73
Patrick Bellasia65ff132016-02-23 12:11:06 +000074
75################################################################################
76# CGroups Utility Functions
77################################################################################
78
79cgroups_get_attributes() {
Patrick Bellasic9761892016-04-26 15:28:16 +010080 test $# -eq 2 || exit -1
Patrick Bellasia65ff132016-02-23 12:11:06 +000081 CGROUP="$1"
82 CONTROLLER="$2"
Patrick Bellasic9761892016-04-26 15:28:16 +010083 # Check if controller is mounted with "noprefix" option, which is quite
84 # common on Android for backward compatibility
85 ls $CGROUP/$CONTROLLER\.* 2>&1 >/dev/null
86 if [ $? -eq 0 ]; then
87 # no "noprefix" option, attributes format is:
88 # mnt_point/controller.attribute_name
89 $GREP '' $CGROUP/* | \
90 $GREP "$CONTROLLER\." | \
91 $SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
92 else
93 # "noprefix" option, attribute format is:
94 # mnt_point/attribute_name
95 $GREP '' $(\
96 $FIND $CGROUP -type f -maxdepth 1 |
97 $GREP -v -e ".*tasks" -e ".*cgroup\..*") | \
98 $SED "s|$CGROUP/||"
99 fi
Patrick Bellasia65ff132016-02-23 12:11:06 +0000100}
101
Patrick Bellasi28739392016-04-22 11:43:49 +0100102cgroups_run_into() {
103
104 # Control groups mount point
Patrick Bellasib2ec9572016-11-29 10:02:57 +0000105 CGMOUNT=${CGMOUNT:-/sys/fs/cgroup}
Patrick Bellasi28739392016-04-22 11:43:49 +0100106 # The control group we want to run into
107 CGP=${1}
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100108 shift 1
Patrick Bellasi28739392016-04-22 11:43:49 +0100109 # The command to run
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100110 CMD="${@}"
Patrick Bellasi28739392016-04-22 11:43:49 +0100111
112 # Execution under root CGgroup
113 if [ "x/" == "x$CGP" ]; then
114
115 $FIND $CGMOUNT -type d -maxdepth 0 | \
116 while read CGPATH; do
117 # Move this shell into that control group
118 echo $$ > $CGPATH/cgroup.procs
119 echo "Moving task into root CGroup ($CGPATH)"
120 done
121
122 # Execution under specified CGroup
123 else
124
125 # Check if the required CGroup exists
126 $FIND $CGMOUNT -type d -mindepth 1 | \
127 $GREP "$CGP" &>/dev/null
128 if [ $? -ne 0 ]; then
129 echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
130 exit 1
131 fi
132
133 $FIND $CGMOUNT -type d -mindepth 1 | \
134 $GREP "$CGP" | \
135 while read CGPATH; do
136 # Move this shell into that control group
137 echo $$ > $CGPATH/cgroup.procs
138 echo "Moving task into $CGPATH"
139 done
140
141 fi
142
143 # Execute the command
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100144 exec $CMD
145
Patrick Bellasi28739392016-04-22 11:43:49 +0100146}
147
148cgroups_tasks_move() {
149 SRC_GRP=${1}
150 DST_GRP=${2}
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100151 shift 2
152 FILTERS=$*
Patrick Bellasi28739392016-04-22 11:43:49 +0100153
Patrick Bellasi83c13122016-08-26 16:12:23 +0100154 $CAT $SRC_GRP/tasks | while read TID; do
Patrick Bellasi28739392016-04-22 11:43:49 +0100155 echo $TID > $DST_GRP/cgroup.procs
156 done
157
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100158 [ "x$FILTERS" = "x" ] && exit 0
Patrick Bellasi28739392016-04-22 11:43:49 +0100159
Brendan Jackman0dc65bd2016-11-25 16:01:20 +0000160 PIDS=`$PS -o comm,pid | $GREP $FILTERS | $AWK '{print $2}'`
Patrick Bellasi28739392016-04-22 11:43:49 +0100161 PIDS=`echo $PIDS`
162 echo "PIDs to save: [$PIDS]"
163 for TID in $PIDS; do
Brendan Jackmane45fcca2016-11-25 16:01:09 +0000164 COMM=`$CAT /proc/$TID/comm`
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100165 echo "$TID : $COMM"
Brendan Jackman454a2d52016-11-28 12:41:41 +0000166 echo $TID > $SRC_GRP/cgroup.procs || true
Patrick Bellasi28739392016-04-22 11:43:49 +0100167 done
168}
169
Patrick Bellasi42efd0a2016-08-26 16:25:43 +0100170cgroups_tasks_in() {
171 GRP=${1}
172 for TID in $($CAT $GRP/tasks); do
173 COMM=`$CAT /proc/$TID/comm 2>/dev/null`
174 [ "$COMM" != "" ] && CMDL=`$CAT /proc/$TID/cmdline 2>/dev/null`
175 [ "$COMM" != "" ] && echo "$TID,$COMM,$CMDL"
176 done
177 exit 0
178}
179
Quentin Perret4b364392017-08-21 18:16:10 +0100180cgroups_freezer_set_state() {
181 STATE=${1}
182 SYSFS_ENTRY=${2}/freezer.state
183
184 # Set the state of the freezer
185 echo $STATE > $SYSFS_ENTRY
186
187 # And check it applied cleanly
188 for i in `seq 1 10`; do
189 [ $($CAT $SYSFS_ENTRY) = $STATE ] && exit 0
190 sleep 1
191 done
192
193 # We have an issue
194 echo "ERROR: Freezer stalled while changing state to \"$STATE\"." >&2
195 exit 1
196}
Patrick Bellasi42efd0a2016-08-26 16:25:43 +0100197
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000198################################################################################
Sergei Trofimovf0426462017-10-04 13:19:26 +0100199# Hotplug
200################################################################################
201
202hotplug_online_all() {
Sergei Trofimova0b273b2017-10-05 13:58:46 +0100203 for path in /sys/devices/system/cpu/cpu[0-9]*; do
Sergei Trofimovf0426462017-10-04 13:19:26 +0100204 if [ $(cat $path/online) -eq 0 ]; then
205 echo 1 > $path/online
206 fi
207 done
208}
209
210################################################################################
Sergei Trofimov181bc182017-10-03 16:28:09 +0100211# Misc
212################################################################################
213
214read_tree_values() {
Sergei Trofimova0b273b2017-10-05 13:58:46 +0100215 BASEPATH=$1
Sergei Trofimov181bc182017-10-03 16:28:09 +0100216 MAXDEPTH=$2
217
Sergei Trofimova0b273b2017-10-05 13:58:46 +0100218 if [ ! -e $BASEPATH ]; then
219 echo "ERROR: $BASEPATH does not exist"
Sergei Trofimovd560aea2017-10-05 09:35:11 +0100220 exit 1
221 fi
222
Sergei Trofimova0b273b2017-10-05 13:58:46 +0100223 PATHS=$($BUSYBOX find $BASEPATH -follow -maxdepth $MAXDEPTH)
224 i=0
225 for path in $PATHS; do
226 i=$(expr $i + 1)
227 done
228 if [ $i -gt 1 ]; then
229 $BUSYBOX grep -s '' $PATHS
Sergei Trofimov181bc182017-10-03 16:28:09 +0100230 fi
231}
232
233################################################################################
Patrick Bellasif2eac512015-11-27 16:35:57 +0000234# Main Function Dispatcher
235################################################################################
236
237case $CMD in
Patrick Bellasicf761312015-11-27 16:38:30 +0000238cpufreq_set_all_frequencies)
239 cpufreq_set_all_frequencies $*
240 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000241cpufreq_get_all_frequencies)
242 cpufreq_get_all_frequencies
243 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000244cpufreq_set_all_governors)
245 cpufreq_set_all_governors $*
246 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000247cpufreq_get_all_governors)
248 cpufreq_get_all_governors
249 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000250cpufreq_trace_all_frequencies)
251 cpufreq_trace_all_frequencies $*
252 ;;
Brendan Jackman6cdae6b2016-11-01 18:21:49 +0000253cpuidle_wake_all_cpus)
254 cpuidle_wake_all_cpus $*
255 ;;
Patrick Bellasia65ff132016-02-23 12:11:06 +0000256cgroups_get_attributes)
257 cgroups_get_attributes $*
258 ;;
Patrick Bellasi28739392016-04-22 11:43:49 +0100259cgroups_run_into)
260 cgroups_run_into $*
261 ;;
262cgroups_tasks_move)
263 cgroups_tasks_move $*
264 ;;
Patrick Bellasi42efd0a2016-08-26 16:25:43 +0100265cgroups_tasks_in)
266 cgroups_tasks_in $*
267 ;;
Quentin Perret4b364392017-08-21 18:16:10 +0100268cgroups_freezer_set_state)
269 cgroups_freezer_set_state $*
270 ;;
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000271ftrace_get_function_stats)
272 ftrace_get_function_stats
273 ;;
Sergei Trofimovf0426462017-10-04 13:19:26 +0100274hotplug_online_all)
275 hotplug_online_all
276 ;;
Sergei Trofimov181bc182017-10-03 16:28:09 +0100277read_tree_values)
278 read_tree_values $*
279 ;;
Patrick Bellasif2eac512015-11-27 16:35:57 +0000280*)
281 echo "Command [$CMD] not supported"
282 exit -1
283esac
284
285# vim: tabstop=4 shiftwidth=4