001: /*
002: * <copyright>
003: *
004: * Copyright 2002-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.adaptivity;
028:
029: import org.cougaar.core.component.ServiceBroker;
030: import org.cougaar.core.persist.NotPersistable;
031: import org.cougaar.core.plugin.ServiceUserPlugin;
032: import org.cougaar.core.qos.metrics.Metric;
033: import org.cougaar.core.qos.metrics.MetricsService;
034: import org.cougaar.core.service.ConditionService;
035:
036: public class MetricsTestPlugin extends ServiceUserPlugin {
037: public static final String JIPS_CONDITION_NAME = "MetricsTestPlugin.JIPS";
038:
039: private static final OMCRange[] JIPS_RANGES = { new OMCRange(0.0,
040: Double.MAX_VALUE) };
041:
042: private static final OMCRangeList JIPS_VALUES = new OMCRangeList(
043: JIPS_RANGES);
044:
045: private ConditionService conditionService;
046:
047: private MetricsService metricsService;
048:
049: /**
050: * Private inner class precludes use by others to set our
051: * measurement. Others can only reference the base Condition
052: * class which has no setter method.
053: **/
054: private static class MetricsTestCondition extends SensorCondition
055: implements NotPersistable {
056: public MetricsTestCondition(String name,
057: OMCRangeList allowedValues, Comparable value) {
058: super (name, allowedValues, value);
059: }
060:
061: public void setValue(Comparable newValue) {
062: super .setValue(newValue);
063: }
064: }
065:
066: private static final Class[] requiredServices = {
067: ConditionService.class, MetricsService.class };
068:
069: public MetricsTestPlugin() {
070: super (requiredServices);
071: }
072:
073: public void setupSubscriptions() {
074: MetricsTestCondition jips = new MetricsTestCondition(
075: JIPS_CONDITION_NAME, JIPS_VALUES, new Double(1.0));
076: getBlackboardService().publishAdd(jips);
077: if (haveServices())
078: setMetricsConditions();
079: }
080:
081: private boolean haveServices() {
082: if (conditionService != null)
083: return true;
084: if (acquireServices()) {
085: ServiceBroker sb = getServiceBroker();
086: conditionService = (ConditionService) sb.getService(this ,
087: ConditionService.class, null);
088: metricsService = (MetricsService) sb.getService(this ,
089: MetricsService.class, null);
090: return true;
091: }
092: return false;
093: }
094:
095: public void execute() {
096: if (timerExpired()) {
097: if (haveServices()) {
098: cancelTimer();
099: setMetricsConditions();
100: }
101: }
102: }
103:
104: private void setMetricsConditions() {
105: logger.info("setMetricsConditions");
106: // raw CPU capacity
107: Metric metric = metricsService.getValue("Agent(Provider):Jips");
108: // Includes effects of load average, but different units
109: // Metric metric = svc.getValue("Agent(3ID):EffectiveMJips");
110: MetricsTestCondition jips = (MetricsTestCondition) conditionService
111: .getConditionByName(JIPS_CONDITION_NAME);
112: if (metric != null) {
113: if (jips != null) {
114: Double value = new Double(metric.doubleValue());
115: if (logger.isInfoEnabled())
116: logger.info("Setting jips = " + value);
117: jips.setValue(value);
118: getBlackboardService().publishChange(jips);
119: } else {
120: logger.warn("jips is null");
121: }
122: } else {
123: logger.warn("metric is null");
124: }
125: resetTimer(10000);
126: }
127: }
|