Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. |
5 |
* |
6 |
* The contents of this file are subject to the terms of either the GNU |
7 |
* General Public License Version 2 only ("GPL") or the Common |
8 |
* Development and Distribution License("CDDL") (collectively, the |
9 |
* "License"). You may not use this file except in compliance with the |
10 |
* License. You can obtain a copy of the License at |
11 |
* http://www.netbeans.org/cddl-gplv2.html |
12 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
13 |
* specific language governing permissions and limitations under the |
14 |
* License. When distributing the software, include this License Header |
15 |
* Notice in each file and include the License file at |
16 |
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this |
17 |
* particular file as subject to the "Classpath" exception as provided |
18 |
* by Sun in the GPL Version 2 section of the License file that |
19 |
* accompanied this code. If applicable, add the following below the |
20 |
* License Header, with the fields enclosed by brackets [] replaced by |
21 |
* your own identifying information: |
22 |
* "Portions Copyrighted [year] [name of copyright owner]" |
23 |
* |
24 |
* Contributor(s): |
25 |
* |
26 |
* The Original Software is NetBeans. The Initial Developer of the Original |
27 |
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun |
28 |
* Microsystems, Inc. All Rights Reserved. |
29 |
* |
30 |
* If you wish your version of this file to be governed by only the CDDL |
31 |
* or only the GPL Version 2, indicate your decision by adding |
32 |
* "[Contributor] elects to include this software in this distribution |
33 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
34 |
* single choice of license, a recipient has the option to distribute |
35 |
* your version of this file under either the CDDL, the GPL Version 2 or |
36 |
* to extend the choice of license to its licensees as provided above. |
37 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
38 |
* Version 2 license, then the option applies only if the new code is |
39 |
* made subject to such option by the copyright holder. |
40 |
*/ |
41 |
package org.openide.filesystems; |
42 |
|
43 |
import java.io.*; |
44 |
import java.util.*; |
45 |
import java.util.logging.Level; |
46 |
import java.util.logging.Logger; |
47 |
|
48 |
/** |
49 |
* Collects data and print them when JVM shutting down. |
50 |
* |
51 |
* @author Pavel Flaška |
52 |
*/ |
53 |
final class Statistics { |
54 |
|
55 |
private static final boolean streamLog = false; |
56 |
private static final boolean dirLog = true; |
57 |
private static final boolean streamCreation = false; |
58 |
/** singleton instance */ |
59 |
private static Statistics INSTANCE; |
60 |
private static int absoluteStacks; |
61 |
private Map<String, Integer> isDirInvoc = new HashMap<String, Integer>(); |
62 |
private Map<Closeable, Integer> streams = new HashMap<Closeable, Integer>(); |
63 |
private Map<String, Integer> stacks = new HashMap<String, Integer>(); |
64 |
|
65 |
private Statistics() { |
66 |
} |
67 |
|
68 |
/** |
69 |
* Get the class instance. |
70 |
* |
71 |
* @return singleton of Statistics class. |
72 |
*/ |
73 |
static synchronized Statistics getDefault() { |
74 |
if (INSTANCE == null) { |
75 |
INSTANCE = new Statistics(); |
76 |
} |
77 |
return INSTANCE; |
78 |
} |
79 |
|
80 |
static synchronized void reset() { |
81 |
INSTANCE = null; |
82 |
} |
83 |
|
84 |
/** |
85 |
* Counts in isDirectory() call on <tt>file</tt>. |
86 |
* |
87 |
* @param file file name |
88 |
*/ |
89 |
public static void fileIsDirectory(String file) { |
90 |
if (!dirLog) { |
91 |
return; |
92 |
} |
93 |
Integer i = Statistics.getDefault().isDirInvoc.get(file); |
94 |
if (i == null) { |
95 |
i = 1; |
96 |
} else { |
97 |
i++; |
98 |
} |
99 |
Statistics.getDefault().isDirInvoc.put(file, i); |
100 |
|
101 |
//////////////////// |
102 |
StringBuilder sb = new StringBuilder(300); |
103 |
StackTraceElement[] ste = Thread.currentThread().getStackTrace(); |
104 |
for (i = 2; i < ste.length; i++) { |
105 |
sb.append(ste[i].toString()).append('\n'); |
106 |
} |
107 |
String s = sb.toString(); |
108 |
i = Statistics.getDefault().stacks.get(s); |
109 |
if (i == null) { |
110 |
i = 1; |
111 |
} else { |
112 |
i++; |
113 |
} |
114 |
Statistics.getDefault().stacks.put(s, i); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Counts in isDirectory() call on <tt>file</tt>. |
119 |
* |
120 |
* @param file file name |
121 |
*/ |
122 |
// public static void fileIsDirectory(String file) { |
123 |
// StringBuilder sb = new StringBuilder(300); |
124 |
// StackTraceElement[] ste = Thread.currentThread().getStackTrace(); |
125 |
//// if ("InstalledFileLocatorImpl.java".equals(ste[6].getFileName())) { |
126 |
// if (file.endsWith(".jar")) { |
127 |
// Integer i = Statistics.getDefault().isDirInvoc.get(file); |
128 |
// if (i == null) { |
129 |
// i = 1; |
130 |
// } else { |
131 |
// i++; |
132 |
// } |
133 |
// Statistics.getDefault().isDirInvoc.put(file, i); |
134 |
// for (i = 2; i < ste.length; i++) { |
135 |
// sb.append(ste[i].toString()).append('\n'); |
136 |
// } |
137 |
// String s = sb.toString(); |
138 |
// i = Statistics.getDefault().stacks.get(s); |
139 |
// if (i == null) { |
140 |
// i = 1; |
141 |
// } else { |
142 |
// i++; |
143 |
// } |
144 |
// Statistics.getDefault().stacks.put(s, i); |
145 |
// } |
146 |
// } |
147 |
/** |
148 |
* Counts in read() call on <tt>inputStream</tt>. |
149 |
* |
150 |
* @param inputStream file name |
151 |
*/ |
152 |
public static void streams(Closeable closeable) { |
153 |
if (!streamLog) { |
154 |
return; |
155 |
} |
156 |
Integer i = Statistics.getDefault().streams.get(closeable); |
157 |
if (i == null) { |
158 |
i = 1; |
159 |
} else { |
160 |
i++; |
161 |
} |
162 |
Statistics.getDefault().streams.put(closeable, i); |
163 |
|
164 |
//////////////////// |
165 |
StringBuilder sb = new StringBuilder(); |
166 |
StackTraceElement[] ste = Thread.currentThread().getStackTrace(); |
167 |
for (i = 2; i < ste.length; i++) { |
168 |
sb.append(ste[i].toString()).append('\n'); |
169 |
} |
170 |
String s = sb.toString(); |
171 |
i = Statistics.getDefault().stacks.get(s); |
172 |
if (i == null) { |
173 |
i = 1; |
174 |
} else { |
175 |
i++; |
176 |
} |
177 |
Statistics.getDefault().stacks.put(s, i); |
178 |
} |
179 |
private Map<Closeable, String> streamToName = new IdentityHashMap<Closeable, String>(); |
180 |
private Map<Closeable, StackTraceElement[]> streamDump = new IdentityHashMap<Closeable, StackTraceElement[]>(); |
181 |
|
182 |
public static void streamToName(Closeable is, String name) { |
183 |
Statistics.getDefault().streamToName.put(is, name); |
184 |
if (streamCreation) Statistics.getDefault().streamDump.put(is, Thread.currentThread().getStackTrace()); |
185 |
} |
186 |
|
187 |
//////////////////////////////////////////////////////////////////////////// |
188 |
// private members |
189 |
void print(PrintWriter out) { |
190 |
for (String s : isDirInvoc.keySet()) { |
191 |
out.printf("%4d", isDirInvoc.get(s)); |
192 |
out.println("; " + s); |
193 |
} |
194 |
for (String s : stacks.keySet()) { |
195 |
int value = stacks.get(s); |
196 |
absoluteStacks += value; |
197 |
out.printf("count %5d; Stack:\n", value); |
198 |
out.println(s); |
199 |
} |
200 |
out.println("Total stacks recorded: " + absoluteStacks); |
201 |
for (Closeable s : streams.keySet()) { |
202 |
out.printf("count %5d", streams.get(s)); |
203 |
out.println("; " + s.getClass() + ' ' + streamToName.get(s)); |
204 |
} |
205 |
for (Closeable s : streamDump.keySet()) { |
206 |
out.println(streamToName.get(s)); |
207 |
for (StackTraceElement ste : streamDump.get(s)) { |
208 |
out.println(ste); |
209 |
} |
210 |
out.println(); |
211 |
} |
212 |
} |
213 |
} |