1. 涉及的props
名称 | 说明 |
---|---|
dalvik.vm.boot-dex2oat-threads | 要求 post_bootcomplete 为false |
dalvik.vm.restore-dex2oat-threads | 要求 for_restore 为 true,备份prop 为 dalvik.vm.dex2oat-threads |
dalvik.vm.background-dex2oat-threads | 要求 background_job_compile,备份prop 为 dalvik.vm.dex2oat-threads |
dalvik.vm.dex2oat-threads | 通常作为备份的prop,上述prop 没有配置时,会使用这个备份prop |
2. log 打印
在安装apk 的时候会有这样的打印:
07-26 07:36:09.266 24997 24997 I dex2oat32: /apex/com.android.art/bin/dex2oat32 --output-vdex-fd=8 --class-loader-context-fds=10:11 --class-loader-context=PCL[]{PCL[/system/framework/org.apache.http.legacy.jar]#PCL[/system/framework/org.apache.http.legacy.jar]} --classpath-dir=/data/app/~~hJg6lVY8jC9UtJ-HIrJCVg==/com.alibaba.intl.android.apps.poseidon-ReSJr9u5S2aonOhzWncgMg== --compiler-filter=verify --compilation-reason=install --compact-dex-level=none --max-image-block-size=524288 --resolve-startup-const-strings=true --generate-mini-debug-info -j1 --comments=app-version-name:8.18.1,app-version-code:81801,art-version:341711000
07-26 07:36:22.484 24997 24997 I dex2oat32: dex2oat took 13.224s (13.041s cpu) (threads: 1) arena alloc=0B (0B) java alloc=49MB (51850816B) native alloc=4109KB (4208272B) free=2304KB (2359488B) swap=16MB (16777216B)
在dex2oat 安装app 时,logcat 会输出上面两行:
-
第一行使用 dex2oat 的命令,包括命令行参数;
-
第二行是在 dex2oat 完成时,打印耗时以及使用threads 个数等信息;
3. thread_count_
dex2oat 的编译线程数主要由成员变量thread_count_
控制。在运行 dex2oat 的时候会将线程数通过-j
的方式传入。
3.1 默认值
art/dex2oat/dex2oat.cc
class Dex2Oat final {
public:
explicit Dex2Oat(TimingLogger* timings)
: compiler_kind_(Compiler::kOptimizing),
// Take the default set of instruction features from the build.
key_value_store_(nullptr),
verification_results_(nullptr),
runtime_(nullptr),
thread_count_(sysconf(_SC_NPROCESSORS_CONF)),
在 Dex2Oat 构造的时候会初始化thread_count_
,即dex2oat 编译的线程总数默认为 CPU 的数量。
所以,当第一节中的prop 都没有配置时,会看到 logcat 中:
-
dex2oat 命令行不会出现
-j
的字样; -
dex2oat 安装完成的log 中
threads: 8
的字样(假如处理器个数为8);
3.2 LogCompletionTime()
art/dex2oat/dex2oat.cc
void LogCompletionTime() {
// Note: when creation of a runtime fails, e.g., when trying to compile an app but when there
// is no image, there won't be a Runtime::Current().
// Note: driver creation can fail when loading an invalid dex file.
LOG(INFO) << "dex2oat took "
<< PrettyDuration(NanoTime() - start_ns_)
<< " (" << PrettyDuration(ProcessCpuNanoTime() - start_cputime_ns_) << " cpu)"
<< " (threads: " << thread_count_ << ") "
<< ((Runtime::Current() != nullptr && driver_ != nullptr) ?
driver_->GetMemoryUsageString(kIsDebugBuild || VLOG_IS_ON(compiler)) :
"");
}
dex2oat 的编译线程数主要由成员变量thread_count_
控制。在运行 dex2oat 的时候会将线程数通过-j
的方式传入。
3.3 RunDex2Oat::Initialize()
在安装app 时(例如,installd),会创建 RunDex2Oat 并进行初始化:
frameworks/native/cmds/installd/run_dex2oat.cpp
void RunDex2Oat::Initialize(const UniqueFile& output_oat,
const UniqueFile& output_vdex,
const UniqueFile& output_image,
const UniqueFile& input_dex,
const UniqueFile& input_vdex,
const UniqueFile& dex_metadata,
const UniqueFile& profile,
const char* class_loader_context,
const std::string& class_loader_context_fds,
int swap_fd,
const char* instruction_set,
const char* compiler_filter,
bool debuggable,
bool post_bootcomplete,
bool for_restore,
int target_sdk_version,
bool enable_hidden_api_checks,
bool generate_compact_dex,
bool use_jitzygote,
bool background_job_compile,
const char* compilation_reason) {
PrepareBootImageFlags(use_jitzygote);
PrepareInputFileFlags(output_oat, output_vdex, output_image, input_dex, input_vdex,
dex_metadata, profile, swap_fd, class_loader_context,
class_loader_context_fds);
PrepareCompilerConfigFlags(input_vdex, output_vdex, instruction_set, compiler_filter,
debuggable, target_sdk_version, enable_hidden_api_checks,
generate_compact_dex, compilation_reason);
PrepareCompilerRuntimeAndPerfConfigFlags(post_bootcomplete, for_restore,
background_job_compile);
frameworks/native/cmds/installd/run_dex2oat.cpp
void RunDex2Oat::PrepareCompilerRuntimeAndPerfConfigFlags(bool post_bootcomplete,
bool for_restore,
bool background_job_compile) {
// CPU set
{
...
}
// Number of threads
{
std::string threads_format = "-j%s";
std::string dex2oat_threads_arg = post_bootcomplete
? (for_restore
? MapPropertyToArgWithBackup(
"dalvik.vm.restore-dex2oat-threads",
"dalvik.vm.dex2oat-threads",
threads_format)
: (background_job_compile
? MapPropertyToArgWithBackup(
"dalvik.vm.background-dex2oat-threads",
"dalvik.vm.dex2oat-threads",
threads_format)
: MapPropertyToArg("dalvik.vm.dex2oat-threads", threads_format)))
: MapPropertyToArg("dalvik.vm.boot-dex2oat-threads", threads_format);
AddArg(dex2oat_threads_arg);
}
上面两个行数是配置 dex2oat 的 thread_count_ 的主要逻辑,从代码中可以清晰看到-j
的 参数是通过 prop 控制的,详细对照第 1 节表中。而当这些prop 都没有配置时,-j 携带的线程数为空,系统将使用默认值,详细看第 3.1 节。
补充:
dex2oat -h 时,会在logcat 中显示usage:
dex2oat : Command: dex2oat -h
dex2oat : Usage: dex2oat [options]...
dex2oat :
dex2oat : --dex-file=<dex-file> [--dex-file=<dex-file>...]
dex2oat : Specifies a .dex, .jar, or .apk file to compile.
dex2oat : Eg: --dex-file=/system/framework/core.jar
dex2oat :
dex2oat : --dex-location=<dex-location> [--dex-location=<dex-location>...]
dex2oat : specifies an alternative dex location to encode in the oat file for the
dex2oat : corresponding --dex-file argument. The first --dex-location corresponds to
dex2oat : the first --dex-file, the second to the second and so on.
dex2oat : Eg: --dex-file=/home/build/out/system/framework/core.jar
dex2oat : --dex-location=/system/framework/core.jar
dex2oat :
dex2oat : --dex-fd={int values} [--dex-fd={int values}...]
dex2oat : Specifies a file descriptor of a dex file. It can be specified for multiple
dex2oat : times, but the number must match the number of --dex-file. Eg: --dex-fd=5
dex2oat :
dex2oat : --zip-fd={integer value}
dex2oat : specifies a file descriptor of a zip file containing a classes.dex file to
dex2oat : compile. Eg: --zip-fd=5
dex2oat :
dex2oat : --zip-location={string value}
dex2oat : Specifies a symbolic name for the file corresponding to the FD given by
dex2oat : --zip-fd.
dex2oat :
dex2oat : --boot-image={string value}
dex2oat : provide the image file for the boot class path.
dex2oat : Do not include the arch as part of the name, it is added automatically.
dex2oat : Example: --boot-image=/system/framework/boot.art
dex2oat : (specifies /system/framework/<arch>/boot.art as the image file)
dex2oat : Example: --boot-image=boot.art:boot-framework.art
dex2oat : (specifies <bcp-path1>/<arch>/boot.art as the image file and
dex2oat : <bcp-path2>/<arch>/boot-framework.art as the image extension file
dex2oat : with paths taken from corresponding boot class path components)
dex2oat : Example: --boot-image=/apex/com.android.art/boot.art:/system/framework/*:*
dex2oat : (specifies /apex/com.android.art/<arch>/boot.art as the image
dex2oat : file and search for extensions in /framework/system and boot
dex2oat : class path components' paths)
dex2oat : Default: $ANDROID_ROOT/system/framework/boot.art
dex2oat :
dex2oat : --input-vdex-fd={integer value}
dex2oat : specifies the vdex input source via a file descriptor.
dex2oat :
dex2oat : --input-vdex={string value}
dex2oat : specifies the vdex input source via a filename.
dex2oat :
dex2oat : --output-vdex-fd={integer value}
dex2oat : specifies the vdex output destination via a file descriptor.
dex2oat :
dex2oat : --output-vdex={string value}
dex2oat : specifies the vdex output destination via a filename.
dex2oat :
dex2oat : --dm-fd={integer value}
dex2oat : specifies the dm output destination via a file descriptor.
dex2oat :
dex2oat : --dm-file={string value}
dex2oat : specifies the dm output destination via a filename.
dex2oat :
dex2oat : --oat-file={string value}
dex2oat : Specifies an oat output destination via a filename.
dex2oat : Eg: --oat-file=/system/framework/boot.oat
dex2oat :
dex2oat : --oat-symbols={string value}
dex2oat : Specifies a symbolized oat output destination.
dex2oat : Eg: --oat-symbols=symbols/system/framework/boot.oat
dex2oat :
dex2oat : --strip
dex2oat : remove all debugging sections at the end (but keep mini-debug-info).
dex2oat : This is equivalent to the "strip" command as build post-processing step.
dex2oat : It is intended to be used with --oat-symbols and it happens after it.
dex2oat : Eg: --oat-symbols=/symbols/system/framework/boot.oat
dex2oat :
dex2oat : --oat-fd={integer value}
dex2oat : Specifies the oat output destination via a file descriptor. Eg: --oat-fd=5
dex2oat :
dex2oat : --oat-location={string value}
dex2oat : specifies a symbolic name for the file corresponding to the file descriptor
dex2oat : specified by --oat-fd.
dex2oat : Eg: --oat-location=/data/dalvik-cache/system@app@Calculator.apk.oat
dex2oat :
dex2oat : --image={string value}
dex2oat : specifies an output image filename. Eg: --image=/system/framework/boot.art
dex2oat :
dex2oat : --image-fd={integer value}
dex2oat : specifies an output image file descriptor. Cannot be used with --image.
dex2oat : Eg: --image-fd=7
dex2oat :
dex2oat : --base={hex address}
dex2oat : Specifies the base address when creating a boot image. Eg: --base=0x50000000
dex2oat :
dex2oat : --app-image-file={string value}
dex2oat : Specify a file name for app image. Only used if a profile is passed in.
dex2oat :
dex2oat : --app-image-fd={integer value}
dex2oat : Specify a file descriptor for app image. Only used if a profile is passed in.
dex2oat :
dex2oat : --multi-image
dex2oat : --single-image
dex2oat : Specifies if separate oat and image files should be generated for each dex
dex2oat : file. --multi-image is default for boot image and --single-image for app
dex2oat : images.
dex2oat :
dex2oat : --dirty-image-objects={string value}
dex2oat : list of known dirty objects in the image. The image writer will group them together
dex2oat :
dex2oat : --dirty-image-objects-fd={integer value}
dex2oat : Specify a file descriptor for reading the list of known dirty objects in
dex2oat : the image. The image writer will group them together
dex2oat :
dex2oat : --updatable-bcp-packages-file={string value}
dex2oat : Deprecated. No longer takes effect.
dex2oat :
dex2oat : --updatable-bcp-packages-fd={integer value}
dex2oat : Deprecated. No longer takes effect.
dex2oat :
dex2oat : --image-format={lz4|lz4hc|uncompressed}
dex2oat : Which format to store the image Defaults to uncompressed. Eg: --image-format=lz4
dex2oat :
dex2oat : --swap-file={string value}
dex2oat : Specify a file to use for swap. Eg: --swap-file=/data/tmp/swap.001
dex2oat :
dex2oat : --swap-fd={integer value}
dex2oat : Specify a file to use for swap by file-descriptor. Eg: --swap-fd=3
dex2oat :
dex2oat : --swap-dex-size-threshold={unsigned integer value}
dex2oat : specifies the minimum total dex file size in bytes to allow the use of swap.
dex2oat :
dex2oat : --swap-dex-count-threshold={unsigned integer value}
dex2oat : specifies the minimum number of dex file to allow the use of swap.
dex2oat :
dex2oat : --run-passes={string value}
dex2oat :
dex2oat : --profile-file={string value} [--profile-file={string value}...]
dex2oat : Specify profiler output file to use for compilation using a filename.
dex2oat : When multiple profiles are used, the order matters: If multiple profiles
dex2oat : contain classes and methods of the same dex file with different checksums,
dex2oat : only the classes and methods from the first profile will be used for that
dex2oat : particular dex file.
dex2oat :
dex2oat : --profile-file-fd={int values} [--profile-file-fd={int values}...]
dex2oat : Specify profiler output file to use for compilation using a file-descriptor.
dex2oat :
dex2oat : --no-inline-from={string value}
dex2oat :
dex2oat : --preloaded-classes={string value} [--preloaded-classes={string value}...]
dex2oat : Specify files containing list of classes preloaded in the zygote.
dex2oat :
dex2oat : --preloaded-classes-fds={int values} [--preloaded-classes-fds={int values}...]
dex2oat : Specify files containing list of classes preloaded in the zygote.
dex2oat :
dex2oat : --instruction-set={arm|arm64|x86|x86_64|none}
dex2oat : Compile for a particular instruction set.
dex2oat :
dex2oat : --instruction-set-variant={Variant Name}
dex2oat : Specify instruction set features using variant name.
dex2oat : Eg: --instruction-set-variant=silvermont
dex2oat :
dex2oat : --instruction-set-features={string value}
dex2oat : Specify instruction set features.
dex2oat : On target the value 'runtime' can be used to detect features at run time.
dex2oat : If target does not support run-time detection the value 'runtime'
dex2oat : has the same effect as the value 'default'.
dex2oat : Note: the value 'runtime' has no effect if it is used on host.
dex2oat : Example: --instruction-set-features=div
dex2oat : Default: default
dex2oat :
dex2oat : --watch-dog
dex2oat : --no-watch-dog
dex2oat : Enable or disable the watchdog timer.
dex2oat :
dex2oat : --watchdog-timeout={integer value}
dex2oat : Set the watchdog timeout value in milliseconds.
dex2oat :
dex2oat : -j{unsigned integer value}
dex2oat : specifies the number of threads used for compilation. Default is the number
dex2oat : of detected hardware threads available on the host system.
dex2oat :
dex2oat : --cpu-set=<set>
dex2oat : sets the cpu affinitiy to the given <set>. The <set> is a comma separated
dex2oat : list of cpus. Eg: --cpu-set=0,1,2,3
dex2oat :
dex2oat : --android-root={string value}
dex2oat : Used to locate libraries for portable linking.
dex2oat : Eg: --android-root=out/host/linux-x86
dex2oat : Default: $ANDROID_ROOT
dex2oat :
dex2oat : --compiler-backend={Quick|Optimizing}
dex2oat : Select a compiler backend set. Default: optimizing
dex2oat :
dex2oat : --host
dex2oat : Run in host mode
dex2oat :
dex2oat : --avoid-storing-invocation
dex2oat : Avoid storing the invocation args in the key-value store. Used to test
dex2oat : determinism with different args.
dex2oat :
dex2oat : --very-large-app-threshold={unsigned integer value}
dex2oat : Specifies the minimum total dex file size in bytes to consider the input
dex2oat : "very large" and reduce compilation done.
dex2oat :
dex2oat : --force-determinism
dex2oat : Force the compiler to emit a deterministic output
dex2oat :
dex2oat : --check-linkage-conditions
dex2oat :
dex2oat : --crash-on-linkage-violation
dex2oat :
dex2oat : --copy-dex-files={true|false|always}
dex2oat : enable|disable copying the dex files into the output vdex.
dex2oat :
dex2oat : --force-allow-oj-inlines
dex2oat : Disables automatic no-inline for core-oj on host. Has no effect on target. FOR TESTING USE ONLY! DO NOT DISTRIBUTE BINARIES BUILT WITH THIS OPTION!
dex2oat :
dex2oat : --write-invocation-to={string value}
dex2oat : Write the invocation commandline to the given file for later use. Used to
dex2oat : test determinism with different args.
dex2oat :
dex2oat : --classpath-dir={string value}
dex2oat : Directory used to resolve relative class paths.
dex2oat :
dex2oat : --class-loader-context={string value}
dex2oat : a string specifying the intended runtime loading context for the compiled
dex2oat : dex files.
dex2oat :
dex2oat : --class-loader-context-fds={string value}
dex2oat : a colon-separated list of file descriptors for dex files in
dex2oat : --class-loader-context. Their order must be the same as dex files in a
dex2oat : flattened class loader context
dex2oat :
dex2oat : --stored-class-loader-context={string value}
dex2oat : a string specifying the intended runtime loading context that is stored
dex2oat : in the oat file. Overrides --class-loader-context. Note that this ignores
dex2oat : the classpath_dir arg.
dex2oat :
dex2oat : It describes how the class loader chain should be built in order to ensure
dex2oat : classes are resolved during dex2aot as they would be resolved at runtime.
dex2oat : This spec will be encoded in the oat file. If at runtime the dex file is
dex2oat : loaded in a different context, the oat file will be rejected.
dex2oat :
dex2oat : The chain is interpreted in the natural 'parent order', meaning that class
dex2oat : loader 'i+1' will be the parent of class loader 'i'.
dex2oat : The compilation sources will be appended to the classpath of the first class
dex2oat : loader.
dex2oat :
dex2oat : E.g. if the context is 'PCL[lib1.dex];DLC[lib2.dex]' and
dex2oat : --dex-file=src.dex then dex2oat will setup a PathClassLoader with classpath
dex2oat : 'lib1.dex:src.dex' and set its parent to a DelegateLastClassLoader with
dex2oat : classpath 'lib2.dex'.
dex2oat :
dex2oat : Note that the compiler will be tolerant if the source dex files specified
dex2oat : with --dex-file are found in the classpath. The source dex files will be
dex2oat : removed from any class loader's classpath possibly resulting in empty
dex2oat : class loaders.
dex2oat :
dex2oat : Example: --class-loader-context=PCL[lib1.dex:lib2.dex];DLC[lib3.dex]
dex2oat :
dex2oat : --compact-dex-level={none|fast}
dex2oat : This flag is obsolete and does nothing.
dex2oat :
dex2oat : --runtime-arg {dalvikvm-arg} [--runtime-arg {dalvikvm-arg}...]
dex2oat : used to specify various arguments for the runtime, such as initial heap
dex2oat : size, maximum heap size, and verbose output. Use a separate --runtime-arg
dex2oat : switch for each argument.
dex2oat : Example: --runtime-arg -Xms256m
dex2oat :
dex2oat : --compilation-reason={string value}
dex2oat : optional metadata specifying the reason for compiling the apk. If specified,
dex2oat : the string will be embedded verbatim in the key value store of the oat file.
dex2oat : Example: --compilation-reason=install
dex2oat :
dex2oat : --compile-individually
dex2oat : Compiles dex files individually, unloading classes in between compiling each file.
dex2oat :
dex2oat : --public-sdk={string value}
dex2oat :
dex2oat : --apex-versions={string value}
dex2oat : Versions of apexes in the boot classpath, separated by '/'
dex2oat :
dex2oat : --force-jit-zygote
dex2oat : Optimizes the app to be executed in an environment that uses JIT Zygote.
dex2oat :
dex2oat : --force-palette-compilation-hooks
dex2oat : Force PaletteNotify{Start,End}Dex2oatCompilation calls.
dex2oat :
dex2oat : --compiler-filter={assume-verified|verify|space{,-profile}|speed{,-profile}|everything{,-profile}}
dex2oat : Select compiler filter
dex2oat : Default: speed-profile if profile provided, speed otherwise
dex2oat :
dex2oat : --compile-art-test
dex2oat : --no-compile-art-test
dex2oat :
dex2oat : --huge-method-max={unsigned integer value}
dex2oat : threshold size for a huge method for compiler filter tuning.
dex2oat :
dex2oat : --large-method-max={unsigned integer value}
dex2oat : threshold size for a large method for compiler filter tuning.
dex2oat :
dex2oat : --num-dex-methods={unsigned integer value}
dex2oat : threshold size for a small dex file for compiler filter tuning. If the input
dex2oat : has fewer than this many methods and the filter is not interpret-only or
dex2oat : verify-none or verify-at-runtime, overrides the filter to use speed
dex2oat :
dex2oat : --inline-max-code-units={unsigned integer value}
dex2oat : the maximum code units that a methodcan have to be considered for inlining.
dex2oat : A zero value will disable inlining. Honored only by Optimizing. Has priority
dex2oat : over the --compiler-filter option. Intended for development/experimental use.
dex2oat :
dex2oat : --generate-debug-info
dex2oat : -g
dex2oat : --no-generate-debug-info
dex2oat : Generate (or don't generate) debug information for native debugging, such as
dex2oat : stack unwinding information, ELF symbols and dwarf sections. If used without
dex2oat : --debuggable it will be best effort only. Does not affect the generated
dex2oat : code. Disabled by default.
dex2oat :
dex2oat : --generate-mini-debug-info
dex2oat : --no-generate-mini-debug-info
dex2oat : Whether or not to generate minimal amount of LZMA-compressed debug
dex2oat : information necessary to print backtraces (disabled by default).
dex2oat :
dex2oat : --generate-build-id
dex2oat : --no-generate-build-id
dex2oat : Generate GNU-compatible linker build ID ELF section with SHA-1 of the file
dex2oat : content (and thus stable across identical builds)
dex2oat :
dex2oat : --deduplicate-code={false|true}
dex2oat : enable|disable code deduplication. Deduplicated code will have an arbitrary
dex2oat : symbol tagged with [DEDUPED].
dex2oat :
dex2oat : --count-hotness-in-compiled-code
dex2oat :
dex2oat : --check-profiled-methods={log|abort}
dex2oat :
dex2oat : --dump-timings
dex2oat : Display a breakdown of where time was spent.
dex2oat :
dex2oat : --dump-pass-timings
dex2oat : Display a breakdown time spent in optimization passes for each compiled method.
dex2oat :
dex2oat : --dump-stats
dex2oat : Display overall compilation statistics.
dex2oat :
dex2oat : --debuggable
dex2oat : Produce code debuggable with a java-debugger.
dex2oat :
dex2oat : --baseline
dex2oat : Produce code using the baseline compilation
dex2oat :
dex2oat : --top-k-profile-threshold={double value}
dex2oat :
dex2oat : --abort-on-hard-verifier-error
dex2oat : --no-abort-on-hard-verifier-error
dex2oat :
dex2oat : --abort-on-soft-verifier-error
dex2oat : --no-abort-on-soft-verifier-error
dex2oat :
dex2oat : --dump-init-failures={string value}
dex2oat :
dex2oat : --dump-cfg={string value}
dex2oat : Dump control-flow graphs (CFGs) to specified file.
dex2oat :
dex2oat : --dump-cfg-append
dex2oat : when dumping CFGs to an existing file, append new CFG data to existing data
dex2oat : (instead of overwriting existing data with new data, which is the default
dex2oat : behavior). This option is only meaningful when used with --dump-cfg.
dex2oat :
dex2oat : --register-allocation-strategy={string value}
dex2oat :
dex2oat : --resolve-startup-const-strings={false|true}
dex2oat : If true, the compiler eagerly resolves strings referenced from const-string
dex2oat : of startup methods.
dex2oat :
dex2oat : --initialize-app-image-classes={false|true}
dex2oat :
dex2oat : --verbose-methods={list separated by ','}
dex2oat : Restrict the dumped CFG data to methods whose name is listed.
dex2oat : Eg: --verbose-methods=toString,hashCode
dex2oat :
dex2oat : --max-image-block-size={unsigned integer value}
dex2oat : Maximum solid block size for compressed images.
dex2oat : The following arguments are ignored for compatibility:
dex2oat : --comments=_
dex2oat : --cache-info-fd=_