Android Art里的Jit&AOT

简单介绍一下Art里的jit和aot.
本文分成三个部分:

  1. JIT Introduction
  2. AOT Introduction
  3. Relation between JIT&AOT

JIT Introduction

JIT这里没有看太多的内容,先占个位:D

Profile文件

Art会在app执行过程中对它做profiling, 记录hot method等优化信息, profiling的结果会存在storage当中, 以便下次执行的时候供art参考.

profile的文件格式如下:

324 /**
325 * Serialization format:
326 * [profile_header, zipped[[profile_line_header1, profile_line_header2...],[profile_line_data1,
327 *    profile_line_data2...]]]
328 * profile_header:
329 *   magic,version,number_of_dex_files,uncompressed_size_of_zipped_data,compressed_data_size
330 * profile_line_header:
331 *   dex_location,number_of_classes,methods_region_size,dex_location_checksum,num_method_ids
332 * profile_line_data:
333 *   method_encoding_1,method_encoding_2...,class_id1,class_id2...,startup/post startup bitmap
334 * The method_encoding is:
335 *    method_id,number_of_inline_caches,inline_cache1,inline_cache2...
336 * The inline_cache is:
337 *    dex_pc,[M|dex_map_size], dex_profile_index,class_id1,class_id2...,dex_profile_index2,...
338 *    dex_map_size is the number of dex_indeces that follows.
339 *       Classes are grouped per their dex files and the line
340 *       `dex_profile_index,class_id1,class_id2...,dex_profile_index2,...` encodes the
341 *       mapping from `dex_profile_index` to the set of classes `class_id1,class_id2...`
342 *    M stands for megamorphic or missing types and it's encoded as either
343 *    the byte kIsMegamorphicEncoding or kIsMissingTypesEncoding.
344 *    When present, there will be no class ids following.
345 **/

在art目录下面, google还提供了profman来作为profile相关操作的接口.
使用profman可以轻松地将profile打印成人类友好模式.

Example:

1. adb shell profman --generate-test-profile=/data/testprofile --generate-test-profile-seed=0 --apk=/system/framework/am.jar --dex-location=/system/framework/am.jar 
2. adb shell profman --dump-only --profile-file=/data/testprofile 

以下内容来自am.jar的profile, just an example:

=== Dex files  ===
=== profile ===
ProfileInfo:
am.jar [index=0] [checksum=ccdf4a91]
        hot methods: 40[], 50[], 64[], 89[], 91[], 99[], 135[], 144[],
        startup methods: 40, 50, 64, 144,
        post startup methods: 89, 91, 99, 135,
        classes:

根据profile文件的优化信息, art会在下面这个函数里面去判断是否要将method编译成native code.

986 bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
987  // Profile compilation info may be null if no profile is passed.
988  if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) {
989    // Use the compiler filter instead of the presence of profile_compilation_info_ since
990    // we may want to have full speed compilation along with profile based layout optimizations.
991    return true;
992  }
993  // If we are using a profile filter but do not have a profile compilation info, compile nothing.
994  if (profile_compilation_info_ == nullptr) {
995    return false;
996  }
997  // Compile only hot methods, it is the profile saver's job to decide what startup methods to mark
998  // as hot.
999  bool result = profile_compilation_info_->GetMethodHotness(method_ref).IsHot();
1000
1001  if (kDebugProfileGuidedCompilation) {
1002    LOG(INFO) << "[ProfileGuidedCompilation] "
1003        << (result ? "Compiled" : "Skipped") << " method:" << method_ref.PrettyMethod(true);
1004  }
1005  return result;
1006}

AOT Introduction

AOT的编译策略

dex2oat有个选项叫--compiler-filter, 它的参数如下.

32  enum Filter {
33    kAssumeVerified,      // Skip verification but mark all classes as verified anyway.
34    kExtract,             // Delay verication to runtime, do not compile anything.
35    kVerify,              // Only verify classes.
36    kQuicken,             // Verify, quicken, and compile JNI stubs.
37    kSpaceProfile,        // Maximize space savings based on profile.
38    kSpace,               // Maximize space savings.
39    kSpeedProfile,        // Maximize runtime performance based on profile.
40    kSpeed,               // Maximize runtime performance.
41    kEverythingProfile,   // Compile everything capable of being compiled based on profile.
42    kEverything,          // Compile everything capable of being compiled.
                            // 注意kEverything这个参数
43  };

相关逻辑在这里

986 bool CompilerDriver::ShouldCompileBasedOnProfile(const MethodReference& method_ref) const {
987  // Profile compilation info may be null if no profile is passed.
    // 当选择了everthing之后, DependsOnProfile返回false,所以ShouldCompileBasedOnProfile返回true.
988  if (!CompilerFilter::DependsOnProfile(compiler_options_->GetCompilerFilter())) { 
989    // Use the compiler filter instead of the presence of profile_compilation_info_ since
990    // we may want to have full speed compilation along with profile based layout optimizations.
991    return true;
992  }
993  // If we are using a profile filter but do not have a profile compilation info, compile nothing.
994  if (profile_compilation_info_ == nullptr) {
995    return false;
996  }
997  // Compile only hot methods, it is the profile saver's job to decide what startup methods to mark
998  // as hot.
     // 假设选了kSpeedProfile, 会走到这里, 然后会去查询profile, 如果是hot的话, 才会返回true
999  bool result = profile_compilation_info_->GetMethodHotness(method_ref).IsHot(); 
1000
1001  if (kDebugProfileGuidedCompilation) {
1002    LOG(INFO) << "[ProfileGuidedCompilation] "
1003        << (result ? "Compiled" : "Skipped") << " method:" << method_ref.PrettyMethod(true);
1004  }
1005  return result;
1006 }

如果想要在安装apk的时候,将app完全编译成native, 可以这么做:

//在install apk的时候就会默认全部aot
setprop pm.dexopt.install everything

或者重编译指定的apk:

// everything是编译所有能编译的code
pm compile -m everything -f com.kiloo.subwaysurf
// speed-profile会根据profile文件来编译
pm compile -m speed-profile -f com.kiloo.subwaysurf

我尝试编译了一个apk. 下面是编译后的文件大小.

speed-profile (default mode) everything
base.art 32K 0
base.odex 224K 21M
base.vdex 8M 7.4M

base.odex是一个elf, aot之后的native code就存在里面.
因为是首次安装, profile信息几乎没有, 所以speed-profile模式下的base.odex很小.

PS: profile的信息都保存在/data/misc/profiles, 查看profile的内容可用以下命令
profman --profile-file=./primary.prof --dump-only

everything模式的缺点就是(1) install的时候慢一些 (2) 占用空间大一些

dex2oat的参数解析

--instruction-set-variant= (default | atom | sandybridge | silvermont)
--instruction-set-features = (default | sse3 | sse4.1 | avx…)
这两个参数共同决定了编译时候的cpu feature.

instruction-set-variant和instruction-set-features的值是由system property来的:

  • dalvik.vm.isa.x86.variant/ dalvik.vm.isa.x86_64.variant
  • dalvik.vm.isa.x86.features/ dalvik.vm.isa.x86_64.features

--huge-method-max/--large-method-max/--small-method-max/--tiny-method-max
定义包含多少条指令的method是huge/large/small/tiny method. 编译器对于它们有不同的处理方法.

  1. compiler filter!= everything && huge, 会跳过编译步骤.
  2. compiler filter!= everything && large && no branch, 会跳过编译步骤.
76 bool HGraphBuilder::SkipCompilation(size_t number_of_branches) {
  1. small/tiny method在P的code里没有任何特殊处理.

--inline-max-code-units

定义一个函数最多包含多少code unit, 可以被优化成inline函数.
这个值等于0的话, 就不进行inline优化.

--compiler-backend=(Quick|Optimizing)

这个选项其实没什么用, 就算选quick也会跳转到optimizing.

31    case kQuick:
32      // TODO: Remove Quick in options.
33    case kOptimizing:
34      return CreateOptimizingCompiler(driver);
.art/.odex/.vdex的具体含义

.vdex: verified dex,其中包含 APK 的 DEX 代码,另外还有一些旨在加快验证速度的元数据.
验证主要指的是, 当系统OTA等原因, 会需要重新验证dex的合法性.
而vdex存放的是已经验证过的dex. 如果vdex存在, 就可以跳过验证这个步骤.

.odex: elf, native code存放的位置

.art: .art就是art虚拟机内部数据的一个snapshot.
因为有一些数据是通用的, 是每次vm起来都会需要的, 所以可以将它们提前存储起来.
而vm起来的时候通过mmap .art文件来达到加速目的.

Relation between JIT&AOT

在jit运行的时候, 它会收集hot method/class的数据, 然后把profile持久化到disk上面. 在android空闲的时候, BackgroundDexOptService会根据这个profile信息, 把apk重新aot.


jit-arch.png
jit-profile-comp.png

BackgroundDexOptService的调用大致是:
runIdleOptimization(BackgroundDexOptService.java)
-> performDexOptWithStatus(PackageManagerService.java)
-> dexopt(InstalldNativeService.cpp)
-> run_dex2oat(dexopt.cpp) //这里调用了dex2oat这个binary去做aot

BackgroundDexOptService会在特定条件下对apk进行aot.
它被调用的条件需要同时满足以下三条:

  • setRequiresDeviceIdle(true) // 设备处于idle状态, idle的定义是: 息屏71mins or 睡眠状态71mins or无线充电71mins
  • setRequiresCharging(true) // 设备处于充电状态
  • setPeriodic(IDLE_OPTIMIZATION_PERIOD) // 一天仅触发一次

BackgroundDexOptService的触发条件:

123        // Schedule a daily job which scans installed packages and compiles
124        // those with fresh profiling data.
125        js.schedule(new JobInfo.Builder(JOB_IDLE_OPTIMIZE, sDexoptServiceName)
126                    .setRequiresDeviceIdle(true)
127                    .setRequiresCharging(true)
128                    .setPeriodic(IDLE_OPTIMIZATION_PERIOD)
129                    .build());

关于Idle的定义:

44    // Policy: we decide that we're "idle" if the device has been unused /
45    // screen off or dreaming or wireless charging dock idle for at least this long
46    private long mInactivityIdleThreshold; // 这个值等于71mins
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,185评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,652评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,524评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,339评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,387评论 6 391
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,287评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,130评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,985评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,420评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,617评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,779评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,477评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,088评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,716评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,857评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,876评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,700评论 2 354