blob: ef4d3bd01e15c38fac47115d97ef5fc0fc2909fa [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}
93 # The command to run
94 CMD=${2}
95
96 # Execution under root CGgroup
97 if [ "x/" == "x$CGP" ]; then
98
99 $FIND $CGMOUNT -type d -maxdepth 0 | \
100 while read CGPATH; do
101 # Move this shell into that control group
102 echo $$ > $CGPATH/cgroup.procs
103 echo "Moving task into root CGroup ($CGPATH)"
104 done
105
106 # Execution under specified CGroup
107 else
108
109 # Check if the required CGroup exists
110 $FIND $CGMOUNT -type d -mindepth 1 | \
111 $GREP "$CGP" &>/dev/null
112 if [ $? -ne 0 ]; then
113 echo "ERROR: could not find any $CGP cgroup under $CGMOUNT"
114 exit 1
115 fi
116
117 $FIND $CGMOUNT -type d -mindepth 1 | \
118 $GREP "$CGP" | \
119 while read CGPATH; do
120 # Move this shell into that control group
121 echo $$ > $CGPATH/cgroup.procs
122 echo "Moving task into $CGPATH"
123 done
124
125 fi
126
127 # Execute the command
128 $CMD
129}
130
131cgroups_tasks_move() {
132 SRC_GRP=${1}
133 DST_GRP=${2}
134 GREP_EXCLUSE=${3:-''}
135
136 cat $SRC_GRP/tasks | while read TID; do
137 echo $TID > $DST_GRP/cgroup.procs
138 done
139
140 [ "$GREP_EXCLUSE" = "" ] && exit 0
141
142 PIDS=`ps | $GREP "$GREP_EXCLUSE" | awk '{print $2}'`
143 PIDS=`echo $PIDS`
144 echo "PIDs to save: [$PIDS]"
145 for TID in $PIDS; do
146 CMDLINE=`cat /proc/$TID/cmdline`
147 echo "$TID : $CMDLINE"
148 echo $TID > $SRC_GRP/cgroup.procs
149 done
150}
151
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000152################################################################################
Patrick Bellasif2eac512015-11-27 16:35:57 +0000153# Main Function Dispatcher
154################################################################################
155
156case $CMD in
Patrick Bellasicf761312015-11-27 16:38:30 +0000157cpufreq_set_all_frequencies)
158 cpufreq_set_all_frequencies $*
159 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000160cpufreq_get_all_frequencies)
161 cpufreq_get_all_frequencies
162 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000163cpufreq_set_all_governors)
164 cpufreq_set_all_governors $*
165 ;;
Patrick Bellasi51b7f012015-11-27 16:40:58 +0000166cpufreq_get_all_governors)
167 cpufreq_get_all_governors
168 ;;
Patrick Bellasicf761312015-11-27 16:38:30 +0000169cpufreq_trace_all_frequencies)
170 cpufreq_trace_all_frequencies $*
171 ;;
Patrick Bellasia65ff132016-02-23 12:11:06 +0000172cgroups_get_attributes)
173 cgroups_get_attributes $*
174 ;;
Patrick Bellasi28739392016-04-22 11:43:49 +0100175cgroups_run_into)
176 cgroups_run_into $*
177 ;;
178cgroups_tasks_move)
179 cgroups_tasks_move $*
180 ;;
Patrick Bellasi082a82c2016-01-26 15:38:44 +0000181ftrace_get_function_stats)
182 ftrace_get_function_stats
183 ;;
Patrick Bellasif2eac512015-11-27 16:35:57 +0000184*)
185 echo "Command [$CMD] not supported"
186 exit -1
187esac
188
189# vim: tabstop=4 shiftwidth=4