art apk像facebook、微信等apk安装失败,安装慢等问题

      

目前有些apk像facebook、微信等apk,本身应用较大且code复杂度高,往往会出现安装失败,安装慢等问题。

       安装失败是由于dex2oat进程编译时间过久达到了timeout,安装慢当然就是dex2oat做的compiler久的原因。

      还有像微信 这种在启动应用的时候是会优化插件的(而不是在安装的时候优化),这样就会导致应用lunch时间过久,给用户的感觉就是很晚才进入。

      说明:app 安装/ lunch时间的长短取决于CPU核心数,8核心的肯定是必4核心的优化要快.
 
 
     对这类apk添加white_list,即编译的时候让其做的简单些,即采用art 模式的interpret-only.
     此方法适用于 预置apk/ 正常安装的apk /自带jar包插件的apk...
 
wechat 示例:
//安装时打印此log, 从原本的14.559s减少安装时间到6.057s
01-01 08:34:11.374 I/dex2oat ( 7542): This apk is in whitelist from property so set interpret-only
01-01 08:34:11.374 I/dex2oat ( 7542): /system/bin/dex2oat --zip-fd=11 --zip-location=/data/app/com.tencent.mm-1/base.apk --oat-fd=12 --oat-location=/data/dalvik-cache/arm/data@app@com.tencent.mm-1@base.apk@classes.dex --instruction-set=arm --instruction-set-features=default --runtime-arg -Xms64m --runtime-arg -Xmx512m --swap-fd=13
01-01 08:34:17.432 I/dex2oat ( 7542): dex2oat took 6.057s (threads: 4) arena alloc=0B java alloc=21MB native alloc=1540KB free=10MB
//lunch时打印此log, 优化插件secondary-1.dex.jar
01-01 08:34:26.990 I/dex2oat ( 7709): This jar is in whitelist from property so set interpret-only
01-01 08:34:26.991 I/dex2oat ( 7709): /system/bin/dex2oat --runtime-arg -classpath --runtime-arg --instruction-set=arm --instruction-set-features=default --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --dex-file=/data/data/com.tencent.mm/app_dex/secondary-1.dex.jar --oat-fd=62 --oat-location=/data/data/com.tencent.mm/app_cache/secondary-1.dex.dex --runtime-arg -Xms64m --runtime-arg -Xmx512m
01-01 08:34:27.024 I/dex2oat ( 7709): dex2oat took 32.972ms (threads: 4) arena alloc=0B java alloc=1648B native alloc=294KB free=7MB
//lunch时打印此log, 优化插件secondary-2.dex.jar从原本12.570s减少到4.617s
01-01 08:34:27.063 I/dex2oat ( 7714): This jar is in whitelist from property so set interpret-only
01-01 08:34:27.063 I/dex2oat ( 7714): /system/bin/dex2oat --runtime-arg -classpath --runtime-arg --instruction-set=arm --instruction-set-features=default --runtime-arg -Xrelocate --boot-image=/system/framework/boot.art --dex-file=/data/data/com.tencent.mm/app_dex/secondary-2.dex.jar --oat-fd=62 --oat-location=/data/data/com.tencent.mm/app_cache/secondary-2.dex.dex --runtime-arg -Xms64m --runtime-arg -Xmx512m
01-01 08:34:31.681 I/dex2oat ( 7714): dex2oat took 4.617s (threads: 4) arena alloc=0B java alloc=42MB native alloc=1308KB free=10MB
 
      
[SOLUTION M版本]
/art/dex2oat/dex2oat.cc


方法1:
// add begin


if ((!oat_filename_.empty() && (oat_filename_.find("facebook") != std::string::npos))||(!zip_location_.empty() && (zip_location_.find("facebook") !=std::string::npos))||((!oat_location_.empty()) && oat_location_.find("facebook"))){  // mtk modify 2016-06-06
                                    compiler_filter_string = "interpret-only";
                                    LOG(INFO) <<" This apk is in whitelist from property so set interpret-only ";
            }
//  add end


if (compiler_filter_string = nullptr){


      compiler_filter_string = "speed";


}


方法2:
      if (num_methods <= compiler_options_->GetNumDexMethodsThreshold()) {
        compiler_options_->SetCompilerFilter(CompilerOptions::kSpeed);
        VLOG(compiler) << "Below method threshold, compiling anyways";
     }
    }

//add_begin
    static constexpr size_t kMinCompileDexSize = 4;
    if (!image_ && dex_files_.size() > kMinCompileDexSize) {
      compiler_options_->SetCompilerFilter(CompilerOptions::kInterpretOnly);
      LOG(INFO) << "Enable Whitelist Rules. Current Dex File Sizes:" << dex_files_.size();
   }
//add_end

    return true;
  }

  // Create and invoke the compiler driver. This will compile all the dex files.
  void Compile() {
    TimingLogger::ScopedTiming t("dex2oat Compile", timings_);
    compiler_phases_timings_.reset(new CumulativeLogger("compilation times"))


art集中模式verify-none、interpret-only、verify-at-runtime、space、balance、speed、everything、time

如果persist.sys.dalvik.vm.lib.2 是libart开始则为art模式,使用/system/bin/dex2oat

否则时dalvik模式,使用/system/bin/dexopt 



interpret-only模式相关源码
在文件build/core/main.mk中,设置dex2oat模式: 
   ADDITIONAL_BUILD_PROPERTIES += dalvik.vm.dex2oat-filter=interpret-only
编译android源码后这个编译属性会记录在system/build.prop文件中。
在文件frameworks/native/cmds/installd/commands.c中,函数dexopt(...)调用run_dex2oat(...),使用property_get()从文件build.prop中获取这个属性:
  bool have_dex2oat_compiler_filter_flag = property_get("dalvik.vm.dex2oat-filter",
                                                          dex2oat_compiler_filter_flag, NULL) > 0;
...
   else if (have_dex2oat_compiler_filter_flag) {
       sprintf(dex2oat_compiler_filter_arg, "--compiler-filter=%s", dex2oat_compiler_filter_flag);
   }
...
    if (have_dex2oat_compiler_filter_flag) {
        argv[i++] = dex2oat_compiler_filter_arg;
    }
...
然后调用execv(DEX2OAT_BIN,(char* const*)argv) , 执行 system/bin/dex2oat,幷将这个属性作为参数传递给dex2oat . 


在文件 art/dex2oat/dex2oat.cc 中函数 ParseArgs(...) 内,在解析参数argv时将变量 compiler_filter_string 设置为interpret-only ,继而变量compiler_filter(初值为kDefaultCompilerFilter,即Speed)被设置为 kInterpretOnly,从而编译器被设置为 interpret-only 模式。
instruction_set_被设置为Mips。当instruction_set_ 为 kMips32r6 和 kMips64 时只有interpret-only 模式可以工作。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值