xref: /haiku/src/system/kernel/scheduler/scheduler.cpp (revision 37c7d5d83a2372a6971e383411d5bacbeef0ebdc)
1 /*
2  * Copyright 2008-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3  * Copyright 2010, Axel Dörfler, axeld@pinc-software.de.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 
8 #include <kscheduler.h>
9 #include <listeners.h>
10 #include <smp.h>
11 
12 #include "scheduler_affine.h"
13 #include "scheduler_simple.h"
14 #include "scheduler_simple_smp.h"
15 #include "scheduler_tracing.h"
16 
17 
18 struct scheduler_ops* gScheduler;
19 SchedulerListenerList gSchedulerListeners;
20 
21 
22 SchedulerListener::~SchedulerListener()
23 {
24 }
25 
26 
27 /*!	Add the given scheduler listener. Thread lock must be held.
28 */
29 void
30 scheduler_add_listener(struct SchedulerListener* listener)
31 {
32 	gSchedulerListeners.Add(listener);
33 }
34 
35 
36 /*!	Remove the given scheduler listener. Thread lock must be held.
37 */
38 void
39 scheduler_remove_listener(struct SchedulerListener* listener)
40 {
41 	gSchedulerListeners.Remove(listener);
42 }
43 
44 
45 void
46 scheduler_init(void)
47 {
48 	int32 cpuCount = smp_get_num_cpus();
49 	dprintf("scheduler_init: found %ld logical cpu%s\n", cpuCount,
50 		cpuCount != 1 ? "s" : "");
51 
52 	if (cpuCount > 1) {
53 #if 0
54 		dprintf("scheduler_init: using affine scheduler\n");
55 		scheduler_affine_init();
56 #else
57 		dprintf("scheduler_init: using simple SMP scheduler\n");
58 		scheduler_simple_smp_init();
59 #endif
60 	} else {
61 		dprintf("scheduler_init: using simple scheduler\n");
62 		scheduler_simple_init();
63 	}
64 
65 #if SCHEDULER_TRACING
66 	add_debugger_command_etc("scheduler", &cmd_scheduler,
67 		"Analyze scheduler tracing information",
68 		"<thread>\n"
69 		"Analyzes scheduler tracing information for a given thread.\n"
70 		"  <thread>  - ID of the thread.\n", 0);
71 #endif
72 }
73 
74 
75 // #pragma mark - Syscalls
76 
77 
78 bigtime_t
79 _user_estimate_max_scheduling_latency(thread_id id)
80 {
81 	syscall_64_bit_return_value();
82 
83 	InterruptsSpinLocker locker(gThreadSpinlock);
84 
85 	struct thread* thread = id < 0
86 		? thread_get_current_thread() : thread_get_thread_struct_locked(id);
87 	if (thread == NULL)
88 		return 0;
89 
90 	return gScheduler->estimate_max_scheduling_latency(thread);
91 }
92