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.persist;
028:
029: import java.util.ArrayList;
030: import java.util.List;
031:
032: import org.cougaar.core.service.PersistenceMetricsService;
033:
034: /**
035: * {@link PersistenceMetricsService} implementation.
036: */
037: public class PersistenceMetricsServiceImpl implements
038: PersistenceMetricsService {
039:
040: private PersistenceMetricImpl fullAverageMetric = new PersistenceMetricImpl();
041: private PersistenceMetricImpl deltaAverageMetric = new PersistenceMetricImpl();
042: private PersistenceMetricImpl allAverageMetric = new PersistenceMetricImpl();
043: private int base = 0;
044: private int size = 0;
045: private Metric[] metrics = new Metric[MAX_METRICS];
046:
047: void addMetric(PersistenceMetricsService.Metric metric) {
048: synchronized (metrics) {
049: if (size >= metrics.length) {
050: metrics[base++] = metric;
051: if (base >= metrics.length)
052: base = 0;
053: } else {
054: metrics[size++] = metric;
055: }
056: allAverageMetric.average(metric);
057: if (metric.isFull()) {
058: fullAverageMetric.average(metric);
059: } else {
060: deltaAverageMetric.average(metric);
061: }
062: }
063: }
064:
065: /**
066: * Get all retained metrics. The maximum number retained is
067: * currently a constant MAX_METRICS
068: */
069: public Metric[] getAll(int which) {
070: synchronized (metrics) {
071: if (which == ALL) {
072: Metric[] result = new Metric[size];
073: System.arraycopy(metrics, base, result, 0, size - base);
074: System.arraycopy(metrics, 0, result, size - base, base);
075: return result;
076: } else {
077: List result = new ArrayList(size);
078: boolean wantFull = which == FULL;
079: for (int i = 0; i < size; i++) {
080: Metric metric = metrics[(base + i) % metrics.length];
081: if (metric.isFull() == wantFull) {
082: result.add(metric);
083: }
084: }
085: return (Metric[]) result.toArray(new Metric[result
086: .size()]);
087: }
088: }
089: }
090:
091: /**
092: * Get the average of all metrics ever generated including the ones
093: * that have been dropped due to exceeding MAX_METRICS
094: */
095: public Metric getAverage(int which) {
096: switch (which) {
097: case FULL:
098: return fullAverageMetric;
099: case DELTA:
100: return deltaAverageMetric;
101: case ALL:
102: return allAverageMetric;
103: }
104: return null;
105: }
106:
107: public int getCount(int which) {
108: switch (which) {
109: case FULL:
110: return fullAverageMetric.getCount();
111: case DELTA:
112: return deltaAverageMetric.getCount();
113: case ALL:
114: return allAverageMetric.getCount();
115: }
116: return 01;
117: }
118: }
|