Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 1 | #!__DEVLIB_SHELL__ |
| 2 | |
| 3 | CMD=$1 |
| 4 | shift |
| 5 | |
| 6 | BUSYBOX=${BUSYBOX:-__DEVLIB_BUSYBOX__} |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 7 | FIND=${FIND:-$BUSYBOX find} |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 8 | GREP=${GREP:-$BUSYBOX grep} |
| 9 | SED=${SED:-$BUSYBOX sed} |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 10 | CAT=${CAT:-$BUSYBOX cat} |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 11 | AWK=${AWK:-$BUSYBOX awk} |
Brendan Jackman | 0dc65bd | 2016-11-25 16:01:20 +0000 | [diff] [blame] | 12 | PS=${PS:-$BUSYBOX ps} |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 13 | |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 14 | ################################################################################ |
| 15 | # CPUFrequency Utility Functions |
| 16 | ################################################################################ |
| 17 | |
| 18 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 25 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 30 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 37 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 42 | cpufreq_trace_all_frequencies() { |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 43 | FREQS=$($CAT /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq) |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 44 | CPU=0; for F in $FREQS; do |
Michele Di Giorgio | d7f3092 | 2016-10-17 08:34:24 +0100 | [diff] [blame] | 45 | echo "cpu_frequency_devlib: state=$F cpu_id=$CPU" > /sys/kernel/debug/tracing/trace_marker |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 46 | CPU=$((CPU + 1)) |
| 47 | done |
| 48 | } |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 49 | |
| 50 | ################################################################################ |
Brendan Jackman | 6cdae6b | 2016-11-01 18:21:49 +0000 | [diff] [blame] | 51 | # CPUIdle Utility Functions |
| 52 | ################################################################################ |
| 53 | |
| 54 | cpuidle_wake_all_cpus() { |
| 55 | CPU_PATHS=/sys/devices/system/cpu/cpu[0-9]* |
| 56 | MASK=0x1; for F in $CPU_PATHS; do |
Brendan Jackman | e276abf | 2017-04-20 15:38:46 +0100 | [diff] [blame] | 57 | $BUSYBOX taskset $MASK true & |
Brendan Jackman | 6cdae6b | 2016-11-01 18:21:49 +0000 | [diff] [blame] | 58 | MASK=$($BUSYBOX printf '0x%x' $((MASK * 2))) |
| 59 | done |
| 60 | } |
| 61 | |
| 62 | ################################################################################ |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 63 | # FTrace Utility Functions |
| 64 | ################################################################################ |
| 65 | |
| 66 | ftrace_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 Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 69 | $CAT /sys/kernel/debug/tracing/trace_stat/function$CPU \ |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 70 | | sed "$REPLACE_STRING" |
| 71 | done |
| 72 | } |
| 73 | |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 74 | |
| 75 | ################################################################################ |
| 76 | # CGroups Utility Functions |
| 77 | ################################################################################ |
| 78 | |
| 79 | cgroups_get_attributes() { |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame] | 80 | test $# -eq 2 || exit -1 |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 81 | CGROUP="$1" |
| 82 | CONTROLLER="$2" |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame] | 83 | # 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 Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 102 | cgroups_run_into() { |
| 103 | |
| 104 | # Control groups mount point |
Patrick Bellasi | b2ec957 | 2016-11-29 10:02:57 +0000 | [diff] [blame] | 105 | CGMOUNT=${CGMOUNT:-/sys/fs/cgroup} |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 106 | # The control group we want to run into |
| 107 | CGP=${1} |
Patrick Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 108 | shift 1 |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 109 | # The command to run |
Patrick Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 110 | CMD="${@}" |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 111 | |
| 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 Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 144 | exec $CMD |
| 145 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | cgroups_tasks_move() { |
| 149 | SRC_GRP=${1} |
| 150 | DST_GRP=${2} |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 151 | shift 2 |
| 152 | FILTERS=$* |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 153 | |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 154 | $CAT $SRC_GRP/tasks | while read TID; do |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 155 | echo $TID > $DST_GRP/cgroup.procs |
| 156 | done |
| 157 | |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 158 | [ "x$FILTERS" = "x" ] && exit 0 |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 159 | |
Brendan Jackman | 0dc65bd | 2016-11-25 16:01:20 +0000 | [diff] [blame] | 160 | PIDS=`$PS -o comm,pid | $GREP $FILTERS | $AWK '{print $2}'` |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 161 | PIDS=`echo $PIDS` |
| 162 | echo "PIDs to save: [$PIDS]" |
| 163 | for TID in $PIDS; do |
Brendan Jackman | e45fcca | 2016-11-25 16:01:09 +0000 | [diff] [blame] | 164 | COMM=`$CAT /proc/$TID/comm` |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 165 | echo "$TID : $COMM" |
Brendan Jackman | 454a2d5 | 2016-11-28 12:41:41 +0000 | [diff] [blame] | 166 | echo $TID > $SRC_GRP/cgroup.procs || true |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 167 | done |
| 168 | } |
| 169 | |
Patrick Bellasi | 42efd0a | 2016-08-26 16:25:43 +0100 | [diff] [blame] | 170 | cgroups_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 Perret | 4b36439 | 2017-08-21 18:16:10 +0100 | [diff] [blame] | 180 | cgroups_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 Bellasi | 42efd0a | 2016-08-26 16:25:43 +0100 | [diff] [blame] | 197 | |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 198 | ################################################################################ |
Sergei Trofimov | f042646 | 2017-10-04 13:19:26 +0100 | [diff] [blame] | 199 | # Hotplug |
| 200 | ################################################################################ |
| 201 | |
| 202 | hotplug_online_all() { |
| 203 | PATHS=(/sys/devices/system/cpu/cpu[0-9]*) |
| 204 | for path in "${PATHS[@]}"; do |
| 205 | if [ $(cat $path/online) -eq 0 ]; then |
| 206 | echo 1 > $path/online |
| 207 | fi |
| 208 | done |
| 209 | } |
| 210 | |
| 211 | ################################################################################ |
Sergei Trofimov | 181bc18 | 2017-10-03 16:28:09 +0100 | [diff] [blame] | 212 | # Misc |
| 213 | ################################################################################ |
| 214 | |
| 215 | read_tree_values() { |
| 216 | PATH=$1 |
| 217 | MAXDEPTH=$2 |
| 218 | |
Sergei Trofimov | d560aea | 2017-10-05 09:35:11 +0100 | [diff] [blame] | 219 | if [ ! -e $PATH ]; then |
| 220 | echo "ERROR: $PATH does not exist" |
| 221 | exit 1 |
| 222 | fi |
| 223 | |
Sergei Trofimov | 5c28e41 | 2017-10-05 10:59:22 +0100 | [diff] [blame^] | 224 | PATHS=($($BUSYBOX find $PATH -follow -maxdepth $MAXDEPTH)) |
Sergei Trofimov | d560aea | 2017-10-05 09:35:11 +0100 | [diff] [blame] | 225 | if [ ${#PATHS[@]} -gt 1 ]; then |
Sergei Trofimov | 5c28e41 | 2017-10-05 10:59:22 +0100 | [diff] [blame^] | 226 | $BUSYBOX grep -s '' ${PATHS[@]} |
Sergei Trofimov | 181bc18 | 2017-10-03 16:28:09 +0100 | [diff] [blame] | 227 | fi |
| 228 | } |
| 229 | |
| 230 | ################################################################################ |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 231 | # Main Function Dispatcher |
| 232 | ################################################################################ |
| 233 | |
| 234 | case $CMD in |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 235 | cpufreq_set_all_frequencies) |
| 236 | cpufreq_set_all_frequencies $* |
| 237 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 238 | cpufreq_get_all_frequencies) |
| 239 | cpufreq_get_all_frequencies |
| 240 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 241 | cpufreq_set_all_governors) |
| 242 | cpufreq_set_all_governors $* |
| 243 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 244 | cpufreq_get_all_governors) |
| 245 | cpufreq_get_all_governors |
| 246 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 247 | cpufreq_trace_all_frequencies) |
| 248 | cpufreq_trace_all_frequencies $* |
| 249 | ;; |
Brendan Jackman | 6cdae6b | 2016-11-01 18:21:49 +0000 | [diff] [blame] | 250 | cpuidle_wake_all_cpus) |
| 251 | cpuidle_wake_all_cpus $* |
| 252 | ;; |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 253 | cgroups_get_attributes) |
| 254 | cgroups_get_attributes $* |
| 255 | ;; |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 256 | cgroups_run_into) |
| 257 | cgroups_run_into $* |
| 258 | ;; |
| 259 | cgroups_tasks_move) |
| 260 | cgroups_tasks_move $* |
| 261 | ;; |
Patrick Bellasi | 42efd0a | 2016-08-26 16:25:43 +0100 | [diff] [blame] | 262 | cgroups_tasks_in) |
| 263 | cgroups_tasks_in $* |
| 264 | ;; |
Quentin Perret | 4b36439 | 2017-08-21 18:16:10 +0100 | [diff] [blame] | 265 | cgroups_freezer_set_state) |
| 266 | cgroups_freezer_set_state $* |
| 267 | ;; |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 268 | ftrace_get_function_stats) |
| 269 | ftrace_get_function_stats |
| 270 | ;; |
Sergei Trofimov | f042646 | 2017-10-04 13:19:26 +0100 | [diff] [blame] | 271 | hotplug_online_all) |
| 272 | hotplug_online_all |
| 273 | ;; |
Sergei Trofimov | 181bc18 | 2017-10-03 16:28:09 +0100 | [diff] [blame] | 274 | read_tree_values) |
| 275 | read_tree_values $* |
| 276 | ;; |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 277 | *) |
| 278 | echo "Command [$CMD] not supported" |
| 279 | exit -1 |
| 280 | esac |
| 281 | |
| 282 | # vim: tabstop=4 shiftwidth=4 |