blob: 0bfab5cbe9f6f86b5cc8d8124f5902a8106d93dc [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}
Patrick Bellasif2eac512015-11-27 16:35:57 +000012
Patrick Bellasicf761312015-11-27 16:38:30 +000013################################################################################
14# CPUFrequency Utility Functions
15################################################################################
16
17cpufreq_set_all_frequencies() {
18 FREQ=$1
19 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
20 echo $FREQ > $CPU/cpufreq/scaling_cur_freq
21 done
22}
23
Patrick Bellasi51b7f012015-11-27 16:40:58 +000024cpufreq_get_all_frequencies() {
25 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq | \
26 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_cur_freq:| |'
27}
28
Patrick Bellasicf761312015-11-27 16:38:30 +000029cpufreq_set_all_governors() {
30 GOV=$1
31 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
32 echo $GOV > $CPU/cpufreq/scaling_governor
33 done
34}
35
Patrick Bellasi51b7f012015-11-27 16:40:58 +000036cpufreq_get_all_governors() {
37 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | \
38 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_governor:| |'
39}
40
Patrick Bellasicf761312015-11-27 16:38:30 +000041cpufreq_trace_all_frequencies() {
Patrick Bellasi83c13122016-08-26 16:12:23 +010042 FREQS=$($CAT /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)
Patrick Bellasicf761312015-11-27 16:38:30 +000043 CPU=0; for F in $FREQS; do
44 echo "cpu_frequency: state=$F cpu_id=$CPU" > /sys/kernel/debug/tracing/trace_marker
45 CPU=$((CPU + 1))
46 done
47}
Patrick Bellasif2eac512015-11-27 16:35:57 +000048
49################################################################################
Brendan Jackman6cdae6b2016-11-01 18:21:49 +000050# CPUIdle Utility Functions
51################################################################################
52
53cpuidle_wake_all_cpus() {
54 CPU_PATHS=/sys/devices/system/cpu/cpu[0-9]*
55 MASK=0x1; for F in $CPU_PATHS; do
56 $BUSYBOX taskset $MASK true
57 MASK=$($BUSYBOX printf '0x%x' $((MASK * 2)))
58 done
59}
60
61################################################################################
Patrick Bellasi082a82c2016-01-26 15:38:44 +000062# FTrace Utility Functions
63################################################################################
64
65ftrace_get_function_stats() {
66 for CPU in $(ls /sys/kernel/debug/tracing/trace_stat | sed 's/function//'); do
67 REPLACE_STRING="s/ Function/\n Function (CPU$CPU)/"
Patrick Bellasi83c13122016-08-26 16:12:23 +010068 $CAT /sys/kernel/debug/tracing/trace_stat/function$CPU \
Patrick Bellasi082a82c2016-01-26 15:38:44 +000069 | sed "$REPLACE_STRING"
70 done
71}
72
Patrick Bellasia65ff132016-02-23 12:11:06 +000073
74################################################################################
75# CGroups Utility Functions
76################################################################################
77
78cgroups_get_attributes() {
Patrick Bellasic9761892016-04-26 15:28:16 +010079 test $# -eq 2 || exit -1
Patrick Bellasia65ff132016-02-23 12:11:06 +000080 CGROUP="$1"
81 CONTROLLER="$2"
Patrick Bellasic9761892016-04-26 15:28:16 +010082 # Check if controller is mounted with "noprefix" option, which is quite
83 # common on Android for backward compatibility
84 ls $CGROUP/$CONTROLLER\.* 2>&1 >/dev/null
85 if [ $? -eq 0 ]; then
86 # no "noprefix" option, attributes format is:
87 # mnt_point/controller.attribute_name
88 $GREP '' $CGROUP/* | \
89 $GREP "$CONTROLLER\." | \
90 $SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
91 else
92 # "noprefix" option, attribute format is:
93 # mnt_point/attribute_name
94 $GREP '' $(\
95 $FIND $CGROUP -type f -maxdepth 1 |
96 $GREP -v -e ".*tasks" -e ".*cgroup\..*") | \
97 $SED "s|$CGROUP/||"
98 fi
Patrick Bellasia65ff132016-02-23 12:11:06 +000099}
100
Patrick Bellasi28739392016-04-22 11:43:49 +0100101cgroups_run_into() {
102
103 # Control groups mount point
104 CGMOUNT=${CGMOUNT:-/sys/fs/cgroup/devlib_*}
105 # The control group we want to run into
106 CGP=${1}
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100107 shift 1
Patrick Bellasi28739392016-04-22 11:43:49 +0100108 # The command to run
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100109 CMD="${@}"
Patrick Bellasi28739392016-04-22 11:43:49 +0100110
111 # Execution under root CGgroup
112 if [ "x/" == "x$CGP" ]; then
113
114 $FIND $CGMOUNT -type d -maxdepth 0 | \
115 while read CGPATH; do
116 # Move this shell into that control group
117 echo $$ > $CGPATH/cgroup.procs
118 echo "Moving task into root CGroup ($CGPATH)"
119 done
120
121 # Execution under specified CGroup
122 else
123
124 # Check if the required CGroup exists
125 $FIND $CGMOUNT -type d -mindepth 1 | \
126 $GREP "$CGP" &>/dev/null
127 if [ $? -ne 0 ]; then
128 echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
129 exit 1
130 fi
131
132 $FIND $CGMOUNT -type d -mindepth 1 | \
133 $GREP "$CGP" | \
134 while read CGPATH; do
135 # Move this shell into that control group
136 echo $$ > $CGPATH/cgroup.procs
137 echo "Moving task into $CGPATH"
138 done
139
140 fi
141
142 # Execute the command
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100143 exec $CMD
144
Patrick Bellasi28739392016-04-22 11:43:49 +0100145}
146
147cgroups_tasks_move() {
148 SRC_GRP=${1}
149 DST_GRP=${2}
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100150 shift 2
151 FILTERS=$*
Patrick Bellasi28739392016-04-22 11:43:49 +0100152
Patrick Bellasi83c13122016-08-26 16:12:23 +0100153 $CAT $SRC_GRP/tasks | while read TID; do
Patrick Bellasi28739392016-04-22 11:43:49 +0100154 echo $TID > $DST_GRP/cgroup.procs
155 done
156
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100157 [ "x$FILTERS" = "x" ] && exit 0
Patrick Bellasi28739392016-04-22 11:43:49 +0100158
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100159 PIDS=`ps | $GREP $FILTERS | $AWK '{print $2}'`
Patrick Bellasi28739392016-04-22 11:43:49 +0100160 PIDS=`echo $PIDS`
161 echo "PIDs to save: [$PIDS]"
162 for TID in $PIDS; do
Brendan Jackmane45fcca2016-11-25 16:01:09 +0000163 COMM=`$CAT /proc/$TID/comm`
Patrick Bellasi21d18f82016-08-26 18:13:47 +0100164 echo "$TID : $COMM"
Patrick Bellasi28739392016-04-22 11:43:49 +0100165 echo $TID > $SRC_GRP/cgroup.procs
166 done
167}
168
Patrick Bellasi42efd0a2016-08-26 16:25:43 +0100169cgroups_tasks_in() {
170 GRP=${1}
171 for TID in $($CAT $GRP/tasks); do
172 COMM=`$CAT /proc/$TID/comm 2>/dev/null`
173 [ "$COMM" != "" ] && CMDL=`$CAT /proc/$TID/cmdline 2>/dev/null`
174 [ "$COMM" != "" ] && echo "$TID,$COMM,$CMDL"
175 done
176 exit 0
177}
178
179
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000180################################################################################
Patrick Bellasif2eac512015-11-27 16:35:57 +0000181# Main Function Dispatcher
182################################################################################
183
184case $CMD in
Patrick Bellasicf761312015-11-27 16:38:30 +0000185cpufreq_set_all_frequencies)
186 cpufreq_set_all_frequencies $*
187 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000188cpufreq_get_all_frequencies)
189 cpufreq_get_all_frequencies
190 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000191cpufreq_set_all_governors)
192 cpufreq_set_all_governors $*
193 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000194cpufreq_get_all_governors)
195 cpufreq_get_all_governors
196 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000197cpufreq_trace_all_frequencies)
198 cpufreq_trace_all_frequencies $*
199 ;;
Brendan Jackman6cdae6b2016-11-01 18:21:49 +0000200cpuidle_wake_all_cpus)
201 cpuidle_wake_all_cpus $*
202 ;;
Patrick Bellasia65ff132016-02-23 12:11:06 +0000203cgroups_get_attributes)
204 cgroups_get_attributes $*
205 ;;
Patrick Bellasi28739392016-04-22 11:43:49 +0100206cgroups_run_into)
207 cgroups_run_into $*
208 ;;
209cgroups_tasks_move)
210 cgroups_tasks_move $*
211 ;;
Patrick Bellasi42efd0a2016-08-26 16:25:43 +0100212cgroups_tasks_in)
213 cgroups_tasks_in $*
214 ;;
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000215ftrace_get_function_stats)
216 ftrace_get_function_stats
217 ;;
Patrick Bellasif2eac512015-11-27 16:35:57 +0000218*)
219 echo "Command [$CMD] not supported"
220 exit -1
221esac
222
223# vim: tabstop=4 shiftwidth=4