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