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; |
| 23 | import java.util.ArrayList; |
| 24 | import java.util.List; |
| 25 | import java.util.Set; |
| 26 | import java.util.TreeSet; |
| 27 | |
| 28 | /** |
| 29 | * Writes /frameworks/base/preloaded-classes. Also updates LoadedClass.preloaded |
| 30 | * fields and writes over compiled log file. |
| 31 | */ |
| 32 | public class WritePreloadedClassFile { |
| 33 | |
| 34 | public static void main(String[] args) throws IOException, ClassNotFoundException { |
| 35 | |
| 36 | // Process command-line arguments first |
| 37 | List<String> wiredProcesses = new ArrayList<String>(); |
| 38 | String inputFileName = null; |
| 39 | int argOffset = 0; |
| 40 | try { |
| 41 | while ("--preload-all-process".equals(args[argOffset])) { |
| 42 | argOffset++; |
| 43 | wiredProcesses.add(args[argOffset++]); |
| 44 | } |
| 45 | |
| 46 | inputFileName = args[argOffset++]; |
| 47 | } catch (RuntimeException e) { |
| 48 | System.err.println("Usage: WritePreloadedClassFile " + |
| 49 | "[--preload-all-process process-name] " + |
| 50 | "[compiled log file]"); |
| 51 | System.exit(0); |
| 52 | } |
| 53 | |
| 54 | Root root = Root.fromFile(inputFileName); |
| 55 | |
| 56 | for (LoadedClass loadedClass : root.loadedClasses.values()) { |
| 57 | loadedClass.preloaded = false; |
| 58 | } |
| 59 | |
| 60 | Writer out = new BufferedWriter(new OutputStreamWriter( |
| 61 | new FileOutputStream(Policy.getPreloadedClassFileName()), |
| 62 | Charset.forName("US-ASCII"))); |
| 63 | |
| 64 | out.write("# Classes which are preloaded by com.android.internal.os.ZygoteInit.\n"); |
| 65 | out.write("# Automatically generated by /frameworks/base/tools/preload.\n"); |
| 66 | out.write("# percent=" + Proc.PERCENTAGE_TO_PRELOAD + ", weight=" |
| 67 | + ClassRank.SEQUENCE_WEIGHT |
| 68 | + ", bucket_size=" + ClassRank.BUCKET_SIZE |
| 69 | + "\n"); |
| 70 | for (String wiredProcess : wiredProcesses) { |
| 71 | out.write("# forcing classes loaded by: " + wiredProcess + "\n"); |
| 72 | } |
| 73 | |
| 74 | Set<LoadedClass> highestRanked = new TreeSet<LoadedClass>(); |
| 75 | for (Proc proc : root.processes.values()) { |
| 76 | // test to see if this is one of the wired-down ("take all classes") processes |
| 77 | boolean isWired = wiredProcesses.contains(proc.name); |
| 78 | |
| 79 | List<LoadedClass> highestForProc = proc.highestRankedClasses(isWired); |
| 80 | |
| 81 | System.out.println(proc.name + ": " + highestForProc.size()); |
| 82 | |
| 83 | for (LoadedClass loadedClass : highestForProc) { |
| 84 | loadedClass.preloaded = true; |
| 85 | } |
| 86 | highestRanked.addAll(highestForProc); |
| 87 | } |
| 88 | |
| 89 | for (LoadedClass loadedClass : highestRanked) { |
| 90 | out.write(loadedClass.name); |
| 91 | out.write('\n'); |
| 92 | } |
| 93 | |
| 94 | out.close(); |
| 95 | |
| 96 | System.out.println(highestRanked.size() |
| 97 | + " classes will be preloaded."); |
| 98 | |
| 99 | // Update data to reflect LoadedClass.preloaded changes. |
| 100 | root.toFile(inputFileName); |
| 101 | } |
| 102 | } |