package com.sun.solaris.domain.pools;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import com.sun.solaris.service.pools.Component;
import com.sun.solaris.service.pools.Resource;
import com.sun.solaris.service.pools.PoolsException;
import com.sun.solaris.service.pools.UnsignedInt64;
class ResourceMonitor extends HashMap
{
private Resource target;
private final int maxSize;
private LinkedList compList;
public ResourceMonitor()
{
this(null, 50);
}
public ResourceMonitor(Resource target, int maxSize)
{
super();
this.target = target;
this.maxSize = maxSize;
compList = new LinkedList();
}
public void initialize() throws PoolsException
{
compList.clear();
List candidates = target.getComponents(null);
Iterator candIt = candidates.iterator();
while (candIt.hasNext()) {
Component comp = (Component) candIt.next();
String status = comp.getStringProperty("cpu.status");
if (status.compareTo("off-line") != 0 &&
status.compareTo("powered-off") != 0)
compList.add(comp);
}
}
public List getComponents()
{
return ((List) compList.clone());
}
public int getMaxSampleSize()
{
return (maxSize);
}
public Resource getMonitored()
{
return (target);
}
public void setResource(Resource target)
{
if (this.target != null)
this.target = target;
else
throw new IllegalArgumentException("Once the target " +
"of a ResourceMonitor is set, it cannot be " +
"changed.");
}
public String getName() throws PoolsException
{
String type = target.getStringProperty("type");
return (target.getStringProperty(type + ".name"));
}
public void updateDerived()
{
StatisticList util = (StatisticList) get("utilization");
AggregateStatistic stat = calcDerivedStatistic("utilization");
if (stat != null)
util.add(stat);
}
public AggregateStatistic getDerivedStatistic(String name)
{
return ((AggregateStatistic)((StatisticList)get(name)).
getLast());
}
private AggregateStatistic calcDerivedStatistic(String name)
{
if (name.compareTo("utilization") != 0)
throw new IllegalArgumentException("No such derived "
+ "statistic: " + name);
StatisticList first = (StatisticList) get("idle");
switch (first.size()) {
case 0:
case 1:
return (null);
default:
BigInteger total = new BigInteger("0");
double utilV = 0.0;
double idleV = 0.0;
LinkedList keys = new LinkedList(keySet());
keys.remove("utilization");
for (int i = 0; i < keys.size(); i++) {
StatisticList sl = (StatisticList) get(keys.
get(i));
AggregateStatistic sv1 = (AggregateStatistic)
sl.getLast();
AggregateStatistic sv2 = (AggregateStatistic)
sl.get(sl.size() - 2);
if (sl.getName().compareTo("idle") == 0)
idleV = ((UnsignedInt64) sv1.
subtract(sv2).getValue()).
doubleValue();
total = total.add((UnsignedInt64) sv1.
subtract(sv2).getValue());
}
utilV = 100 * ((total.doubleValue() - idleV) /
total.doubleValue());
return (new DoubleStatistic(Double.valueOf(utilV),
((AggregateStatistic)first.get(first.size() -
2)).getStart(), ((AggregateStatistic)first.
getLast()).getEnd()));
}
}
void resetData(String name)
{
StatisticList sl = (StatisticList) get(name);
sl.clear();
}
}