CoreProcessScheduler System Process Balancing

CoreProcessScheduler System Process Balancing

Why?

Some processes require more CPU time than others. Over time, balancing these demands improves overall system responsiveness and efficiency.

How?

The scheduler adjusts a process's PTime (Process Time) using its RTime (Run Time). If a process runs more than its allotted share, the system modifies both PTime and RTime, and adjusts the process's affinity accordingly.

UPDATE: A new variable UTime (Usage time) has been introduced to track the total time a process has been running, which helps in determining how much CPU time it should receive next.

Where?

View the implementation on NeKernel's GitHub Organization

Code Snippet

Taken from the dev branch commit 58d2af1:

if (UserProcessHelper::CanBeScheduled(process)) {
  /// ...
  
  if (UserProcessHelper::Switch(process.StackFrame, process.ProcessId)) {
    process.PTime = static_cast<Int32>(process.Affinity);

    if (process.PTime < process.RTime && AffinityKind::kRealTime = process.Affinity) {
      if (process.RTime < (Int32) AffinityKind::kVeryHigh)
        process.RTime = (Int32) AffinityKind::kLowUsage;
      else if (process.RTime < (Int32) AffinityKind::kHigh)
        process.RTime = (Int32) AffinityKind::kStandard;
      else if (process.RTime < (Int32) AffinityKind::kStandard)
        process.RTime = (Int32) AffinityKind::kHigh;

      process.PTime -= process.RTime;
      process.RTime= 0UL;
    }
  }
} else {
  ++process.RTime;
  --process.PTime;
}

Summary

This logic dynamically adapts the execution privileges of each process to avoid starvation and ensure real-time constraints are met, while still maintaining fairness across system workloads.