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 | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 11 | |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 12 | ################################################################################ |
| 13 | # CPUFrequency Utility Functions |
| 14 | ################################################################################ |
| 15 | |
| 16 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 23 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 28 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 35 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 40 | cpufreq_trace_all_frequencies() { |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame^] | 41 | FREQS=$($CAT /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq) |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 42 | 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 Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 47 | |
| 48 | ################################################################################ |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 49 | # FTrace Utility Functions |
| 50 | ################################################################################ |
| 51 | |
| 52 | ftrace_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 Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame^] | 55 | $CAT /sys/kernel/debug/tracing/trace_stat/function$CPU \ |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 56 | | sed "$REPLACE_STRING" |
| 57 | done |
| 58 | } |
| 59 | |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 60 | |
| 61 | ################################################################################ |
| 62 | # CGroups Utility Functions |
| 63 | ################################################################################ |
| 64 | |
| 65 | cgroups_get_attributes() { |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame] | 66 | test $# -eq 2 || exit -1 |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 67 | CGROUP="$1" |
| 68 | CONTROLLER="$2" |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame] | 69 | # 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 Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 88 | cgroups_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 Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 94 | shift 1 |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 95 | # The command to run |
Patrick Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 96 | CMD="${@}" |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 97 | |
| 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 Bellasi | 96392fd | 2016-04-26 15:30:03 +0100 | [diff] [blame] | 130 | exec $CMD |
| 131 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | cgroups_tasks_move() { |
| 135 | SRC_GRP=${1} |
| 136 | DST_GRP=${2} |
| 137 | GREP_EXCLUSE=${3:-''} |
| 138 | |
Patrick Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame^] | 139 | $CAT $SRC_GRP/tasks | while read TID; do |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 140 | 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 Bellasi | 83c1312 | 2016-08-26 16:12:23 +0100 | [diff] [blame^] | 149 | CMDLINE=`$CAT /proc/$TID/cmdline` |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 150 | echo "$TID : $CMDLINE" |
| 151 | echo $TID > $SRC_GRP/cgroup.procs |
| 152 | done |
| 153 | } |
| 154 | |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 155 | ################################################################################ |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 156 | # Main Function Dispatcher |
| 157 | ################################################################################ |
| 158 | |
| 159 | case $CMD in |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 160 | cpufreq_set_all_frequencies) |
| 161 | cpufreq_set_all_frequencies $* |
| 162 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 163 | cpufreq_get_all_frequencies) |
| 164 | cpufreq_get_all_frequencies |
| 165 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 166 | cpufreq_set_all_governors) |
| 167 | cpufreq_set_all_governors $* |
| 168 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 169 | cpufreq_get_all_governors) |
| 170 | cpufreq_get_all_governors |
| 171 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 172 | cpufreq_trace_all_frequencies) |
| 173 | cpufreq_trace_all_frequencies $* |
| 174 | ;; |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 175 | cgroups_get_attributes) |
| 176 | cgroups_get_attributes $* |
| 177 | ;; |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 178 | cgroups_run_into) |
| 179 | cgroups_run_into $* |
| 180 | ;; |
| 181 | cgroups_tasks_move) |
| 182 | cgroups_tasks_move $* |
| 183 | ;; |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 184 | ftrace_get_function_stats) |
| 185 | ftrace_get_function_stats |
| 186 | ;; |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 187 | *) |
| 188 | echo "Command [$CMD] not supported" |
| 189 | exit -1 |
| 190 | esac |
| 191 | |
| 192 | # vim: tabstop=4 shiftwidth=4 |