ReactOS  r76032
balmgr.c File Reference
#include <ntoskrnl.h>
#include <debug.h>
Include dependency graph for balmgr.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define THREAD_BOOST_PRIORITY   (LOW_REALTIME_PRIORITY - 1)
 

Functions

VOID NTAPI KiScanReadyQueues (IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
 
VOID NTAPI KeBalanceSetManager (IN PVOID Context)
 

Variables

ULONG KiReadyScanLast
 

Macro Definition Documentation

#define NDEBUG

Definition at line 12 of file balmgr.c.

#define THREAD_BOOST_PRIORITY   (LOW_REALTIME_PRIORITY - 1)

Definition at line 17 of file balmgr.c.

Referenced by KiScanReadyQueues().

Function Documentation

VOID NTAPI KeBalanceSetManager ( IN PVOID  Context)

Definition at line 137 of file balmgr.c.

Referenced by MmInitBsmThread().

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 */
165  Status = KeWaitForMultipleObjects(1,
166  WaitObjects,
167  WaitAny,
168  Executive,
169  KernelMode,
170  FALSE,
171  NULL,
172  WaitBlockArray);
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 }
DWORD *typedef PVOID
Definition: winlogon.h:52
#define TRUE
Definition: types.h:120
ULONG KiReadyScanLast
Definition: balmgr.c:18
_In_ LARGE_INTEGER DueTime
Definition: kefuncs.h:524
BOOLEAN NTAPI KeSetTimerEx(IN OUT PKTIMER Timer, IN LARGE_INTEGER DueTime, IN LONG Period, IN PKDPC Dpc OPTIONAL)
Definition: timerobj.c:294
#define LOW_REALTIME_PRIORITY
#define STATUS_WAIT_1
Definition: ntstatus.h:71
#define STATUS_WAIT_0
Definition: ntstatus.h:223
#define FALSE
Definition: types.h:117
smooth NULL
Definition: ftsmooth.c:557
VOID NTAPI KiScanReadyQueues(IN PKDPC Dpc, IN PVOID DeferredContext, IN PVOID SystemArgument1, IN PVOID SystemArgument2)
Definition: balmgr.c:24
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
Definition: ketypes.h:672
Status
Definition: gdiplustypes.h:24
LONG NTSTATUS
Definition: DriverTester.h:11
KPRIORITY NTAPI KeSetPriorityThread(IN PKTHREAD Thread, IN KPRIORITY Priority)
Definition: thrdobj.c:1327
#define DPRINT1
Definition: precomp.h:8
_Must_inspect_result_ _In_ WAIT_TYPE _In_opt_ PLARGE_INTEGER _In_opt_ PKWAIT_BLOCK WaitBlockArray
Definition: fsrtlfuncs.h:1151
#define KeGetCurrentThread
Definition: hal.h:44
VOID NTAPI KeInitializeDpc(IN PKDPC Dpc, IN PKDEFERRED_ROUTINE DeferredRoutine, IN PVOID DeferredContext)
Definition: dpc.c:711
VOID NTAPI KeInitializeTimerEx(OUT PKTIMER Timer, IN TIMER_TYPE Type)
Definition: timerobj.c:244
LONGLONG QuadPart
Definition: typedefs.h:112
VOID NTAPI KiScanReadyQueues ( IN PKDPC  Dpc,
IN PVOID  DeferredContext,
IN PVOID  SystemArgument1,
IN PVOID  SystemArgument2 
)

Definition at line 24 of file balmgr.c.

Referenced by KeBalanceSetManager().

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;
36  KIRQL OldIrql;
37  PLIST_ENTRY ListHead, NextEntry;
39 
40  /* Lock the dispatcher and PRCB */
41  OldIrql = KiAcquireDispatcherLock();
42  KiAcquirePrcbLock(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;
76  ASSERT((Prcb->ReadySummary & PRIORITY_MASK(Index)));
77  if (RemoveEntryList(NextEntry->Flink))
78  {
79  /* The list is empty now */
80  Prcb->ReadySummary ^= PRIORITY_MASK(Index);
81  }
82 
83  /* Verify priority decrement and set the new one */
84  ASSERT((Thread->PriorityDecrement >= 0) &&
85  (Thread->PriorityDecrement <=
86  Thread->Priority));
88  Thread->Priority);
89  ASSERT((Thread->PriorityDecrement >= 0) &&
90  (Thread->PriorityDecrement <=
92 
93  /* Update priority and insert into ready list */
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);
113  KiReleaseDispatcherLock(OldIrql);
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 }
ULONG LowPart
Definition: ketypes.h:895
#define PRIORITY_MASK(Id)
Definition: ke.h:149
SCHAR PriorityDecrement
Definition: ketypes.h:1210
FORCEINLINE VOID KiReleasePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:224
ASSERT((InvokeOnSuccess||InvokeOnError||InvokeOnCancel)?(CompletionRoutine!=NULL):TRUE)
struct _LIST_ENTRY * Blink
Definition: typedefs.h:120
FORCEINLINE VOID KiInsertDeferredReadyList(IN PKTHREAD Thread)
Definition: ke_x.h:179
SCHAR Priority
Definition: ketypes.h:974
ULONG QueueIndex
Definition: ketypes.h:727
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
_Must_inspect_result_ _In_ ULONG Index
Definition: fltkernel.h:1824
_Must_inspect_result_ FORCEINLINE BOOLEAN IsListEmpty(_In_ const LIST_ENTRY *ListHead)
Definition: rtlfuncs.h:57
FORCEINLINE BOOLEAN RemoveEntryList(_In_ PLIST_ENTRY Entry)
Definition: rtlfuncs.h:105
UCHAR KIRQL
Definition: env_spec_w32.h:591
FORCEINLINE VOID KiAcquirePrcbLock(IN PKPRCB Prcb)
Definition: ke_x.h:214
struct _LIST_ENTRY * Flink
Definition: typedefs.h:119
#define THREAD_BOOST_PRIORITY
Definition: balmgr.c:17
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
#define WAIT_QUANTUM_DECREMENT
Definition: ketypes.h:137
NTKERNELAPI volatile KSYSTEM_TIME KeTickCount
Definition: clock.c:19
_Requires_lock_held_ Interrupt _Releases_lock_ Interrupt _In_ _IRQL_restores_ KIRQL OldIrql
Definition: kefuncs.h:803
Definition: typedefs.h:117
_In_opt_ PENTER_STATE_SYSTEM_HANDLER _In_opt_ PVOID _In_ LONG _In_opt_ LONG volatile * Number
Definition: ntpoapi.h:204
CCHAR KeNumberProcessors
Definition: krnlinit.c:35
ULONG ReadySummary
Definition: ketypes.h:726
FORCEINLINE VOID KiReleaseDispatcherLock(IN KIRQL OldIrql)
Definition: ke_x.h:152
FORCEINLINE KIRQL KiAcquireDispatcherLock(VOID)
Definition: ke_x.h:144
PKPRCB KiProcessorBlock[]
Definition: krnlinit.c:32
unsigned int * PULONG
Definition: retypes.h:1
LIST_ENTRY DispatcherReadyListHead[32]
Definition: ketypes.h:731
unsigned int ULONG
Definition: retypes.h:1
#define CONTAINING_RECORD(address, type, field)
Definition: typedefs.h:259
ULONG WaitTime
Definition: ketypes.h:1046
_In_opt_ PVOID DeferredContext
Definition: ketypes.h:660

Variable Documentation

ULONG KiReadyScanLast

Definition at line 18 of file balmgr.c.

Referenced by KeBalanceSetManager().