001: /*
002: * This file is part of PFIXCORE.
003: *
004: * PFIXCORE is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU Lesser General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * PFIXCORE is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public License
015: * along with PFIXCORE; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: *
018: */
019:
020: package de.schlund.pfixxml.targets.cachestat;
021:
022: import java.util.Timer;
023: import java.util.TimerTask;
024:
025: import org.apache.log4j.Logger;
026:
027: /**
028: * Provides a queue with QUEUE_SIZE entries which
029: * are of type CacheHitMissPair. It provides a
030: * pointer which points to the current element. The
031: * current element is used for recording cache hits and missses.
032: * After NEXT_TICK (in ms) the next entry in the queue is selected(
033: * if there was an old entry, it will be overridden).
034: *
035: * @author Joerg Haecker <haecker@schlund.de>
036: *
037: */
038:
039: public class AdvanceCacheStatistic {
040: private CacheHitMissPair[] statQueue;
041: private int index = 0;
042: private int queueSize = 0;
043: private final static Logger LOG = Logger
044: .getLogger(AdvanceCacheStatistic.class);
045:
046: public AdvanceCacheStatistic(Timer timer, int queuesize,
047: int queueticks) {
048: if (timer == null) {
049: throw new IllegalArgumentException(
050: "A NP passed as timer is not valid here.");
051: }
052: if (queuesize < 1) {
053: queuesize = 60;
054: //throw new IllegalArgumentException("queuesize must not be < 1");
055: }
056: if (queueticks < 1) {
057: queueticks = 60000;
058: //throw new IllegalArgumentException("queueticks must not be < 1");
059: }
060:
061: queueSize = queuesize;
062: statQueue = new CacheHitMissPair[queueSize];
063: for (int i = 0; i < queueSize; i++) {
064: statQueue[i] = new CacheHitMissPair();
065: }
066: timer.schedule(new AdvanceTask(), queueticks, queueticks);
067: }
068:
069: public long getHits() {
070: long hits = 0;
071: for (int i = 0; i < statQueue.length; i++) {
072: CacheHitMissPair pair = statQueue[i];
073: if (pair != null) {
074: hits += pair.getHits();
075: }
076: }
077: if (LOG.isDebugEnabled())
078: LOG.debug(this .hashCode() + "Hits: " + hits);
079: return hits;
080: }
081:
082: public long getMisses() {
083: long misses = 0;
084: for (int i = 0; i < statQueue.length; i++) {
085: CacheHitMissPair pair = statQueue[i];
086: if (pair != null) {
087: misses += pair.getMisses();
088: }
089: }
090: if (LOG.isDebugEnabled())
091: LOG.debug(this .hashCode() + " Misses: " + misses);
092: return misses;
093: }
094:
095: synchronized void registerHit() {
096: statQueue[index].increaseHits();
097: }
098:
099: synchronized void registerMiss() {
100: statQueue[index].increaseMisses();
101: }
102:
103: synchronized void advance() {
104: if (index == (queueSize - 1)) {
105: index = 0;
106: } else {
107: index++;
108: }
109: if (LOG.isDebugEnabled())
110: LOG.debug("--->Reset CacheHitMissPair at:" + index);
111: statQueue[index].resetHits();
112: statQueue[index].resetMisses();
113: }
114:
115: /**
116: * Timer task which is executed all NEXT_TICK ms.
117: */
118: class AdvanceTask extends TimerTask {
119:
120: public void run() {
121: advance();
122: }
123: }
124:
125: }
|