1#!/bin/bash
2#
3# Copyright (C) 2023 Google LLC
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16
17WATCHDOG_TASK="watchdog_cycles"
18WATCHDOG_CYCLES_TASK_HOST="${OUT}/system/bin/${WATCHDOG_TASK}"
19WATCHDOG_CYCLES_TASK_ANDROID="/data/local/tmp/${WATCHDOG_TASK}"
20
21if [ -z "${RESULTS_DIR}" ] ; then
22  echo "Error: you need to specify CARWATCHDOG_CUSTOM_DUMP"
23  exit 1
24fi
25
26# Check if the watchdog cycle file is present in Android
27adb shell ls "${WATCHDOG_CYCLES_TASK_ANDROID}"
28if [ $? -ne 0 ]; then
29  if [ -f "${WATCHDOG_CYCLES_TASK_HOST}" ]; then
30    adb push "${WATCHDOG_CYCLES_TASK_HOST}" "${WATCHDOG_CYCLES_TASK_ANDROID}"
31    adb shell chmod +x "${WATCHDOG_CYCLES_TASK_ANDROID}"
32  else
33    echo "Did not find ${WATCHDOG_CYCLES_TASK_HOST} in host.
34    Try running 'm ${WATCHDOG_TASK}' to generate the file."
35    exit 1
36  fi
37fi
38
39SYSFS_CPUFREQ="/sys/devices/system/cpu/cpufreq"
40GOVERNOR='userspace'
41
42# Available frequencies for the CPU freq policies in Seahawk
43# policy0:
44# - 300000 403200 499200 576000 672000 768000 844800 940800 1036800 1113600
45#   1209600 1305600 1382400 1478400 1555200 1632000 1708800 1785600
46# policy4:
47# - 710400 825600 940800 1056000 1171200 1286400 1401600 1497600 1612800 1708800
48#   1804800 1920000 2016000 2131200
49# policy7:
50# - 825600 940800 1056000 1171200 1286400 1401600 1497600 1612800 1708800
51#   1804800 1920000 2016000 2131200 2227200 2323200 2419200
52
53declare -A POLICY_SPEED
54POLICY_SPEED[policy0]=403200
55POLICY_SPEED[policy4]=710400
56POLICY_SPEED[policy7]=2419200
57
58adb root
59
60# Stop the framework
61adb shell stop
62
63# Set the CPU frequency to the same value across all CPU freq policies
64for POLICY in $(adb shell ls /sys/devices/system/cpu/cpufreq) ; do
65  POLICY_FILE="${SYSFS_CPUFREQ}/${POLICY}/scaling_governor"
66  SPEED_FILE="${SYSFS_CPUFREQ}/${POLICY}/scaling_setspeed"
67  SCALING_SPEED=${POLICY_SPEED[${POLICY}]}
68  adb shell "echo ${GOVERNOR} > ${POLICY_FILE}"
69  adb shell "echo ${SCALING_SPEED} > ${SYSFS_CPUFREQ}/${POLICY}/scaling_min_freq"
70  adb shell "echo ${SCALING_SPEED} > ${SYSFS_CPUFREQ}/${POLICY}/scaling_max_freq"
71  adb shell "echo ${SCALING_SPEED} > ${SPEED_FILE}"
72  SET_GOVERNOR=$(adb shell cat "${POLICY_FILE}")
73  CUR_FREQ=$(adb shell cat "${SYSFS_CPUFREQ}/${POLICY}/cpuinfo_cur_freq")
74  if [ "${SET_GOVERNOR}" != "${GOVERNOR}" ] ; then
75    echo "Unable to set the governor to '${GOVERNOR}' in ${POLICY_FILE}."
76    echo "Exiting experiment"
77    exit 1
78  fi
79  if [ "${CUR_FREQ}" != "${SCALING_SPEED}" ] ; then
80    echo "Unable to set '${SCALING_SPEED}' in ${SPEED_FILE}."
81    echo "Exiting experiment"
82    exit 1
83  fi
84done
85
86# Set the desired cpuset to only use cpu7
87CPUSET="top-app"
88CPUSET_DIR="/dev/cpuset/${CPUSET}"
89CORE_NUMS="7" # Possible values: 0-3, 4-6, 7
90CORE_POLICY="policy7" # The policy which contains the cores above
91
92PREV_CORES=$(adb shell cat "${CPUSET_DIR}/cpus")
93
94adb shell "echo ${CORE_NUMS} > ${CPUSET_DIR}/cpus"
95
96# Add the shell process to run on the cpuset
97# Does it also need to be pushed to $CPUSET_DIR/tasks?
98adb shell "echo $$ > ${CPUSET_DIR}/cgroup.procs"
99
100
101# Set the outputs
102CARWATCHDOG_CUSTOM_DUMP="${RESULTS_DIR}/cw_freq_${POLICY_SPEED[${CORE_POLICY}]}.txt"
103CARWATCHDOG_CUSTOM_PB="${RESULTS_DIR}/cw_freq_${POLICY_SPEED[${CORE_POLICY}]}.pb"
104
105# Start a carwatchdog custom collection filtering only for 'root' package
106adb shell dumpsys android.automotive.watchdog.ICarWatchdog/default --start_perf \
107           --max_duration 600 --interval 1 --filter_packages root
108if [ $? -ne 0 ]; then
109  echo "Unable to start carwatchdog custom collection."
110  exit 1
111fi
112
113sleep 2
114
115# Process will be running on cores=$CORE_NUMS, hence the CPU frequency used is
116# the will be the one set by the CPU freq policy containing the cores.
117time adb shell "${WATCHDOG_CYCLES_TASK_ANDROID}"
118
119sleep 2
120
121adb shell dumpsys android.automotive.watchdog.ICarWatchdog/default \
122           --stop_perf > "${CARWATCHDOG_CUSTOM_DUMP}"
123if [ $? -ne 0 ]; then
124  echo "Unable to stop carwatchdog custom collection."
125fi
126
127# Generate the proto for the carwatchdog stats
128perf_stats_parser -f "${CARWATCHDOG_CUSTOM_DUMP}" -o "${CARWATCHDOG_CUSTOM_PB}"
129
130# Set the cpuset back to the original values
131adb shell "echo ${PREV_CORES} > ${CPUSET_DIR}/cpus"
132
133# Start the framework again
134adb shell start