blob: 849f6b4e0296aa22b04881264faf104beb23edd0 [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 Bellasif2eac512015-11-27 16:35:57 +000011
Patrick Bellasicf761312015-11-27 16:38:30 +000012################################################################################
13# CPUFrequency Utility Functions
14################################################################################
15
16cpufreq_set_all_frequencies() {
17 FREQ=$1
18 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
19 echo $FREQ > $CPU/cpufreq/scaling_cur_freq
20 done
21}
22
Patrick Bellasi51b7f012015-11-27 16:40:58 +000023cpufreq_get_all_frequencies() {
24 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq | \
25 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_cur_freq:| |'
26}
27
Patrick Bellasicf761312015-11-27 16:38:30 +000028cpufreq_set_all_governors() {
29 GOV=$1
30 for CPU in /sys/devices/system/cpu/cpu[0-9]*; do
31 echo $GOV > $CPU/cpufreq/scaling_governor
32 done
33}
34
Patrick Bellasi51b7f012015-11-27 16:40:58 +000035cpufreq_get_all_governors() {
36 $GREP '' /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor | \
37 $SED -e 's|/sys/devices/system/cpu/cpu||' -e 's|/cpufreq/scaling_governor:| |'
38}
39
Patrick Bellasicf761312015-11-27 16:38:30 +000040cpufreq_trace_all_frequencies() {
Patrick Bellasi83c13122016-08-26 16:12:23 +010041 FREQS=$($CAT /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq)
Patrick Bellasicf761312015-11-27 16:38:30 +000042 CPU=0; for F in $FREQS; do
43 echo "cpu_frequency: state=$F cpu_id=$CPU" > /sys/kernel/debug/tracing/trace_marker
44 CPU=$((CPU + 1))
45 done
46}
Patrick Bellasif2eac512015-11-27 16:35:57 +000047
48################################################################################
Patrick Bellasi082a82c2016-01-26 15:38:44 +000049# FTrace Utility Functions
50################################################################################
51
52ftrace_get_function_stats() {
53 for CPU in $(ls /sys/kernel/debug/tracing/trace_stat | sed 's/function//'); do
54 REPLACE_STRING="s/ Function/\n Function (CPU$CPU)/"
Patrick Bellasi83c13122016-08-26 16:12:23 +010055 $CAT /sys/kernel/debug/tracing/trace_stat/function$CPU \
Patrick Bellasi082a82c2016-01-26 15:38:44 +000056 | sed "$REPLACE_STRING"
57 done
58}
59
Patrick Bellasia65ff132016-02-23 12:11:06 +000060
61################################################################################
62# CGroups Utility Functions
63################################################################################
64
65cgroups_get_attributes() {
Patrick Bellasic9761892016-04-26 15:28:16 +010066 test $# -eq 2 || exit -1
Patrick Bellasia65ff132016-02-23 12:11:06 +000067 CGROUP="$1"
68 CONTROLLER="$2"
Patrick Bellasic9761892016-04-26 15:28:16 +010069 # Check if controller is mounted with "noprefix" option, which is quite
70 # common on Android for backward compatibility
71 ls $CGROUP/$CONTROLLER\.* 2>&1 >/dev/null
72 if [ $? -eq 0 ]; then
73 # no "noprefix" option, attributes format is:
74 # mnt_point/controller.attribute_name
75 $GREP '' $CGROUP/* | \
76 $GREP "$CONTROLLER\." | \
77 $SED -e "s|$CONTROLLER\.||" -e "s|$CGROUP/||"
78 else
79 # "noprefix" option, attribute format is:
80 # mnt_point/attribute_name
81 $GREP '' $(\
82 $FIND $CGROUP -type f -maxdepth 1 |
83 $GREP -v -e ".*tasks" -e ".*cgroup\..*") | \
84 $SED "s|$CGROUP/||"
85 fi
Patrick Bellasia65ff132016-02-23 12:11:06 +000086}
87
Patrick Bellasi28739392016-04-22 11:43:49 +010088cgroups_run_into() {
89
90 # Control groups mount point
91 CGMOUNT=${CGMOUNT:-/sys/fs/cgroup/devlib_*}
92 # The control group we want to run into
93 CGP=${1}
Patrick Bellasi96392fd2016-04-26 15:30:03 +010094 shift 1
Patrick Bellasi28739392016-04-22 11:43:49 +010095 # The command to run
Patrick Bellasi96392fd2016-04-26 15:30:03 +010096 CMD="${@}"
Patrick Bellasi28739392016-04-22 11:43:49 +010097
98 # Execution under root CGgroup
99 if [ "x/" == "x$CGP" ]; then
100
101 $FIND $CGMOUNT -type d -maxdepth 0 | \
102 while read CGPATH; do
103 # Move this shell into that control group
104 echo $$ > $CGPATH/cgroup.procs
105 echo "Moving task into root CGroup ($CGPATH)"
106 done
107
108 # Execution under specified CGroup
109 else
110
111 # Check if the required CGroup exists
112 $FIND $CGMOUNT -type d -mindepth 1 | \
113 $GREP "$CGP" &>/dev/null
114 if [ $? -ne 0 ]; then
115 echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
116 exit 1
117 fi
118
119 $FIND $CGMOUNT -type d -mindepth 1 | \
120 $GREP "$CGP" | \
121 while read CGPATH; do
122 # Move this shell into that control group
123 echo $$ > $CGPATH/cgroup.procs
124 echo "Moving task into $CGPATH"
125 done
126
127 fi
128
129 # Execute the command
Patrick Bellasi96392fd2016-04-26 15:30:03 +0100130 exec $CMD
131
Patrick Bellasi28739392016-04-22 11:43:49 +0100132}
133
134cgroups_tasks_move() {
135 SRC_GRP=${1}
136 DST_GRP=${2}
137 GREP_EXCLUSE=${3:-''}
138
Patrick Bellasi83c13122016-08-26 16:12:23 +0100139 $CAT $SRC_GRP/tasks | while read TID; do
Patrick Bellasi28739392016-04-22 11:43:49 +0100140 echo $TID > $DST_GRP/cgroup.procs
141 done
142
143 [ "$GREP_EXCLUSE" = "" ] && exit 0
144
145 PIDS=`ps | $GREP "$GREP_EXCLUSE" | awk '{print $2}'`
146 PIDS=`echo $PIDS`
147 echo "PIDs to save: [$PIDS]"
148 for TID in $PIDS; do
Patrick Bellasi83c13122016-08-26 16:12:23 +0100149 CMDLINE=`$CAT /proc/$TID/cmdline`
Patrick Bellasi28739392016-04-22 11:43:49 +0100150 echo "$TID : $CMDLINE"
151 echo $TID > $SRC_GRP/cgroup.procs
152 done
153}
154
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000155################################################################################
Patrick Bellasif2eac512015-11-27 16:35:57 +0000156# Main Function Dispatcher
157################################################################################
158
159case $CMD in
Patrick Bellasicf761312015-11-27 16:38:30 +0000160cpufreq_set_all_frequencies)
161 cpufreq_set_all_frequencies $*
162 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000163cpufreq_get_all_frequencies)
164 cpufreq_get_all_frequencies
165 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000166cpufreq_set_all_governors)
167 cpufreq_set_all_governors $*
168 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000169cpufreq_get_all_governors)
170 cpufreq_get_all_governors
171 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000172cpufreq_trace_all_frequencies)
173 cpufreq_trace_all_frequencies $*
174 ;;
Patrick Bellasia65ff132016-02-23 12:11:06 +0000175cgroups_get_attributes)
176 cgroups_get_attributes $*
177 ;;
Patrick Bellasi28739392016-04-22 11:43:49 +0100178cgroups_run_into)
179 cgroups_run_into $*
180 ;;
181cgroups_tasks_move)
182 cgroups_tasks_move $*
183 ;;
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000184ftrace_get_function_stats)
185 ftrace_get_function_stats
186 ;;
Patrick Bellasif2eac512015-11-27 16:35:57 +0000187*)
188 echo "Command [$CMD] not supported"
189 exit -1
190esac
191
192# vim: tabstop=4 shiftwidth=4