blob: b209af0c25f607c4e68ce8f771305487428f3ad1 [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/**
27 * Writes /frameworks/base/preloaded-classes. Also updates LoadedClass.preloaded
28 * fields and writes over compiled log file.
29 */
30public class WritePreloadedClassFile {
31
32 public static void main(String[] args) throws IOException, ClassNotFoundException {
Bob Lee2e93f652009-08-11 01:16:03 -070033 if (args.length != 1) {
34 System.err.println("Usage: WritePreloadedClassFile [compiled log]");
35 System.exit(-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036 }
Bob Lee2e93f652009-08-11 01:16:03 -070037 String rootFile = args[0];
38 Root root = Root.fromFile(rootFile);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039
Bob Lee2e93f652009-08-11 01:16:03 -070040 // No classes are preloaded to start.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041 for (LoadedClass loadedClass : root.loadedClasses.values()) {
42 loadedClass.preloaded = false;
43 }
44
Bob Lee2e93f652009-08-11 01:16:03 -070045 // Open preloaded-classes file for output.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046 Writer out = new BufferedWriter(new OutputStreamWriter(
47 new FileOutputStream(Policy.getPreloadedClassFileName()),
48 Charset.forName("US-ASCII")));
49
50 out.write("# Classes which are preloaded by com.android.internal.os.ZygoteInit.\n");
51 out.write("# Automatically generated by /frameworks/base/tools/preload.\n");
Bob Lee2e93f652009-08-11 01:16:03 -070052 out.write("# percent=" + Proc.PERCENTAGE_TO_PRELOAD
53 + ", weight=" + ClassRank.SEQUENCE_WEIGHT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054 + ", bucket_size=" + ClassRank.BUCKET_SIZE
55 + "\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Bob Lee2e93f652009-08-11 01:16:03 -070057 Set<LoadedClass> toPreload = new TreeSet<LoadedClass>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058
Bob Lee2e93f652009-08-11 01:16:03 -070059 // Preload all classes that were loaded by at least 2 apps, if both
60 // apps run at the same time, they'll share memory.
61 for (LoadedClass loadedClass : root.loadedClasses.values()) {
62 if (!loadedClass.isPreloadable()) {
63 continue;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064 }
Bob Lee2e93f652009-08-11 01:16:03 -070065
66 Set<String> appNames = loadedClass.applicationNames();
67
68 if (appNames.size() > 3) {
69 toPreload.add(loadedClass);
70 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 }
72
Bob Lee2e93f652009-08-11 01:16:03 -070073 // Try to make individual apps start faster by preloading slowest
74 // classes.
75 for (Proc proc : root.processes.values()) {
76 toPreload.addAll(proc.highestRankedClasses());
77 }
78
79 System.out.println(toPreload.size() + " classes will be preloaded.");
80
81 // Make classes that were already loaded by the zygote explicit.
82 // This adds minimal overhead but avoid confusion about classes not
83 // appearing in the list.
84 addAllClassesFor("zygote", root, toPreload);
85
86 for (LoadedClass loadedClass : toPreload) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 out.write(loadedClass.name);
88 out.write('\n');
89 }
90
91 out.close();
92
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 // Update data to reflect LoadedClass.preloaded changes.
Bob Lee2e93f652009-08-11 01:16:03 -070094 for (LoadedClass loadedClass : toPreload) {
95 loadedClass.preloaded = true;
96 }
97 root.toFile(rootFile);
98 }
99
100 private static void addAllClassesFor(String packageName, Root root,
101 Set<LoadedClass> toPreload) {
102 for (Proc proc : root.processes.values()) {
103 if (proc.name.equals(packageName)) {
104 for (Operation operation : proc.operations) {
105 // TODO: I'm not sure how the zygote loaded classes that
106 // aren't supposed to be preloadable...
107 if (operation.loadedClass.isPreloadable()) {
108 toPreload.add(operation.loadedClass);
109 }
110 }
111 }
112 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 }
114}