001: /*
002: * Copyright (c) 1998-2005 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Sam
027: */
028:
029: package com.caucho.tools.profiler;
030:
031: import com.caucho.jmx.Jmx;
032: import com.caucho.util.L10N;
033:
034: import javax.management.ObjectName;
035: import javax.management.openmbean.TabularData;
036: import java.util.logging.Level;
037: import java.util.logging.Logger;
038:
039: public class ProfilerAdmin implements ProfilerMBean {
040: private static final L10N L = new L10N(ProfilerAdmin.class);
041: private static final Logger log = Logger
042: .getLogger(ProfilerAdmin.class.getName());
043:
044: private final ProfilerManager _profilerManager;
045: private final ObjectName _objectName;
046:
047: ProfilerAdmin(ProfilerManager profilerManager) {
048: _profilerManager = profilerManager;
049:
050: ObjectName objectName;
051:
052: try {
053: objectName = Jmx.getObjectName("type=Profiler");
054:
055: objectName = new ObjectName(objectName.getCanonicalName());
056:
057: Jmx.register(this , objectName);
058: } catch (Throwable e) {
059: objectName = null;
060:
061: log.log(Level.FINER, e.toString(), e);
062: }
063:
064: _objectName = objectName;
065: }
066:
067: public ObjectName getObjectName() {
068: return _objectName;
069: }
070:
071: public boolean isEnabled() {
072: return _profilerManager.isEnabled();
073: }
074:
075: public void enable() {
076: _profilerManager.enable();
077: }
078:
079: public void disable() {
080: _profilerManager.disable();
081: }
082:
083: public void reset() {
084: _profilerManager.reset();
085: }
086:
087: // Snapshot - jmx ugh.
088:
089: public TabularData snapshot() {
090: return null;
091: }
092:
093: /**
094: *
095: * XXX: need to do aggreagateTime calculation
096: private static final CompositeType SNAPSHOT_COMPOSITE_TYPE;
097:
098: private static final TabularType SNAPSHOT_TABULAR_TYPE;
099:
100: private static final String[] SNAPSHOT_COMPOSITE_TYPE_ITEM_NAMES
101: = new String[]{L.l("Name"),
102: L.l("Average Time"),
103: L.l("Total Time"),
104: L.l("Invocation Count")};
105:
106: private static final String[] SNAPSHOT_COMPOSITE_TYPE_ITEM_DESCRIPTIONS
107: = new String[]{L.l("Name"),
108: L.l("Average Time in nanoseconds"),
109: L.l("Total Time in nanoseconds"),
110: L.l("Invocation Count")};
111:
112: private static final OpenType[] SNAPSHOT_COMPOSITE_TYPE_ITEM_TYPES
113: = new OpenType[]{SimpleType.STRING,
114: SimpleType.LONG,
115: SimpleType.LONG,
116: SimpleType.LONG};
117:
118: static {
119: try {
120: SNAPSHOT_COMPOSITE_TYPE = new CompositeType(
121: L.l("Profiler Snapshot Item"),
122: L.l(
123: "A snapshot of the current profiler statistics for a named profiler point."),
124: SNAPSHOT_COMPOSITE_TYPE_ITEM_NAMES,
125: SNAPSHOT_COMPOSITE_TYPE_ITEM_DESCRIPTIONS,
126: SNAPSHOT_COMPOSITE_TYPE_ITEM_TYPES
127: );
128:
129: SNAPSHOT_TABULAR_TYPE = new TabularType(
130: L.l("Profiler Snapshot"),
131: L.l("A snapshot of the current profiler statistics."),
132: SNAPSHOT_COMPOSITE_TYPE,
133: new String[]{L.l("Name")});
134: }
135: catch (OpenDataException e) {
136: throw new AssertionError(e);
137: }
138: }
139:
140: public TabularData snapshot()
141: throws Exception
142: {
143: ProfilerNodeComparator comparator = new TimeComparator();
144: comparator.setDescending(true);
145:
146: TabularDataSupport tabularData = new TabularDataSupport(
147: SNAPSHOT_TABULAR_TYPE);
148:
149: addChildren(tabularData, comparator, null, null);
150:
151: return tabularData;
152: }
153:
154: private void addChildren(TabularData tabularData,
155: ProfilerNodeComparator comparator,
156: ProfilerNode parent,
157: String parentName)
158: throws Exception
159: {
160: for (ProfilerNode child : _profilerManager.getChildProfilerNodes(parent,
161: comparator)) {
162:
163: if (log.isLoggable(Level.FINEST))
164: log.finest(L.l("jmx snapshot row for {0}", child));
165:
166: String childName = parentName == null
167: ? child.getName()
168: : parentName + " --> " + child.getName();
169:
170: Object[] compositeDataValues = new Object[]{
171: childName,
172: child.getAggregateAverageTime(),
173: child.getAggregateTime(),
174: child.getInvocationCount()
175: };
176:
177: CompositeDataSupport compositeData = new CompositeDataSupport(
178: SNAPSHOT_COMPOSITE_TYPE,
179: SNAPSHOT_COMPOSITE_TYPE_ITEM_NAMES,
180: compositeDataValues);
181:
182: tabularData.put(compositeData);
183:
184: addChildren(tabularData, comparator, child, childName);
185: }
186: }
187: */
188:
189: }
|