KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > tools > profiler > ProfilerAdmin


1 /*
2  * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Sam
27  */

28
29
30 package com.caucho.tools.profiler;
31
32 import com.caucho.jmx.Jmx;
33 import com.caucho.util.L10N;
34
35 import javax.management.ObjectName JavaDoc;
36 import javax.management.openmbean.TabularData JavaDoc;
37 import java.util.logging.Level JavaDoc;
38 import java.util.logging.Logger JavaDoc;
39
40 public class ProfilerAdmin
41   implements ProfilerMBean
42 {
43   private static final L10N L = new L10N(ProfilerAdmin.class);
44   private static final Logger JavaDoc log = Logger.getLogger(ProfilerAdmin.class.getName());
45
46   private final ProfilerManager _profilerManager;
47   private final ObjectName JavaDoc _objectName;
48
49   ProfilerAdmin(ProfilerManager profilerManager)
50   {
51     _profilerManager = profilerManager;
52
53     ObjectName JavaDoc objectName;
54
55     try {
56       objectName = Jmx.getObjectName("type=Profiler");
57
58       objectName = new ObjectName JavaDoc(objectName.getCanonicalName());
59
60       Jmx.register(this, objectName);
61     }
62     catch (Throwable JavaDoc e) {
63       objectName = null;
64
65       log.log(Level.FINER, e.toString(), e);
66     }
67
68     _objectName = objectName;
69   }
70
71   public ObjectName JavaDoc getObjectName()
72   {
73     return _objectName;
74   }
75
76   public boolean isEnabled()
77   {
78     return _profilerManager.isEnabled();
79   }
80
81   public void enable()
82   {
83     _profilerManager.enable();
84   }
85
86   public void disable()
87   {
88     _profilerManager.disable();
89   }
90
91   public void reset()
92   {
93     _profilerManager.reset();
94   }
95
96   // Snapshot - jmx ugh.
97

98   public TabularData JavaDoc snapshot()
99   {
100     return null;
101   }
102
103   /**
104    *
105    * XXX: need to do aggreagateTime calculation
106   private static final CompositeType SNAPSHOT_COMPOSITE_TYPE;
107
108   private static final TabularType SNAPSHOT_TABULAR_TYPE;
109
110   private static final String[] SNAPSHOT_COMPOSITE_TYPE_ITEM_NAMES
111     = new String[]{L.l("Name"),
112                    L.l("Average Time"),
113                    L.l("Total Time"),
114                    L.l("Invocation Count")};
115
116   private static final String[] SNAPSHOT_COMPOSITE_TYPE_ITEM_DESCRIPTIONS
117     = new String[]{L.l("Name"),
118                    L.l("Average Time in nanoseconds"),
119                    L.l("Total Time in nanoseconds"),
120                    L.l("Invocation Count")};
121
122   private static final OpenType[] SNAPSHOT_COMPOSITE_TYPE_ITEM_TYPES
123     = new OpenType[]{SimpleType.STRING,
124                      SimpleType.LONG,
125                      SimpleType.LONG,
126                      SimpleType.LONG};
127
128   static {
129     try {
130       SNAPSHOT_COMPOSITE_TYPE = new CompositeType(
131         L.l("Profiler Snapshot Item"),
132         L.l(
133           "A snapshot of the current profiler statistics for a named profiler point."),
134         SNAPSHOT_COMPOSITE_TYPE_ITEM_NAMES,
135         SNAPSHOT_COMPOSITE_TYPE_ITEM_DESCRIPTIONS,
136         SNAPSHOT_COMPOSITE_TYPE_ITEM_TYPES
137       );
138
139       SNAPSHOT_TABULAR_TYPE = new TabularType(
140         L.l("Profiler Snapshot"),
141         L.l("A snapshot of the current profiler statistics."),
142         SNAPSHOT_COMPOSITE_TYPE,
143         new String[]{L.l("Name")});
144     }
145     catch (OpenDataException e) {
146       throw new AssertionError(e);
147     }
148   }
149
150   public TabularData snapshot()
151     throws Exception
152   {
153     ProfilerNodeComparator comparator = new TimeComparator();
154     comparator.setDescending(true);
155
156     TabularDataSupport tabularData = new TabularDataSupport(
157       SNAPSHOT_TABULAR_TYPE);
158
159     addChildren(tabularData, comparator, null, null);
160
161     return tabularData;
162   }
163
164   private void addChildren(TabularData tabularData,
165                            ProfilerNodeComparator comparator,
166                            ProfilerNode parent,
167                            String parentName)
168     throws Exception
169   {
170     for (ProfilerNode child : _profilerManager.getChildProfilerNodes(parent,
171                                                                      comparator)) {
172
173       if (log.isLoggable(Level.FINEST))
174         log.finest(L.l("jmx snapshot row for {0}", child));
175
176       String childName = parentName == null
177                          ? child.getName()
178                          : parentName + " --> " + child.getName();
179
180       Object[] compositeDataValues = new Object[]{
181         childName,
182         child.getAggregateAverageTime(),
183         child.getAggregateTime(),
184         child.getInvocationCount()
185       };
186
187       CompositeDataSupport compositeData = new CompositeDataSupport(
188         SNAPSHOT_COMPOSITE_TYPE,
189         SNAPSHOT_COMPOSITE_TYPE_ITEM_NAMES,
190         compositeDataValues);
191
192       tabularData.put(compositeData);
193
194       addChildren(tabularData, comparator, child, childName);
195     }
196   }
197    */

198
199 }
200
Popular Tags