comparison graal/Runtime/src/com/sun/hotspot/c1x/logging/CountingProxy.java @ 2297:099e697d8934

Renaming c1x4hotspotsrc => graal and HotSpotVM => Runtime
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Fri, 22 Apr 2011 15:08:53 +0200
parents
children
comparison
equal deleted inserted replaced
2296:34354e2e40a3 2297:099e697d8934
1 /*
2 * Copyright (c) 2010 Sun Microsystems, Inc. All rights reserved.
3 *
4 * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product
5 * that is described in this document. In particular, and without limitation, these intellectual property
6 * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or
7 * more additional patents or pending patent applications in the U.S. and in other countries.
8 *
9 * U.S. Government Rights - Commercial software. Government users are subject to the Sun
10 * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its
11 * supplements.
12 *
13 * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or
14 * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks
15 * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the
16 * U.S. and other countries.
17 *
18 * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open
19 * Company, Ltd.
20 */
21 package com.sun.hotspot.c1x.logging;
22
23 import java.lang.reflect.*;
24 import java.util.*;
25
26 import com.sun.hotspot.c1x.server.*;
27
28 /**
29 * A java.lang.reflect proxy that hierarchically logs all method invocations along with their parameters and return
30 * values.
31 *
32 * @author Lukas Stadler
33 */
34 public class CountingProxy<T> implements InvocationHandler {
35
36 public static final boolean ENABLED = Boolean.valueOf(System.getProperty("c1x.countcalls"));
37
38 private T delegate;
39
40 private Map<Method, Long> calls = new HashMap<Method, Long>();
41
42 public CountingProxy(T delegate) {
43 assert ENABLED;
44 System.out.println("Counting proxy for " + delegate.getClass().getSimpleName() + " created");
45 this.delegate = delegate;
46 proxies.add(this);
47 }
48
49 @Override
50 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
51 int argCount = args == null ? 0 : args.length;
52 if (method.getParameterTypes().length != argCount) {
53 throw new RuntimeException("wrong parameter count");
54 }
55 final Object result;
56 long count = calls.containsKey(method) ? calls.get(method) : 0;
57 calls.put(method, count + 1);
58 try {
59 if (args == null) {
60 result = method.invoke(delegate);
61 } else {
62 result = method.invoke(delegate, args);
63 }
64 } catch (InvocationTargetException e) {
65 throw e.getCause();
66 }
67 return result;
68 }
69
70 public static <T> T getProxy(Class<T> interf, T delegate) {
71 Class<?>[] interfaces = ReplacingStreams.getAllInterfaces(delegate.getClass());
72 Object obj = Proxy.newProxyInstance(interf.getClassLoader(), interfaces, new CountingProxy<T>(delegate));
73 return interf.cast(obj);
74 }
75
76 private static ArrayList<CountingProxy> proxies = new ArrayList<CountingProxy>();
77
78 static {
79 if (ENABLED) {
80 Runtime.getRuntime().addShutdownHook(new Thread() {
81
82 @Override
83 public void run() {
84 for (CountingProxy proxy : proxies) {
85 proxy.print();
86 }
87 }
88 });
89 }
90 }
91
92 protected void print() {
93 long sum = 0;
94 for (Map.Entry<Method, Long> entry : calls.entrySet()) {
95 Method method = entry.getKey();
96 long count = entry.getValue();
97 sum += count;
98 System.out.println(delegate.getClass().getSimpleName() + "." + method.getName() + ": " + count);
99 }
100 System.out.println(delegate.getClass().getSimpleName() + " calls: " + sum);
101 }
102 }