view src/org/dancres/blitz/SearchTasks.java @ 35:6f68e94c1fb8 default tip

Add CondensedStats monitoring utility, equivalent to vmstat
author Dominic Cleal <dominic-cleal@cdo2.com>
date Thu, 05 Aug 2010 11:07:25 +0100
parents 3dc0c5604566
children
line wrap: on
line source

package org.dancres.blitz;

import org.dancres.blitz.stats.BlockingOpsStat;
import org.dancres.blitz.stats.MissedOpsStat;
import org.dancres.blitz.stats.Stat;
import org.dancres.blitz.stats.StatGenerator;
import org.dancres.blitz.stats.StatsBoard;

import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicInteger;
import edu.emory.mathcs.backport.java.util.concurrent.atomic.AtomicLong;

/**
 * Tracks the number ouf outstanding blocked operations (take and read)
 */
class SearchTasks {
    private static SearchTasks theTasks = new SearchTasks();

    private AtomicInteger theTakeCount = new AtomicInteger();
    private AtomicInteger theReadCount = new AtomicInteger();
    private AtomicLong theMissedTakes = new AtomicLong();
    private AtomicLong theMissedReads = new AtomicLong();
    
    public static SearchTasks get() {
        return theTasks;
    }

    private SearchTasks() {
        StatsBoard.get().add(new BlockingStatGenerator());
        StatsBoard.get().add(new MissedStatGenerator());
    }

    public void add(MatchTask aTask) {
        if (aTask.getVisitor().isDeleter())
            theTakeCount.incrementAndGet();
        else
            theReadCount.incrementAndGet();
    }

    public void remove(MatchTask aTask, boolean didMiss) {
        if (aTask.getVisitor().isDeleter()) {
            theTakeCount.decrementAndGet();
            
            if (didMiss)
                theMissedTakes.incrementAndGet();
        } else {
            theReadCount.decrementAndGet();

            if (didMiss)
                theMissedReads.incrementAndGet();
        }
    }

    private class BlockingStatGenerator implements StatGenerator {
        private long theStatId = StatGenerator.UNSET_ID;

        public long getId() {
            return theStatId;
        }
        
        public void setId(long anId) {
            theStatId = anId;
        }
        
        public Stat generate() {
            return new BlockingOpsStat(
                    theStatId, theReadCount.intValue(), theTakeCount.intValue());
        }
    }

    private class MissedStatGenerator implements StatGenerator {
        private long theStatId = StatGenerator.UNSET_ID;

        public long getId() {
            return theStatId;
        }
        
        public void setId(long anId) {
            theStatId = anId;
        }
        
        public Stat generate() {
            return new MissedOpsStat(
                    theStatId, theMissedReads.longValue(), 
                    theMissedTakes.longValue());
        }
    }
}