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.io.FileWriter;
030: import java.io.PrintWriter;
031: import java.text.DecimalFormat;
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.Set;
035:
036: import org.cougaar.core.agent.AgentContainer;
037: import org.cougaar.core.component.ServiceBroker;
038: import org.cougaar.core.mts.MessageAddress;
039: import org.cougaar.core.node.NodeControlService;
040: import org.cougaar.core.node.NodeIdentificationService;
041: import org.cougaar.core.plugin.ComponentPlugin;
042: import org.cougaar.core.service.ThreadService;
043: import org.cougaar.core.thread.Schedulable;
044:
045: /**
046: * This Plugin periodically dumps Agent load data (1sec load average)
047: * for each local Agent to the file
048: * <code><node>-agent-data.log</code>, where <node> is the
049: * name of the Node.
050: */
051: public class AgentLoadLoggerPlugin extends ComponentPlugin implements
052: Constants {
053: private AgentContainer agentContainer;
054: private MetricsService metricsService;
055: private String node;
056: private String filename = "agent-data.log";
057: private PrintWriter out;
058: private DecimalFormat formatter = new DecimalFormat("00.00");
059: private boolean first_time = true;
060: private ArrayList agents;
061: private long start;
062:
063: private class Poller implements Runnable {
064: public void run() {
065: if (first_time)
066: collectNames();
067: else
068: dump();
069: first_time = false;
070: }
071: }
072:
073: public AgentLoadLoggerPlugin() {
074: super ();
075: }
076:
077: private void collectNames() {
078: start = System.currentTimeMillis();
079: Set localAgents = getLocalAgents();
080: if (localAgents == null)
081: return;
082:
083: agents = new ArrayList();
084: out.print("Time");
085: for (Iterator itr = localAgents.iterator(); itr.hasNext();) {
086: MessageAddress addr = (MessageAddress) itr.next();
087: String name = addr.getAddress();
088: out.print('\t');
089: out.print(name);
090: agents.add(name);
091: }
092: out.println("");
093: out.flush();
094: }
095:
096: private void dumpAgentData(String name) {
097: String path = "Agent(" + name + ")" + PATH_SEPR + CPU_LOAD_AVG
098: + "(" + _1_SEC_AVG + ")";
099: Metric metric = metricsService.getValue(path);
100: double value = metric.doubleValue();
101: String formattedValue = formatter.format(value);
102: out.print('\t');
103: out.print(formattedValue);
104: }
105:
106: private long relativeTimeMillis() {
107: return System.currentTimeMillis() - start;
108: }
109:
110: private void dump() {
111: out.print(relativeTimeMillis() / 1000.0);
112: Iterator itr = agents.iterator();
113: while (itr.hasNext()) {
114: String name = (String) itr.next();
115: dumpAgentData(name);
116: }
117: out.println("");
118: out.flush();
119: }
120:
121: public void load() {
122: super .load();
123:
124: ServiceBroker sb = getServiceBroker();
125:
126: NodeControlService ncs = (NodeControlService) sb.getService(
127: this , NodeControlService.class, null);
128: if (ncs != null) {
129: agentContainer = ncs.getRootContainer();
130: sb.releaseService(this , NodeControlService.class, ncs);
131: }
132:
133: metricsService = (MetricsService) sb.getService(this ,
134: MetricsService.class, null);
135:
136: NodeIdentificationService nis = (NodeIdentificationService) sb
137: .getService(this , NodeIdentificationService.class, null);
138: node = nis.getMessageAddress().toString();
139:
140: ThreadService tsvc = (ThreadService) sb.getService(this ,
141: ThreadService.class, null);
142:
143: filename = node + "-" + filename;
144:
145: try {
146: FileWriter fw = new FileWriter(filename);
147: out = new PrintWriter(fw);
148: } catch (java.io.IOException ex) {
149: ex.printStackTrace();
150: return;
151: }
152:
153: Poller poller = new Poller();
154: Schedulable sched = tsvc.getThread(this , poller, "LoadPoller");
155: sched.schedule(60000, 500);
156: sb.releaseService(this , ThreadService.class, tsvc);
157:
158: }
159:
160: /**
161: * @return the message addresses of the agents on this
162: * node, or null if that information is not available.
163: */
164: protected final Set getLocalAgents() {
165: if (agentContainer == null) {
166: return null;
167: } else {
168: return agentContainer.getAgentAddresses();
169: }
170: }
171:
172: protected void setupSubscriptions() {
173: }
174:
175: protected void execute() {
176: //System.out.println("Uninteresting");
177: }
178:
179: }
|