blob: 7e8bdb81aedfb070ab01ee550219c220903b3496 [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}
10
Patrick Bellasicf761312015-11-27 16:38:30 +000011################################################################################
12# CPUFrequency Utility Functions
13################################################################################
14
15cpufreq_set_all_frequencies() {
16 FREQ=$1
17 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
18 echo $FREQ > $CPU/cpufreq/scaling_cur_freq
19 done
20}
21
Patrick Bellasi51b7f012015-11-27 16:40:58 +000022cpufreq_get_all_frequencies() {
23 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq | \
24 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_cur_freq:| |'
25}
26
Patrick Bellasicf761312015-11-27 16:38:30 +000027cpufreq_set_all_governors() {
28 GOV=$1
29 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
30 echo $GOV > $CPU/cpufreq/scaling_governor
31 done
32}
33
Patrick Bellasi51b7f012015-11-27 16:40:58 +000034cpufreq_get_all_governors() {
35 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | \
36 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_governor:| |'
37}
38
Patrick Bellasicf761312015-11-27 16:38:30 +000039cpufreq_trace_all_frequencies() {
40 FREQS=$(cat /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)
41 CPU=0; for F in $FREQS; do
42 echo "cpu_frequency: state=$F cpu_id=$CPU" > /sys/kernel/debug/tracing/trace_marker
43 CPU=$((CPU + 1))
44 done
45}
Patrick Bellasif2eac512015-11-27 16:35:57 +000046
47################################################################################
Patrick Bellasi082a82c2016-01-26 15:38:44 +000048# FTrace Utility Functions
49################################################################################
50
51ftrace_get_function_stats() {
52 for CPU in $(ls /sys/kernel/debug/tracing/trace_stat | sed 's/function//'); do
53 REPLACE_STRING="s/ Function/\n Function (CPU$CPU)/"
54 cat /sys/kernel/debug/tracing/trace_stat/function$CPU \
55 | sed "$REPLACE_STRING"
56 done
57}
58
Patrick Bellasia65ff132016-02-23 12:11:06 +000059
60################################################################################
61# CGroups Utility Functions
62################################################################################
63
64cgroups_get_attributes() {
Patrick Bellasic9761892016-04-26 15:28:16 +010065 test $# -eq 2 || exit -1
Patrick Bellasia65ff132016-02-23 12:11:06 +000066 CGROUP="$1"
67 CONTROLLER="$2"
Patrick Bellasic9761892016-04-26 15:28:16 +010068 # Check if controller is mounted with "noprefix" option, which is quite
69 # common on Android for backward compatibility
70 ls $CGROUP/$CONTROLLER\.* 2>&1 >/dev/null
71 if [ $? -eq 0 ]; then
72 # no "noprefix" option, attributes format is:
73 # mnt_point/controller.attribute_name
74 $GREP '' $CGROUP/* | \
75 $GREP "$CONTROLLER\." | \
76 $SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
77 else
78 # "noprefix" option, attribute format is:
79 # mnt_point/attribute_name
80 $GREP '' $(\
81 $FIND $CGROUP -type f -maxdepth 1 |
82 $GREP -v -e ".*tasks" -e ".*cgroup\..*") | \
83 $SED "s|$CGROUP/||"
84 fi
Patrick Bellasia65ff132016-02-23 12:11:06 +000085}
86
Patrick Bellasi28739392016-04-22 11:43:49 +010087cgroups_run_into() {
88
89 # Control groups mount point
90 CGMOUNT=${CGMOUNT:-/sys/fs/cgroup/devlib_*}
91 # The control group we want to run into
92 CGP=${1}
Patrick Bellasi96392fd2016-04-26 15:30:03 +010093 shift 1
Patrick Bellasi28739392016-04-22 11:43:49 +010094 # The command to run
Patrick Bellasi96392fd2016-04-26 15:30:03 +010095 CMD="${@}"
Patrick Bellasi28739392016-04-22 11:43:49 +010096
97 # Execution under root CGgroup
98 if [ "x/" == "x$CGP" ]; then
99
100 $FIND $CGMOUNT -type d -maxdepth 0 | \
101 while read CGPATH; do
102 # Move this shell into that control group
103 echo $$ > $CGPATH/cgroup.procs
104 echo "Moving task into root CGroup ($CGPATH)"
105 done
106
107 # Execution under specified CGroup
108 else
109
110 # Check if the required CGroup exists
111 $FIND $CGMOUNT -type d -mindepth 1 | \
112 $GREP "$CGP" &>/dev/null
113 if [ $? -ne 0 ]; then
114 echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
115 exit 1
116 fi
117
118 $FIND $CGMOUNT -type d -mindepth 1 | \
119 $GREP "$CGP" | \
120 while read CGPATH; do
121 # Move this shell into that control group
122 echo $$ > $CGPATH/cgroup.procs
123 echo "Moving task into $CGPATH"
124 done
125
126 fi
127
128 # Execute the command
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100129 exec $CMD
130
Patrick Bellasi28739392016-04-22 11:43:49 +0100131}
132
133cgroups_tasks_move() {
134 SRC_GRP=${1}
135 DST_GRP=${2}
136 GREP_EXCLUSE=${3:-''}
137
138 cat $SRC_GRP/tasks | while read TID; do
139 echo $TID > $DST_GRP/cgroup.procs
140 done
141
142 [ "$GREP_EXCLUSE" = "" ] && exit 0
143
144 PIDS=`ps | $GREP "$GREP_EXCLUSE" | awk '{print $2}'`
145 PIDS=`echo $PIDS`
146 echo "PIDs to save: [$PIDS]"
147 for TID in $PIDS; do
148 CMDLINE=`cat /proc/$TID/cmdline`
149 echo "$TID : $CMDLINE"
150 echo $TID > $SRC_GRP/cgroup.procs
151 done
152}
153
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000154################################################################################
Patrick Bellasif2eac512015-11-27 16:35:57 +0000155# Main Function Dispatcher
156################################################################################
157
158case $CMD in
Patrick Bellasicf761312015-11-27 16:38:30 +0000159cpufreq_set_all_frequencies)
160 cpufreq_set_all_frequencies $*
161 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000162cpufreq_get_all_frequencies)
163 cpufreq_get_all_frequencies
164 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000165cpufreq_set_all_governors)
166 cpufreq_set_all_governors $*
167 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000168cpufreq_get_all_governors)
169 cpufreq_get_all_governors
170 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000171cpufreq_trace_all_frequencies)
172 cpufreq_trace_all_frequencies $*
173 ;;
Patrick Bellasia65ff132016-02-23 12:11:06 +0000174cgroups_get_attributes)
175 cgroups_get_attributes $*
176 ;;
Patrick Bellasi28739392016-04-22 11:43:49 +0100177cgroups_run_into)
178 cgroups_run_into $*
179 ;;
180cgroups_tasks_move)
181 cgroups_tasks_move $*
182 ;;
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000183ftrace_get_function_stats)
184 ftrace_get_function_stats
185 ;;
Patrick Bellasif2eac512015-11-27 16:35:57 +0000186*)
187 echo "Command [$CMD] not supported"
188 exit -1
189esac
190
191# vim: tabstop=4 shiftwidth=4