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.core.qos.metrics;
028:
029: import java.text.DecimalFormat;
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.Set;
033: import java.text.DecimalFormat;
034:
035: import org.cougaar.core.service.LoggingService;
036: import org.cougaar.core.component.ServiceBroker;
037: import org.cougaar.core.mts.MessageAddress;
038: import org.cougaar.core.plugin.ComponentPlugin;
039: import org.cougaar.core.service.ThreadService;
040: import org.cougaar.core.thread.Schedulable;
041: import org.cougaar.core.mts.AgentStatusService;
042:
043: /**
044: * The test Plugin will dump the time history of an Agent's resource
045: * load into the logging system.
046: */
047: public class AgentLoadTracePlugin extends ComponentPlugin implements
048: Constants {
049: private LoggingService loggingService = null;
050: private MetricsService metricsService;
051: private DecimalFormat formatter = new DecimalFormat("###,#00.0#");
052: private boolean first_time = true;
053: private ArrayList agents;
054: private long start;
055: private static final int BASE_PERIOD = 10; //10SecAVG
056: private AgentStatusService agentStatusService = null;
057:
058: private class Poller implements Runnable {
059: public void run() {
060: if (first_time) {
061:
062: collectNames();
063: // log data key
064: if (loggingService.isInfoEnabled()) {
065: loggingService.info("--- Data Key ---\n"
066: + "Timestamp, " + "AgentName, "
067: + "CPULoad, " + "CPULoadJips, " + "MsgIn, "
068: + "MsgOut, " + "bytesIn, " + "BytesOut, "
069: + "PersistSize");
070: }
071: } else
072: dump();
073: first_time = false;
074: }
075: }
076:
077: public AgentLoadTracePlugin() {
078: super ();
079: }
080:
081: private void collectNames() {
082: start = System.currentTimeMillis();
083: Set localAgents = getLocalAgents();
084: if (localAgents == null) {
085: return;
086: }
087: agents = new ArrayList();
088: for (Iterator itr = localAgents.iterator(); itr.hasNext();) {
089: MessageAddress addr = (MessageAddress) itr.next();
090: String name = addr.getAddress();
091: agents.add(name);
092: }
093: }
094:
095: private void logAgentMetrics(String name) {
096: if (name == null) {
097: loggingService
098: .debug("Agent is null, cannot retrieve any metrics!");
099: }
100: String agentPath = "Agent(" + name + ")" + PATH_SEPR;
101:
102: Metric cpuLoad = metricsService.getValue(agentPath
103: + CPU_LOAD_AVG + "(" + _10_SEC_AVG + ")");
104: double cpuLoadV = cpuLoad.doubleValue();
105:
106: Metric cpuLoadJips = metricsService.getValue(agentPath
107: + CPU_LOAD_MJIPS + "(" + _10_SEC_AVG + ")");
108: double cpuLoadJipsV = cpuLoadJips.doubleValue();
109:
110: Metric msgIn = metricsService.getValue(agentPath + MSG_IN + "("
111: + _10_SEC_AVG + ")");
112: double msgInV = msgIn.doubleValue();
113:
114: Metric msgOut = metricsService.getValue(agentPath + MSG_OUT
115: + "(" + _10_SEC_AVG + ")");
116: double msgOutV = msgOut.doubleValue();
117:
118: Metric bytesIn = metricsService.getValue(agentPath + BYTES_IN
119: + "(" + _10_SEC_AVG + ")");
120: double bytesInV = bytesIn.doubleValue();
121:
122: Metric bytesOut = metricsService.getValue(agentPath + BYTES_OUT
123: + "(" + _10_SEC_AVG + ")");
124: double bytesOutV = bytesOut.doubleValue();
125:
126: Metric persistSize = metricsService.getValue(agentPath
127: + PERSIST_SIZE_LAST);
128: double persistSizeV = persistSize.doubleValue();
129:
130: long now = System.currentTimeMillis();
131:
132: if (loggingService.isInfoEnabled()) {
133: loggingService.info(now + ", " + name + ","
134: + formatter.format(cpuLoadV) + ","
135: + formatter.format(cpuLoadJipsV) + ","
136: + formatter.format(msgInV) + ","
137: + formatter.format(msgOutV) + ","
138: + formatter.format(bytesInV) + ","
139: + formatter.format(bytesOutV) + ","
140: + formatter.format(persistSizeV));
141: }
142: }
143:
144: private long relativeTimeMillis() {
145: return System.currentTimeMillis() - start;
146: }
147:
148: private void dump() {
149: Iterator itr = agents.iterator();
150: while (itr.hasNext()) {
151: String name = (String) itr.next();
152: logAgentMetrics(name);
153: }
154: }
155:
156: public void load() {
157: super .load();
158:
159: ServiceBroker sb = getServiceBroker();
160:
161: metricsService = (MetricsService) sb.getService(this ,
162: MetricsService.class, null);
163:
164: loggingService = (LoggingService) sb.getService(this ,
165: LoggingService.class, null);
166:
167: agentStatusService = (AgentStatusService) sb.getService(this ,
168: AgentStatusService.class, null);
169:
170: ThreadService tsvc = (ThreadService) sb.getService(this ,
171: ThreadService.class, null);
172:
173: Poller poller = new Poller();
174: Schedulable sched = tsvc.getThread(this , poller, "LoadPoller");
175: //sched.schedule(60000, 500);
176: sched.schedule(5000, BASE_PERIOD * 1000);
177: sb.releaseService(this , ThreadService.class, tsvc);
178:
179: }
180:
181: /**
182: * @return the message addresses of the agents on this
183: * node, or null if that information is not available.
184: */
185: protected final Set getLocalAgents() {
186: // instead of asking the NodeControlService's AgentContainer for:
187: // agentContainer.getAgentAddresses()
188: // we try getting agents from agentstatus service
189: if (agentStatusService == null) {
190: if (loggingService.isDebugEnabled())
191: loggingService
192: .debug("No LocalAgents from AgentStatusService");
193: return null;
194: } else {
195: return agentStatusService.getLocalAgents();
196: }
197: }
198:
199: protected void setupSubscriptions() {
200: }
201:
202: protected void execute() {
203: }
204:
205: }
|