Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1030 lines
26 KiB

  1. /*++
  2. Copyright (c) 1991-1994 Microsoft Corporation
  3. Module Name:
  4. balmgr.c
  5. Abstract:
  6. This module implements the NT balance set manager. Normally the kernel
  7. does not contain "policy" code. However, the balance set manager needs
  8. to be able to traverse the kernel data structures and, therefore, the
  9. code has been located as logically part of the kernel.
  10. The balance set manager performs the following operations:
  11. 1. Makes the kernel stack of threads that have been waiting for a
  12. certain amount of time, nonresident.
  13. 2. Removes processes from the balance set when memory gets tight
  14. and brings processes back into the balance set when there is
  15. more memory available.
  16. 3. Makes the kernel stack resident for threads whose wait has been
  17. completed, but whose stack is nonresident.
  18. 4. Arbitrarily boosts the priority of a selected set of threads
  19. to prevent priority inversion in variable priority levels.
  20. In general, the balance set manager only is active during periods when
  21. memory is tight.
  22. Author:
  23. David N. Cutler (davec) 13-Jul-1991
  24. Environment:
  25. Kernel mode only.
  26. Revision History:
  27. --*/
  28. #include "ki.h"
  29. //
  30. // Define balance set wait object types.
  31. //
  32. typedef enum _BALANCE_OBJECT {
  33. TimerExpiration,
  34. WorkingSetManagerEvent,
  35. MaximumObject
  36. } BALANCE_OBJECT;
  37. //
  38. // Define maximum number of thread stacks that can be out swapped in
  39. // a single time period.
  40. //
  41. #define MAXIMUM_THREAD_STACKS 5
  42. //
  43. // Define periodic wait interval value.
  44. //
  45. #define PERIODIC_INTERVAL (1 * 1000 * 1000 * 10)
  46. //
  47. // Define amount of time a thread can be in the ready state without having
  48. // is priority boosted (approximately 4 seconds).
  49. //
  50. #define READY_WITHOUT_RUNNING (4 * 75)
  51. //
  52. // Define kernel stack protect time. For small systems the protect time
  53. // is 3 seconds. For all other systems, the protect time is 5x seconds.
  54. //
  55. #define SMALL_SYSTEM_STACK_PROTECT_TIME (3 * 75)
  56. #define LARGE_SYSTEM_STACK_PROTECT_TIME (SMALL_SYSTEM_STACK_PROTECT_TIME * 5)
  57. #define STACK_SCAN_PERIOD 4
  58. ULONG KiStackProtectTime;
  59. //
  60. // Define number of threads to scan each period and the priority boost bias.
  61. //
  62. #define THREAD_BOOST_BIAS 1
  63. #define THREAD_BOOST_PRIORITY (LOW_REALTIME_PRIORITY - THREAD_BOOST_BIAS)
  64. #define THREAD_SCAN_PRIORITY (THREAD_BOOST_PRIORITY - 1)
  65. #define THREAD_READY_COUNT 10
  66. #define THREAD_SCAN_COUNT 16
  67. //
  68. // Define the last processor examined.
  69. //
  70. ULONG KiLastProcessor = 0;
  71. ULONG KiReadyScanLast = 0;
  72. //
  73. // Define local procedure prototypes.
  74. //
  75. VOID
  76. KiAdjustIrpCredits (
  77. VOID
  78. );
  79. VOID
  80. KiInSwapKernelStacks (
  81. IN PSINGLE_LIST_ENTRY SwapEntry
  82. );
  83. VOID
  84. KiInSwapProcesses (
  85. IN PSINGLE_LIST_ENTRY SwapEntry
  86. );
  87. VOID
  88. KiOutSwapKernelStacks (
  89. VOID
  90. );
  91. VOID
  92. KiOutSwapProcesses (
  93. IN PSINGLE_LIST_ENTRY SwapEntry
  94. );
  95. VOID
  96. KiScanReadyQueues (
  97. IN PKDPC Dpc,
  98. IN PVOID DeferredContext,
  99. IN PVOID SystemArgument1,
  100. IN PVOID SystemArgument2
  101. );
  102. //
  103. // Define thread table index static data.
  104. //
  105. ULONG KiReadyQueueIndex = 1;
  106. //
  107. // Define swap request flag.
  108. //
  109. LONG KiStackOutSwapRequest = FALSE;
  110. VOID
  111. KeBalanceSetManager (
  112. IN PVOID Context
  113. )
  114. /*++
  115. Routine Description:
  116. This function is the startup code for the balance set manager. The
  117. balance set manager thread is created during system initialization
  118. and begins execution in this function.
  119. Arguments:
  120. Context - Supplies a pointer to an arbitrary data structure (NULL).
  121. Return Value:
  122. None.
  123. --*/
  124. {
  125. LARGE_INTEGER DueTime;
  126. KIRQL OldIrql;
  127. KTIMER PeriodTimer;
  128. KDPC ScanDpc;
  129. ULONG StackScanPeriod;
  130. ULONG StackScanTime;
  131. NTSTATUS Status;
  132. PKTHREAD Thread;
  133. KWAIT_BLOCK WaitBlockArray[MaximumObject];
  134. PVOID WaitObjects[MaximumObject];
  135. UNREFERENCED_PARAMETER(Context);
  136. //
  137. // Raise the thread priority to the lowest realtime level.
  138. //
  139. KeSetPriorityThread(KeGetCurrentThread(), LOW_REALTIME_PRIORITY);
  140. //
  141. // Initialize the periodic timer, initialize the ready queue scan DPC,
  142. // and set the periodic timer it to expire one period from now.
  143. //
  144. KeInitializeTimerEx(&PeriodTimer, SynchronizationTimer);
  145. KeInitializeDpc(&ScanDpc, &KiScanReadyQueues, NULL);
  146. DueTime.QuadPart = - PERIODIC_INTERVAL;
  147. KeSetTimerEx(&PeriodTimer,
  148. DueTime,
  149. PERIODIC_INTERVAL / (10 * 1000),
  150. &ScanDpc);
  151. //
  152. // Compute the stack protect and scan period time based on the system
  153. // size.
  154. //
  155. if (MmQuerySystemSize() == MmSmallSystem) {
  156. KiStackProtectTime = SMALL_SYSTEM_STACK_PROTECT_TIME;
  157. StackScanTime = STACK_SCAN_PERIOD;
  158. } else {
  159. KiStackProtectTime = LARGE_SYSTEM_STACK_PROTECT_TIME;
  160. StackScanTime = STACK_SCAN_PERIOD * 2;
  161. }
  162. StackScanPeriod = StackScanTime;
  163. //
  164. // Initialize the wait objects array.
  165. //
  166. WaitObjects[TimerExpiration] = (PVOID)&PeriodTimer;
  167. WaitObjects[WorkingSetManagerEvent] = (PVOID)&MmWorkingSetManagerEvent;
  168. //
  169. // Loop forever processing balance set manager events.
  170. //
  171. do {
  172. //
  173. // Wait for a memory management memory low event, a swap event,
  174. // or the expiration of the period timout rate that the balance
  175. // set manager runs at.
  176. //
  177. Status = KeWaitForMultipleObjects(MaximumObject,
  178. &WaitObjects[0],
  179. WaitAny,
  180. Executive,
  181. KernelMode,
  182. FALSE,
  183. NULL,
  184. &WaitBlockArray[0]);
  185. //
  186. // Switch on the wait status.
  187. //
  188. switch (Status) {
  189. //
  190. // Periodic timer expiration.
  191. //
  192. case TimerExpiration:
  193. //
  194. // Adjust I/O lookaside credits.
  195. //
  196. #if !defined(NT_UP)
  197. KiAdjustIrpCredits();
  198. #endif
  199. //
  200. // Adjust the depth of lookaside lists.
  201. //
  202. ExAdjustLookasideDepth();
  203. //
  204. // Execute the virtual memory working set manager.
  205. //
  206. MmWorkingSetManager();
  207. //
  208. // Attempt to initiate outswaping of kernel stacks.
  209. //
  210. // N.B. If outswapping is initiated, then the dispatcher
  211. // lock is not released until the wait at the top
  212. // of the loop is executed.
  213. //
  214. StackScanPeriod -= 1;
  215. if (StackScanPeriod == 0) {
  216. StackScanPeriod = StackScanTime;
  217. if (InterlockedCompareExchange(&KiStackOutSwapRequest,
  218. TRUE,
  219. FALSE) == FALSE) {
  220. KiLockDispatcherDatabase(&OldIrql);
  221. KiSetInternalEvent(&KiSwapEvent, KiSwappingThread);
  222. Thread = KeGetCurrentThread();
  223. Thread->WaitNext = TRUE;
  224. Thread->WaitIrql = OldIrql;
  225. }
  226. }
  227. break;
  228. //
  229. // Working set manager event.
  230. //
  231. case WorkingSetManagerEvent:
  232. //
  233. // Call the working set manager to trim working sets.
  234. //
  235. MmWorkingSetManager();
  236. break;
  237. //
  238. // Illegal return status.
  239. //
  240. default:
  241. KdPrint(("BALMGR: Illegal wait status, %lx =\n", Status));
  242. break;
  243. }
  244. } while (TRUE);
  245. return;
  246. }
  247. VOID
  248. KeSwapProcessOrStack (
  249. IN PVOID Context
  250. )
  251. /*++
  252. Routine Description:
  253. This thread controls the swapping of processes and kernel stacks. The
  254. order of evaluation is:
  255. Outswap kernel stacks
  256. Outswap processes
  257. Inswap processes
  258. Inswap kernel stacks
  259. Arguments:
  260. Context - Supplies a pointer to the routine context - not used.
  261. Return Value:
  262. None.
  263. --*/
  264. {
  265. PSINGLE_LIST_ENTRY SwapEntry;
  266. UNREFERENCED_PARAMETER(Context);
  267. //
  268. // Set address of swap thread object and raise the thread priority to
  269. // the lowest realtime level + 7 (i.e., priority 23).
  270. //
  271. KiSwappingThread = KeGetCurrentThread();
  272. KeSetPriorityThread(KeGetCurrentThread(), LOW_REALTIME_PRIORITY + 7);
  273. //
  274. // Loop for ever processing swap events.
  275. //
  276. // N.B. This is the ONLY thread that processes swap events.
  277. //
  278. do {
  279. //
  280. // Wait for a swap event to occur.
  281. //
  282. KeWaitForSingleObject(&KiSwapEvent,
  283. Executive,
  284. KernelMode,
  285. FALSE,
  286. NULL);
  287. //
  288. // The following events are processed one after the other. If
  289. // another event of a particular type arrives after having
  290. // processed the respective event type, them the swap event
  291. // will have been set and the above wait will immediately be
  292. // satisifed.
  293. //
  294. // Check to determine if there is a kernel stack out swap scan
  295. // request pending.
  296. //
  297. if (InterlockedCompareExchange(&KiStackOutSwapRequest,
  298. FALSE,
  299. TRUE) == TRUE) {
  300. KiOutSwapKernelStacks();
  301. }
  302. //
  303. // Check if there are any process out swap requests pending.
  304. //
  305. SwapEntry = InterlockedFlushSingleList(&KiProcessOutSwapListHead);
  306. if (SwapEntry != NULL) {
  307. KiOutSwapProcesses(SwapEntry);
  308. }
  309. //
  310. // Check if there are any process in swap requests pending.
  311. //
  312. SwapEntry = InterlockedFlushSingleList(&KiProcessInSwapListHead);
  313. if (SwapEntry != NULL) {
  314. KiInSwapProcesses(SwapEntry);
  315. }
  316. //
  317. // Check if there are any kernel stack in swap requests pending.
  318. //
  319. SwapEntry = InterlockedFlushSingleList(&KiStackInSwapListHead);
  320. if (SwapEntry != NULL) {
  321. KiInSwapKernelStacks(SwapEntry);
  322. }
  323. } while (TRUE);
  324. return;
  325. }
  326. #if !defined(NT_UP)
  327. VOID
  328. KiAdjustIrpCredits (
  329. VOID
  330. )
  331. /*++
  332. Routine Description:
  333. This function adjusts the lookaside IRP float credits for two processors
  334. during each one second scan interval. IRP credits are adjusted by using
  335. a moving average of two processors. It is possible for the IRP credits
  336. for a processor to become negative, but this condition will be self
  337. correcting.
  338. Arguments:
  339. None.
  340. Return Value:
  341. None.
  342. --*/
  343. {
  344. LONG Average;
  345. LONG Adjust;
  346. ULONG Index;
  347. ULONG Number;
  348. PKPRCB Prcb;
  349. LONG TotalAdjust;
  350. //
  351. // Adjust IRP credits if there are two or more processors.
  352. //
  353. Number = KeNumberProcessors;
  354. if (Number > 1) {
  355. //
  356. // Compute the target average value by averaging the IRP credits
  357. // across all processors.
  358. //
  359. Index = 0;
  360. Average = 0;
  361. do {
  362. Average += KiProcessorBlock[Index]->LookasideIrpFloat;
  363. Index += 1;
  364. } while (Index < Number);
  365. //
  366. // Adjust IRP credits for processor 0..n-1.
  367. //
  368. Average /= (LONG)Number;
  369. Number -= 1;
  370. Index = 0;
  371. TotalAdjust = 0;
  372. do {
  373. Prcb = KiProcessorBlock[Index];
  374. Adjust = Average - Prcb->LookasideIrpFloat;
  375. if (Adjust != 0) {
  376. InterlockedExchangeAdd(&Prcb->LookasideIrpFloat, Adjust);
  377. TotalAdjust += Adjust;
  378. }
  379. Index += 1;
  380. } while (Index < Number);
  381. //
  382. // Adjust IRP credit for last processor.
  383. //
  384. if (TotalAdjust != 0) {
  385. Prcb = KiProcessorBlock[Index];
  386. InterlockedExchangeAdd(&Prcb->LookasideIrpFloat, -TotalAdjust);
  387. }
  388. }
  389. return;
  390. }
  391. #endif
  392. VOID
  393. KiInSwapKernelStacks (
  394. IN PSINGLE_LIST_ENTRY SwapEntry
  395. )
  396. /*++
  397. Routine Description:
  398. This function in swaps the kernel stack for threads whose wait has been
  399. completed and whose kernel stack is nonresident.
  400. Arguments:
  401. SwapEntry - Supplies a pointer to the first entry in the in swap list.
  402. Return Value:
  403. None.
  404. --*/
  405. {
  406. KIRQL OldIrql;
  407. PKTHREAD Thread;
  408. //
  409. // Process the stack in swap SLIST and for each thread removed from the
  410. // SLIST, make its kernel stack resident, and ready it for execution.
  411. //
  412. do {
  413. Thread = CONTAINING_RECORD(SwapEntry, KTHREAD, SwapListEntry);
  414. SwapEntry = SwapEntry->Next;
  415. MmInPageKernelStack(Thread);
  416. KiLockDispatcherDatabase(&OldIrql);
  417. Thread->KernelStackResident = TRUE;
  418. KiInsertDeferredReadyList(Thread);
  419. KiUnlockDispatcherDatabase(OldIrql);
  420. } while (SwapEntry != NULL);
  421. return;
  422. }
  423. VOID
  424. KiInSwapProcesses (
  425. IN PSINGLE_LIST_ENTRY SwapEntry
  426. )
  427. /*++
  428. Routine Description:
  429. This function in swaps processes.
  430. Arguments:
  431. SwapEntry - Supplies a pointer to the first entry in the SLIST.
  432. Return Value:
  433. None.
  434. --*/
  435. {
  436. PLIST_ENTRY NextEntry;
  437. KIRQL OldIrql;
  438. PKPROCESS Process;
  439. PKTHREAD Thread;
  440. //
  441. // Process the process in swap list and for each process removed from
  442. // the list, make the process resident, and process its ready list.
  443. //
  444. do {
  445. Process = CONTAINING_RECORD(SwapEntry, KPROCESS, SwapListEntry);
  446. SwapEntry = SwapEntry->Next;
  447. Process->State = ProcessInSwap;
  448. MmInSwapProcess(Process);
  449. KiLockDispatcherDatabase(&OldIrql);
  450. Process->State = ProcessInMemory;
  451. NextEntry = Process->ReadyListHead.Flink;
  452. while (NextEntry != &Process->ReadyListHead) {
  453. Thread = CONTAINING_RECORD(NextEntry, KTHREAD, WaitListEntry);
  454. RemoveEntryList(NextEntry);
  455. Thread->ProcessReadyQueue = FALSE;
  456. KiReadyThread(Thread);
  457. NextEntry = Process->ReadyListHead.Flink;
  458. }
  459. KiUnlockDispatcherDatabase(OldIrql);
  460. } while (SwapEntry != NULL);
  461. return;
  462. }
  463. VOID
  464. KiOutSwapKernelStacks (
  465. VOID
  466. )
  467. /*++
  468. Routine Description:
  469. This function attempts to out swap the kernel stack for threads whose
  470. wait mode is user and which have been waiting longer than the stack
  471. protect time.
  472. Arguments:
  473. None.
  474. Return Value:
  475. None.
  476. --*/
  477. {
  478. PLIST_ENTRY NextEntry;
  479. ULONG NumberOfThreads;
  480. KIRQL OldIrql;
  481. PKPRCB Prcb;
  482. PKPROCESS Process;
  483. PKTHREAD Thread;
  484. PKTHREAD ThreadObjects[MAXIMUM_THREAD_STACKS];
  485. ULONG WaitLimit;
  486. //
  487. // Scan the waiting in list and check if the wait time exceeds the
  488. // stack protect time. If the protect time is exceeded, then make
  489. // the kernel stack of the waiting thread nonresident. If the count
  490. // of the number of stacks that are resident for the process reaches
  491. // zero, then insert the process in the outswap list and set its state
  492. // to transition.
  493. //
  494. // Raise IRQL and lock the dispatcher database.
  495. //
  496. NumberOfThreads = 0;
  497. Prcb = KiProcessorBlock[KiLastProcessor];
  498. WaitLimit = KiQueryLowTickCount() - KiStackProtectTime;
  499. KiLockDispatcherDatabase(&OldIrql);
  500. NextEntry = Prcb->WaitListHead.Flink;
  501. while ((NextEntry != &Prcb->WaitListHead) &&
  502. (NumberOfThreads < MAXIMUM_THREAD_STACKS)) {
  503. Thread = CONTAINING_RECORD(NextEntry, KTHREAD, WaitListEntry);
  504. ASSERT(Thread->WaitMode == UserMode);
  505. NextEntry = NextEntry->Flink;
  506. //
  507. // Threads are inserted at the end of the wait list in very nearly
  508. // reverse time order, i.e., the longest waiting thread is at the
  509. // beginning of the list followed by the next oldest, etc. Thus when
  510. // a thread is encountered which still has a protected stack it is
  511. // known that all further threads in the wait also have protected
  512. // stacks, and therefore, the scan can be terminated.
  513. //
  514. // N.B. It is possible due to a race condition in wait that a high
  515. // priority thread was placed in the wait list. If this occurs,
  516. // then the thread is removed from the wait list without swapping
  517. // the stack.
  518. //
  519. if (WaitLimit < Thread->WaitTime) {
  520. break;
  521. } else if (Thread->Priority >= (LOW_REALTIME_PRIORITY + 9)) {
  522. RemoveEntryList(&Thread->WaitListEntry);
  523. Thread->WaitListEntry.Flink = NULL;
  524. } else if (KiIsThreadNumericStateSaved(Thread)) {
  525. Thread->KernelStackResident = FALSE;
  526. ThreadObjects[NumberOfThreads] = Thread;
  527. NumberOfThreads += 1;
  528. RemoveEntryList(&Thread->WaitListEntry);
  529. Thread->WaitListEntry.Flink = NULL;
  530. Process = Thread->ApcState.Process;
  531. Process->StackCount -= 1;
  532. if (Process->StackCount == 0) {
  533. Process->State = ProcessOutTransition;
  534. InterlockedPushEntrySingleList(&KiProcessOutSwapListHead,
  535. &Process->SwapListEntry);
  536. KiSwapEvent.Header.SignalState = 1;
  537. }
  538. }
  539. }
  540. //
  541. // Unlock the dispatcher database and lower IRQL to its previous
  542. // value.
  543. //
  544. KiUnlockDispatcherDatabase(OldIrql);
  545. //
  546. // If the maximum number of threads is not being swapped, then increment
  547. // the last processor number.
  548. //
  549. KiLastProcessor += 1;
  550. if (KiLastProcessor == (ULONG)KeNumberProcessors) {
  551. KiLastProcessor = 0;
  552. }
  553. //
  554. // Out swap the kernel stack for the selected set of threads.
  555. //
  556. while (NumberOfThreads > 0) {
  557. NumberOfThreads -= 1;
  558. Thread = ThreadObjects[NumberOfThreads];
  559. //
  560. // Wait until the context has been swapped for the thread and outswap
  561. // the thread stack.
  562. //
  563. KeWaitForContextSwap(Thread);
  564. MmOutPageKernelStack(Thread);
  565. }
  566. return;
  567. }
  568. VOID
  569. KiOutSwapProcesses (
  570. IN PSINGLE_LIST_ENTRY SwapEntry
  571. )
  572. /*++
  573. Routine Description:
  574. This function out swaps processes.
  575. Arguments:
  576. SwapEntry - Supplies a pointer to the first entry in the SLIST.
  577. Return Value:
  578. None.
  579. --*/
  580. {
  581. PLIST_ENTRY NextEntry;
  582. KIRQL OldIrql;
  583. PKPROCESS Process;
  584. PKTHREAD Thread;
  585. //
  586. // Process the process out swap list and for each process removed from
  587. // the list, make the process nonresident, and process its ready list.
  588. //
  589. do {
  590. Process = CONTAINING_RECORD(SwapEntry, KPROCESS, SwapListEntry);
  591. SwapEntry = SwapEntry->Next;
  592. //
  593. // If there are any threads in the process ready list, then don't
  594. // out swap the process and ready all threads in the process ready
  595. // list. Otherwise, out swap the process.
  596. //
  597. KiLockDispatcherDatabase(&OldIrql);
  598. NextEntry = Process->ReadyListHead.Flink;
  599. if (NextEntry != &Process->ReadyListHead) {
  600. Process->State = ProcessInMemory;
  601. while (NextEntry != &Process->ReadyListHead) {
  602. Thread = CONTAINING_RECORD(NextEntry, KTHREAD, WaitListEntry);
  603. RemoveEntryList(NextEntry);
  604. Thread->ProcessReadyQueue = FALSE;
  605. KiReadyThread(Thread);
  606. NextEntry = Process->ReadyListHead.Flink;
  607. }
  608. KiUnlockDispatcherDatabase(OldIrql);
  609. } else {
  610. Process->State = ProcessOutSwap;
  611. KiUnlockDispatcherDatabase(OldIrql);
  612. MmOutSwapProcess(Process);
  613. //
  614. // While the process was being outswapped there may have been one
  615. // or more threads that attached to the process. If the process
  616. // ready list is not empty, then in swap the process. Otherwise,
  617. // mark the process as out of memory.
  618. //
  619. KiLockDispatcherDatabase(&OldIrql);
  620. NextEntry = Process->ReadyListHead.Flink;
  621. if (NextEntry != &Process->ReadyListHead) {
  622. Process->State = ProcessInTransition;
  623. InterlockedPushEntrySingleList(&KiProcessInSwapListHead,
  624. &Process->SwapListEntry);
  625. KiSwapEvent.Header.SignalState = 1;
  626. } else {
  627. Process->State = ProcessOutOfMemory;
  628. }
  629. KiUnlockDispatcherDatabase(OldIrql);
  630. }
  631. } while (SwapEntry != NULL);
  632. return;
  633. }
  634. VOID
  635. KiScanReadyQueues (
  636. IN PKDPC Dpc,
  637. IN PVOID DeferredContext,
  638. IN PVOID SystemArgument1,
  639. IN PVOID SystemArgument2
  640. )
  641. /*++
  642. Routine Description:
  643. This function scans a section of the ready queues and attempts to
  644. boost the priority of threads that run at variable priority levels.
  645. N.B. This function is executed as a DPC from the periodic timer that
  646. drives the balance set manager.
  647. Arguments:
  648. Dpc - Supplies a pointer to a DPC object - not used.
  649. DeferredContext - Supplies the DPC context - not used.
  650. SystemArgument1 - Supplies the first system argument - note used.
  651. SystemArgument2 - Supplies the second system argument - note used.
  652. Return Value:
  653. None.
  654. --*/
  655. {
  656. ULONG Count = 0;
  657. PLIST_ENTRY Entry;
  658. ULONG Index;
  659. PLIST_ENTRY ListHead;
  660. ULONG Number = 0;
  661. KIRQL OldIrql;
  662. PKPRCB Prcb;
  663. ULONG Summary;
  664. PKTHREAD Thread;
  665. ULONG WaitLimit;
  666. UNREFERENCED_PARAMETER(Dpc);
  667. UNREFERENCED_PARAMETER(DeferredContext);
  668. UNREFERENCED_PARAMETER(SystemArgument1);
  669. UNREFERENCED_PARAMETER(SystemArgument2);
  670. //
  671. // Lock the dispatcher database, acquire the PRCB lock, and check if
  672. // there are any ready threads queued at the scanable priority levels.
  673. //
  674. Index = KiReadyQueueIndex;
  675. Count = THREAD_READY_COUNT;
  676. Number = THREAD_SCAN_COUNT;
  677. Prcb = KiProcessorBlock[KiReadyScanLast];
  678. WaitLimit = KiQueryLowTickCount() - READY_WITHOUT_RUNNING;
  679. KiLockDispatcherDatabase(&OldIrql);
  680. KiAcquirePrcbLock(Prcb);
  681. Summary = Prcb->ReadySummary & ((1 << THREAD_BOOST_PRIORITY) - 2);
  682. if (Summary != 0) {
  683. do {
  684. //
  685. // If the current ready queue index is beyond the end of the range
  686. // of priorities that are scanned, then wrap back to the beginning
  687. // priority.
  688. //
  689. if (Index > THREAD_SCAN_PRIORITY) {
  690. Index = 1;
  691. }
  692. //
  693. // If there are any ready threads queued at the current priority
  694. // level, then attempt to boost the thread priority.
  695. //
  696. if (Summary & PRIORITY_MASK(Index)) {
  697. ASSERT(IsListEmpty(&Prcb->DispatcherReadyListHead[Index]) == FALSE);
  698. Summary ^= PRIORITY_MASK(Index);
  699. ListHead = &Prcb->DispatcherReadyListHead[Index];
  700. Entry = ListHead->Flink;
  701. do {
  702. //
  703. // If the thread has been waiting for an extended period,
  704. // then boost the priority of the selected.
  705. //
  706. Thread = CONTAINING_RECORD(Entry, KTHREAD, WaitListEntry);
  707. ASSERT(Thread->Priority == (KPRIORITY)Index);
  708. if (WaitLimit >= Thread->WaitTime) {
  709. //
  710. // Remove the thread from the respective ready queue.
  711. //
  712. Entry = Entry->Blink;
  713. ASSERT((Prcb->ReadySummary & PRIORITY_MASK(Index)) != 0);
  714. if (RemoveEntryList(Entry->Flink) != FALSE) {
  715. Prcb->ReadySummary ^= PRIORITY_MASK(Index);
  716. }
  717. //
  718. // Compute the priority decrement value, set the new
  719. // thread priority, set the thread quantum to a value
  720. // appropriate for lock ownership, and insert the
  721. // thread in the ready list.
  722. //
  723. ASSERT((Thread->PriorityDecrement >= 0) &&
  724. (Thread->PriorityDecrement <= Thread->Priority));
  725. Thread->PriorityDecrement +=
  726. (THREAD_BOOST_PRIORITY - Thread->Priority);
  727. ASSERT((Thread->PriorityDecrement >= 0) &&
  728. (Thread->PriorityDecrement <= THREAD_BOOST_PRIORITY));
  729. Thread->Priority = THREAD_BOOST_PRIORITY;
  730. Thread->Quantum = LOCK_OWNERSHIP_QUANTUM;
  731. KiInsertDeferredReadyList(Thread);
  732. Count -= 1;
  733. }
  734. Entry = Entry->Flink;
  735. Number -= 1;
  736. } while ((Entry != ListHead) && (Number != 0) && (Count != 0));
  737. }
  738. Index += 1;
  739. } while ((Summary != 0) && (Number != 0) && (Count != 0));
  740. }
  741. //
  742. // Release the PRCB lock, unlock the dispatcher database, and save the
  743. // last ready queue index for the next scan.
  744. //
  745. KiReleasePrcbLock(Prcb);
  746. KiUnlockDispatcherDatabase(OldIrql);
  747. if ((Count != 0) && (Number != 0)) {
  748. KiReadyQueueIndex = 1;
  749. KiReadyScanLast += 1;
  750. if (KiReadyScanLast == (ULONG)KeNumberProcessors) {
  751. KiReadyScanLast = 0;
  752. }
  753. } else {
  754. KiReadyQueueIndex = Index;
  755. }
  756. return;
  757. }