xref: /haiku/src/apps/debuganalyzer/model/ThreadModel.h (revision 8cd416d9e638d1b89f0608d22c92e2e6f530e708)
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 
33*8cd416d9SIngo Weinhold 			bool				AddSchedulingEvent(
34*8cd416d9SIngo Weinhold 									const system_profiler_event_header*
35*8cd416d9SIngo Weinhold 										eventHeader);
36*8cd416d9SIngo Weinhold 	inline	int32				CountSchedulingEvents() const;
37*8cd416d9SIngo Weinhold 	inline	const system_profiler_event_header* SchedulingEventAt(
38*8cd416d9SIngo Weinhold 									int32 index) const;
39*8cd416d9SIngo Weinhold 			int32				FindSchedulingEvent(bigtime_t time);
40*8cd416d9SIngo Weinhold 
41ded186e9SIngo Weinhold private:
42ded186e9SIngo Weinhold 			typedef BObjectList<WaitObjectGroup> WaitObjectGroupList;
43*8cd416d9SIngo Weinhold 			typedef BObjectList<const system_profiler_event_header> EventList;
44ded186e9SIngo Weinhold 
45ded186e9SIngo Weinhold private:
46ded186e9SIngo Weinhold 			Model*				fModel;
47ded186e9SIngo Weinhold 			Model::Thread*		fThread;
48ded186e9SIngo Weinhold 			WaitObjectGroupList	fWaitObjectGroups;
49*8cd416d9SIngo Weinhold 			EventList			fSchedulingEvents;
50ded186e9SIngo Weinhold };
51ded186e9SIngo Weinhold 
52ded186e9SIngo Weinhold 
53ded186e9SIngo Weinhold struct ThreadModel::type_and_name {
54ded186e9SIngo Weinhold 	uint32		type;
55ded186e9SIngo Weinhold 	const char*	name;
56ded186e9SIngo Weinhold };
57ded186e9SIngo Weinhold 
58ded186e9SIngo Weinhold 
59ded186e9SIngo Weinhold class ThreadModel::WaitObjectGroup {
60ded186e9SIngo Weinhold public:
61ded186e9SIngo Weinhold 								WaitObjectGroup(
62ded186e9SIngo Weinhold 									Model::ThreadWaitObject** waitObjects,
63ded186e9SIngo Weinhold 									int32 count);
64ded186e9SIngo Weinhold 								~WaitObjectGroup();
65ded186e9SIngo Weinhold 
66ded186e9SIngo Weinhold 	inline	uint32				Type() const;
67ded186e9SIngo Weinhold 	inline	const char*			Name() const;
68ded186e9SIngo Weinhold 
694ab2927dSIngo Weinhold 	inline	int64				Waits() const;
704ab2927dSIngo Weinhold 	inline	bigtime_t			TotalWaitTime() const;
714ab2927dSIngo Weinhold 
72ded186e9SIngo Weinhold 	inline	int32				CountWaitObjects() const;
73ded186e9SIngo Weinhold 	inline	Model::ThreadWaitObject* WaitObjectAt(int32 index) const;
74ded186e9SIngo Weinhold 
75ded186e9SIngo Weinhold 	static inline int			CompareByTypeName(const WaitObjectGroup* a,
76ded186e9SIngo Weinhold 									const WaitObjectGroup* b);
77ded186e9SIngo Weinhold 	static inline int			CompareWithTypeName(
78ded186e9SIngo Weinhold 									const type_and_name* key,
79ded186e9SIngo Weinhold 									const WaitObjectGroup* group);
80ded186e9SIngo Weinhold 
81ded186e9SIngo Weinhold private:
82ded186e9SIngo Weinhold 			Model::ThreadWaitObject** fWaitObjects;
83ded186e9SIngo Weinhold 			int32				fCount;
844ab2927dSIngo Weinhold 			int64				fWaits;
854ab2927dSIngo Weinhold 			bigtime_t			fTotalWaitTime;
86ded186e9SIngo Weinhold };
87ded186e9SIngo Weinhold 
88ded186e9SIngo Weinhold 
89ded186e9SIngo Weinhold // #pragma mark - ThreadModel
90ded186e9SIngo Weinhold 
91ded186e9SIngo Weinhold 
92ded186e9SIngo Weinhold int32
93ded186e9SIngo Weinhold ThreadModel::CountWaitObjectGroups() const
94ded186e9SIngo Weinhold {
95ded186e9SIngo Weinhold 	return fWaitObjectGroups.CountItems();
96ded186e9SIngo Weinhold }
97ded186e9SIngo Weinhold 
98ded186e9SIngo Weinhold 
99ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup*
100ded186e9SIngo Weinhold ThreadModel::WaitObjectGroupAt(int32 index) const
101ded186e9SIngo Weinhold {
102ded186e9SIngo Weinhold 	return fWaitObjectGroups.ItemAt(index);
103ded186e9SIngo Weinhold }
104ded186e9SIngo Weinhold 
105ded186e9SIngo Weinhold 
106*8cd416d9SIngo Weinhold int32
107*8cd416d9SIngo Weinhold ThreadModel::CountSchedulingEvents() const
108*8cd416d9SIngo Weinhold {
109*8cd416d9SIngo Weinhold 	return fSchedulingEvents.CountItems();
110*8cd416d9SIngo Weinhold }
111*8cd416d9SIngo Weinhold 
112*8cd416d9SIngo Weinhold 
113*8cd416d9SIngo Weinhold const system_profiler_event_header*
114*8cd416d9SIngo Weinhold ThreadModel::SchedulingEventAt(int32 index) const
115*8cd416d9SIngo Weinhold {
116*8cd416d9SIngo Weinhold 	return fSchedulingEvents.ItemAt(index);
117*8cd416d9SIngo Weinhold }
118*8cd416d9SIngo Weinhold 
119*8cd416d9SIngo Weinhold 
120ded186e9SIngo Weinhold // #pragma mark - WaitObjectGroup
121ded186e9SIngo Weinhold 
122ded186e9SIngo Weinhold 
123ded186e9SIngo Weinhold uint32
124ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Type() const
125ded186e9SIngo Weinhold {
126ded186e9SIngo Weinhold 	return fWaitObjects[0]->Type();
127ded186e9SIngo Weinhold }
128ded186e9SIngo Weinhold 
129ded186e9SIngo Weinhold 
130ded186e9SIngo Weinhold const char*
131ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::Name() const
132ded186e9SIngo Weinhold {
133ded186e9SIngo Weinhold 	return fWaitObjects[0]->Name();
134ded186e9SIngo Weinhold }
135ded186e9SIngo Weinhold 
136ded186e9SIngo Weinhold 
1374ab2927dSIngo Weinhold int64
1384ab2927dSIngo Weinhold ThreadModel::WaitObjectGroup::Waits() const
1394ab2927dSIngo Weinhold {
1404ab2927dSIngo Weinhold 	return fWaits;
1414ab2927dSIngo Weinhold }
1424ab2927dSIngo Weinhold 
1434ab2927dSIngo Weinhold 
1444ab2927dSIngo Weinhold bigtime_t
1454ab2927dSIngo Weinhold ThreadModel::WaitObjectGroup::TotalWaitTime() const
1464ab2927dSIngo Weinhold {
1474ab2927dSIngo Weinhold 	return fTotalWaitTime;
1484ab2927dSIngo Weinhold }
1494ab2927dSIngo Weinhold 
1504ab2927dSIngo Weinhold 
151ded186e9SIngo Weinhold int32
152ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CountWaitObjects() const
153ded186e9SIngo Weinhold {
154ded186e9SIngo Weinhold 	return fCount;
155ded186e9SIngo Weinhold }
156ded186e9SIngo Weinhold 
157ded186e9SIngo Weinhold 
158ded186e9SIngo Weinhold Model::ThreadWaitObject*
159ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::WaitObjectAt(int32 index) const
160ded186e9SIngo Weinhold {
16184b7a2d0SIngo Weinhold 	return index >= 0 && index < fCount ? fWaitObjects[index] : NULL;
162ded186e9SIngo Weinhold }
163ded186e9SIngo Weinhold 
164ded186e9SIngo Weinhold 
165ded186e9SIngo Weinhold /*static*/ int
166ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareByTypeName(const WaitObjectGroup* a,
167ded186e9SIngo Weinhold 	const WaitObjectGroup* b)
168ded186e9SIngo Weinhold {
169ded186e9SIngo Weinhold 	type_and_name key;
170ded186e9SIngo Weinhold 	key.type = a->Type();
171ded186e9SIngo Weinhold 	key.name = a->Name();
172ded186e9SIngo Weinhold 	return CompareWithTypeName(&key, b);
173ded186e9SIngo Weinhold }
174ded186e9SIngo Weinhold 
175ded186e9SIngo Weinhold 
176ded186e9SIngo Weinhold /*static*/ int
177ded186e9SIngo Weinhold ThreadModel::WaitObjectGroup::CompareWithTypeName(const type_and_name* key,
178ded186e9SIngo Weinhold 	const WaitObjectGroup* group)
179ded186e9SIngo Weinhold {
180ded186e9SIngo Weinhold 	if (key->type != group->Type())
181ded186e9SIngo Weinhold 		return key->type < group->Type() ? -1 : 1;
182ded186e9SIngo Weinhold 
183ded186e9SIngo Weinhold 	return strcmp(key->name, group->Name());
184ded186e9SIngo Weinhold }
185ded186e9SIngo Weinhold 
186ded186e9SIngo Weinhold 
187ded186e9SIngo Weinhold #endif	// THREAD_MODEL_h
188