The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | import java.io.BufferedWriter; |
| 18 | import java.io.FileOutputStream; |
| 19 | import java.io.IOException; |
| 20 | import java.io.OutputStreamWriter; |
| 21 | import java.io.Writer; |
| 22 | import java.nio.charset.Charset; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import java.util.Set; |
| 24 | import java.util.TreeSet; |
| 25 | |
| 26 | /** |
| 27 | * Writes /frameworks/base/preloaded-classes. Also updates LoadedClass.preloaded |
| 28 | * fields and writes over compiled log file. |
| 29 | */ |
| 30 | public class WritePreloadedClassFile { |
| 31 | |
| 32 | public static void main(String[] args) throws IOException, ClassNotFoundException { |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 33 | if (args.length != 1) { |
| 34 | System.err.println("Usage: WritePreloadedClassFile [compiled log]"); |
| 35 | System.exit(-1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | } |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 37 | String rootFile = args[0]; |
| 38 | Root root = Root.fromFile(rootFile); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 39 | |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 40 | // No classes are preloaded to start. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | for (LoadedClass loadedClass : root.loadedClasses.values()) { |
| 42 | loadedClass.preloaded = false; |
| 43 | } |
| 44 | |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 45 | // Open preloaded-classes file for output. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | 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 Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 52 | out.write("# percent=" + Proc.PERCENTAGE_TO_PRELOAD |
| 53 | + ", weight=" + ClassRank.SEQUENCE_WEIGHT |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | + ", bucket_size=" + ClassRank.BUCKET_SIZE |
| 55 | + "\n"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 57 | Set<LoadedClass> toPreload = new TreeSet<LoadedClass>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 59 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 65 | |
| 66 | Set<String> appNames = loadedClass.applicationNames(); |
| 67 | |
| 68 | if (appNames.size() > 3) { |
| 69 | toPreload.add(loadedClass); |
| 70 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 73 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | out.write(loadedClass.name); |
| 88 | out.write('\n'); |
| 89 | } |
| 90 | |
| 91 | out.close(); |
| 92 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 93 | // Update data to reflect LoadedClass.preloaded changes. |
Bob Lee | 2e93f65 | 2009-08-11 01:16:03 -0700 | [diff] [blame] | 94 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | } |
| 114 | } |