001: /*
002: * <copyright>
003: *
004: * Copyright 1997-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026:
027: package org.cougaar.planning.examples;
028:
029: import javax.swing.*;
030: import java.awt.event.*;
031: import java.awt.LayoutManager;
032: import java.awt.BorderLayout;
033:
034: import org.cougaar.core.plugin.ComponentPlugin;
035:
036: import org.cougaar.core.service.BlackboardMetricsService;
037:
038: import org.cougaar.core.service.MessageStatisticsService;
039: import org.cougaar.core.service.MessageWatcherService;
040: import org.cougaar.core.mts.MessageTransportWatcher;
041:
042: import org.cougaar.core.service.NodeMetricsService;
043: import org.cougaar.core.mts.MessageStatistics.Statistics;
044: import org.cougaar.core.mts.MessageAddress;
045: import org.cougaar.core.mts.Message;
046:
047: import org.cougaar.planning.service.PrototypeRegistryService;
048: import org.cougaar.planning.ldm.asset.Asset;
049: import org.cougaar.planning.ldm.plan.Notification;
050: import org.cougaar.planning.ldm.plan.PlanElement;
051: import org.cougaar.planning.ldm.plan.Task;
052: import org.cougaar.core.blackboard.Directive;
053:
054: import org.cougaar.core.blackboard.DirectiveMessage;
055:
056: import org.cougaar.core.component.ServiceRevokedListener;
057: import org.cougaar.core.component.ServiceRevokedEvent;
058:
059: public class MetricsComponentPlugin extends ComponentPlugin {
060: /** frame for 1-button UI **/
061: private JFrame frame;
062: JLabel metricsLabel;
063: protected JButton metricsButton;
064:
065: private PrototypeRegistryService protoRegistryService = null;
066: private int cachedProtoCount = 0;
067: private int propProviderCount = 0;
068: private int protoProviderCount = 0;
069: private BlackboardMetricsService bbMetricsService = null;
070: private int assetCount = 0;
071: private int planElementCount = 0;
072: private int taskCount = 0;
073: private int totalBlackboardCount = 0;
074: private MessageStatisticsService messageStatsService = null;
075: private MessageWatcherService messageWatchService = null;
076: private NodeMetricsService nodeMetricsService = null;
077:
078: public MetricsComponentPlugin() {
079: }
080:
081: protected void setupSubscriptions() {
082: createGUI();
083: protoRegistryService = (PrototypeRegistryService) getServiceBroker()
084: .getService(this , PrototypeRegistryService.class,
085: new ServiceRevokedListener() {
086: public void serviceRevoked(
087: ServiceRevokedEvent re) {
088: if (PrototypeRegistryService.class
089: .equals(re.getService()))
090: protoRegistryService = null;
091: }
092: });
093: bbMetricsService = (BlackboardMetricsService) getServiceBroker()
094: .getService(this , BlackboardMetricsService.class,
095: new ServiceRevokedListener() {
096: public void serviceRevoked(
097: ServiceRevokedEvent re) {
098: if (BlackboardMetricsService.class
099: .equals(re.getService())) {
100: bbMetricsService = null;
101: }
102: }
103: });
104: nodeMetricsService = (NodeMetricsService) getServiceBroker()
105: .getService(this , NodeMetricsService.class,
106: new ServiceRevokedListener() {
107: public void serviceRevoked(
108: ServiceRevokedEvent re) {
109: if (NodeMetricsService.class.equals(re
110: .getService())) {
111: nodeMetricsService = null;
112: }
113: }
114: });
115: messageStatsService = (MessageStatisticsService) getServiceBroker()
116: .getService(this , MessageStatisticsService.class,
117: new ServiceRevokedListener() {
118: public void serviceRevoked(
119: ServiceRevokedEvent re) {
120: if (MessageStatisticsService.class
121: .equals(re.getService())) {
122: messageStatsService = null;
123: }
124: }
125: });
126: messageWatchService = (MessageWatcherService) getServiceBroker()
127: .getService(this , MessageWatcherService.class,
128: new ServiceRevokedListener() {
129: public void serviceRevoked(
130: ServiceRevokedEvent re) {
131: if (MessageWatcherService.class
132: .equals(re.getService()))
133: messageWatchService = null;
134: }
135: });
136: messageWatchService
137: .addMessageTransportWatcher(_messageWatcher = new MessageWatcher());
138: }
139:
140: private void createGUI() {
141: frame = new JFrame("MetricsComponentPlugin");
142: // JPanel panel = new JPanel((LayoutManager) null);
143:
144: JPanel panel = new JPanel(new BorderLayout());
145: // Create the button
146: metricsButton = new JButton("Get Metrics");
147: metricsLabel = new JLabel("Press to Retrieve Metrics.");
148: metricsLabel.setHorizontalAlignment(JLabel.RIGHT);
149:
150: // Register a listener for the check box
151: MetricsButtonListener myMetricsListener = new MetricsButtonListener();
152: metricsButton.addActionListener(myMetricsListener);
153: metricsButton.setEnabled(true);
154:
155: panel.add(metricsButton, BorderLayout.WEST);
156: panel.add(metricsLabel, BorderLayout.EAST);
157: // UICoordinator.layoutButtonAndLabel(panel, metricsButton, metricsLabel);
158: frame.setContentPane(panel);
159: frame.pack();
160: // UICoordinator.setBounds(frame);
161: frame.setVisible(true);
162: }
163:
164: /** An ActionListener that listens to the GLS buttons. */
165: class MetricsButtonListener implements ActionListener {
166: public void actionPerformed(ActionEvent ae) {
167: getAllMetrics();
168: }
169: }
170:
171: /**
172: * Do nothing
173: */
174: public void execute() {
175: }
176:
177: public void getAllMetrics() {
178:
179: //get all PrototypeRegistryService metrics
180: cachedProtoCount = protoRegistryService
181: .getCachedPrototypeCount();
182: propProviderCount = protoRegistryService
183: .getPropertyProviderCount();
184: protoProviderCount = protoRegistryService
185: .getPrototypeProviderCount();
186: System.out.println("\n");
187: System.out.println("Cached Prototype Count: "
188: + cachedProtoCount);
189: System.out.println("Property Provider Count: "
190: + propProviderCount);
191: System.out.println("Prototype Provider Count: "
192: + protoProviderCount);
193:
194: //get all BlackBoardMetricsServices metrics
195: assetCount = bbMetricsService.getBlackboardCount(Asset.class);
196: planElementCount = bbMetricsService
197: .getBlackboardCount(PlanElement.class);
198: taskCount = bbMetricsService.getBlackboardCount(Task.class);
199: totalBlackboardCount = bbMetricsService.getBlackboardCount();
200: System.out.println("Asset Count: " + assetCount);
201: System.out.println("Plan Element Count: " + planElementCount);
202: System.out.println("Task Count: " + taskCount);
203: System.out.println("Total Blackboard Object Count: "
204: + totalBlackboardCount);
205:
206: //get all NodeMetricsSerivices metrics
207: System.out.println("Active Thread Count: "
208: + nodeMetricsService.getActiveThreadCount());
209: System.out.println("Free Memory: "
210: + nodeMetricsService.getFreeMemory());
211: System.out.println("Total Memory: "
212: + nodeMetricsService.getTotalMemory());
213:
214: //get all MessageStatistics metrics
215: if (messageStatsService != null) {
216: System.out
217: .println("Message Queue: "
218: + messageStatsService
219: .getMessageStatistics(false).averageMessageQueueLength);
220: System.out
221: .println("Message Bytes: "
222: + messageStatsService
223: .getMessageStatistics(false).totalSentMessageBytes);
224: System.out
225: .println("Message Count: "
226: + messageStatsService
227: .getMessageStatistics(false).totalSentMessageCount);
228: System.out
229: .println("Histogram: "
230: + messageStatsService
231: .getMessageStatistics(false).histogram);
232: } else
233: System.out.println("MessageStatisticsService not returned");
234:
235: //get all MessageWatcher metrics
236: System.out.println("Directives In: "
237: + _messageWatcher.getDirectivesIn());
238: System.out.println("Directives Out: "
239: + _messageWatcher.getDirectivesOut());
240: System.out.println("Notifications In: "
241: + _messageWatcher.getNotificationsIn());
242: System.out.println("Notifications Out: "
243: + _messageWatcher.getNotificationsOut());
244:
245: } //close method getAllMetrics()
246:
247: protected MessageWatcher _messageWatcher = null;
248:
249: class MessageWatcher implements MessageTransportWatcher {
250:
251: MessageAddress me;
252: private int directivesIn = 0;
253: private int directivesOut = 0;
254: private int notificationsIn = 0;
255: private int notificationsOut = 0;
256:
257: public MessageWatcher() {
258: me = getAgentIdentifier();
259: }
260:
261: public void messageSent(Message m) {
262: if (m.getOriginator().equals(me)) {
263: if (m instanceof DirectiveMessage) {
264: Directive[] directives = ((DirectiveMessage) m)
265: .getDirectives();
266: for (int i = 0; i < directives.length; i++) {
267: if (directives[i] instanceof Notification)
268: notificationsOut++;
269: else
270: directivesOut++;
271: }
272: }
273: }
274: } // close messageSent
275:
276: public void messageReceived(Message m) {
277: if (m.getTarget().equals(me)) {
278: if (m instanceof DirectiveMessage) {
279: Directive[] directives = ((DirectiveMessage) m)
280: .getDirectives();
281: for (int i = 0; i < directives.length; i++) {
282: if (directives[i] instanceof Notification)
283: notificationsIn++;
284: else
285: directivesIn++;
286: }
287: }
288: }
289: } // close messageReceived
290:
291: public int getDirectivesIn() {
292: return directivesIn;
293: }
294:
295: public int getDirectivesOut() {
296: return directivesOut;
297: }
298:
299: public int getNotificationsIn() {
300: return notificationsIn;
301: }
302:
303: public int getNotificationsOut() {
304: return notificationsOut;
305: }
306: } // end of MessageWatcher
307:
308: } // end of MetricsComponentPlugin.java
|