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.IOException;
18 import java.io.FileInputStream;
19 import java.io.ObjectInputStream;
20 import java.io.BufferedInputStream;
21 import java.io.Writer;
22 import java.io.PrintStream;
23 import java.util.Set;
24 import java.util.HashSet;
25 import java.util.TreeSet;
26 import java.util.Iterator;
27 
28 /**
29  * Prints raw information in CSV format.
30  */
31 public class PrintCsv {
32 
main(String[] args)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 
printHeaders(PrintStream out)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 
printRow(PrintStream out, MemoryUsage baseline, LoadedClass loadedClass)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