1ded186e9SIngo Weinhold /* 2ded186e9SIngo Weinhold * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3ded186e9SIngo Weinhold * Distributed under the terms of the MIT License. 4ded186e9SIngo Weinhold */ 5ded186e9SIngo Weinhold #ifndef THREAD_MODEL_H 6ded186e9SIngo Weinhold #define THREAD_MODEL_H 7ded186e9SIngo Weinhold 8ded186e9SIngo Weinhold #include <string.h> 9ded186e9SIngo Weinhold 10ded186e9SIngo Weinhold #include "Model.h" 11ded186e9SIngo Weinhold 12ded186e9SIngo Weinhold 13ded186e9SIngo Weinhold class ThreadModel { 14ded186e9SIngo Weinhold public: 15ded186e9SIngo Weinhold struct type_and_name; 16ded186e9SIngo Weinhold class WaitObjectGroup; 17ded186e9SIngo Weinhold 18ded186e9SIngo Weinhold public: 19ded186e9SIngo Weinhold ThreadModel(Model* model, 20ded186e9SIngo Weinhold Model::Thread* thread); 21ded186e9SIngo Weinhold ~ThreadModel(); 22ded186e9SIngo Weinhold 23ded186e9SIngo Weinhold Model* GetModel() const { return fModel; } 24ded186e9SIngo Weinhold Model::Thread* GetThread() const { return fThread; } 25ded186e9SIngo Weinhold 26ded186e9SIngo Weinhold WaitObjectGroup* AddWaitObjectGroup( 27ded186e9SIngo Weinhold const BObjectList<Model::ThreadWaitObject>& 28ded186e9SIngo Weinhold waitObjects, 29ded186e9SIngo Weinhold int32 start, int32 end); 30ded186e9SIngo Weinhold inline int32 CountWaitObjectGroups() const; 31ded186e9SIngo Weinhold inline WaitObjectGroup* WaitObjectGroupAt(int32 index) const; 32ded186e9SIngo Weinhold 33ded186e9SIngo Weinhold private: 34ded186e9SIngo Weinhold typedef BObjectList<WaitObjectGroup> WaitObjectGroupList; 35ded186e9SIngo Weinhold 36ded186e9SIngo Weinhold private: 37ded186e9SIngo Weinhold Model* fModel; 38ded186e9SIngo Weinhold Model::Thread* fThread; 39ded186e9SIngo Weinhold WaitObjectGroupList fWaitObjectGroups; 40ded186e9SIngo Weinhold }; 41ded186e9SIngo Weinhold 42ded186e9SIngo Weinhold 43ded186e9SIngo Weinhold struct ThreadModel::type_and_name { 44ded186e9SIngo Weinhold uint32 type; 45ded186e9SIngo Weinhold const char* name; 46ded186e9SIngo Weinhold }; 47ded186e9SIngo Weinhold 48ded186e9SIngo Weinhold 49ded186e9SIngo Weinhold class ThreadModel::WaitObjectGroup { 50ded186e9SIngo Weinhold public: 51ded186e9SIngo Weinhold WaitObjectGroup( 52ded186e9SIngo Weinhold Model::ThreadWaitObject** waitObjects, 53ded186e9SIngo Weinhold int32 count); 54ded186e9SIngo Weinhold ~WaitObjectGroup(); 55ded186e9SIngo Weinhold 56ded186e9SIngo Weinhold inline uint32 Type() const; 57ded186e9SIngo Weinhold inline const char* Name() const; 58ded186e9SIngo Weinhold 594ab2927dSIngo Weinhold inline int64 Waits() const; 604ab2927dSIngo Weinhold inline bigtime_t TotalWaitTime() const; 614ab2927dSIngo Weinhold 62ded186e9SIngo Weinhold inline int32 CountWaitObjects() const; 63ded186e9SIngo Weinhold inline Model::ThreadWaitObject* WaitObjectAt(int32 index) const; 64ded186e9SIngo Weinhold 65ded186e9SIngo Weinhold static inline int CompareByTypeName(const WaitObjectGroup* a, 66ded186e9SIngo Weinhold const WaitObjectGroup* b); 67ded186e9SIngo Weinhold static inline int CompareWithTypeName( 68ded186e9SIngo Weinhold const type_and_name* key, 69ded186e9SIngo Weinhold const WaitObjectGroup* group); 70ded186e9SIngo Weinhold 71ded186e9SIngo Weinhold private: 72ded186e9SIngo Weinhold Model::ThreadWaitObject** fWaitObjects; 73ded186e9SIngo Weinhold int32 fCount; 744ab2927dSIngo Weinhold int64 fWaits; 754ab2927dSIngo Weinhold bigtime_t fTotalWaitTime; 76ded186e9SIngo Weinhold }; 77ded186e9SIngo Weinhold 78ded186e9SIngo Weinhold 79ded186e9SIngo Weinhold // #pragma mark - ThreadModel 80ded186e9SIngo Weinhold 81ded186e9SIngo Weinhold 82ded186e9SIngo Weinhold int32 83ded186e9SIngo Weinhold ThreadModel::CountWaitObjectGroups() const 84ded186e9SIngo Weinhold { 85ded186e9SIngo Weinhold return fWaitObjectGroups.CountItems(); 86ded186e9SIngo Weinhold } 87ded186e9SIngo Weinhold 88ded186e9SIngo Weinhold 89ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup* 90ded186e9SIngo Weinhold ThreadModel::WaitObjectGroupAt(int32 index) const 91ded186e9SIngo Weinhold { 92ded186e9SIngo Weinhold return fWaitObjectGroups.ItemAt(index); 93ded186e9SIngo Weinhold } 94ded186e9SIngo Weinhold 95ded186e9SIngo Weinhold 96ded186e9SIngo Weinhold // #pragma mark - WaitObjectGroup 97ded186e9SIngo Weinhold 98ded186e9SIngo Weinhold 99ded186e9SIngo Weinhold uint32 100ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Type() const 101ded186e9SIngo Weinhold { 102ded186e9SIngo Weinhold return fWaitObjects[0]->Type(); 103ded186e9SIngo Weinhold } 104ded186e9SIngo Weinhold 105ded186e9SIngo Weinhold 106ded186e9SIngo Weinhold const char* 107ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Name() const 108ded186e9SIngo Weinhold { 109ded186e9SIngo Weinhold return fWaitObjects[0]->Name(); 110ded186e9SIngo Weinhold } 111ded186e9SIngo Weinhold 112ded186e9SIngo Weinhold 1134ab2927dSIngo Weinhold int64 1144ab2927dSIngo Weinhold ThreadModel::WaitObjectGroup::Waits() const 1154ab2927dSIngo Weinhold { 1164ab2927dSIngo Weinhold return fWaits; 1174ab2927dSIngo Weinhold } 1184ab2927dSIngo Weinhold 1194ab2927dSIngo Weinhold 1204ab2927dSIngo Weinhold bigtime_t 1214ab2927dSIngo Weinhold ThreadModel::WaitObjectGroup::TotalWaitTime() const 1224ab2927dSIngo Weinhold { 1234ab2927dSIngo Weinhold return fTotalWaitTime; 1244ab2927dSIngo Weinhold } 1254ab2927dSIngo Weinhold 1264ab2927dSIngo Weinhold 127ded186e9SIngo Weinhold int32 128ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CountWaitObjects() const 129ded186e9SIngo Weinhold { 130ded186e9SIngo Weinhold return fCount; 131ded186e9SIngo Weinhold } 132ded186e9SIngo Weinhold 133ded186e9SIngo Weinhold 134ded186e9SIngo Weinhold Model::ThreadWaitObject* 135ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::WaitObjectAt(int32 index) const 136ded186e9SIngo Weinhold { 137*84b7a2d0SIngo Weinhold return index >= 0 && index < fCount ? fWaitObjects[index] : NULL; 138ded186e9SIngo Weinhold } 139ded186e9SIngo Weinhold 140ded186e9SIngo Weinhold 141ded186e9SIngo Weinhold /*static*/ int 142ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareByTypeName(const WaitObjectGroup* a, 143ded186e9SIngo Weinhold const WaitObjectGroup* b) 144ded186e9SIngo Weinhold { 145ded186e9SIngo Weinhold type_and_name key; 146ded186e9SIngo Weinhold key.type = a->Type(); 147ded186e9SIngo Weinhold key.name = a->Name(); 148ded186e9SIngo Weinhold return CompareWithTypeName(&key, b); 149ded186e9SIngo Weinhold } 150ded186e9SIngo Weinhold 151ded186e9SIngo Weinhold 152ded186e9SIngo Weinhold /*static*/ int 153ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareWithTypeName(const type_and_name* key, 154ded186e9SIngo Weinhold const WaitObjectGroup* group) 155ded186e9SIngo Weinhold { 156ded186e9SIngo Weinhold if (key->type != group->Type()) 157ded186e9SIngo Weinhold return key->type < group->Type() ? -1 : 1; 158ded186e9SIngo Weinhold 159ded186e9SIngo Weinhold return strcmp(key->name, group->Name()); 160ded186e9SIngo Weinhold } 161ded186e9SIngo Weinhold 162ded186e9SIngo Weinhold 163ded186e9SIngo Weinhold #endif // THREAD_MODEL_h 164