Android 6.0 PackageManagerService dex 优化源码分析

一:dex 相关文件生成流程

1. PKMS.scanPackageDirtyLI

PKMS 中,进行dex 相关处理的流程入口是在PKMS.scanPackageDirtyLI() 函数中:

private PackageParser.Package scanPackageDirtyLI(PackageParser.Package pkg, int parseFlags,
            int scanFlags, long currentTime, UserHandle user) throws PackageManagerException {
			
        ... ...
        final boolean forceDex = (scanFlags & SCAN_FORCE_DEX) != 0;		
        ... ...
        if ((scanFlags & SCAN_NO_DEX) == 0) {
            int result = mPackageDexOptimizer.performDexOpt(pkg, null /* instruction sets */,
                    forceDex, (scanFlags & SCAN_DEFER_DEX) != 0, false /* inclDependencies */,
                    (scanFlags & SCAN_BOOTING) == 0);
            if (result == PackageDexOptimizer.DEX_OPT_FAILED) {
                throw new PackageManagerException(INSTALL_FAILED_DEXOPT, "scanPackageLI");
            }
        }
        ... ...		
			
}

调用了PackageDexOptimizer.performDexOpt函数,这个函数又继续调用了performDexOptLI函数:

1.1 performDexOptLI

    private int performDexOptLI(PackageParser.Package pkg, String[] targetInstructionSets,
            boolean forceDex, boolean defer, boolean bootComplete, ArraySet<String> done) {

... ...

        if (done != null) {
            done.add(pkg.packageName);
            if (pkg.usesLibraries != null) {
                performDexOptLibsLI(pkg.usesLibraries, instructionSets, forceDex, defer,
                        bootComplete, done);
            }
            if (pkg.usesOptionalLibraries != null) {
                performDexOptLibsLI(pkg.usesOptionalLibraries, instructionSets, forceDex, defer,
                        bootComplete, done);
            }
        }

... ...
}

  performDexOptLI 先处理 共享库dex 相关问题:调用performDexOptLibsLI 检查对应的共享库是否已经dex:

  1.1.1 performDexOptLibsLI

    private void performDexOptLibsLI(ArrayList<String> libs, String[] instructionSets,
            boolean forceDex, boolean defer, boolean bootComplete, ArraySet<String> done) {
        for (String libName : libs) {
            PackageParser.Package libPkg = mPackageManagerService.findSharedNonSystemLibrary(
                    libName);
            if (libPkg != null && !done.contains(libName)) {
                performDexOptLI(libPkg, instructionSets, forceDex, defer, bootComplete, done);
            }
        }
    }

   遍历需要dex 的库组合libs中每个库对应的pkg是否已经dex,没有则先调用 performDexOptLI 来dex 之前需要dex的lib所对应的pkg。

1.2 performDexOptLI (2)

... ...
        final List<String> paths = pkg.getAllCodePathsExcludingResourceOnly();
        boolean performedDexOpt = false;
        // There are three basic cases here:
        // 1.) we need to dexopt, either because we are forced or it is needed
        // 2.) we are deferring a needed dexopt
        // 3.) we are skipping an unneeded dexopt
        final String[] dexCodeInstructionSets = getDexCodeInstructionSets(instructionSets);
        for (String dexCodeInstructionSet : dexCodeInstructionSets) {
            if (!forceDex && pkg.mDexOptPerformed.contains(dexCodeInstructionSet)) {//没有强制或者已经dex优化过直接continue
                continue;
            }
             //(1): 遍历此pkg所以普resource相关路径
            for (String path : paths) {
                final int dexoptNeeded;
                if (forceDex) {
                    dexoptNeeded = DexFile.DEX2OAT_NEEDED;
                } else {
                    try {
                        dexoptNeeded = DexFile.getDexOptNeeded(path, pkg.packageName,
                                dexCodeInstructionSet, defer);
                    } catch (IOException ioe) {
                        Slog.w(TAG, "IOException reading apk: " + path, ioe);
                        return DEX_OPT_FAILED;
                    }
               }

                if (!forceDex && defer && dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
                    // We're deciding to defer a needed dexopt. Don't bother dexopting for other
                    // paths and instruction sets. We'll deal with them all together when we process
                    // our list of deferred dexopts.
                    addPackageForDeferredDexopt(pkg);
                    return DEX_OPT_DEFERRED;
                }
 
                if (dexoptNeeded != DexFile.NO_DEXOPT_NEEDED) {
                    final String dexoptType;
                    String oatDir = null;
                    if (dexoptNeeded == DexFile.DEX2OAT_NEEDED) {
                        dexoptType = "dex2oat";//dex-opt类型
                        try {
                            oatDir = createOatDirIfSupported(pkg, dexCodeInstructionSet);//获取otaDir
                        } catch (IOException ioe) {
                            Slog.w(TAG, "Unable to create oatDir for package: " + pkg.packageName);
                            return DEX_OPT_FAILED;
                        }
                    } else if (dexoptNeeded == DexFile.PATCHOAT_NEEDED) {
                        dexoptType = "patchoat";
                    } else if (dexoptNeeded == DexFile.SELF_PATCHOAT_NEEDED) {
                        dexoptType = "self patchoat";
                    } else {
                        throw new IllegalStateException("Invalid dexopt needed: " + dexoptNeeded);
                    }
 
                    Log.i(TAG, "Running dexopt (" + dexoptType + ") on: " + path + " pkg="//关键信息打印
                            + pkg.applicationInfo.packageName + " isa=" + dexCodeInstructionSet
                            + " vmSafeMode=" + vmSafeMode + " debuggable=" + debuggable
                            + " oatDir = " + oatDir + " bootComplete=" + bootComplete);
                    final int sharedGid = UserHandle.getSharedAppGid(pkg.applicationInfo.uid);
                    final int ret = mPackageManagerService.mInstaller.dexopt(path, sharedGid,//调用installd的dexopt
                            !pkg.isForwardLocked(), pkg.packageName, dexCodeInstructionSet,
                            dexoptNeeded, vmSafeMode, debuggable, oatDir, bootComplete);
 
                    // Dex2oat might fail due to compiler / verifier errors. We soldier on
                    // regardless, and attempt to interpret the app as a safety net.
                    if (ret == 0) {//Installd dexopt成功了
                        performedDexOpt = true;
                    }
                }
            }
 
            // At this point we haven't failed dexopt and we haven't deferred dexopt. We must
            // either have either succeeded dexopt, or have had getDexOptNeeded tell us
            // it isn't required. We therefore mark that this package doesn't need dexopt unless
            // it's forced. performedDexOpt will tell us whether we performed dex-opt or skipped
            // it.
            pkg.mDexOptPerformed.add(dexCodeInstructionSet);//这代表已经处理过了
        }
 
        // If we've gotten here, we're sure that no error occurred and that we haven't
        // deferred dex-opt. We've either dex-opted one more paths or instruction sets or
        // we've skipped all of them because they are up to date. In both cases this
        // package doesn't need dexopt any longer.
        return performedDexOpt ? DEX_OPT_PERFORMED : DEX_OPT_SKIPPED;

遍历apk所有的代码路径,根据解析得到dexoptType,最后用installd来完成dexopt工作,其中:

dexoptType = dex2oat 时 ,调用createOatDirIfSupported 获取oatdir ,其他dexoptType 则oatdir = null :

    private String createOatDirIfSupported(PackageParser.Package pkg, String dexInstructionSet)
            throws IOException {
        if (!pkg.canHaveOatDir()) {
            return null;
        }
        File codePath = new File(pkg.codePath);
        if (codePath.isDirectory()) {
            File oatDir = getOatDir(codePath);
            mPackageManagerService.mInstaller.createOatDir(oatDir.getAbsolutePath(),
                    dexInstructionSet);
            return oatDir.getAbsolutePath();
        }
        return null;
    }

createOatDirIfSupported函数的逻辑是 :

1 .codePath如果是目录,就用Installd在该目录下创建一个目录,如果是apk文件直接返回空。

2.调用pkg.canHaveOatDir 来判断,pkg 是一个NOT updated 的system app。

3.Installd.dexopt

int dexopt(const char *apk_path, uid_t uid, bool is_public,
           const char *pkgname, const char *instruction_set, int dexopt_needed,
           bool vm_safe_mode, bool debuggable, const char* oat_dir, bool boot_complete)
{
    ......
    // Early best-effort check whether we can fit the the path into our buffers.
    // Note: the cache path will require an additional 5 bytes for ".swap", but we'll try to run
    // without a swap file, if necessary.
    if (strlen(apk_path) >= (PKG_PATH_MAX - 8)) {
        ALOGE("apk_path too long '%s'\n", apk_path);
        return -1;
    }
 
    if (oat_dir != NULL && oat_dir[0] != '!') {
        if (validate_apk_path(oat_dir)) {
            ALOGE("invalid oat_dir '%s'\n", oat_dir);
            return -1;
        }
        if (calculate_oat_file_path(out_path, oat_dir, apk_path, instruction_set)) {
            return -1;
        }
    } else {
        if (create_cache_path(out_path, apk_path, instruction_set)) {
            return -1;
        }
    }

 1.oat_dir为空则调用create_cache_path函数来计算out_path

 2.oat_dir不为空且有效则会根据这个oat_dir调用calculate_oat_file_path计算这个out_path

3.1 calculate_oat_file_path

int calculate_oat_file_path(char path[PKG_PATH_MAX], const char *oat_dir, const char *apk_path,
        const char *instruction_set) {
    char *file_name_start;
    char *file_name_end;
 
    file_name_start = strrchr(apk_path, '/');
    if (file_name_start == NULL) {
         ALOGE("apk_path '%s' has no '/'s in it\n", apk_path);
        return -1;
    }
    file_name_end = strrchr(apk_path, '.');
    if (file_name_end < file_name_start) {
        ALOGE("apk_path '%s' has no extension\n", apk_path);
        return -1;
    }
 
    // Calculate file_name
    int file_name_len = file_name_end - file_name_start - 1;
    char file_name[file_name_len + 1];
    memcpy(file_name, file_name_start + 1, file_name_len);
    file_name[file_name_len] = '\0';
 
    // <apk_parent_dir>/oat/<isa>/<file_name>.odex
    snprintf(path, PKG_PATH_MAX, "%s/%s/%s.odex", oat_dir, instruction_set, file_name);
    return 0;
}

  最终生成/sytstem/app/XXXX/arm/oat/XXXX.odex

3.2 create_cache_path

int create_cache_path(char path[PKG_PATH_MAX], const char *src, const char *instruction_set)
{
    char *tmp;
    int srclen;
    int dstlen;
 
    srclen = strlen(src);
 
        /* demand that we are an absolute path */
    if ((src == 0) || (src[0] != '/') || strstr(src,"..")) {
        return -1;
    }
 
    if (srclen > PKG_PATH_MAX) {        // XXX: PKG_NAME_MAX?
        return -1;
    }
 
    dstlen = srclen + strlen(DALVIK_CACHE_PREFIX) +
        strlen(instruction_set) +
        strlen(DALVIK_CACHE_POSTFIX) + 2;
 
    if (dstlen > PKG_PATH_MAX) {
        return -1;
    }
 
    sprintf(path,"%s%s/%s%s",
            DALVIK_CACHE_PREFIX,
            instruction_set,
            src + 1, /* skip the leading / */
            DALVIK_CACHE_POSTFIX);
 
    for(tmp = path + strlen(DALVIK_CACHE_PREFIX) + strlen(instruction_set) + 1; *tmp; tmp++) {
        if (*tmp == '/') {
            *tmp = '@';
        }
    }
 
    return 0;
}

其中,DALVIK_CACHE_PREFIX = “/data/dalvik-cache/” ,,最后会在DALVIK_CACHE_PREFIX目录下创建,也就是最终会在这个目录下生成dex文件。

继续分析dexopt函数,根据dexopt类型来看源文件,一般是apk文件:

    switch (dexopt_needed) {
        case DEXOPT_DEX2OAT_NEEDED:
            input_file = apk_path;
            break;
 
        case DEXOPT_PATCHOAT_NEEDED:
            if (!calculate_odex_file_path(in_odex_path, apk_path, instruction_set)) {
                return -1;
            }
            input_file = in_odex_path;
            break;
 
        case DEXOPT_SELF_PATCHOAT_NEEDED:
            input_file = out_path;
            break;
 
        default:
            ALOGE("Invalid dexopt needed: %d\n", dexopt_needed);
            exit(72);
    }

二.优化:

1.预编译提取Odex:

     在BoardConfig.mk中定义:WITH_DEXPREOPT := true。打开这个宏之后,无论是有源码还是无源码的预置apk预编译时都会提取odex文件。

    优点:

    开启预编译后,生成的system.img 中APP 文件夹中包含已经odex 的文件,系统第一次开机时将不必进行Odex:

    
    缺点:   

    1.打开WITH_DEXPREOPT 宏之后,会导致system.img 的size 变大,需要对应的调整partition.xml  才能正常烧录;

   1.1 预编译时跳过一些apk的odex提取

     Android.mk 中 “LOCAL_DEX_PREOPT = false ”则不进行预优化 ;

                            “LOCAL_DEX_PREOPT = true ” 则进行预优化

猜你喜欢

转载自blog.csdn.net/pirionFordring/article/details/83783595