author | lagergren |
Fri, 22 Feb 2013 12:22:16 +0100 | |
changeset 16256 | f2d9a0c49914 |
parent 16234 | 86cb162cec6c |
child 16277 | fd698c5ee684 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
*/ |
|
25 |
||
26 |
package jdk.nashorn.internal.runtime.linker; |
|
27 |
||
28 |
import static jdk.nashorn.internal.runtime.linker.Lookup.MH; |
|
29 |
||
30 |
import java.io.FileNotFoundException; |
|
31 |
import java.io.FileOutputStream; |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
32 |
import java.io.PrintWriter; |
16147 | 33 |
import java.lang.invoke.MethodHandle; |
34 |
import java.lang.invoke.MethodHandles; |
|
35 |
import java.lang.invoke.MethodType; |
|
36 |
import java.util.ArrayList; |
|
37 |
import java.util.Collections; |
|
38 |
import java.util.Comparator; |
|
39 |
import java.util.HashMap; |
|
40 |
import java.util.LinkedList; |
|
41 |
import java.util.Map; |
|
42 |
import java.util.Map.Entry; |
|
43 |
import java.util.Random; |
|
44 |
import java.util.Set; |
|
45 |
import java.util.concurrent.atomic.AtomicInteger; |
|
16234
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16201
diff
changeset
|
46 |
import jdk.internal.dynalink.ChainedCallSite; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16201
diff
changeset
|
47 |
import jdk.internal.dynalink.DynamicLinker; |
86cb162cec6c
8008085: Integrate Dynalink source code into Nashorn codebase
attila
parents:
16201
diff
changeset
|
48 |
import jdk.internal.dynalink.linker.GuardedInvocation; |
16147 | 49 |
import jdk.nashorn.internal.runtime.Context; |
50 |
import jdk.nashorn.internal.runtime.Debug; |
|
51 |
import jdk.nashorn.internal.runtime.ScriptObject; |
|
52 |
import jdk.nashorn.internal.runtime.ScriptRuntime; |
|
53 |
import jdk.nashorn.internal.runtime.options.Options; |
|
54 |
||
55 |
||
56 |
/** |
|
57 |
* Relinkable form of call site. |
|
58 |
*/ |
|
59 |
public class LinkerCallSite extends ChainedCallSite { |
|
60 |
/** Maximum number of arguments passed directly. */ |
|
61 |
public static final int ARGLIMIT = 250; |
|
62 |
||
63 |
private static final String PROFILEFILE = Options.getStringProperty("nashorn.profilefile", "NashornProfile.txt"); |
|
64 |
||
65 |
private static final MethodHandle INCREASE_MISS_COUNTER = findOwnMH("increaseMissCount", Object.class, String.class, Object.class); |
|
66 |
||
67 |
LinkerCallSite(final NashornCallSiteDescriptor descriptor) { |
|
68 |
super(descriptor); |
|
69 |
if (Context.DEBUG) { |
|
70 |
LinkerCallSite.count++; |
|
71 |
} |
|
72 |
} |
|
73 |
||
74 |
/** |
|
75 |
* Construct a new linker call site. |
|
76 |
* @param name Name of method. |
|
77 |
* @param type Method type. |
|
78 |
* @param flags Call site specific flags. |
|
79 |
* @return New LinkerCallSite. |
|
80 |
*/ |
|
81 |
static LinkerCallSite newLinkerCallSite(final String name, final MethodType type, final int flags) { |
|
82 |
final NashornCallSiteDescriptor desc = NashornCallSiteDescriptor.get(name, type, flags); |
|
83 |
||
84 |
if (desc.isProfile()) { |
|
85 |
return ProfilingLinkerCallSite.newProfilingLinkerCallSite(desc); |
|
86 |
} |
|
87 |
||
88 |
if (desc.isTrace()) { |
|
89 |
return new TracingLinkerCallSite(desc); |
|
90 |
} |
|
91 |
||
92 |
return new LinkerCallSite(desc); |
|
93 |
} |
|
94 |
||
95 |
@Override |
|
96 |
public String toString() { |
|
97 |
return getDescriptor().toString(); |
|
98 |
} |
|
99 |
||
100 |
/** |
|
101 |
* Get the descriptor for this callsite |
|
102 |
* @return a {@link NashornCallSiteDescriptor} |
|
103 |
*/ |
|
104 |
public NashornCallSiteDescriptor getNashornDescriptor() { |
|
105 |
return (NashornCallSiteDescriptor)getDescriptor(); |
|
106 |
} |
|
107 |
||
108 |
@Override |
|
109 |
public void relink(final GuardedInvocation invocation, final MethodHandle relink) { |
|
110 |
super.relink(invocation, getDebuggingRelink(relink)); |
|
111 |
} |
|
112 |
||
113 |
@Override |
|
114 |
public void resetAndRelink(final GuardedInvocation invocation, final MethodHandle relink) { |
|
115 |
super.resetAndRelink(invocation, getDebuggingRelink(relink)); |
|
116 |
} |
|
117 |
||
118 |
private MethodHandle getDebuggingRelink(final MethodHandle relink) { |
|
119 |
if (Context.DEBUG) { |
|
120 |
return MH.filterArguments(relink, 0, getIncreaseMissCounter(relink.type().parameterType(0))); |
|
121 |
} |
|
122 |
return relink; |
|
123 |
} |
|
124 |
||
125 |
private MethodHandle getIncreaseMissCounter(final Class<?> type) { |
|
126 |
final MethodHandle missCounterWithDesc = MH.bindTo(INCREASE_MISS_COUNTER, getDescriptor().getName() + " @ " + getScriptLocation()); |
|
127 |
if (type == Object.class) { |
|
128 |
return missCounterWithDesc; |
|
129 |
} |
|
130 |
return MH.asType(missCounterWithDesc, missCounterWithDesc.type().changeParameterType(0, type).changeReturnType(type)); |
|
131 |
} |
|
132 |
||
133 |
private static String getScriptLocation() { |
|
134 |
final StackTraceElement caller = DynamicLinker.getRelinkedCallSiteLocation(); |
|
135 |
return caller == null ? "unknown location" : (caller.getFileName() + ":" + caller.getLineNumber()); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Instrumentation - increase the miss count when a callsite misses. Used as filter |
|
140 |
* @param desc descriptor for table entry |
|
141 |
* @param self self reference |
|
142 |
* @return self reference |
|
143 |
*/ |
|
144 |
public static Object increaseMissCount(final String desc, final Object self) { |
|
145 |
++missCount; |
|
146 |
if(r.nextInt(100) < missSamplingPercentage) { |
|
147 |
AtomicInteger i = missCounts.get(desc); |
|
148 |
if(i == null) { |
|
149 |
i = new AtomicInteger(1); |
|
150 |
missCounts.put(desc, i); |
|
151 |
} else { |
|
152 |
i.incrementAndGet(); |
|
153 |
} |
|
154 |
} |
|
155 |
return self; |
|
156 |
} |
|
157 |
||
158 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
159 |
try { |
|
160 |
return MH.findStatic(MethodHandles.lookup(), LinkerCallSite.class, name, MH.type(rtype, types)); |
|
161 |
} catch (final MethodHandleFactory.LookupException e) { |
|
162 |
return MH.findVirtual(MethodHandles.lookup(), LinkerCallSite.class, name, MH.type(rtype, types)); |
|
163 |
} |
|
164 |
} |
|
165 |
||
166 |
/* |
|
167 |
* Debugging call sites. |
|
168 |
*/ |
|
169 |
||
170 |
private static class ProfilingLinkerCallSite extends LinkerCallSite { |
|
171 |
/** List of all profiled call sites. */ |
|
172 |
private static LinkedList<ProfilingLinkerCallSite> profileCallSites = null; |
|
173 |
||
174 |
/** Start time when entered at zero depth. */ |
|
175 |
private long startTime; |
|
176 |
||
177 |
/** Depth of nested calls. */ |
|
178 |
private int depth; |
|
179 |
||
180 |
/** Total time spent in this call site. */ |
|
181 |
private long totalTime; |
|
182 |
||
183 |
/** Total number of times call site entered. */ |
|
184 |
private long hitCount; |
|
185 |
||
186 |
private static final MethodHandles.Lookup LOOKUP = MethodHandles.lookup(); |
|
187 |
||
188 |
private static final MethodHandle PROFILEENTRY = MH.findVirtual(LOOKUP, ProfilingLinkerCallSite.class, "profileEntry", MH.type(Object.class, Object.class)); |
|
189 |
private static final MethodHandle PROFILEEXIT = MH.findVirtual(LOOKUP, ProfilingLinkerCallSite.class, "profileExit", MH.type(Object.class, Object.class)); |
|
190 |
private static final MethodHandle PROFILEVOIDEXIT = MH.findVirtual(LOOKUP, ProfilingLinkerCallSite.class, "profileVoidExit", MH.type(void.class)); |
|
191 |
||
192 |
/* |
|
193 |
* Constructor |
|
194 |
*/ |
|
195 |
||
196 |
ProfilingLinkerCallSite(final NashornCallSiteDescriptor desc) { |
|
197 |
super(desc); |
|
198 |
} |
|
199 |
||
200 |
public static ProfilingLinkerCallSite newProfilingLinkerCallSite(final NashornCallSiteDescriptor desc) { |
|
201 |
if (profileCallSites == null) { |
|
202 |
profileCallSites = new LinkedList<>(); |
|
203 |
||
204 |
final Thread profileDumperThread = new Thread(new ProfileDumper()); |
|
205 |
Runtime.getRuntime().addShutdownHook(profileDumperThread); |
|
206 |
} |
|
207 |
||
208 |
final ProfilingLinkerCallSite callSite = new ProfilingLinkerCallSite(desc); |
|
209 |
profileCallSites.add(callSite); |
|
210 |
||
211 |
return callSite; |
|
212 |
} |
|
213 |
||
214 |
@Override |
|
215 |
public void setTarget(final MethodHandle newTarget) { |
|
216 |
final MethodType type = type(); |
|
217 |
final boolean isVoid = type.returnType() == void.class; |
|
218 |
||
219 |
MethodHandle methodHandle = MH.filterArguments(newTarget, 0, MH.bindTo(PROFILEENTRY, this)); |
|
220 |
||
221 |
if (isVoid) { |
|
222 |
methodHandle = MH.filterReturnValue(methodHandle, MH.bindTo(PROFILEVOIDEXIT, this)); |
|
223 |
} else { |
|
224 |
final MethodType filter = MH.type(type.returnType(), type.returnType()); |
|
225 |
methodHandle = MH.filterReturnValue(methodHandle, MH.asType(MH.bindTo(PROFILEEXIT, this), filter)); |
|
226 |
} |
|
227 |
||
228 |
super.setTarget(methodHandle); |
|
229 |
} |
|
230 |
||
231 |
/** |
|
232 |
* Start the clock for a profile entry and increase depth |
|
233 |
* @param self argument to filter |
|
234 |
* @return preserved argument |
|
235 |
*/ |
|
236 |
@SuppressWarnings("unused") |
|
237 |
public Object profileEntry(final Object self) { |
|
238 |
if (depth == 0) { |
|
239 |
startTime = System.nanoTime(); |
|
240 |
} |
|
241 |
||
242 |
depth++; |
|
243 |
hitCount++; |
|
244 |
||
245 |
return self; |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Decrease depth and stop the clock for a profile entry |
|
250 |
* @param result return value to filter |
|
251 |
* @return preserved argument |
|
252 |
*/ |
|
253 |
@SuppressWarnings("unused") |
|
254 |
public Object profileExit(final Object result) { |
|
255 |
depth--; |
|
256 |
||
257 |
if (depth == 0) { |
|
258 |
totalTime += System.nanoTime() - startTime; |
|
259 |
} |
|
260 |
||
261 |
return result; |
|
262 |
} |
|
263 |
||
264 |
/** |
|
265 |
* Decrease depth without return value filter |
|
266 |
*/ |
|
267 |
@SuppressWarnings("unused") |
|
268 |
public void profileVoidExit() { |
|
269 |
depth--; |
|
270 |
||
271 |
if (depth == 0) { |
|
272 |
totalTime += System.nanoTime() - startTime; |
|
273 |
} |
|
274 |
} |
|
275 |
||
276 |
static class ProfileDumper implements Runnable { |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
277 |
@SuppressWarnings("resource") |
16147 | 278 |
@Override |
279 |
public void run() { |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
280 |
PrintWriter out = null; |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
281 |
boolean fileOutput = false; |
16147 | 282 |
|
283 |
try { |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
284 |
try { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
285 |
out = new PrintWriter(new FileOutputStream(PROFILEFILE)); |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
286 |
fileOutput = true; |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
287 |
} catch (final FileNotFoundException e) { |
16185 | 288 |
out = Context.getCurrentErr(); |
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
289 |
} |
16147 | 290 |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
291 |
dump(out); |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
292 |
} finally { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
293 |
if (out != null && fileOutput) { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
294 |
out.close(); |
16147 | 295 |
} |
296 |
} |
|
297 |
} |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
298 |
|
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
299 |
private static void dump(final PrintWriter out) { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
300 |
int index = 0; |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
301 |
for (final ProfilingLinkerCallSite callSite : profileCallSites) { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
302 |
out.println("" + (index++) + '\t' + |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
303 |
callSite.getDescriptor().getName() + '\t' + |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
304 |
callSite.totalTime + '\t' + |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
305 |
callSite.hitCount); |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
306 |
} |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
307 |
} |
16147 | 308 |
} |
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Debug subclass for LinkerCallSite that allows tracing |
|
313 |
*/ |
|
314 |
private static class TracingLinkerCallSite extends LinkerCallSite { |
|
315 |
||
316 |
private static final MethodHandle TRACEOBJECT = findOwnMH("traceObject", Object.class, MethodHandle.class, Object[].class); |
|
317 |
private static final MethodHandle TRACEVOID = findOwnMH("traceVoid", void.class, MethodHandle.class, Object[].class); |
|
318 |
private static final MethodHandle TRACEMISS = findOwnMH("traceMiss", void.class, Object[].class); |
|
319 |
||
320 |
TracingLinkerCallSite(final NashornCallSiteDescriptor desc) { |
|
321 |
super(desc); |
|
322 |
} |
|
323 |
||
324 |
@Override |
|
325 |
public void setTarget(final MethodHandle newTarget) { |
|
326 |
if (!getNashornDescriptor().isTraceEnterExit()) { |
|
327 |
super.setTarget(newTarget); |
|
328 |
return; |
|
329 |
} |
|
330 |
||
331 |
final MethodType type = type(); |
|
332 |
final boolean isVoid = type.returnType() == void.class; |
|
333 |
||
334 |
MethodHandle traceMethodHandle = isVoid ? TRACEVOID : TRACEOBJECT; |
|
335 |
traceMethodHandle = MH.bindTo(traceMethodHandle, this); |
|
336 |
traceMethodHandle = MH.bindTo(traceMethodHandle, newTarget); |
|
337 |
traceMethodHandle = MH.asCollector(traceMethodHandle, Object[].class, type.parameterCount()); |
|
338 |
traceMethodHandle = MH.asType(traceMethodHandle, type); |
|
339 |
||
340 |
super.setTarget(traceMethodHandle); |
|
341 |
} |
|
342 |
||
343 |
@Override |
|
344 |
public void initialize(final MethodHandle relinkAndInvoke) { |
|
345 |
super.initialize(getFallbackLoggingRelink(relinkAndInvoke)); |
|
346 |
} |
|
347 |
||
348 |
@Override |
|
349 |
public void relink(final GuardedInvocation invocation, final MethodHandle relink) { |
|
350 |
super.relink(invocation, getFallbackLoggingRelink(relink)); |
|
351 |
} |
|
352 |
||
353 |
@Override |
|
354 |
public void resetAndRelink(final GuardedInvocation invocation, final MethodHandle relink) { |
|
355 |
super.resetAndRelink(invocation, getFallbackLoggingRelink(relink)); |
|
356 |
} |
|
357 |
||
358 |
private MethodHandle getFallbackLoggingRelink(final MethodHandle relink) { |
|
359 |
if (!getNashornDescriptor().isTraceMisses()) { |
|
360 |
// If we aren't tracing misses, just return relink as-is |
|
361 |
return relink; |
|
362 |
} |
|
363 |
final MethodType type = relink.type(); |
|
364 |
return MH.foldArguments(relink, MH.asType(MH.asCollector(MH.bindTo(TRACEMISS, this), Object[].class, type.parameterCount()), type.changeReturnType(void.class))); |
|
365 |
} |
|
366 |
||
16185 | 367 |
private void printObject(final PrintWriter out, final Object arg) { |
16147 | 368 |
if (!getNashornDescriptor().isTraceObjects()) { |
369 |
out.print((arg instanceof ScriptObject) ? "ScriptObject" : arg); |
|
370 |
return; |
|
371 |
} |
|
372 |
||
373 |
if (arg instanceof ScriptObject) { |
|
374 |
final ScriptObject object = (ScriptObject)arg; |
|
375 |
||
376 |
boolean isFirst = true; |
|
377 |
final Set<Object> keySet = object.keySet(); |
|
378 |
||
379 |
if (keySet.isEmpty()) { |
|
380 |
out.print(ScriptRuntime.safeToString(arg)); |
|
381 |
} else { |
|
382 |
out.print("{ "); |
|
383 |
||
384 |
for (final Object key : keySet) { |
|
385 |
if (!isFirst) { |
|
386 |
out.print(", "); |
|
387 |
} |
|
388 |
||
389 |
out.print(key); |
|
390 |
out.print(":"); |
|
391 |
||
392 |
final Object value = object.get(key); |
|
393 |
||
394 |
if (value instanceof ScriptObject) { |
|
395 |
out.print("..."); |
|
396 |
} else { |
|
16185 | 397 |
printObject(out, value); |
16147 | 398 |
} |
399 |
||
400 |
isFirst = false; |
|
401 |
} |
|
402 |
||
403 |
out.print(" }"); |
|
404 |
} |
|
405 |
} else { |
|
406 |
out.print(ScriptRuntime.safeToString(arg)); |
|
407 |
} |
|
408 |
} |
|
409 |
||
16185 | 410 |
private void tracePrint(final PrintWriter out, final String tag, final Object[] args, final Object result) { |
16147 | 411 |
//boolean isVoid = type().returnType() == void.class; |
412 |
out.print(Debug.id(this) + " TAG " + tag); |
|
413 |
out.print(getDescriptor().getName() + "("); |
|
414 |
||
415 |
if (args.length > 0) { |
|
16185 | 416 |
printObject(out, args[0]); |
16147 | 417 |
for (int i = 1; i < args.length; i++) { |
418 |
final Object arg = args[i]; |
|
419 |
out.print(", "); |
|
420 |
||
421 |
if (getNashornDescriptor().isTraceScope() || !(arg instanceof ScriptObject && ((ScriptObject)arg).isScope())) { |
|
16185 | 422 |
printObject(out, arg); |
16147 | 423 |
} else { |
424 |
out.print("SCOPE"); |
|
425 |
} |
|
426 |
} |
|
427 |
} |
|
428 |
||
429 |
out.print(")"); |
|
430 |
||
431 |
if (tag.equals("EXIT ")) { |
|
432 |
out.print(" --> "); |
|
16185 | 433 |
printObject(out, result); |
16147 | 434 |
} |
435 |
||
436 |
out.println(); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Trace event. Wrap an invocation with a return value |
|
441 |
* |
|
442 |
* @param mh invocation handle |
|
443 |
* @param args arguments to call |
|
444 |
* |
|
445 |
* @return return value from invocation |
|
446 |
* |
|
447 |
* @throws Throwable if invocation fails or throws exception/error |
|
448 |
*/ |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
449 |
@SuppressWarnings({"unused", "resource"}) |
16147 | 450 |
public Object traceObject(final MethodHandle mh, final Object... args) throws Throwable { |
16185 | 451 |
final PrintWriter out = Context.getCurrentErr(); |
452 |
tracePrint(out, "ENTER ", args, null); |
|
16147 | 453 |
final Object result = mh.invokeWithArguments(args); |
16185 | 454 |
tracePrint(out, "EXIT ", args, result); |
16147 | 455 |
|
456 |
return result; |
|
457 |
} |
|
458 |
||
459 |
/** |
|
460 |
* Trace event. Wrap an invocation that returns void |
|
461 |
* |
|
462 |
* @param mh invocation handle |
|
463 |
* @param args arguments to call |
|
464 |
* |
|
465 |
* @throws Throwable if invocation fails or throws exception/error |
|
466 |
*/ |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
467 |
@SuppressWarnings({"unused", "resource"}) |
16147 | 468 |
public void traceVoid(final MethodHandle mh, final Object... args) throws Throwable { |
16185 | 469 |
final PrintWriter out = Context.getCurrentErr(); |
470 |
tracePrint(out, "ENTER ", args, null); |
|
16147 | 471 |
mh.invokeWithArguments(args); |
16185 | 472 |
tracePrint(out, "EXIT ", args, null); |
16147 | 473 |
} |
474 |
||
475 |
/** |
|
476 |
* Tracer function that logs a callsite miss |
|
477 |
* |
|
478 |
* @param args arguments to function |
|
479 |
* |
|
480 |
* @throws Throwable if invocation failes or throws exception/error |
|
481 |
*/ |
|
482 |
@SuppressWarnings("unused") |
|
483 |
public void traceMiss(final Object... args) throws Throwable { |
|
16185 | 484 |
tracePrint(Context.getCurrentErr(), "MISS ", args, null); |
16147 | 485 |
} |
486 |
||
487 |
private static MethodHandle findOwnMH(final String name, final Class<?> rtype, final Class<?>... types) { |
|
488 |
try { |
|
489 |
return MH.findStatic(MethodHandles.lookup(), TracingLinkerCallSite.class, name, MH.type(rtype, types)); |
|
490 |
} catch (final MethodHandleFactory.LookupException e) { |
|
491 |
return MH.findVirtual(MethodHandles.lookup(), TracingLinkerCallSite.class, name, MH.type(rtype, types)); |
|
492 |
} |
|
493 |
} |
|
494 |
} |
|
495 |
||
496 |
// counters updated in debug mode |
|
497 |
private static int count; |
|
498 |
private static final HashMap<String, AtomicInteger> missCounts = new HashMap<>(); |
|
499 |
private static int missCount; |
|
500 |
private static final Random r = new Random(); |
|
501 |
private static final int missSamplingPercentage = Options.getIntProperty("nashorn.tcs.miss.samplePercent", 1); |
|
502 |
||
503 |
/** |
|
504 |
* Get the callsite count |
|
505 |
* @return the count |
|
506 |
*/ |
|
507 |
public static int getCount() { |
|
508 |
return count; |
|
509 |
} |
|
510 |
||
511 |
/** |
|
512 |
* Get the callsite miss count |
|
513 |
* @return the missCount |
|
514 |
*/ |
|
515 |
public static int getMissCount() { |
|
516 |
return missCount; |
|
517 |
} |
|
518 |
||
519 |
/** |
|
520 |
* Get given miss sampling percentage for sampler. Default is 1%. Specified with -Dnashorn.tcs.miss.samplePercent=x |
|
521 |
* @return miss sampling percentage |
|
522 |
*/ |
|
523 |
public static int getMissSamplingPercentage() { |
|
524 |
return missSamplingPercentage; |
|
525 |
} |
|
526 |
||
527 |
/** |
|
528 |
* Dump the miss counts collected so far to a given output stream |
|
529 |
* @param out print stream |
|
530 |
*/ |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
531 |
public static void getMissCounts(final PrintWriter out) { |
16147 | 532 |
final ArrayList<Entry<String, AtomicInteger>> entries = new ArrayList<>(missCounts.entrySet()); |
533 |
||
534 |
Collections.sort(entries, new Comparator<Map.Entry<String, AtomicInteger>>() { |
|
535 |
@Override |
|
536 |
public int compare(final Entry<String, AtomicInteger> o1, final Entry<String, AtomicInteger> o2) { |
|
537 |
return o2.getValue().get() - o1.getValue().get(); |
|
538 |
} |
|
539 |
}); |
|
540 |
||
541 |
for (final Entry<String, AtomicInteger> entry : entries) { |
|
542 |
out.println(" " + entry.getKey() + "\t" + entry.getValue().get()); |
|
543 |
} |
|
544 |
} |
|
545 |
||
546 |
} |