Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit cd8fbd25 authored by Calin Juravle's avatar Calin Juravle
Browse files

Update DexLoadReporter to comply with the new reporting API

This is a partial cherry pick of commit
3bec94d7.

It is partial because it only adapts DexLoadReporter to use the new
reporter BaseDexClassLoader.Reporter API.

Bug: 38138251
Test: make
Merged-In: I2486522fb811f9fc58a44b92642f43a41e7d5bac

(cherry picked from commit 3bec94d7)

Change-Id: I4c41dbeb8a9297caac8b0eb936cf74832569f33e
parent 01d686b8
Loading
Loading
Loading
Loading
+28 −9
Original line number Diff line number Diff line
@@ -28,6 +28,8 @@ import dalvik.system.VMRuntime;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -86,29 +88,46 @@ import java.util.Set;
    }

    @Override
    public void report(List<String> dexPaths) {
        if (dexPaths.isEmpty()) {
    public void report(List<BaseDexClassLoader> classLoadersChain, List<String> classPaths) {
        if (classLoadersChain.size() != classPaths.size()) {
            Slog.wtf(TAG, "Bad call to DexLoadReporter: argument size mismatch");
            return;
        }
        if (classPaths.isEmpty()) {
            Slog.wtf(TAG, "Bad call to DexLoadReporter: empty dex paths");
            return;
        }

        // The first element of classPaths is the list of dex files that should be registered.
        // The classpath is represented as a list of dex files separated by File.pathSeparator.
        String[] dexPathsForRegistration = classPaths.get(0).split(File.pathSeparator);
        if (dexPathsForRegistration.length == 0) {
            // No dex files to register.
            return;
        }

        // Notify the package manager about the dex loads unconditionally.
        // The load might be for either a primary or secondary dex file.
        notifyPackageManager(dexPaths);
        // Check for secondary dex files and register them for profiling if
        // possible.
        registerSecondaryDexForProfiling(dexPaths);
        notifyPackageManager(classLoadersChain, classPaths);
        // Check for secondary dex files and register them for profiling if possible.
        // Note that we only register the dex paths belonging to the first class loader.
        registerSecondaryDexForProfiling(dexPathsForRegistration);
    }

    private void notifyPackageManager(List<String> dexPaths) {
    private void notifyPackageManager(List<BaseDexClassLoader> ignored,
            List<String> classPaths) {
        String packageName = ActivityThread.currentPackageName();
        try {
            // Notify only the paths of the first class loader for now.
            ActivityThread.getPackageManager().notifyDexLoad(
                    packageName, dexPaths, VMRuntime.getRuntime().vmInstructionSet());
                    packageName, Arrays.asList(classPaths.get(0).split(File.pathSeparator)),
                    VMRuntime.getRuntime().vmInstructionSet());
        } catch (RemoteException re) {
            Slog.e(TAG, "Failed to notify PM about dex load for package " + packageName, re);
        }
    }

    private void registerSecondaryDexForProfiling(List<String> dexPaths) {
    private void registerSecondaryDexForProfiling(String[] dexPaths) {
        if (!SystemProperties.getBoolean("dalvik.vm.dexopt.secondary", false)) {
            return;
        }