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.thread;
028:
029: import java.util.Collection;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.List;
033: import java.util.Map;
034: import java.util.RandomAccess;
035:
036: import org.cougaar.core.component.ServiceBroker;
037: import org.cougaar.core.plugin.ComponentPlugin;
038: import org.cougaar.core.qos.metrics.Constants;
039: import org.cougaar.core.qos.metrics.DecayingHistory;
040: import org.cougaar.core.qos.metrics.Metric;
041: import org.cougaar.core.qos.metrics.MetricImpl;
042: import org.cougaar.core.qos.metrics.MetricsUpdateService;
043: import org.cougaar.core.service.ThreadService;
044:
045: /**
046: * This Plugin collects the load history for the Agent in which it's
047: * loaded, and uplaads that data to the metrics service. It
048: * uses the {@link AgentLoadService} to collect the raw date and should
049: * be loaded at LOW priority to ensure that the service is available.
050: *
051: * In the <a
052: * href="../../../../../OnlineManual/MetricsService/sensors.html">Sensor
053: * Data Flow</a> pattern this class plays the role of <b>Rate
054: * Converter</b> for load data (CPU load average, CPU) for Agents,
055: * Nodes and Services.
056: *
057: * @see AgentLoadSensorPlugin
058: * @see org.cougaar.core.qos.metrics.AgentLoadServlet
059: */
060: public class AgentLoadRatePlugin extends ComponentPlugin implements
061: Runnable, Constants {
062: private static final int BASE_PERIOD = 10; //10SecAVG
063:
064: private class AgentLoadHistory extends DecayingHistory {
065: private static final double CREDIBILITY = SECOND_MEAS_CREDIBILITY;
066:
067: String agentKey;
068: String mjipsKey;
069: String loadavgKey;
070:
071: AgentLoadHistory(String name) {
072: super (10, 3, BASE_PERIOD);
073: if (name.startsWith("Service"))
074: agentKey = name;
075: else if (name.startsWith("NodeTotal_"))
076: agentKey = "Node" + KEY_SEPR + name.substring(10);
077: else
078: agentKey = "Agent" + KEY_SEPR + name;
079: mjipsKey = (agentKey + KEY_SEPR + CPU_LOAD_MJIPS).intern();
080: addKey(mjipsKey);
081: loadavgKey = (agentKey + KEY_SEPR + CPU_LOAD_AVG).intern();
082: addKey(loadavgKey);
083: }
084:
085: public void newAddition(KeyMap keys,
086: DecayingHistory.SnapShot now_raw,
087: DecayingHistory.SnapShot last_raw) {
088: AgentLoadService.AgentLoad now = (AgentLoadService.AgentLoad) now_raw;
089: AgentLoadService.AgentLoad last = (AgentLoadService.AgentLoad) last_raw;
090: double deltaT = now.timestamp - last.timestamp;
091: double deltaLoad = now.loadAvgIntegrator
092: - last.loadAvgIntegrator;
093: double deltaMJips = now.loadMjipsIntegrator
094: - last.loadMjipsIntegrator;
095:
096: //Must match the Metrics Constants for CPU_LOAD_AVG_1XXX_SEC_AVG
097: String lKey = keys.getKey(loadavgKey);
098: Metric lData = new MetricImpl(
099: new Double(deltaLoad / deltaT), CREDIBILITY,
100: "threads/sec", "AgentLoadSensor");
101: metricsUpdateService.updateValue(lKey, lData);
102:
103: //Must match the Metrics Constants for CPU_LOAD_MJIPS_1XXX_SEC_AVG
104: String mKey = keys.getKey(mjipsKey);
105: Metric mData = new MetricImpl(new Double(deltaMJips
106: / deltaT), CREDIBILITY, "mjips", "AgentLoadSensor");
107: metricsUpdateService.updateValue(mKey, mData);
108: }
109:
110: }
111:
112: private AgentLoadService agentLoadService;
113: private MetricsUpdateService metricsUpdateService;
114: private Schedulable schedulable;
115: private Map<String, AgentLoadHistory> histories;
116:
117: public AgentLoadRatePlugin() {
118: histories = new HashMap<String, AgentLoadHistory>();
119: }
120:
121: // Local
122: AgentLoadHistory findOrMakeHistory(String agent) {
123: AgentLoadHistory history = histories.get(agent);
124: if (history == null) {
125: history = new AgentLoadHistory(agent);
126: histories.put(agent, history);
127: }
128: return history;
129: }
130:
131: // Component
132: public void load() {
133: super .load();
134:
135: ServiceBroker sb = getServiceBroker();
136: agentLoadService = (AgentLoadService) sb.getService(this ,
137: AgentLoadService.class, null);
138: if (agentLoadService == null) {
139: throw new RuntimeException(
140: "Can't find AgentLoadService. This plugin must be loaded at Low priority");
141: }
142:
143: metricsUpdateService = (MetricsUpdateService) sb.getService(
144: this , MetricsUpdateService.class, null);
145:
146: ThreadService tsvc = (ThreadService) sb.getService(this ,
147: ThreadService.class, null);
148: schedulable = tsvc.getThread(this , this , "AgentLoadRate");
149: schedulable.schedule(5000, BASE_PERIOD * 1000);
150: sb.releaseService(this , ThreadService.class, tsvc);
151: }
152:
153: // Schedulable body
154: public void run() {
155: Collection agentLoadSnapshot = agentLoadService
156: .snapshotRecords();
157: boolean useItr;
158: Iterator itr;
159: List l;
160: if (agentLoadSnapshot instanceof List
161: && agentLoadSnapshot instanceof RandomAccess) {
162: useItr = false;
163: itr = null;
164: l = (List) agentLoadSnapshot;
165: } else {
166: useItr = true;
167: itr = agentLoadSnapshot.iterator();
168: l = null;
169: }
170: for (int i = 0, n = agentLoadSnapshot.size(); i < n; i++) {
171: AgentLoadService.AgentLoad record = (AgentLoadService.AgentLoad) (useItr ? itr
172: .next()
173: : l.get(i));
174: String agent = record.name;
175: AgentLoadHistory history = findOrMakeHistory(agent);
176: history.add(record);
177: }
178: }
179:
180: // Plugin
181: protected void setupSubscriptions() {
182: }
183:
184: protected void execute() {
185: }
186:
187: }
|