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} |
| 10 | |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 11 | ################################################################################ |
| 12 | # CPUFrequency Utility Functions |
| 13 | ################################################################################ |
| 14 | |
| 15 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 22 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 27 | cpufreq_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 Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 34 | cpufreq_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 Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 39 | cpufreq_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 Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 46 | |
| 47 | ################################################################################ |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 48 | # FTrace Utility Functions |
| 49 | ################################################################################ |
| 50 | |
| 51 | ftrace_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 Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 59 | |
| 60 | ################################################################################ |
| 61 | # CGroups Utility Functions |
| 62 | ################################################################################ |
| 63 | |
| 64 | cgroups_get_attributes() { |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame^] | 65 | test $# -eq 2 || exit -1 |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 66 | CGROUP="$1" |
| 67 | CONTROLLER="$2" |
Patrick Bellasi | c976189 | 2016-04-26 15:28:16 +0100 | [diff] [blame^] | 68 | # 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 Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 87 | cgroups_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 | |
| 131 | cgroups_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 Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 152 | ################################################################################ |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 153 | # Main Function Dispatcher |
| 154 | ################################################################################ |
| 155 | |
| 156 | case $CMD in |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 157 | cpufreq_set_all_frequencies) |
| 158 | cpufreq_set_all_frequencies $* |
| 159 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 160 | cpufreq_get_all_frequencies) |
| 161 | cpufreq_get_all_frequencies |
| 162 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 163 | cpufreq_set_all_governors) |
| 164 | cpufreq_set_all_governors $* |
| 165 | ;; |
Patrick Bellasi | 51b7f01 | 2015-11-27 16:40:58 +0000 | [diff] [blame] | 166 | cpufreq_get_all_governors) |
| 167 | cpufreq_get_all_governors |
| 168 | ;; |
Patrick Bellasi | cf76131 | 2015-11-27 16:38:30 +0000 | [diff] [blame] | 169 | cpufreq_trace_all_frequencies) |
| 170 | cpufreq_trace_all_frequencies $* |
| 171 | ;; |
Patrick Bellasi | a65ff13 | 2016-02-23 12:11:06 +0000 | [diff] [blame] | 172 | cgroups_get_attributes) |
| 173 | cgroups_get_attributes $* |
| 174 | ;; |
Patrick Bellasi | 2873939 | 2016-04-22 11:43:49 +0100 | [diff] [blame] | 175 | cgroups_run_into) |
| 176 | cgroups_run_into $* |
| 177 | ;; |
| 178 | cgroups_tasks_move) |
| 179 | cgroups_tasks_move $* |
| 180 | ;; |
Patrick Bellasi | 082a82c | 2016-01-26 15:38:44 +0000 | [diff] [blame] | 181 | ftrace_get_function_stats) |
| 182 | ftrace_get_function_stats |
| 183 | ;; |
Patrick Bellasi | f2eac51 | 2015-11-27 16:35:57 +0000 | [diff] [blame] | 184 | *) |
| 185 | echo "Command [$CMD] not supported" |
| 186 | exit -1 |
| 187 | esac |
| 188 | |
| 189 | # vim: tabstop=4 shiftwidth=4 |