summaryrefslogtreecommitdiffstats
path: root/tools/preload/PrintBugReports.java
blob: a6d418775dc7769d52c0810bdb98f75cac87ca3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.util.Map;
import java.util.List;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
import java.util.Iterator;

/**
 * Prints HTML reports that can be attached to bugs.
 */
public class PrintBugReports {

    private static final String DIR = "out/preload";
    private static boolean PRINT_MEMORY_USAGE = false;

    private static final Comparator<LoadedClass> DEFAULT_ORDER
            = new Comparator<LoadedClass>() {
        public int compare(LoadedClass a, LoadedClass b) {
            // Longest load time first.
            int diff = b.medianTimeMicros() - a.medianTimeMicros();
            if (diff != 0) {
                return diff;
            }

            return a.name.compareTo(b.name);
        }
    };

    public static void main(String[] args)
            throws IOException, ClassNotFoundException {
        Root root = Root.fromFile(args[0]);
        String baseUrl = "";
        if (args.length > 1) {
            baseUrl = args[1];
        }

        new File(DIR).mkdirs();

        Map<String, List<Proc>> procsByName = new HashMap<String, List<Proc>>();
        for (Proc proc : root.processes.values()) {
            if (proc.fromZygote()) {
                List<Proc> procs = procsByName.get(proc.name);
                if (procs == null) {
                    procs = new ArrayList<Proc>();
                    procsByName.put(proc.name, procs);
                }
                procs.add(proc);
            }
        }

        Set<LoadedClass> coreClasses = new TreeSet<LoadedClass>(DEFAULT_ORDER);
        Set<LoadedClass> frameworkClasses = new TreeSet<LoadedClass>(DEFAULT_ORDER);

        for (List<Proc> procs : procsByName.values()) {
            Proc first = procs.get(0);
            Set<LoadedClass> classes = new TreeSet<LoadedClass>(DEFAULT_ORDER);
            Set<LoadedClass> sharedClasses
                    = new TreeSet<LoadedClass>(DEFAULT_ORDER);
            for (Proc proc : procs) {
                for (Operation operation : proc.operations) {
                    LoadedClass clazz = operation.loadedClass;
                    if (clazz.isSharable() && clazz.systemClass) {
                        if (clazz.name.startsWith("dalvik")
                                || clazz.name.startsWith("org")
                                || clazz.name.startsWith("java")) {
                            coreClasses.add(clazz);
                        } else {
                            frameworkClasses.add(clazz);
                        }
                        sharedClasses.add(clazz);
                    } else {
                        classes.add(clazz);
                    }
                }
            }
            printApplicationHtml(first.name, root.baseline, classes,
                    sharedClasses);
        }

        printHtml("core", root.baseline, coreClasses);
        printHtml("framework", root.baseline, frameworkClasses);

        PrintStream out = new PrintStream(DIR + "/toc.html");
        out.println("<html><body>");
        out.println("<a href='" + baseUrl
                + "/core.html'>core</a><br/>");
        out.println("<a href='" + baseUrl
                + "/framework.html'>framework</a><br/>");

        for (String s : new TreeSet<String>(procsByName.keySet())) {
            out.println("<a href='" + baseUrl + "/"
                    + s + ".html'>" + s + "</a><br/>");
        }
        out.println("</body></html>");
        out.close();
    }

    static void printApplicationHtml(String name, MemoryUsage baseline,
            Iterable<LoadedClass> classes, Iterable<LoadedClass> sharedClasses)
            throws IOException {
        PrintStream out = new PrintStream(DIR + "/" + name + ".html");

        printHeader(name, out);
        out.println("<body>");
        out.println("<h1><tt>" + name + "</tt></h1>");
        out.println("<p><i>Click a column header to sort by that column.</i></p>");

        out.println("<p><a href=\"#shared\">Shared Classes</a></p>");

        out.println("<h3>Application-Specific Classes</h3>");

        out.println("<p>These classes were loaded only by " + name + ". If"
                + " the value of the <i>Preloaded</i> column is <i>yes</i> or "
                + " <i>no</i>, the class is in the boot classpath; if it's not"
                + " part of the published API, consider"
                + " moving it into the APK.</p>");

        printTable(out, baseline, classes, false);

        out.println("<p><a href=\"#\">Top</a></p>");

        out.println("<a name=\"shared\"/><h3>Shared Classes</h3>");

        out.println("<p>These classes are in the boot classpath. They are used"
                + " by " + name + " as well as others.");

        printTable(out, baseline, sharedClasses, true);

        out.println("</body></html>");
        out.close();
    }

    static void printHtml(String name, MemoryUsage baseline,
            Iterable<LoadedClass> classes)
            throws IOException {
        PrintStream out = new PrintStream(DIR + "/" + name + ".html");

        printHeader(name, out);
        out.println("<body>");
        out.println("<h1><tt>" + name + "</tt></h1>");
        out.println("<p><i>Click a column header to sort by that column.</i></p>");

        printTable(out, baseline, classes, true);

        out.println("</body></html>");
        out.close();
    }

    private static void printHeader(String name, PrintStream out)
            throws IOException {
        out.println("<html><head>");
        out.println("<title>" + name + "</title>");
        out.println("<style>");
        out.println("a, th, td, h1, h3, p { font-family: arial }");
        out.println("th, td { font-size: small }");
        out.println("</style>");
        out.println("<script language=\"javascript\">");
        out.write(SCRIPT);
        out.println("</script>");
        out.println("</head>");
    }

    static void printTable(PrintStream out, MemoryUsage baseline,
            Iterable<LoadedClass> classes, boolean showProcNames) {
        out.println("<p><table border=\"1\" cellpadding=\"5\""
                + " class=\"sortable\" cellspacing=\"0\">");

        out.println("<thead bgcolor=\"#eeeeee\"><tr>");
        out.println("<th>Name</th>");
        out.println("<th>Preloaded</th>");
        out.println("<th>Total Time (us)</th>");
        out.println("<th>Load Time (us)</th>");
        out.println("<th>Init Time (us)</th>");
        if (PRINT_MEMORY_USAGE) {
            out.println("<th>Total Heap (B)</th>");
            out.println("<th>Dalvik Heap (B)</th>");
            out.println("<th>Native Heap (B)</th>");
            out.println("<th>Total Pages (kB)</th>");
            out.println("<th>Dalvik Pages (kB)</th>");
            out.println("<th>Native Pages (kB)</th>");
            out.println("<th>Other Pages (kB)</th>");
        }
        if (showProcNames) {
            out.println("<th>Loaded by</th>");
        }
        out.println("</tr></thead>");

        for (LoadedClass clazz : classes) {
            out.println("<tr>");
            out.println("<td>" + clazz.name + "</td>");

            out.println("<td>" + ((clazz.systemClass)
                    ? ((clazz.preloaded) ? "yes" : "no") : "n/a") + "</td>");

            out.println("<td>" + clazz.medianTimeMicros() + "</td>");
            out.println("<td>" + clazz.medianLoadTimeMicros() + "</td>");
            out.println("<td>" + clazz.medianInitTimeMicros() + "</td>");

            if (PRINT_MEMORY_USAGE) {
                if (clazz.memoryUsage.isAvailable()) {
                    MemoryUsage subtracted
                            = clazz.memoryUsage.subtract(baseline);

                    long totalHeap = subtracted.javaHeapSize()
                            + subtracted.nativeHeapSize;
                    out.println("<td>" + totalHeap + "</td>");
                    out.println("<td>" + subtracted.javaHeapSize() + "</td>");
                    out.println("<td>" + subtracted.nativeHeapSize + "</td>");

                    out.println("<td>" + subtracted.totalPages() + "</td>");
                    out.println("<td>" + subtracted.javaPagesInK() + "</td>");
                    out.println("<td>" + subtracted.nativePagesInK() + "</td>");
                    out.println("<td>" + subtracted.otherPagesInK() + "</td>");
                } else {
                    for (int i = 0; i < 7; i++) {
                        out.println("<td>&nbsp;</td>");
                    }
                }
            }

            if (showProcNames) {
                out.println("<td>");
                Set<String> procNames = new TreeSet<String>();
                for (Operation op : clazz.loads) {
                    procNames.add(op.process.name);
                }
                for (Operation op : clazz.initializations) {
                    procNames.add(op.process.name);
                }
                if (procNames.size() <= 3) {
                    for (String name : procNames) {
                        out.print(name + "<br/>");
                    }
                } else {
                    Iterator<String> i = procNames.iterator();
                    out.print(i.next() + "<br/>");
                    out.print(i.next() + "<br/>");
                    out.print("...and " + (procNames.size() - 2)
                            + " others.");
                }
                out.println("</td>");
            }

            out.println("</tr>");
        }

        out.println("</table></p>");
    }

    static byte[] SCRIPT;
    static {
        try {
            File script = new File(
                    "frameworks/base/tools/preload/sorttable.js");
            int length = (int) script.length();
            SCRIPT = new byte[length];
            DataInputStream in = new DataInputStream(
                    new FileInputStream(script));
            in.readFully(SCRIPT);
            in.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}