| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.app; |
| 18 |
|
| 19 |
import android.os.FileUtils; |
| 20 |
import android.os.RemoteException; |
| 21 |
import android.os.SystemProperties; |
| 22 |
import android.util.Slog; |
| 23 |
|
| 24 |
import com.android.internal.annotations.GuardedBy; |
| 25 |
|
| 26 |
import dalvik.system.BaseDexClassLoader; |
| 27 |
import dalvik.system.VMRuntime; |
| 28 |
|
| 29 |
import java.io.File; |
| 30 |
import java.io.IOException; |
| 31 |
import java.util.HashSet; |
| 32 |
import java.util.Map; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class DexLoadReporter implements BaseDexClassLoader.Reporter { |
| 46 |
private static final String TAG = "DexLoadReporter"; |
| 47 |
|
| 48 |
private static final DexLoadReporter INSTANCE = new DexLoadReporter(); |
| 49 |
|
| 50 |
private static final boolean DEBUG = false; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
@GuardedBy("mDataDirs") |
| 57 |
private final Set<String> mDataDirs; |
| 58 |
|
| 59 |
private DexLoadReporter() { |
| 60 |
mDataDirs = new HashSet<>(); |
| 61 |
} |
| 62 |
|
| 63 |
static DexLoadReporter getInstance() { |
| 64 |
return INSTANCE; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
void registerAppDataDir(String packageName, String dataDir) { |
| 76 |
if (DEBUG) { |
| 77 |
Slog.i(TAG, "Package " + packageName + " registering data dir: " + dataDir); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
if (dataDir != null) { |
| 82 |
synchronized (mDataDirs) { |
| 83 |
mDataDirs.add(dataDir); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void report(Map<String, String> classLoaderContextMap) { |
| 90 |
if (classLoaderContextMap.isEmpty()) { |
| 91 |
Slog.wtf(TAG, "Bad call to DexLoadReporter: empty classLoaderContextMap"); |
| 92 |
return; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
notifyPackageManager(classLoaderContextMap); |
| 98 |
|
| 99 |
|
| 100 |
registerSecondaryDexForProfiling(classLoaderContextMap.keySet()); |
| 101 |
} |
| 102 |
|
| 103 |
private void notifyPackageManager(Map<String, String> classLoaderContextMap) { |
| 104 |
|
| 105 |
String packageName = ActivityThread.currentPackageName(); |
| 106 |
try { |
| 107 |
ActivityThread.getPackageManager().notifyDexLoad(packageName, |
| 108 |
classLoaderContextMap, VMRuntime.getRuntime().vmInstructionSet()); |
| 109 |
} catch (RemoteException re) { |
| 110 |
Slog.e(TAG, "Failed to notify PM about dex load for package " + packageName, re); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
private void registerSecondaryDexForProfiling(Set<String> dexPaths) { |
| 115 |
if (!SystemProperties.getBoolean("dalvik.vm.dexopt.secondary", false)) { |
| 116 |
return; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
String[] dataDirs; |
| 122 |
synchronized (mDataDirs) { |
| 123 |
dataDirs = mDataDirs.toArray(new String[0]); |
| 124 |
} |
| 125 |
for (String dexPath : dexPaths) { |
| 126 |
registerSecondaryDexForProfiling(dexPath, dataDirs); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
private void registerSecondaryDexForProfiling(String dexPath, String[] dataDirs) { |
| 131 |
if (!isSecondaryDexFile(dexPath, dataDirs)) { |
| 132 |
|
| 133 |
return; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
File dexPathFile = new File(dexPath); |
| 140 |
File secondaryProfileDir = new File(dexPathFile.getParent(), "oat"); |
| 141 |
File secondaryCurProfile = |
| 142 |
new File(secondaryProfileDir, dexPathFile.getName() + ".cur.prof"); |
| 143 |
File secondaryRefProfile = new File(secondaryProfileDir, dexPathFile.getName() + ".prof"); |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
if (!secondaryProfileDir.exists()) { |
| 149 |
if (!secondaryProfileDir.mkdir()) { |
| 150 |
Slog.e(TAG, "Could not create the profile directory: " + secondaryCurProfile); |
| 151 |
|
| 152 |
return; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
try { |
| 157 |
boolean created = secondaryCurProfile.createNewFile(); |
| 158 |
if (DEBUG && created) { |
| 159 |
Slog.i(TAG, "Created profile for secondary dex: " + secondaryCurProfile); |
| 160 |
} |
| 161 |
} catch (IOException ex) { |
| 162 |
Slog.e(TAG, "Failed to create profile for secondary dex " + dexPath |
| 163 |
+ ":" + ex.getMessage()); |
| 164 |
|
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
VMRuntime.registerAppInfo( |
| 171 |
ActivityThread.currentPackageName(), |
| 172 |
secondaryCurProfile.getPath(), |
| 173 |
secondaryRefProfile.getPath(), |
| 174 |
new String[] { dexPath }, |
| 175 |
VMRuntime.CODE_PATH_TYPE_SECONDARY_DEX); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private boolean isSecondaryDexFile(String dexPath, String[] dataDirs) { |
| 181 |
for (String dataDir : dataDirs) { |
| 182 |
if (FileUtils.contains(dataDir, dexPath)) { |
| 183 |
return true; |
| 184 |
} |
| 185 |
} |
| 186 |
return false; |
| 187 |
} |
| 188 |
} |