blob: b067bc278b1a838ab144a66153f4a7979e3279c0 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.io.BufferedWriter;
18import java.io.FileOutputStream;
19import java.io.IOException;
20import java.io.OutputStreamWriter;
21import java.io.Writer;
22import java.nio.charset.Charset;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import java.util.Set;
24import java.util.TreeSet;
25
26/**
Bob Lee9d2d6e12009-08-13 14:41:54 -070027 * Writes /frameworks/base/preloaded-classes. Also updates
28 * {@link LoadedClass#preloaded} fields and writes over compiled log file.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029 */
30public class WritePreloadedClassFile {
31
Bob Lee9d2d6e12009-08-13 14:41:54 -070032 /**
33 * Preload any class that take longer to load than MIN_LOAD_TIME_MICROS us.
34 */
35 static final int MIN_LOAD_TIME_MICROS = 1250;
36
Jesse Wilsoneaca10e2010-10-12 21:35:56 -070037 /**
38 * Preload any class that was loaded by at least MIN_PROCESSES processes.
39 */
40 static final int MIN_PROCESSES = 10;
41
Bob Lee9d2d6e12009-08-13 14:41:54 -070042 public static void main(String[] args) throws IOException,
43 ClassNotFoundException {
Bob Lee2e93f652009-08-11 01:16:03 -070044 if (args.length != 1) {
45 System.err.println("Usage: WritePreloadedClassFile [compiled log]");
46 System.exit(-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047 }
Bob Lee2e93f652009-08-11 01:16:03 -070048 String rootFile = args[0];
49 Root root = Root.fromFile(rootFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Bob Lee2e93f652009-08-11 01:16:03 -070051 // No classes are preloaded to start.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052 for (LoadedClass loadedClass : root.loadedClasses.values()) {
53 loadedClass.preloaded = false;
54 }
55
Bob Lee2e93f652009-08-11 01:16:03 -070056 // Open preloaded-classes file for output.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 Writer out = new BufferedWriter(new OutputStreamWriter(
Bob Lee9d2d6e12009-08-13 14:41:54 -070058 new FileOutputStream(Policy.PRELOADED_CLASS_FILE),
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 Charset.forName("US-ASCII")));
60
Bob Lee9d2d6e12009-08-13 14:41:54 -070061 out.write("# Classes which are preloaded by"
62 + " com.android.internal.os.ZygoteInit.\n");
63 out.write("# Automatically generated by frameworks/base/tools/preload/"
64 + WritePreloadedClassFile.class.getSimpleName() + ".java.\n");
65 out.write("# MIN_LOAD_TIME_MICROS=" + MIN_LOAD_TIME_MICROS + "\n");
Jesse Wilsoneaca10e2010-10-12 21:35:56 -070066 out.write("# MIN_PROCESSES=" + MIN_PROCESSES + "\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Bob Lee9d2d6e12009-08-13 14:41:54 -070068 /*
69 * The set of classes to preload. We preload a class if:
70 *
71 * a) it's loaded in the bootclasspath (i.e., is a system class)
72 * b) it takes > MIN_LOAD_TIME_MICROS us to load, and
73 * c) it's loaded by more than one process, or it's loaded by an
74 * application (i.e., not a long running service)
75 */
Bob Lee2e93f652009-08-11 01:16:03 -070076 Set<LoadedClass> toPreload = new TreeSet<LoadedClass>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077
Bob Lee9d2d6e12009-08-13 14:41:54 -070078 // Preload classes that were loaded by at least 2 processes. Hopefully,
79 // the memory associated with these classes will be shared.
Bob Lee2e93f652009-08-11 01:16:03 -070080 for (LoadedClass loadedClass : root.loadedClasses.values()) {
Bob Lee9d2d6e12009-08-13 14:41:54 -070081 Set<String> names = loadedClass.processNames();
Jesse Wilsoneaca10e2010-10-12 21:35:56 -070082 if (!Policy.isPreloadable(loadedClass)) {
83 continue;
84 }
85
86 if (names.size() >= MIN_PROCESSES ||
87 (loadedClass.medianTimeMicros() > MIN_LOAD_TIME_MICROS && names.size() > 1)) {
Bob Lee2e93f652009-08-11 01:16:03 -070088 toPreload.add(loadedClass);
89 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 }
91
Bob Lee9d2d6e12009-08-13 14:41:54 -070092 int initialSize = toPreload.size();
93 System.out.println(initialSize
94 + " classses were loaded by more than one app.");
95
96 // Preload eligable classes from applications (not long-running
97 // services).
Bob Lee2e93f652009-08-11 01:16:03 -070098 for (Proc proc : root.processes.values()) {
Bob Lee9d2d6e12009-08-13 14:41:54 -070099 if (proc.fromZygote() && !Policy.isService(proc.name)) {
100 for (Operation operation : proc.operations) {
101 LoadedClass loadedClass = operation.loadedClass;
102 if (shouldPreload(loadedClass)) {
103 toPreload.add(loadedClass);
104 }
105 }
106 }
Bob Lee2e93f652009-08-11 01:16:03 -0700107 }
108
Bob Lee9d2d6e12009-08-13 14:41:54 -0700109 System.out.println("Added " + (toPreload.size() - initialSize)
110 + " more to speed up applications.");
Bob Lee2e93f652009-08-11 01:16:03 -0700111
Bob Lee9d2d6e12009-08-13 14:41:54 -0700112 System.out.println(toPreload.size()
113 + " total classes will be preloaded.");
114
115 // Make classes that were implicitly loaded by the zygote explicit.
Bob Lee2e93f652009-08-11 01:16:03 -0700116 // This adds minimal overhead but avoid confusion about classes not
117 // appearing in the list.
Bob Lee9d2d6e12009-08-13 14:41:54 -0700118 addAllClassesFrom("zygote", root, toPreload);
Bob Lee2e93f652009-08-11 01:16:03 -0700119
120 for (LoadedClass loadedClass : toPreload) {
Bob Lee9d2d6e12009-08-13 14:41:54 -0700121 out.write(loadedClass.name + "\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 }
123
124 out.close();
125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 // Update data to reflect LoadedClass.preloaded changes.
Bob Lee2e93f652009-08-11 01:16:03 -0700127 for (LoadedClass loadedClass : toPreload) {
128 loadedClass.preloaded = true;
129 }
130 root.toFile(rootFile);
131 }
132
Bob Lee9d2d6e12009-08-13 14:41:54 -0700133 private static void addAllClassesFrom(String processName, Root root,
134 Set<LoadedClass> toPreload) {
Bob Lee2e93f652009-08-11 01:16:03 -0700135 for (Proc proc : root.processes.values()) {
Bob Lee9d2d6e12009-08-13 14:41:54 -0700136 if (proc.name.equals(processName)) {
Bob Lee2e93f652009-08-11 01:16:03 -0700137 for (Operation operation : proc.operations) {
Bob Lee9d2d6e12009-08-13 14:41:54 -0700138 boolean preloadable
139 = Policy.isPreloadable(operation.loadedClass);
140 if (preloadable) {
Bob Lee2e93f652009-08-11 01:16:03 -0700141 toPreload.add(operation.loadedClass);
142 }
143 }
144 }
145 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 }
Bob Lee9d2d6e12009-08-13 14:41:54 -0700147
148 /**
149 * Returns true if the class should be preloaded.
150 */
151 private static boolean shouldPreload(LoadedClass clazz) {
152 return Policy.isPreloadable(clazz)
153 && clazz.medianTimeMicros() > MIN_LOAD_TIME_MICROS;
154 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155}