ReactOS 0.4.15-dev-7958-gcd0bb1a
balmgr.c
Go to the documentation of this file.
1/*
2* PROJECT: ReactOS Kernel
3* LICENSE: GPL - See COPYING in the top level directory
4* FILE: ntoskrnl/ke/balmgr.c
5* PURPOSE: Balance Set Manager
6* PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7*/
8
9/* INCLUDES ******************************************************************/
10
11#include <ntoskrnl.h>
12#define NDEBUG
13#include <debug.h>
14
15/* GLOBALS *******************************************************************/
16
17#define THREAD_BOOST_PRIORITY (LOW_REALTIME_PRIORITY - 1)
19
20/* PRIVATE FUNCTIONS *********************************************************/
21
22VOID
28{
29 PULONG ScanLast = DeferredContext;
30 ULONG ScanIndex = *ScanLast;
31 ULONG Count = 10, Number = 16;
32 PKPRCB Prcb = KiProcessorBlock[ScanIndex];
33 ULONG Index = Prcb->QueueIndex;
34 ULONG WaitLimit = KeTickCount.LowPart - 300;
35 ULONG Summary;
37 PLIST_ENTRY ListHead, NextEntry;
39
40 /* Lock the dispatcher and PRCB */
43 /* Check if there's any thread that need help */
44 Summary = Prcb->ReadySummary & ((1 << THREAD_BOOST_PRIORITY) - 2);
45 if (Summary)
46 {
47 /* Start scan loop */
48 do
49 {
50 /* Normalize the index */
51 if (Index > (THREAD_BOOST_PRIORITY - 1)) Index = 1;
52
53 /* Loop for ready threads */
54 if (Summary & PRIORITY_MASK(Index))
55 {
56 /* Sanity check */
58
59 /* Update summary and select list */
60 Summary ^= PRIORITY_MASK(Index);
61 ListHead = &Prcb->DispatcherReadyListHead[Index];
62 NextEntry = ListHead->Flink;
63 do
64 {
65 /* Select a thread */
66 Thread = CONTAINING_RECORD(NextEntry,
67 KTHREAD,
68 WaitListEntry);
69 ASSERT(Thread->Priority == Index);
70
71 /* Check if the thread has been waiting too long */
72 if (WaitLimit >= Thread->WaitTime)
73 {
74 /* Remove the thread from the queue */
75 NextEntry = NextEntry->Blink;
77 if (RemoveEntryList(NextEntry->Flink))
78 {
79 /* The list is empty now */
81 }
82
83 /* Verify priority decrement and set the new one */
84 ASSERT((Thread->PriorityDecrement >= 0) &&
85 (Thread->PriorityDecrement <=
86 Thread->Priority));
87 Thread->PriorityDecrement += (THREAD_BOOST_PRIORITY -
88 Thread->Priority);
89 ASSERT((Thread->PriorityDecrement >= 0) &&
90 (Thread->PriorityDecrement <=
92
93 /* Update priority and insert into ready list */
94 Thread->Priority = THREAD_BOOST_PRIORITY;
95 Thread->Quantum = WAIT_QUANTUM_DECREMENT * 4;
97 Count --;
98 }
99
100 /* Go to the next entry */
101 NextEntry = NextEntry->Flink;
102 Number--;
103 } while((NextEntry != ListHead) && (Number) && (Count));
104 }
105
106 /* Increase index */
107 Index++;
108 } while ((Summary) && (Number) && (Count));
109 }
110
111 /* Release the locks and dispatcher */
112 KiReleasePrcbLock(Prcb);
114
115 /* Update the queue index for next time */
116 if ((Count) && (Number))
117 {
118 /* Reset the queue at index 1 */
119 Prcb->QueueIndex = 1;
120 }
121 else
122 {
123 /* Set the index we're in now */
124 Prcb->QueueIndex = Index;
125 }
126
127 /* Increment the CPU number for next time and normalize to CPU count */
128 ScanIndex++;
129 if (ScanIndex == KeNumberProcessors) ScanIndex = 0;
130
131 /* Return the index */
132 *ScanLast = ScanIndex;
133}
134
135VOID
136NTAPI
138{
139 KDPC ScanDpc;
140 KTIMER PeriodTimer;
143 PVOID WaitObjects[1];
145
146 /* Set us at a low real-time priority level */
148
149 /* Setup the timer and scanner DPC */
152
153 /* Setup the periodic timer */
154 DueTime.QuadPart = -1 * 10 * 1000 * 1000;
155 KeSetTimerEx(&PeriodTimer, DueTime, 1000, &ScanDpc);
156
157 /* Setup the wait objects */
158 WaitObjects[0] = &PeriodTimer;
159 //WaitObjects[1] = MmWorkingSetManagerEvent; // NO WS Management Yet!
160
161 /* Start wait loop */
162 do
163 {
164 /* Wait on our objects */
166 WaitObjects,
167 WaitAny,
168 Executive,
170 FALSE,
171 NULL,
173 switch (Status)
174 {
175 /* Check if our timer expired */
176 case STATUS_WAIT_0:
177
178 /* Adjust lookaside lists */
179 //ExAdjustLookasideDepth();
180
181 /* Call the working set manager */
182 //MmWorkingSetManager();
183
184 /* FIXME: Outswap stacks */
185
186 /* Done */
187 break;
188
189 /* Check if the working set manager notified us */
190 case STATUS_WAIT_1:
191
192 /* Call the working set manager */
193 //MmWorkingSetManager();
194 break;
195
196 /* Anything else */
197 default:
198 DPRINT1("BALMGR: Illegal wait status, %lx =\n", Status);
199 break;
200 }
201 } while (TRUE);
202}
ULONG KiReadyScanLast
Definition: balmgr.c:18
#define THREAD_BOOST_PRIORITY
Definition: balmgr.c:17
VOID NTAPI KeBalanceSetManager(IN PVOID Context)
Definition: balmgr.c:137
VOID NTAPI KiScanReadyQueues(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: balmgr.c:24
LONG NTSTATUS
Definition: precomp.h:26
#define DPRINT1
Definition: precomp.h:8
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:712
#define RemoveEntryList(Entry)
Definition: env_spec_w32.h:986
#define IsListEmpty(ListHead)
Definition: env_spec_w32.h:954
UCHAR KIRQL
Definition: env_spec_w32.h:591
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_Must_inspect_result_ _In_ WAIT_TYPE _In_opt_ PLARGE_INTEGER _In_opt_ PKWAIT_BLOCK WaitBlockArray
Definition: fsrtlfuncs.h:1153
Status
Definition: gdiplustypes.h:25
#define KeGetCurrentThread
Definition: hal.h:55
#define LOW_REALTIME_PRIORITY
FORCEINLINE VOID KiReleaseDispatcherLock(IN KIRQL OldIrql)
Definition: ke_x.h:157
FORCEINLINE VOID KiReleasePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:230
FORCEINLINE VOID KiInsertDeferredReadyList(IN PKTHREAD Thread)
Definition: ke_x.h:185
FORCEINLINE VOID KiAcquirePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:220
FORCEINLINE KIRQL KiAcquireDispatcherLock(VOID)
Definition: ke_x.h:149
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
#define ASSERT(a)
Definition: mode.c:44
#define KernelMode
Definition: asm.h:34
#define WAIT_QUANTUM_DECREMENT
Definition: ketypes.h:137
int Count
Definition: noreturn.cpp:7
@ WaitAny
@ SynchronizationTimer
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
#define PRIORITY_MASK(Priority)
Definition: ke.h:158
PKPRCB KiProcessorBlock[]
Definition: krnlinit.c:32
NTSTATUS NTAPI KeWaitForMultipleObjects(IN ULONG Count, IN PVOID Object[], IN WAIT_TYPE WaitType, IN KWAIT_REASON WaitReason, IN KPROCESSOR_MODE WaitMode, IN BOOLEAN Alertable, IN PLARGE_INTEGER Timeout OPTIONAL, OUT PKWAIT_BLOCK WaitBlockArray OPTIONAL)
Definition: wait.c:586
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:207
#define STATUS_WAIT_0
Definition: ntstatus.h:237
#define STATUS_WAIT_1
Definition: ntstatus.h:71
Definition: ketypes.h:699
ULONG QueueIndex
Definition: ketypes.h:811
ULONG ReadySummary
Definition: ketypes.h:810
LIST_ENTRY DispatcherReadyListHead[32]
Definition: ketypes.h:815
ULONG LowPart
Definition: ketypes.h:929
Definition: typedefs.h:120
struct _LIST_ENTRY * Blink
Definition: typedefs.h:122
struct _LIST_ENTRY * Flink
Definition: typedefs.h:121
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1319
BOOLEAN NTAPI KeSetTimerEx(IN OUT PKTIMER Timer, IN LARGE_INTEGER DueTime, IN LONG Period, IN PKDPC Dpc OPTIONAL)
Definition: timerobj.c:294
VOID NTAPI KeInitializeTimerEx(OUT PKTIMER Timer, IN TIMER_TYPE Type)
Definition: timerobj.c:244
uint32_t * PULONG
Definition: typedefs.h:59
#define NTAPI
Definition: typedefs.h:36
#define IN
Definition: typedefs.h:39
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:260
uint32_t ULONG
Definition: typedefs.h:59
_In_ WDFCOLLECTION _In_ ULONG Index
_Must_inspect_result_ _In_ PWDF_DPC_CONFIG _In_ PWDF_OBJECT_ATTRIBUTES _Out_ WDFDPC * Dpc
Definition: wdfdpc.h:112
_In_ WDFTIMER _In_ LONGLONG DueTime
Definition: wdftimer.h:190
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:778
@ Executive
Definition: ketypes.h:415
_In_opt_ PVOID _In_opt_ PVOID SystemArgument1
Definition: ketypes.h:688
_In_opt_ PVOID DeferredContext
Definition: ketypes.h:687
_In_opt_ PVOID _In_opt_ PVOID _In_opt_ PVOID SystemArgument2
Definition: ketypes.h:689