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
8d8d4b902SIngo Weinhold
9ded186e9SIngo Weinhold #include <string.h>
10ded186e9SIngo Weinhold
11ded186e9SIngo Weinhold #include "Model.h"
12ded186e9SIngo Weinhold
13ded186e9SIngo Weinhold
14ded186e9SIngo Weinhold class ThreadModel {
15ded186e9SIngo Weinhold public:
16ded186e9SIngo Weinhold struct type_and_name;
17ded186e9SIngo Weinhold class WaitObjectGroup;
18ded186e9SIngo Weinhold
19ded186e9SIngo Weinhold public:
20ded186e9SIngo Weinhold ThreadModel(Model* model,
21ded186e9SIngo Weinhold Model::Thread* thread);
22ded186e9SIngo Weinhold ~ThreadModel();
23ded186e9SIngo Weinhold
GetModel()24ded186e9SIngo Weinhold Model* GetModel() const { return fModel; }
GetThread()25ded186e9SIngo Weinhold Model::Thread* GetThread() const { return fThread; }
26ded186e9SIngo Weinhold
27ded186e9SIngo Weinhold WaitObjectGroup* AddWaitObjectGroup(
28ded186e9SIngo Weinhold const BObjectList<Model::ThreadWaitObject>&
29ded186e9SIngo Weinhold waitObjects,
30ded186e9SIngo Weinhold int32 start, int32 end);
31ded186e9SIngo Weinhold inline int32 CountWaitObjectGroups() const;
32ded186e9SIngo Weinhold inline WaitObjectGroup* WaitObjectGroupAt(int32 index) const;
33ded186e9SIngo Weinhold
348cd416d9SIngo Weinhold bool AddSchedulingEvent(
358cd416d9SIngo Weinhold const system_profiler_event_header*
368cd416d9SIngo Weinhold eventHeader);
378cd416d9SIngo Weinhold inline int32 CountSchedulingEvents() const;
388cd416d9SIngo Weinhold inline const system_profiler_event_header* SchedulingEventAt(
398cd416d9SIngo Weinhold int32 index) const;
40d8d4b902SIngo Weinhold int32 FindSchedulingEvent(nanotime_t time);
418cd416d9SIngo Weinhold
42ded186e9SIngo Weinhold private:
43ded186e9SIngo Weinhold typedef BObjectList<WaitObjectGroup> WaitObjectGroupList;
448cd416d9SIngo Weinhold typedef BObjectList<const system_profiler_event_header> EventList;
45ded186e9SIngo Weinhold
46ded186e9SIngo Weinhold private:
47ded186e9SIngo Weinhold Model* fModel;
48ded186e9SIngo Weinhold Model::Thread* fThread;
49ded186e9SIngo Weinhold WaitObjectGroupList fWaitObjectGroups;
508cd416d9SIngo Weinhold EventList fSchedulingEvents;
51ded186e9SIngo Weinhold };
52ded186e9SIngo Weinhold
53ded186e9SIngo Weinhold
54ded186e9SIngo Weinhold struct ThreadModel::type_and_name {
55ded186e9SIngo Weinhold uint32 type;
56ded186e9SIngo Weinhold const char* name;
57ded186e9SIngo Weinhold };
58ded186e9SIngo Weinhold
59ded186e9SIngo Weinhold
60ded186e9SIngo Weinhold class ThreadModel::WaitObjectGroup {
61ded186e9SIngo Weinhold public:
62ded186e9SIngo Weinhold WaitObjectGroup(
63ded186e9SIngo Weinhold Model::ThreadWaitObject** waitObjects,
64ded186e9SIngo Weinhold int32 count);
65ded186e9SIngo Weinhold ~WaitObjectGroup();
66ded186e9SIngo Weinhold
67ded186e9SIngo Weinhold inline uint32 Type() const;
68ded186e9SIngo Weinhold inline const char* Name() const;
69*2754b722SIngo Weinhold inline addr_t Object() const;
70ded186e9SIngo Weinhold
714ab2927dSIngo Weinhold inline int64 Waits() const;
72d8d4b902SIngo Weinhold inline nanotime_t TotalWaitTime() const;
734ab2927dSIngo Weinhold
74ded186e9SIngo Weinhold inline int32 CountWaitObjects() const;
75ded186e9SIngo Weinhold inline Model::ThreadWaitObject* WaitObjectAt(int32 index) const;
76ded186e9SIngo Weinhold
77ded186e9SIngo Weinhold static inline int CompareByTypeName(const WaitObjectGroup* a,
78ded186e9SIngo Weinhold const WaitObjectGroup* b);
79ded186e9SIngo Weinhold static inline int CompareWithTypeName(
80ded186e9SIngo Weinhold const type_and_name* key,
81ded186e9SIngo Weinhold const WaitObjectGroup* group);
82ded186e9SIngo Weinhold
83ded186e9SIngo Weinhold private:
84ded186e9SIngo Weinhold Model::ThreadWaitObject** fWaitObjects;
85ded186e9SIngo Weinhold int32 fCount;
864ab2927dSIngo Weinhold int64 fWaits;
87d8d4b902SIngo Weinhold nanotime_t fTotalWaitTime;
88ded186e9SIngo Weinhold };
89ded186e9SIngo Weinhold
90ded186e9SIngo Weinhold
91ded186e9SIngo Weinhold // #pragma mark - ThreadModel
92ded186e9SIngo Weinhold
93ded186e9SIngo Weinhold
94ded186e9SIngo Weinhold int32
CountWaitObjectGroups()95ded186e9SIngo Weinhold ThreadModel::CountWaitObjectGroups() const
96ded186e9SIngo Weinhold {
97ded186e9SIngo Weinhold return fWaitObjectGroups.CountItems();
98ded186e9SIngo Weinhold }
99ded186e9SIngo Weinhold
100ded186e9SIngo Weinhold
101ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup*
WaitObjectGroupAt(int32 index)102ded186e9SIngo Weinhold ThreadModel::WaitObjectGroupAt(int32 index) const
103ded186e9SIngo Weinhold {
104ded186e9SIngo Weinhold return fWaitObjectGroups.ItemAt(index);
105ded186e9SIngo Weinhold }
106ded186e9SIngo Weinhold
107ded186e9SIngo Weinhold
1088cd416d9SIngo Weinhold int32
CountSchedulingEvents()1098cd416d9SIngo Weinhold ThreadModel::CountSchedulingEvents() const
1108cd416d9SIngo Weinhold {
1118cd416d9SIngo Weinhold return fSchedulingEvents.CountItems();
1128cd416d9SIngo Weinhold }
1138cd416d9SIngo Weinhold
1148cd416d9SIngo Weinhold
1158cd416d9SIngo Weinhold const system_profiler_event_header*
SchedulingEventAt(int32 index)1168cd416d9SIngo Weinhold ThreadModel::SchedulingEventAt(int32 index) const
1178cd416d9SIngo Weinhold {
1188cd416d9SIngo Weinhold return fSchedulingEvents.ItemAt(index);
1198cd416d9SIngo Weinhold }
1208cd416d9SIngo Weinhold
1218cd416d9SIngo Weinhold
122ded186e9SIngo Weinhold // #pragma mark - WaitObjectGroup
123ded186e9SIngo Weinhold
124ded186e9SIngo Weinhold
125ded186e9SIngo Weinhold uint32
Type()126ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Type() const
127ded186e9SIngo Weinhold {
128ded186e9SIngo Weinhold return fWaitObjects[0]->Type();
129ded186e9SIngo Weinhold }
130ded186e9SIngo Weinhold
131ded186e9SIngo Weinhold
132ded186e9SIngo Weinhold const char*
Name()133ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Name() const
134ded186e9SIngo Weinhold {
135ded186e9SIngo Weinhold return fWaitObjects[0]->Name();
136ded186e9SIngo Weinhold }
137ded186e9SIngo Weinhold
138ded186e9SIngo Weinhold
139*2754b722SIngo Weinhold addr_t
Object()140*2754b722SIngo Weinhold ThreadModel::WaitObjectGroup::Object() const
141*2754b722SIngo Weinhold {
142*2754b722SIngo Weinhold return fWaitObjects[0]->Object();
143*2754b722SIngo Weinhold }
144*2754b722SIngo Weinhold
145*2754b722SIngo Weinhold
1464ab2927dSIngo Weinhold int64
Waits()1474ab2927dSIngo Weinhold ThreadModel::WaitObjectGroup::Waits() const
1484ab2927dSIngo Weinhold {
1494ab2927dSIngo Weinhold return fWaits;
1504ab2927dSIngo Weinhold }
1514ab2927dSIngo Weinhold
1524ab2927dSIngo Weinhold
153d8d4b902SIngo Weinhold nanotime_t
TotalWaitTime()1544ab2927dSIngo Weinhold ThreadModel::WaitObjectGroup::TotalWaitTime() const
1554ab2927dSIngo Weinhold {
1564ab2927dSIngo Weinhold return fTotalWaitTime;
1574ab2927dSIngo Weinhold }
1584ab2927dSIngo Weinhold
1594ab2927dSIngo Weinhold
160ded186e9SIngo Weinhold int32
CountWaitObjects()161ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CountWaitObjects() const
162ded186e9SIngo Weinhold {
163ded186e9SIngo Weinhold return fCount;
164ded186e9SIngo Weinhold }
165ded186e9SIngo Weinhold
166ded186e9SIngo Weinhold
167ded186e9SIngo Weinhold Model::ThreadWaitObject*
WaitObjectAt(int32 index)168ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::WaitObjectAt(int32 index) const
169ded186e9SIngo Weinhold {
17084b7a2d0SIngo Weinhold return index >= 0 && index < fCount ? fWaitObjects[index] : NULL;
171ded186e9SIngo Weinhold }
172ded186e9SIngo Weinhold
173ded186e9SIngo Weinhold
174ded186e9SIngo Weinhold /*static*/ int
CompareByTypeName(const WaitObjectGroup * a,const WaitObjectGroup * b)175ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareByTypeName(const WaitObjectGroup* a,
176ded186e9SIngo Weinhold const WaitObjectGroup* b)
177ded186e9SIngo Weinhold {
178ded186e9SIngo Weinhold type_and_name key;
179ded186e9SIngo Weinhold key.type = a->Type();
180ded186e9SIngo Weinhold key.name = a->Name();
181ded186e9SIngo Weinhold return CompareWithTypeName(&key, b);
182ded186e9SIngo Weinhold }
183ded186e9SIngo Weinhold
184ded186e9SIngo Weinhold
185ded186e9SIngo Weinhold /*static*/ int
CompareWithTypeName(const type_and_name * key,const WaitObjectGroup * group)186ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareWithTypeName(const type_and_name* key,
187ded186e9SIngo Weinhold const WaitObjectGroup* group)
188ded186e9SIngo Weinhold {
189ded186e9SIngo Weinhold if (key->type != group->Type())
190ded186e9SIngo Weinhold return key->type < group->Type() ? -1 : 1;
191ded186e9SIngo Weinhold
192ded186e9SIngo Weinhold return strcmp(key->name, group->Name());
193ded186e9SIngo Weinhold }
194ded186e9SIngo Weinhold
195ded186e9SIngo Weinhold
196ded186e9SIngo Weinhold #endif // THREAD_MODEL_h
197