17 #ifndef INC_KY_Kernel_Stats_H
18 #define INC_KY_Kernel_Stats_H
45 StatGroup_Default = 0,
48 Stat_Mem = StatGroup_Default + 1,
49 Stat_Default_Mem = Stat_Mem + 1,
59 StatHeap_Start = StatGroup_Core,
64 Stat_EntryCount = 512,
72 HeapId_Global = HeapId_Default,
115 StatValue() : Type(VT_None), pName(
""), IValue(0)
117 StatValue(
const StatValue& src)
118 : Type(src.Type), pName(src.pName), I64Value(src.I64Value)
121 StatValue& operator = (
const StatValue& src)
123 Type = src.Type; pName = src.pName; I64Value = src.I64Value;
128 void SetInt(
const char* pname, UPInt val)
135 void SetInt64(
const char* pname, UInt64 val)
142 void SetFloat(
const char* pname,
float val)
161 class MemoryStat :
public Stat
163 #ifndef KY_ENABLE_STATS
171 void Increment(UPInt, UPInt ) { }
172 MemoryStat& operator += (MemoryStat& ) {
return *
this; }
173 MemoryStat& operator -= (MemoryStat& ) {
return *
this; }
175 StatType GetStatType()
const {
return Stat_Memory; }
176 unsigned GetStatCount()
const {
return 3; }
178 void GetStat(StatValue* pval,
unsigned index)
const
182 case 0: pval->SetInt(
"Allocated", 0);
break;
183 case 1: pval->SetInt(
"Used", 0);
break;
184 case 2: pval->SetInt(
"AllocCount", 0);
break;
188 void Add(MemoryStat*) { }
189 void SetMax(MemoryStat*) { }
190 void SetMin(MemoryStat*) { }
193 UPInt GetAllocated()
const {
return 0; }
194 UPInt GetUsed()
const {
return 0; }
195 UPInt GetAllocCount()
const {
return 0; }
219 void Increment(UPInt alloc, UPInt use)
227 MemoryStat& operator += (MemoryStat& other)
229 Allocated += other.Allocated;
231 AllocCount+= other.AllocCount;
235 MemoryStat& operator -= (MemoryStat& other)
237 Allocated -= other.Allocated;
239 AllocCount-= other.AllocCount;
245 StatType GetStatType()
const {
return Stat_Memory; }
246 unsigned GetStatCount()
const {
return 3; }
248 void GetStat(StatValue* pval,
unsigned index)
const
252 case 0: pval->SetInt(
"Allocated", Allocated);
break;
253 case 1: pval->SetInt(
"Used", Used);
break;
254 case 2: pval->SetInt(
"AllocCount", AllocCount);
break;
259 void Add(MemoryStat* p)
262 void SetMin(MemoryStat* p)
264 Allocated = (p->Allocated < Allocated) ? p->Allocated : Allocated;
265 Used = (p->Used < Used ) ? p->Used : Used;
266 AllocCount= (p->AllocCount<AllocCount) ? p->AllocCount: AllocCount;
268 void SetMax(MemoryStat* p)
270 Allocated = (p->Allocated > Allocated) ? p->Allocated : Allocated;
271 Used = (p->Used > Used ) ? p->Used : Used;
272 AllocCount= (p->AllocCount>AllocCount) ? p->AllocCount: AllocCount;
283 UPInt GetAllocated()
const {
return Allocated; }
284 UPInt GetUsed()
const {
return Used; }
285 UPInt GetAllocCount()
const {
return AllocCount; }
300 class TimerStat :
public Stat
302 #ifndef KY_ENABLE_STATS
309 TimerStat& operator += (TimerStat&) {
return *
this; }
310 StatType GetStatType()
const {
return Stat_Timer; }
311 unsigned GetStatCount()
const {
return 1; }
313 void GetStat(StatValue* pval,
unsigned index)
const
317 case 0: pval->SetInt64(
"Ticks", 0);
break;
321 void Add(TimerStat*) { }
322 void SetMax(TimerStat*) { }
323 void SetMin(TimerStat*) { }
324 void AddTicks(UInt64) { }
330 ScopeTimer(TimerStat*) { }
333 UInt64 GetTicks()
const {
return 0; }
354 TimerStat& operator += (TimerStat& other)
356 Ticks += other.Ticks;
362 StatType GetStatType()
const {
return Stat_Timer; }
363 unsigned GetStatCount()
const {
return 1; }
365 void GetStat(StatValue* pval,
unsigned index)
const
369 case 0: pval->SetInt64(
"Ticks", Ticks);
break;
374 void Add(TimerStat* p)
377 void SetMin(TimerStat* p)
379 Ticks = (p->Ticks < Ticks) ? p->Ticks : Ticks;
381 void SetMax(TimerStat* p)
383 Ticks = (p->Ticks > Ticks) ? p->Ticks : Ticks;
386 void AddTicks(UInt64 ticks)
405 ScopeTimer(TimerStat* ptimer)
408 StartTicks = Timer::GetProfileTicks();
412 UInt64 endTicks = Timer::GetProfileTicks();
413 pTimer->AddTicks(endTicks - StartTicks);
418 UInt64 GetTicks()
const {
return Ticks; }
433 class CounterStat :
public Stat
435 #ifndef KY_ENABLE_STATS
440 CounterStat(UPInt) { }
443 CounterStat& operator += (CounterStat&) {
return *
this; }
444 StatType GetStatType()
const {
return Stat_Counter; }
445 unsigned GetStatCount()
const {
return 1; }
447 void GetStat(StatValue* pval,
unsigned index)
const
451 case 0: pval->SetInt(
"Count", 0);
break;
456 void Add(CounterStat*) { }
457 void SetMin(CounterStat*) { }
458 void SetMax(CounterStat*) { }
460 void AddCount(UPInt) { }
462 UPInt GetCount()
const {
return 0; }
474 CounterStat(UPInt count)
484 CounterStat& operator += (CounterStat& other)
486 Count += other.Count;
492 StatType GetStatType()
const {
return Stat_Counter; }
493 unsigned GetStatCount()
const {
return 1; }
495 void GetStat(StatValue* pval,
unsigned index)
const
499 case 0: pval->SetInt(
"Count", Count);
break;
504 void Add(CounterStat* p)
507 void SetMin(CounterStat* p)
509 Count = (p->Count < Count) ? p->Count : Count;
511 void SetMax(CounterStat* p)
513 Count = (p->Count > Count) ? p->Count : Count;
516 void AddCount(UPInt count)
526 UPInt GetCount()
const {
return Count; }
562 KY_SumGroup = KY_Group | KY_Sum,
563 KY_AutoSumGroup = KY_Group | KY_AutoSum
569 const StatDesc* pDesc;
572 Iterator(
const StatDesc* p) { pDesc = p; }
573 Iterator(
const Iterator&s) { pDesc = s.pDesc; }
574 Iterator& operator = (
const Iterator&s) { pDesc = s.pDesc;
return *
this; }
576 const StatDesc*
operator * ()
const {
return pDesc; }
577 const StatDesc* operator -> ()
const {
return pDesc; }
579 #ifndef KY_ENABLE_STATS
581 bool operator ++ () {
return false; }
582 unsigned GetId()
const {
return 0; }
584 #else // KY_ENABLE_STATS
586 bool operator ++ () {
if (pDesc) pDesc = pDesc->pNextSibling;
return (pDesc != 0); }
587 unsigned GetId()
const {
return pDesc->Id; }
589 #endif // KY_ENABLE_STATS
591 bool operator == (
const Iterator&s)
const {
return pDesc == s.pDesc; }
592 bool operator != (
const Iterator&s)
const {
return pDesc != s.pDesc; }
593 bool IsEnd()
const {
return pDesc == 0; }
596 static void InitChildTree();
598 #ifndef KY_ENABLE_STATS
600 static const StatDesc* KY_CDECL GetDesc(
unsigned) {
return 0; }
601 StatDesc(Stat::StatType, UByte,
unsigned,
const char*,
unsigned) { }
603 bool IsAutoSumGroup()
const {
return false; }
604 const char* GetName()
const {
return ""; }
605 Iterator GetChildIterator()
const {
return Iterator(0); }
606 static Iterator GetGroupIterator(
unsigned) {
return Iterator(0); }
607 unsigned GetId()
const {
return 0; }
609 #else // KY_ENABLE_STATS
612 static void RegisterDesc(StatDesc* pdesc);
613 static const StatDesc* KY_CDECL GetDesc(
unsigned id);
627 StatDesc* pNextSibling;
631 StatDesc(Stat::StatType type, UByte flags,
unsigned id,
632 const char* pname,
unsigned groupId)
645 UByte GetType()
const {
return Type; }
647 unsigned GetId()
const {
return Id; }
648 void SetId(
unsigned value) { Id = value; }
650 unsigned GetGroupId()
const {
return GroupId; }
651 void SetGroupId(
unsigned value) { GroupId = value; }
654 bool IsAutoSumGroup()
const
656 return (Flags & KY_AutoSumGroup) == KY_AutoSumGroup;
659 const char* GetName()
const {
return pName; }
663 Iterator GetChildIterator()
const {
return Iterator(pChild); }
666 static Iterator GetGroupIterator(
unsigned groupId) {
return Iterator(GetDesc(groupId)); }
668 #endif // KY_ENABLE_STATS
677 #define KY_DECLARE_STAT_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_LogicalGroup, Kaim::StatDesc::KY_Group, id, name, group);
680 #define KY_DECLARE_MEMORY_STAT_SUM_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Memory, Kaim::StatDesc::KY_SumGroup, id, name, group);
681 #define KY_DECLARE_TIMER_STAT_SUM_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Timer, Kaim::StatDesc::KY_SumGroup, id, name, group);
682 #define KY_DECLARE_COUNTER_STAT_SUM_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Counter, Kaim::StatDesc::KY_SumGroup, id, name, group);
684 #define KY_DECLARE_MEMORY_STAT_AUTOSUM_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Memory, Kaim::StatDesc::KY_AutoSumGroup, id, name, group);
685 #define KY_DECLARE_TIMER_STAT_AUTOSUM_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Timer, Kaim::StatDesc::KY_AutoSumGroup, id, name, group);
686 #define KY_DECLARE_COUNTER_STAT_AUTOSUM_GROUP(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Counter, Kaim::StatDesc::KY_AutoSumGroup, id, name, group);
688 #define KY_DECLARE_MEMORY_STAT(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Memory, 0, id, name, group);
689 #define KY_DECLARE_TIMER_STAT(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Timer, 0, id, name, group);
690 #define KY_DECLARE_COUNTER_STAT(id, name, group) Kaim::StatDesc KY_STAT_##id(Kaim::Stat::Stat_Counter, 0, id, name, group);
697 class StatInfo :
public Stat
704 virtual ~StatInterface() { }
706 virtual StatType GetType(Stat* p) = 0;
707 virtual unsigned GetStatCount(Stat* p) = 0;
708 virtual void GetStat(Stat* p, StatValue* pval,
unsigned index) = 0;
709 virtual UPInt GetStatDataSize()
const = 0;
711 virtual void Init(Stat* p) = 0;
712 virtual void Add(Stat* p1, Stat* p2) = 0;
713 virtual void SetMin(Stat* p1, Stat* p2) = 0;
714 virtual void SetMax(Stat* p1, Stat* p2) = 0;
715 virtual void Reset(Stat* p) = 0;
720 StatInterface* pInterface;
726 : StatId(0), pInterface(0), pData(0)
729 StatInfo(
unsigned statId, StatInterface* psi, Stat* pdata)
730 : StatId(statId), pInterface(psi), pData(pdata)
733 StatInfo(
const StatInfo& src)
736 pInterface = src.pInterface;
740 bool IsNull()
const {
return pInterface == 0; }
743 StatType GetType()
const {
return pInterface->GetType(pData); }
744 unsigned GetStatCount()
const {
return pInterface->GetStatCount(pData); }
745 void GetStat(StatValue* pval,
unsigned index) { pInterface->GetStat(pData, pval, index); }
747 void Add(StatInfo& si2)
749 KY_ASSERT(si2.GetType() == GetType());
750 pInterface->Add(pData, si2.pData);
752 void SetMin(StatInfo& si2)
754 KY_ASSERT(si2.GetType() == GetType());
755 pInterface->SetMin(pData, si2.pData);
757 void SetMax(StatInfo& si2)
759 KY_ASSERT(si2.GetType() == GetType());
760 pInterface->SetMax(pData, si2.pData);
764 pInterface->Reset(pData);
767 const char* GetName()
const {
return StatDesc::GetDesc(StatId)->GetName(); }
771 bool IsMemory()
const {
return GetType() == Stat::Stat_Memory; }
772 bool IsTimer()
const {
return GetType() == Stat::Stat_Timer; }
773 bool IsCounter()
const {
return GetType() == Stat::Stat_Counter; }
775 MemoryStat* ToMemoryStat()
const { KY_ASSERT(IsMemory());
return (MemoryStat*)pData; }
776 TimerStat* ToTimerStat()
const { KY_ASSERT(IsTimer());
return (TimerStat*)pData; }
777 CounterStat* ToCounterStat()
const { KY_ASSERT(IsCounter());
return (CounterStat*)pData; }
784 class StatInfo_InterfaceImpl :
public StatInfo::StatInterface
786 typedef Stat::StatType StatType;
787 typedef Stat::StatValue StatValue;
789 StatType GetType(Stat* p) {
return ((C*)p)->GetStatType(); }
790 unsigned GetStatCount(Stat* p) {
return ((C*)p)->GetStatCount(); }
791 void GetStat(Stat* p, StatValue* pval,
unsigned index) { ((C*)p)->GetStat(pval, index); }
792 UPInt GetStatDataSize()
const {
return sizeof(C); }
793 void Init(Stat* p) { ((C*)p)->Init(); }
794 void Add(Stat* p, Stat* p2) { ((C*)p)->Add((C*)p2); }
795 void SetMin(Stat* p, Stat* p2) { ((C*)p)->SetMin((C*)p2); }
796 void SetMax(Stat* p, Stat* p2) { ((C*)p)->SetMax((C*)p2); }
797 void Reset(Stat* p) { ((C*)p)->Reset(); }
813 typedef StatInfo::StatInterface StatInterface;
816 #ifndef KY_ENABLE_STATS
820 StatBag(MemoryHeap* = 0,
unsigned = 0) { }
821 StatBag(
const StatBag&) { }
826 bool Add(
unsigned, Stat* ) {
return false; }
827 StatBag& operator += (
const StatBag&) {
return *
this; }
828 StatBag& operator = (
const StatBag&) {
return *
this; }
829 void UpdateGroups() { }
831 bool GetStat(StatInfo *,
unsigned)
const {
return false; }
833 inline bool AddStat(
unsigned,
const Stat&) {
return false; }
836 bool SetMin(
unsigned statId, Stat* pstat) { KY_UNUSED(statId); KY_UNUSED(pstat);
return false; }
837 bool SetMax(
unsigned statId, Stat* pstat) { KY_UNUSED(statId); KY_UNUSED(pstat);
return false; }
839 void SetMin(
const StatBag& other) { KY_UNUSED(other); }
840 void SetMax(
const StatBag& other) { KY_UNUSED(other); }
847 Iterator(StatBag*,
unsigned,
unsigned) { }
848 Iterator(
const Iterator&) { }
850 const StatInfo&
operator * ()
const {
return Result; }
851 const StatInfo* operator -> ()
const {
return &Result; }
852 bool operator ++ () {
return false; }
853 unsigned GetId()
const {
return StatGroup_Default; }
854 bool operator == (
const Iterator&)
const {
return false; }
855 bool operator != (
const Iterator&)
const {
return true; }
856 bool IsEnd()
const {
return true; }
858 Iterator GetIterator(
unsigned groupId = Stat_MaxId) {
return Iterator (
this, 0, groupId);}
860 #else // KY_ENABLE_STATS
867 StatBag_MemGranularity = 8,
868 StatBag_PageShift = 4,
869 StatBag_PageSize = 1 << StatBag_PageShift,
870 StatBag_PageTableSize = Stat_MaxId / StatBag_PageSize,
871 StatBag_EndId = Stat_MaxId,
875 StatBag_IdUnused = 0xFFFF
878 static StatInterface* GetInterface(
unsigned id);
885 UPInt MemAllocOffset;
888 UInt16 IdPageTable[StatBag_PageTableSize];
890 Stat* GetStatRef(
unsigned statId)
const;
891 UByte* AllocStatData(
unsigned statId, UPInt size);
893 void RecursiveGroupUpdate(StatDesc::Iterator it);
896 void CombineStatBags(
const StatBag& other,
bool (StatBag::*func)(
unsigned id, Stat*));
900 StatBag(MemoryHeap* pheap = 0,
unsigned memReserve = Stat_EntryCount * 16);
901 StatBag(
const StatBag& source);
914 bool Add(
unsigned statId, Stat* pstat);
915 bool SetMin(
unsigned statId, Stat* pstat);
916 bool SetMax(
unsigned statId, Stat* pstat);
918 inline bool AddStat(
unsigned statId,
const Stat& stat)
920 return Add(statId, const_cast<Stat*>(&stat));
924 bool AddMemoryStat(
unsigned statId,
const MemoryStat& stat);
925 bool IncrementMemoryStat(
unsigned statId, UPInt alloc, UPInt use);
928 void Add(
const StatBag& other) { CombineStatBags(other, &StatBag::Add); }
929 void SetMin(
const StatBag& other) { CombineStatBags(other, &StatBag::SetMin); }
930 void SetMax(
const StatBag& other) { CombineStatBags(other, &StatBag::SetMax); }
933 StatBag& operator += (
const StatBag& other) { Add(other);
return *
this; }
934 StatBag& operator = (
const StatBag& other)
947 bool GetStat(StatInfo *pstat,
unsigned statId)
const;
961 bool AdvanceTillValid();
966 Iterator(StatBag* pbag = 0,
unsigned id = Stat_MaxId,
unsigned groupId = Stat_MaxId);
968 Iterator(
const Iterator& src)
969 : Id(src.Id), GroupId(src.GroupId), Result(src.Result)
973 const StatInfo&
operator * ()
const {
return Result; }
974 const StatInfo* operator -> ()
const {
return &Result; }
978 KY_ASSERT(pBag != 0);
980 return AdvanceTillValid();
983 unsigned GetId()
const {
return Id; }
985 bool operator == (
const Iterator& it)
const
989 bool operator != (
const Iterator& it)
const
991 return ! (*
this == it);
996 return (Id >= StatBag_EndId) ;
1002 Iterator GetIterator(
unsigned groupId = Stat_MaxId);
Definition: gamekitcrowddispersion.h:20
Vec2f operator*(KyFloat32 s, const Vec2f &v)
Multiplies the X and Y coordinates of v by s.
Definition: vec2f.h:184