xref: /haiku/src/system/kernel/scheduler/scheduler_tracing.h (revision 0a7f661ae2a40d70dd7b0fbf72dbd781deb4b75d)
1 /*
2  * Copyright 2008-2011, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2002-2007, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 #ifndef KERNEL_SCHEDULER_TRACING_H
7 #define KERNEL_SCHEDULER_TRACING_H
8 
9 
10 #include <arch/debug.h>
11 #include <cpu.h>
12 #include <thread.h>
13 #include <tracing.h>
14 
15 
16 #if SCHEDULER_TRACING
17 
18 namespace SchedulerTracing {
19 
20 class SchedulerTraceEntry : public AbstractTraceEntry {
21 public:
SchedulerTraceEntry(Thread * thread)22 	SchedulerTraceEntry(Thread* thread)
23 		:
24 		fID(thread->id)
25 	{
26 	}
27 
ThreadID()28 	thread_id ThreadID() const	{ return fID; }
29 
30 	virtual const char* Name() const = 0;
31 
32 protected:
33 	thread_id			fID;
34 };
35 
36 
37 class EnqueueThread : public SchedulerTraceEntry {
38 public:
EnqueueThread(Thread * thread,int32 effectivePriority)39 	EnqueueThread(Thread* thread, int32 effectivePriority)
40 		:
41 		SchedulerTraceEntry(thread),
42 		fPriority(thread->priority),
43 		fEffectivePriority(effectivePriority)
44 	{
45 		fName = alloc_tracing_buffer_strcpy(thread->name, B_OS_NAME_LENGTH,
46 			false);
47 		Initialized();
48 	}
49 
50 	virtual void AddDump(TraceOutput& out);
51 
52 	virtual const char* Name() const;
53 
54 private:
55 	char*				fName;
56 	int32				fPriority;
57 	int32				fEffectivePriority;
58 };
59 
60 
61 class RemoveThread : public SchedulerTraceEntry {
62 public:
RemoveThread(Thread * thread)63 	RemoveThread(Thread* thread)
64 		:
65 		SchedulerTraceEntry(thread),
66 		fPriority(thread->priority)
67 	{
68 		Initialized();
69 	}
70 
71 	virtual void AddDump(TraceOutput& out);
72 
73 	virtual const char* Name() const;
74 
75 private:
76 	int32				fPriority;
77 };
78 
79 
80 class ScheduleThread : public SchedulerTraceEntry {
81 public:
ScheduleThread(Thread * thread,Thread * previous)82 	ScheduleThread(Thread* thread, Thread* previous)
83 		:
84 		SchedulerTraceEntry(thread),
85 		fPreviousID(previous->id),
86 		fCPU(previous->cpu->cpu_num),
87 		fPriority(thread->priority),
88 		fPreviousState(previous->state),
89 		fPreviousWaitObjectType(previous->wait.type)
90 	{
91 		fName = alloc_tracing_buffer_strcpy(thread->name, B_OS_NAME_LENGTH,
92 			false);
93 
94 #if SCHEDULER_TRACING >= 2
95 		if (fPreviousState == B_THREAD_READY)
96 			fPreviousPC = arch_debug_get_interrupt_pc(NULL);
97 		else
98 #endif
99 			fPreviousWaitObject = previous->wait.object;
100 
101 		Initialized();
102 	}
103 
104 	virtual void AddDump(TraceOutput& out);
105 
106 	virtual const char* Name() const;
107 
PreviousThreadID()108 	thread_id PreviousThreadID() const		{ return fPreviousID; }
PreviousState()109 	uint8 PreviousState() const				{ return fPreviousState; }
PreviousWaitObjectType()110 	uint16 PreviousWaitObjectType() const	{ return fPreviousWaitObjectType; }
PreviousWaitObject()111 	const void* PreviousWaitObject() const	{ return fPreviousWaitObject; }
112 
113 private:
114 	thread_id			fPreviousID;
115 	int32				fCPU;
116 	char*				fName;
117 	int32				fPriority;
118 	uint8				fPreviousState;
119 	uint16				fPreviousWaitObjectType;
120 	union {
121 		const void*		fPreviousWaitObject;
122 		void*			fPreviousPC;
123 	};
124 };
125 
126 }	// namespace SchedulerTracing
127 
128 #	define T(x) new(std::nothrow) SchedulerTracing::x;
129 #else
130 #	define T(x) ;
131 #endif
132 
133 
134 #if SCHEDULER_TRACING
135 
136 namespace SchedulerTracing {
137 
138 enum ScheduleState {
139 	RUNNING,
140 	STILL_RUNNING,
141 	PREEMPTED,
142 	READY,
143 	WAITING,
144 	UNKNOWN
145 };
146 
147 }
148 
149 int cmd_scheduler(int argc, char** argv);
150 
151 #endif	// SCHEDULER_TRACING
152 
153 #endif	// KERNEL_SCHEDULER_TRACING_H
154