xref: /aosp_15_r20/art/tools/boot-image-profile-generate.sh (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker#!/bin/bash
2*795d594fSAndroid Build Coastguard Worker#
3*795d594fSAndroid Build Coastguard Worker# Copyright (C) 2020 The Android Open Source Project
4*795d594fSAndroid Build Coastguard Worker#
5*795d594fSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*795d594fSAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*795d594fSAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*795d594fSAndroid Build Coastguard Worker#
9*795d594fSAndroid Build Coastguard Worker#      http://www.apache.org/licenses/LICENSE-2.0
10*795d594fSAndroid Build Coastguard Worker#
11*795d594fSAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*795d594fSAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*795d594fSAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*795d594fSAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*795d594fSAndroid Build Coastguard Worker# limitations under the License.
16*795d594fSAndroid Build Coastguard Worker
17*795d594fSAndroid Build Coastguard Worker#
18*795d594fSAndroid Build Coastguard Worker# This script creates the final boot image profile (suitable to include in the platform build).
19*795d594fSAndroid Build Coastguard Worker# The input to the script are:
20*795d594fSAndroid Build Coastguard Worker#   1) the boot.zip file which contains the boot classpath and system server jars.
21*795d594fSAndroid Build Coastguard Worker#      This file can be obtained from running `m dist` or by configuring the device with
22*795d594fSAndroid Build Coastguard Worker#      the `art/tools/boot-image-profile-configure-device.sh` script.
23*795d594fSAndroid Build Coastguard Worker#   2) the preloaded classes denylist which specify what clases should not be preloaded
24*795d594fSAndroid Build Coastguard Worker#      in Zygote. Usually located in usually in frameworks/base/config/preloaded-classes-denylist
25*795d594fSAndroid Build Coastguard Worker#   3) a list of raw boot image profiles extracted from devices. An example how to do that is
26*795d594fSAndroid Build Coastguard Worker#      by running `art/tools/boot-image-profile-extract-profile.sh` script.
27*795d594fSAndroid Build Coastguard Worker#
28*795d594fSAndroid Build Coastguard Worker# It is strongly recommended that you make use of extensive critical user journeys flows in order
29*795d594fSAndroid Build Coastguard Worker# to capture the raw boot image profiles described in #3.
30*795d594fSAndroid Build Coastguard Worker#
31*795d594fSAndroid Build Coastguard Worker# NOTE: by default, the script uses default arguments for producing the boot image profiles.
32*795d594fSAndroid Build Coastguard Worker# You might want to adjust the default generation arguments based on the shape of profile
33*795d594fSAndroid Build Coastguard Worker# and based on the metrics that matter for each product.
34*795d594fSAndroid Build Coastguard Worker#
35*795d594fSAndroid Build Coastguard Worker
36*795d594fSAndroid Build Coastguard Workerif [[ -z "$ANDROID_BUILD_TOP" ]]; then
37*795d594fSAndroid Build Coastguard Worker  echo "You must run on this after running envsetup.sh and launch target"
38*795d594fSAndroid Build Coastguard Worker  exit 1
39*795d594fSAndroid Build Coastguard Workerfi
40*795d594fSAndroid Build Coastguard Worker
41*795d594fSAndroid Build Coastguard Workerif [[ "$#" -lt 4 ]]; then
42*795d594fSAndroid Build Coastguard Worker  echo "Usage $0 <output-dir> <boot.zip-location> <preloaded-denylist-location> <profile-input1> <profile-input2> ... <profman args>"
43*795d594fSAndroid Build Coastguard Worker  echo "Without any profman args the script will use defaults."
44*795d594fSAndroid Build Coastguard Worker  echo "Example: $0 output-dir boot.zip frameworks/base/config/preloaded-classes-denylist android1.prof android2.prof"
45*795d594fSAndroid Build Coastguard Worker  echo "         $0 output-dir boot.zip frameworks/base/config/preloaded-classes-denylist android.prof --profman-arg --upgrade-startup-to-hot=true"
46*795d594fSAndroid Build Coastguard Worker  echo "preloaded-deny-list-location is usually frameworks/base/config/preloaded-classes-denylist"
47*795d594fSAndroid Build Coastguard Worker  exit 1
48*795d594fSAndroid Build Coastguard Workerfi
49*795d594fSAndroid Build Coastguard Worker
50*795d594fSAndroid Build Coastguard Workerecho "Creating work dir"
51*795d594fSAndroid Build Coastguard WorkerWORK_DIR=/tmp/android-bcp
52*795d594fSAndroid Build Coastguard Workermkdir -p "$WORK_DIR"
53*795d594fSAndroid Build Coastguard Worker
54*795d594fSAndroid Build Coastguard WorkerOUT_DIR="$1"
55*795d594fSAndroid Build Coastguard WorkerBOOT_ZIP="$2"
56*795d594fSAndroid Build Coastguard WorkerPRELOADED_DENYLIST="$3"
57*795d594fSAndroid Build Coastguard Workershift 3
58*795d594fSAndroid Build Coastguard Worker
59*795d594fSAndroid Build Coastguard Worker# Read the profile input args.
60*795d594fSAndroid Build Coastguard Workerprofman_profile_input_args=()
61*795d594fSAndroid Build Coastguard Workerwhile [[ "$#" -ge 1 ]] && [[ ! "$1" = '--profman-arg' ]]; do
62*795d594fSAndroid Build Coastguard Worker  profman_profile_input_args+=("--profile-file=$1")
63*795d594fSAndroid Build Coastguard Worker  shift
64*795d594fSAndroid Build Coastguard Workerdone
65*795d594fSAndroid Build Coastguard Worker
66*795d594fSAndroid Build Coastguard Worker# Read the profman args.
67*795d594fSAndroid Build Coastguard Workerprofman_args=()
68*795d594fSAndroid Build Coastguard Workerwhile [[ "$#" -ge 2 ]] && [[ "$1" = '--profman-arg' ]]; do
69*795d594fSAndroid Build Coastguard Worker  profman_args+=("$2")
70*795d594fSAndroid Build Coastguard Worker  shift 2
71*795d594fSAndroid Build Coastguard Workerdone
72*795d594fSAndroid Build Coastguard Worker
73*795d594fSAndroid Build Coastguard WorkerOUT_BOOT_PROFILE="$OUT_DIR"/boot-image-profile.txt
74*795d594fSAndroid Build Coastguard WorkerOUT_PRELOADED_CLASSES="$OUT_DIR"/preloaded-classes
75*795d594fSAndroid Build Coastguard WorkerOUT_SYSTEM_SERVER="$OUT_DIR"/art-profile
76*795d594fSAndroid Build Coastguard Worker
77*795d594fSAndroid Build Coastguard Workerecho "Changing dirs to the build top"
78*795d594fSAndroid Build Coastguard Workercd "$ANDROID_BUILD_TOP"
79*795d594fSAndroid Build Coastguard Worker
80*795d594fSAndroid Build Coastguard Workerecho "Unziping boot.zip"
81*795d594fSAndroid Build Coastguard WorkerBOOT_UNZIP_DIR="$WORK_DIR"/boot-dex
82*795d594fSAndroid Build Coastguard WorkerBOOT_JARS="$BOOT_UNZIP_DIR"/dex_bootjars_input
83*795d594fSAndroid Build Coastguard WorkerSYSTEM_SERVER_JAR="$BOOT_UNZIP_DIR"/system/framework/services.jar
84*795d594fSAndroid Build Coastguard Worker
85*795d594fSAndroid Build Coastguard Workerunzip -o "$BOOT_ZIP" -d "$BOOT_UNZIP_DIR"
86*795d594fSAndroid Build Coastguard Worker
87*795d594fSAndroid Build Coastguard Workerecho "Processing boot image jar files"
88*795d594fSAndroid Build Coastguard Workerjar_args=()
89*795d594fSAndroid Build Coastguard Workerfor entry in "$BOOT_JARS"/*
90*795d594fSAndroid Build Coastguard Workerdo
91*795d594fSAndroid Build Coastguard Worker  jar_args+=("--apk=$entry")
92*795d594fSAndroid Build Coastguard Workerdone
93*795d594fSAndroid Build Coastguard Workerprofman_args+=("${jar_args[@]}")
94*795d594fSAndroid Build Coastguard Worker
95*795d594fSAndroid Build Coastguard Workerecho "Running profman for boot image profiles"
96*795d594fSAndroid Build Coastguard Worker# NOTE:
97*795d594fSAndroid Build Coastguard Worker# You might want to adjust the default generation arguments based on the data
98*795d594fSAndroid Build Coastguard Worker# For example, to update the selection thresholds you could specify:
99*795d594fSAndroid Build Coastguard Worker#  --method-threshold=10 \
100*795d594fSAndroid Build Coastguard Worker#  --class-threshold=10 \
101*795d594fSAndroid Build Coastguard Worker#  --preloaded-class-threshold=10 \
102*795d594fSAndroid Build Coastguard Worker#  --special-package=android:1 \
103*795d594fSAndroid Build Coastguard Worker#  --special-package=com.android.systemui:1 \
104*795d594fSAndroid Build Coastguard Worker# The threshold is percentage of total aggregation, that is, a method/class is
105*795d594fSAndroid Build Coastguard Worker# included in the profile only if it's used by at least x% of the packages.
106*795d594fSAndroid Build Coastguard Worker# (from 0% - include everything to 100% - include only the items that
107*795d594fSAndroid Build Coastguard Worker# are used by all packages on device).
108*795d594fSAndroid Build Coastguard Worker# The --special-package allows you to give a prioriority to certain packages,
109*795d594fSAndroid Build Coastguard Worker# meaning, if the methods is used by that package then the algorithm will use a
110*795d594fSAndroid Build Coastguard Worker# different selection thresholds.
111*795d594fSAndroid Build Coastguard Worker# (system server is identified as the "android" package)
112*795d594fSAndroid Build Coastguard Workerprofman \
113*795d594fSAndroid Build Coastguard Worker  --generate-boot-image-profile \
114*795d594fSAndroid Build Coastguard Worker  "${profman_profile_input_args[@]}" \
115*795d594fSAndroid Build Coastguard Worker  --out-profile-path="$OUT_BOOT_PROFILE" \
116*795d594fSAndroid Build Coastguard Worker  --out-preloaded-classes-path="$OUT_PRELOADED_CLASSES" \
117*795d594fSAndroid Build Coastguard Worker  --preloaded-classes-denylist="$PRELOADED_DENYLIST" \
118*795d594fSAndroid Build Coastguard Worker  --special-package=android:1 \
119*795d594fSAndroid Build Coastguard Worker  --special-package=com.android.systemui:1 \
120*795d594fSAndroid Build Coastguard Worker  "${profman_args[@]}"
121*795d594fSAndroid Build Coastguard Worker
122*795d594fSAndroid Build Coastguard Workerecho "Done boot image profile"
123*795d594fSAndroid Build Coastguard Worker
124*795d594fSAndroid Build Coastguard Workerecho "Running profman for system server"
125*795d594fSAndroid Build Coastguard Worker# For system server profile we want to include everything usually
126*795d594fSAndroid Build Coastguard Worker# We also don't have a preloaded-classes file for it, so we ignore the argument.
127*795d594fSAndroid Build Coastguard Workerprofman \
128*795d594fSAndroid Build Coastguard Worker  --generate-boot-image-profile \
129*795d594fSAndroid Build Coastguard Worker  "${profman_profile_input_args[@]}" \
130*795d594fSAndroid Build Coastguard Worker  --out-profile-path="$OUT_SYSTEM_SERVER" \
131*795d594fSAndroid Build Coastguard Worker  --apk="$SYSTEM_SERVER_JAR" \
132*795d594fSAndroid Build Coastguard Worker  --method-threshold=0 \
133*795d594fSAndroid Build Coastguard Worker  --class-threshold=0
134*795d594fSAndroid Build Coastguard Worker
135*795d594fSAndroid Build Coastguard Workerecho "Done system server"
136*795d594fSAndroid Build Coastguard Worker
137*795d594fSAndroid Build Coastguard Workerecho ""
138*795d594fSAndroid Build Coastguard Workerecho "Boot profile methods+classes count:          $(wc -l $OUT_BOOT_PROFILE)"
139*795d594fSAndroid Build Coastguard Workerecho "Preloaded classes count:                     $(wc -l $OUT_PRELOADED_CLASSES)"
140*795d594fSAndroid Build Coastguard Workerecho "System server profile methods+classes count: $(wc -l $OUT_SYSTEM_SERVER)"
141*795d594fSAndroid Build Coastguard Worker
142*795d594fSAndroid Build Coastguard WorkerCLEAN_UP="${CLEAN_UP:-true}"
143*795d594fSAndroid Build Coastguard Workerif [[ "$CLEAN_UP" = "true" ]]; then
144*795d594fSAndroid Build Coastguard Worker  rm -rf "$WORK_DIR"
145*795d594fSAndroid Build Coastguard Workerfi
146