/* Web Polygraph       http://www.web-polygraph.org/
 * (C) 2003-2006 The Measurement Factory
 * Licensed under the Apache License, Version 2.0 */

#ifndef POLYGRAPH__RUNTIME_STATPHASE_H
#define POLYGRAPH__RUNTIME_STATPHASE_H

#include "xstd/Array.h"
#include "base/ErrorStat.h"
#include "base/StatPhaseRec.h"
#include "runtime/Goal.h"
#include "runtime/TransFactor.h"
#include "runtime/StatIntvl.h"

class StatPhaseMgr;
class StatsSampleCfg;
class PhaseSym;
class StatsSampleSym;
class Rptmstat;
class DutWatchdog;
class Script;

// stats phase is a named [long] interval during which
// stats are accumulated; the phase goal may not be [just] time based
// the stats are logged when instructed by the manager
// when phase goal is reached stat phase terminates notifying the manager

class StatPhase: public StatIntvl, public GoalSubj {
	public:
		StatPhase();
		virtual ~StatPhase();

		void configure(const PhaseSym *ps, const StatPhase *prevPh);
		void addWatchdog(DutWatchdog *dog);

		const String &name() const { return theName; }
		bool used() const { return wasUsed; }
		bool unlockToStop() const { return readyToStop; }
		const GoalRec &goal() const { return theGoal; }
		const TransFactor &populusFactor() const { return thePopulusFactor; }
		const TransFactor &loadFactor() const { return theLoadFactor; }
		const TransFactor &recurFactor() const { return theRecurFactor; }
		const TransFactor &specialMsgFactor() const { return theSpecialMsgFactor; }

		// finalize GoalSubj interface
		virtual Time duration() const;
		virtual int xactCnt() const;
		virtual BigSize fillSz() const;
		virtual int fillCnt() const;
		virtual int xactErrCnt() const;

		TransFactor &populusFactor() { return thePopulusFactor; }
		TransFactor &loadFactor() { return theLoadFactor; }
		OidGenStat &oidGenStat();
		ErrorStat &errors(int logCat);

		void statsLogged(bool are);
		void name(const String &aName);

		void lock();
		void unlock();

		void start(StatPhaseMgr *aMgr, const StatPhase *prevPhase);
		void flush();

		virtual void wakeUp(const Alarm &alarm);

		virtual void noteConnEvent(BcastChannel *ch, const Connection *c);
		virtual void noteXactEvent(BcastChannel *ch, const Xaction *x);
		virtual void noteIcpXactEvent(BcastChannel *ch, const IcpXaction *x);
		virtual void notePageEvent(BcastChannel *ch, const PageInfo *p);
		virtual void noteInfoEvent(BcastChannel *ch, InfoEvent ev);

		// should these be replaced with BcastChannels
		void noteSockRead(Size sz, int cat) { theRecs[cat]->theSockRdSzH.record(sz); }
		void noteSockWrite(Size sz, int cat) { theRecs[cat]->theSockWrSzH.record(sz); }

		virtual StatIntvlRec &getRec(int cat);
		virtual const StatIntvlRec &getRec(int cat) const;

		void reportCfg(ostream &os) const;

	protected:
		void configureSamples(const PhaseSym *cfg);
		void checkFactor(const TransFactor &f, const String &label) const;

		virtual bool checkpoint();

		bool locked() const;
		void stop();
		void report() const;

	protected:
		Array<StatPhaseRec*> theRecs;
		Array<StatsSampleCfg*> theSamples;

		StatPhaseMgr *theMgr;
		String theName;
		Goal theGoal;          // configurable GoalRec with reached()
		Rptmstat *theRptmstat;
		Script *theScript;
		Array<DutWatchdog*> theDutWatchdogs;

		TransFactor thePopulusFactor;
		TransFactor theLoadFactor;
		TransFactor theRecurFactor;
		TransFactor theSpecialMsgFactor;

		bool wasUsed;          // was listening for events
		bool logStats;         // log stats when flushed
		bool waitWssFreeze;    // wait for working sset to freeze
		bool doSynchronize;    // sync with remote phases
		bool readyToStop;      // locked after the was met
		bool wasStopped;       // stop() was called

		int theLockCount;
};


#endif


syntax highlighted by Code2HTML, v. 0.9.1