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} |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 12 | |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 13 | ################################################################################ |
| 14 | # CPUFrequency Utility Functions |
| 15 | ################################################################################ |
| 16 | |
| 17 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 24 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 29 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 36 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 41 | cpufreq_trace_all_frequencies() { |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 42 | FREQS=$($CAT /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq) |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 43 | 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 Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 48 | |
| 49 | ################################################################################ |
Brendan Jackman | 6cdae6b | 2016-11-01 18:21:49 +0000 | [diff] [blame] | 50 | # CPUIdle Utility Functions |
| 51 | ################################################################################ |
| 52 | |
| 53 | cpuidle_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 Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 62 | # FTrace Utility Functions |
| 63 | ################################################################################ |
| 64 | |
| 65 | ftrace_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 Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 68 | $CAT /sys/kernel/debug/tracing/trace_stat/function$CPU \ |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 69 | | sed "$REPLACE_STRING" |
| 70 | done |
| 71 | } |
| 72 | |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 73 | |
| 74 | ################################################################################ |
| 75 | # CGroups Utility Functions |
| 76 | ################################################################################ |
| 77 | |
| 78 | cgroups_get_attributes() { |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame] | 79 | test $# -eq 2 || exit -1 |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 80 | CGROUP="$1" |
| 81 | CONTROLLER="$2" |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame] | 82 | # 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 Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 101 | cgroups_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 Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 107 | shift 1 |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 108 | # The command to run |
Patrick Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 109 | CMD="${@}" |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 110 | |
| 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 Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 143 | exec $CMD |
| 144 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | cgroups_tasks_move() { |
| 148 | SRC_GRP=${1} |
| 149 | DST_GRP=${2} |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 150 | shift 2 |
| 151 | FILTERS=$* |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 152 | |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame] | 153 | $CAT $SRC_GRP/tasks | while read TID; do |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 154 | echo $TID > $DST_GRP/cgroup.procs |
| 155 | done |
| 156 | |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 157 | [ "x$FILTERS" = "x" ] && exit 0 |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 158 | |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 159 | PIDS=`ps | $GREP $FILTERS | $AWK '{print $2}'` |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 160 | PIDS=`echo $PIDS` |
| 161 | echo "PIDs to save: [$PIDS]" |
| 162 | for TID in $PIDS; do |
Brendan Jackman | e45fcca | 2016-11-25 16:01:09 +0000 | [diff] [blame^] | 163 | COMM=`$CAT /proc/$TID/comm` |
Patrick Bellasi | 21d18f8 | 2016-08-26 18:13:47 +0100 | [diff] [blame] | 164 | echo "$TID : $COMM" |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 165 | echo $TID > $SRC_GRP/cgroup.procs |
| 166 | done |
| 167 | } |
| 168 | |
Patrick Bellasi | 42efd0a | 2016-08-26 16:25:43 +0100 | [diff] [blame] | 169 | cgroups_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 Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 180 | ################################################################################ |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 181 | # Main Function Dispatcher |
| 182 | ################################################################################ |
| 183 | |
| 184 | case $CMD in |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 185 | cpufreq_set_all_frequencies) |
| 186 | cpufreq_set_all_frequencies $* |
| 187 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 188 | cpufreq_get_all_frequencies) |
| 189 | cpufreq_get_all_frequencies |
| 190 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 191 | cpufreq_set_all_governors) |
| 192 | cpufreq_set_all_governors $* |
| 193 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 194 | cpufreq_get_all_governors) |
| 195 | cpufreq_get_all_governors |
| 196 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 197 | cpufreq_trace_all_frequencies) |
| 198 | cpufreq_trace_all_frequencies $* |
| 199 | ;; |
Brendan Jackman | 6cdae6b | 2016-11-01 18:21:49 +0000 | [diff] [blame] | 200 | cpuidle_wake_all_cpus) |
| 201 | cpuidle_wake_all_cpus $* |
| 202 | ;; |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 203 | cgroups_get_attributes) |
| 204 | cgroups_get_attributes $* |
| 205 | ;; |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 206 | cgroups_run_into) |
| 207 | cgroups_run_into $* |
| 208 | ;; |
| 209 | cgroups_tasks_move) |
| 210 | cgroups_tasks_move $* |
| 211 | ;; |
Patrick Bellasi | 42efd0a | 2016-08-26 16:25:43 +0100 | [diff] [blame] | 212 | cgroups_tasks_in) |
| 213 | cgroups_tasks_in $* |
| 214 | ;; |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 215 | ftrace_get_function_stats) |
| 216 | ftrace_get_function_stats |
| 217 | ;; |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 218 | *) |
| 219 | echo "Command [$CMD] not supported" |
| 220 | exit -1 |
| 221 | esac |
| 222 | |
| 223 | # vim: tabstop=4 shiftwidth=4 |