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
17import java.io.IOException;
18import java.io.FileInputStream;
19import java.io.ObjectInputStream;
20import java.io.BufferedInputStream;
21import java.io.Writer;
22import java.io.PrintStream;
23import java.util.Set;
24import java.util.HashSet;
25import java.util.TreeSet;
26import java.util.Iterator;
27
28/**
29 * Prints raw information in CSV format.
30 */
31public class PrintCsv {
32
33    public static void main(String[] args)
34            throws IOException, ClassNotFoundException {
35        if (args.length != 1) {
36            System.err.println("Usage: PrintCsv [compiled log file]");
37            System.exit(0);
38        }
39
40        Root root = Root.fromFile(args[0]);
41
42        printHeaders(System.out);
43
44        MemoryUsage baseline = MemoryUsage.baseline();
45
46        for (LoadedClass loadedClass : root.loadedClasses.values()) {
47            if (!loadedClass.systemClass) {
48                continue;
49            }
50
51            printRow(System.out, baseline, loadedClass);
52        }
53    }
54
55    static void printHeaders(PrintStream out) {
56        out.println("Name"
57                + ",Preloaded"
58                + ",Median Load Time (us)"
59                + ",Median Init Time (us)"
60                + ",Process Names"
61                + ",Load Count"
62                + ",Init Count"
63                + ",Managed Heap (B)"
64                + ",Native Heap (B)"
65                + ",Managed Pages (kB)"
66                + ",Native Pages (kB)"
67                + ",Other Pages (kB)");
68    }
69
70    static void printRow(PrintStream out, MemoryUsage baseline,
71            LoadedClass loadedClass) {
72        out.print(loadedClass.name);
73        out.print(',');
74        out.print(loadedClass.preloaded);
75        out.print(',');
76        out.print(loadedClass.medianLoadTimeMicros());
77        out.print(',');
78        out.print(loadedClass.medianInitTimeMicros());
79        out.print(',');
80        out.print('"');
81
82        Set<String> procNames = new TreeSet<String>();
83        for (Operation op : loadedClass.loads)
84            procNames.add(op.process.name);
85        for (Operation op : loadedClass.initializations)
86            procNames.add(op.process.name);
87
88        if (procNames.size() <= 3) {
89            for (String name : procNames) {
90                out.print(name + "\n");
91            }
92        } else {
93            Iterator<String> i = procNames.iterator();
94            out.print(i.next() + "\n");
95            out.print(i.next() + "\n");
96            out.print("...and " + (procNames.size() - 2)
97                    + " others.");
98        }
99
100        out.print('"');
101        out.print(',');
102        out.print(loadedClass.loads.size());
103        out.print(',');
104        out.print(loadedClass.initializations.size());
105
106        if (loadedClass.memoryUsage.isAvailable()) {
107            MemoryUsage subtracted
108                    = loadedClass.memoryUsage.subtract(baseline);
109
110            out.print(',');
111            out.print(subtracted.javaHeapSize());
112            out.print(',');
113            out.print(subtracted.nativeHeapSize);
114            out.print(',');
115            out.print(subtracted.javaPagesInK());
116            out.print(',');
117            out.print(subtracted.nativePagesInK());
118            out.print(',');
119            out.print(subtracted.otherPagesInK());
120
121        } else {
122            out.print(",n/a,n/a,n/a,n/a,n/a");
123        }
124
125        out.println();
126    }
127}
128