1 /* 2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef THREAD_MODEL_H 6 #define THREAD_MODEL_H 7 8 #include <string.h> 9 10 #include "Model.h" 11 12 13 class ThreadModel { 14 public: 15 struct type_and_name; 16 class WaitObjectGroup; 17 18 public: 19 ThreadModel(Model* model, 20 Model::Thread* thread); 21 ~ThreadModel(); 22 23 Model* GetModel() const { return fModel; } 24 Model::Thread* GetThread() const { return fThread; } 25 26 WaitObjectGroup* AddWaitObjectGroup( 27 const BObjectList<Model::ThreadWaitObject>& 28 waitObjects, 29 int32 start, int32 end); 30 inline int32 CountWaitObjectGroups() const; 31 inline WaitObjectGroup* WaitObjectGroupAt(int32 index) const; 32 33 bool AddSchedulingEvent( 34 const system_profiler_event_header* 35 eventHeader); 36 inline int32 CountSchedulingEvents() const; 37 inline const system_profiler_event_header* SchedulingEventAt( 38 int32 index) const; 39 int32 FindSchedulingEvent(bigtime_t time); 40 41 private: 42 typedef BObjectList<WaitObjectGroup> WaitObjectGroupList; 43 typedef BObjectList<const system_profiler_event_header> EventList; 44 45 private: 46 Model* fModel; 47 Model::Thread* fThread; 48 WaitObjectGroupList fWaitObjectGroups; 49 EventList fSchedulingEvents; 50 }; 51 52 53 struct ThreadModel::type_and_name { 54 uint32 type; 55 const char* name; 56 }; 57 58 59 class ThreadModel::WaitObjectGroup { 60 public: 61 WaitObjectGroup( 62 Model::ThreadWaitObject** waitObjects, 63 int32 count); 64 ~WaitObjectGroup(); 65 66 inline uint32 Type() const; 67 inline const char* Name() const; 68 69 inline int64 Waits() const; 70 inline bigtime_t TotalWaitTime() const; 71 72 inline int32 CountWaitObjects() const; 73 inline Model::ThreadWaitObject* WaitObjectAt(int32 index) const; 74 75 static inline int CompareByTypeName(const WaitObjectGroup* a, 76 const WaitObjectGroup* b); 77 static inline int CompareWithTypeName( 78 const type_and_name* key, 79 const WaitObjectGroup* group); 80 81 private: 82 Model::ThreadWaitObject** fWaitObjects; 83 int32 fCount; 84 int64 fWaits; 85 bigtime_t fTotalWaitTime; 86 }; 87 88 89 // #pragma mark - ThreadModel 90 91 92 int32 93 ThreadModel::CountWaitObjectGroups() const 94 { 95 return fWaitObjectGroups.CountItems(); 96 } 97 98 99 ThreadModel::WaitObjectGroup* 100 ThreadModel::WaitObjectGroupAt(int32 index) const 101 { 102 return fWaitObjectGroups.ItemAt(index); 103 } 104 105 106 int32 107 ThreadModel::CountSchedulingEvents() const 108 { 109 return fSchedulingEvents.CountItems(); 110 } 111 112 113 const system_profiler_event_header* 114 ThreadModel::SchedulingEventAt(int32 index) const 115 { 116 return fSchedulingEvents.ItemAt(index); 117 } 118 119 120 // #pragma mark - WaitObjectGroup 121 122 123 uint32 124 ThreadModel::WaitObjectGroup::Type() const 125 { 126 return fWaitObjects[0]->Type(); 127 } 128 129 130 const char* 131 ThreadModel::WaitObjectGroup::Name() const 132 { 133 return fWaitObjects[0]->Name(); 134 } 135 136 137 int64 138 ThreadModel::WaitObjectGroup::Waits() const 139 { 140 return fWaits; 141 } 142 143 144 bigtime_t 145 ThreadModel::WaitObjectGroup::TotalWaitTime() const 146 { 147 return fTotalWaitTime; 148 } 149 150 151 int32 152 ThreadModel::WaitObjectGroup::CountWaitObjects() const 153 { 154 return fCount; 155 } 156 157 158 Model::ThreadWaitObject* 159 ThreadModel::WaitObjectGroup::WaitObjectAt(int32 index) const 160 { 161 return index >= 0 && index < fCount ? fWaitObjects[index] : NULL; 162 } 163 164 165 /*static*/ int 166 ThreadModel::WaitObjectGroup::CompareByTypeName(const WaitObjectGroup* a, 167 const WaitObjectGroup* b) 168 { 169 type_and_name key; 170 key.type = a->Type(); 171 key.name = a->Name(); 172 return CompareWithTypeName(&key, b); 173 } 174 175 176 /*static*/ int 177 ThreadModel::WaitObjectGroup::CompareWithTypeName(const type_and_name* key, 178 const WaitObjectGroup* group) 179 { 180 if (key->type != group->Type()) 181 return key->type < group->Type() ? -1 : 1; 182 183 return strcmp(key->name, group->Name()); 184 } 185 186 187 #endif // THREAD_MODEL_h 188