blob: d87b1f056a0152a4fa5e10c815322c8dfd7ce75a [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;
23import java.util.ArrayList;
24import java.util.List;
25import java.util.Set;
26import java.util.TreeSet;
27
28/**
29 * Writes /frameworks/base/preloaded-classes. Also updates LoadedClass.preloaded
30 * fields and writes over compiled log file.
31 */
32public 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}