Skip to content

Conversation

assembler-0
Copy link
Owner

@assembler-0 assembler-0 commented Jul 10, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a multi-level feedback queue (MLFQ) scheduler for improved process management and prioritization.
    • Added new process scheduling metrics and priority-based queues.
    • Provided functions to view and manage scheduler state.
  • Bug Fixes

    • Corrected process state enumeration for better compatibility and clarity.
  • Refactor

    • Replaced the previous round-robin scheduler with a priority-based scheduling framework.
    • Updated process structures to support enhanced scheduling features.
  • Chores

    • Updated integer type definitions and macros for improved consistency.

Copy link

coderabbitai bot commented Jul 10, 2025

Walkthrough

This update replaces the previous round-robin scheduler with a multi-level feedback queue (MLFQ) scheduler in the kernel. It introduces new process scheduling structures, updates process metadata, and adjusts interrupt and process management logic to support the new scheduler. Integer typedefs and macros in the standard header are also revised.

Changes

File(s) Change Summary
Kernel/Core/stdint.h Added INT32_MAX/INT32_MIN macros; added signed integer typedefs (int8_t, int16_t, etc.) and static assertions (note: typedefs are incorrectly unsigned).
Kernel/Drivers/Interrupts.c Moved scheduling call in timer interrupt handler to occur before sending EOI to PIC.
Kernel/Process/Process.c Replaced round-robin scheduler with MLFQ scheduler; added/modified scheduling functions; updated process creation and blocking logic; added debugging and initialization support.
Kernel/Process/Process.h Reordered ProcessState enum; extended Process struct (weight, cpu_time_accumulated, dynamic_priority_score); added scheduler structs and function declarations; removed legacy Schedule().

Sequence Diagram(s)

sequenceDiagram
    participant TimerInterrupt
    participant ProcessScheduler
    participant ProcessQueue
    participant Process

    TimerInterrupt->>ProcessScheduler: FastSchedule(regs)
    ProcessScheduler->>ProcessQueue: Select highest-priority ready process
    ProcessQueue-->>ProcessScheduler: Next process slot
    ProcessScheduler->>Process: Save current context (if any)
    ProcessScheduler->>Process: Restore next process context
    ProcessScheduler->>ProcessQueue: Demote/boost priorities as needed
    ProcessScheduler->>TimerInterrupt: Return to interrupt handler
Loading

Possibly related PRs

  • Development #21: Refactors process slot allocation and modifies ScheduleFromInterrupt to implement a bitmap-based round-robin scheduler; both PRs alter scheduling logic in ScheduleFromInterrupt, but with different algorithms.

Poem

A scheduler hops with queues anew,
Priorities stacked in a clever MLFQ!
Processes leap, some high, some low,
With boosts and demotes as kernels flow.
The rabbit logs, "Integrity’s tight—
Now round-robin’s gone, and feedback is right!"
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🔭 Outside diff range comments (1)
Kernel/Process/Process.c (1)

449-454: Critical: GetCurrentProcess uses wrong variable

The function uses the old current_process variable instead of MLFQscheduler.current_running. This will return incorrect process information.

 Process* GetCurrentProcess(void) {
-    if (current_process >= MAX_PROCESSES) {
+    if (MLFQscheduler.current_running >= MAX_PROCESSES) {
         Panic("GetCurrentProcess: Invalid current process index");
     }
-    return &processes[current_process];
+    return &processes[MLFQscheduler.current_running];
 }
🧹 Nitpick comments (2)
Kernel/Process/Process.h (1)

51-53: Consider removing legacy scheduling fields

The weight and dynamic_priority_score fields are marked as legacy from the previous scheduler. Since you've implemented MLFQ, these fields appear to be unused.

Consider removing these fields if they're no longer needed:

-    uint32_t weight; // Base weight for scheduling (legacy - can remove later)
     uint64_t cpu_time_accumulated; // Accumulated CPU time
-    int32_t dynamic_priority_score; // Score for dynamic adjustment (legacy - can remove later)
Kernel/Process/Process.c (1)

12-14: Clean up legacy scheduler variables

Several variables from the old round-robin scheduler are still present. Consider removing unused ones and documenting why others remain.

The following appear to be legacy:

  • current_process: Should be replaced with MLFQscheduler.current_running
  • last_scheduled_slot: No longer used with MLFQ
  • need_schedule: Still used by RequestSchedule() but could be integrated into MLFQ

Note: active_process_bitmap is still used for slot allocation, so it should remain.

Also applies to: 21-22

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between bd563ae and 8477905.

📒 Files selected for processing (4)
  • Kernel/Core/stdint.h (2 hunks)
  • Kernel/Drivers/Interrupts.c (1 hunks)
  • Kernel/Process/Process.c (6 hunks)
  • Kernel/Process/Process.h (2 hunks)
🧰 Additional context used
🧠 Learnings (2)
Kernel/Drivers/Interrupts.c (1)
Learnt from: assembler-0
PR: assembler-0/VoidFrame#2
File: Kernel/Syscall.c:44-44
Timestamp: 2025-07-08T00:36:50.290Z
Learning: In IdtSetGate function calls, the parameters are: (interrupt_vector, handler_address, selector, flags). The interrupt vector (like 0x80) is the first argument, and the selector (like 0x08 for kernel code segment) is the third argument.
Kernel/Process/Process.c (1)
Learnt from: assembler-0
PR: assembler-0/VoidFrame#2
File: Kernel/Syscall.c:44-44
Timestamp: 2025-07-08T00:36:50.290Z
Learning: In IdtSetGate function calls, the parameters are: (interrupt_vector, handler_address, selector, flags). The interrupt vector (like 0x80) is the first argument, and the selector (like 0x08 for kernel code segment) is the third argument.
🧬 Code Graph Analysis (1)
Kernel/Drivers/Interrupts.c (2)
Kernel/Process/Process.h (1)
  • ScheduleFromInterrupt (93-93)
Kernel/Process/Process.c (1)
  • ScheduleFromInterrupt (431-433)
🔇 Additional comments (4)
Kernel/Drivers/Interrupts.c (1)

73-77: Good: Scheduling before EOI prevents interrupt nesting

Moving the scheduler call before the EOI command ensures that scheduling decisions happen atomically without the risk of nested timer interrupts. This is the correct interrupt handling pattern.

Kernel/Process/Process.h (1)

23-23: PROC_TERMINATED = 0 is correctly handled everywhere

All searches for comparisons and assignments involving PROC_TERMINATED (value 0) show consistent usage:

  • Syscall handlers set current->state = PROC_TERMINATED;
  • New process slots check state == PROC_TERMINATED or (pid == 0 && state == 0) before reuse
  • Cleanup and scheduler routines filter out state == PROC_TERMINATED
  • GetProcessByPid skips terminated processes

No mismatches or hidden assumptions were found. Approving this change as-is.

Kernel/Process/Process.c (2)

166-230: Consider interrupt safety for scheduler operations

The FastSchedule function modifies global scheduler state without explicit interrupt protection. While it's called from an interrupt handler (where interrupts are disabled), ensure this remains true for all code paths.

Document the interrupt safety assumptions or add explicit critical sections if needed for future-proofing.


298-305: Verify PROC_BLOCKED is the intended state for Yield

The Yield function marks the current process as PROC_BLOCKED, which triggers priority boosting. Typically, yielding keeps a process in the PROC_READY state.

Is this intentional behavior to boost cooperative processes? If not, consider using PROC_READY instead.

Comment on lines +24 to +34
typedef unsigned char int8_t;
_Static_assert(sizeof(int8_t) == 1, "sizeof(int8_t) != 1");

typedef unsigned short int16_t;
_Static_assert(sizeof(int16_t) == 2, "sizeof(int16_t) != 2");

typedef unsigned int int32_t;
_Static_assert(sizeof(int32_t) == 4, "sizeof(int32_t) != 4");

typedef unsigned long long int64_t;
_Static_assert(sizeof(int64_t) == 8, "sizeof(int64_t) != 8");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Signed integer types are incorrectly defined as unsigned types

All signed integer typedefs are incorrectly using unsigned base types. This will cause serious issues with negative values, arithmetic operations, and comparisons throughout the kernel.

Apply this diff to fix the type definitions:

-typedef unsigned char int8_t;
+typedef signed char int8_t;
 _Static_assert(sizeof(int8_t) == 1, "sizeof(int8_t) != 1");

-typedef unsigned short int16_t;
+typedef short int16_t;
 _Static_assert(sizeof(int16_t) == 2, "sizeof(int16_t) != 2");

-typedef unsigned int int32_t;
+typedef int int32_t;
 _Static_assert(sizeof(int32_t) == 4, "sizeof(int32_t) != 4");

-typedef unsigned long long int64_t;
+typedef long long int64_t;
 _Static_assert(sizeof(int64_t) == 8, "sizeof(int64_t) != 8");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
typedef unsigned char int8_t;
_Static_assert(sizeof(int8_t) == 1, "sizeof(int8_t) != 1");
typedef unsigned short int16_t;
_Static_assert(sizeof(int16_t) == 2, "sizeof(int16_t) != 2");
typedef unsigned int int32_t;
_Static_assert(sizeof(int32_t) == 4, "sizeof(int32_t) != 4");
typedef unsigned long long int64_t;
_Static_assert(sizeof(int64_t) == 8, "sizeof(int64_t) != 8");
typedef signed char int8_t;
_Static_assert(sizeof(int8_t) == 1, "sizeof(int8_t) != 1");
typedef short int16_t;
_Static_assert(sizeof(int16_t) == 2, "sizeof(int16_t) != 2");
typedef int int32_t;
_Static_assert(sizeof(int32_t) == 4, "sizeof(int32_t) != 4");
typedef long long int64_t;
_Static_assert(sizeof(int64_t) == 8, "sizeof(int64_t) != 8");
🤖 Prompt for AI Agents
In Kernel/Core/stdint.h around lines 24 to 34, the signed integer typedefs
int8_t, int16_t, int32_t, and int64_t are incorrectly defined as unsigned types.
To fix this, change the base types from unsigned char, unsigned short, unsigned
int, and unsigned long long to signed char, signed short, signed int, and signed
long long respectively, ensuring the types correctly represent signed integers.

Comment on lines 329 to 339
void ProcessInit(void) {
FastMemset(processes, 0, sizeof(Process) * MAX_PROCESSES);
active_process_bitmap = 0;
last_scheduled_slot = 0;

// Create idle process (PID 0)
// Initialize idle process
processes[0].pid = 0;
processes[0].state = PROC_RUNNING;
processes[0].priority = 255;
processes[0].privilege_level = PROC_PRIV_SYSTEM;
processes[0].is_user_mode = 0;

// Don't set bit 0 in bitmap - idle process is special
init_token(&processes[0].token, 0, PROC_PRIV_SYSTEM, 0);

InitScheduler(); // Initialize new scheduler
process_count = 1;
current_process = 0;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Initialize current_process variable

The current_process variable is not initialized but is still used by GetCurrentProcess(). This could lead to undefined behavior.

 void ProcessInit(void) {
     FastMemset(processes, 0, sizeof(Process) * MAX_PROCESSES);
 
     // Initialize idle process
     processes[0].pid = 0;
     processes[0].state = PROC_RUNNING;
     processes[0].privilege_level = PROC_PRIV_SYSTEM;
+    current_process = 0;  // Start with idle process
 
     InitScheduler();  // Initialize new scheduler
     process_count = 1;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void ProcessInit(void) {
FastMemset(processes, 0, sizeof(Process) * MAX_PROCESSES);
active_process_bitmap = 0;
last_scheduled_slot = 0;
// Create idle process (PID 0)
// Initialize idle process
processes[0].pid = 0;
processes[0].state = PROC_RUNNING;
processes[0].priority = 255;
processes[0].privilege_level = PROC_PRIV_SYSTEM;
processes[0].is_user_mode = 0;
// Don't set bit 0 in bitmap - idle process is special
init_token(&processes[0].token, 0, PROC_PRIV_SYSTEM, 0);
InitScheduler(); // Initialize new scheduler
process_count = 1;
current_process = 0;
}
void ProcessInit(void) {
FastMemset(processes, 0, sizeof(Process) * MAX_PROCESSES);
// Initialize idle process
processes[0].pid = 0;
processes[0].state = PROC_RUNNING;
processes[0].privilege_level = PROC_PRIV_SYSTEM;
current_process = 0; // Start with idle process
InitScheduler(); // Initialize new scheduler
process_count = 1;
}
🤖 Prompt for AI Agents
In Kernel/Process/Process.c around lines 329 to 339, the current_process
variable is not initialized in ProcessInit(), which can cause undefined behavior
when accessed by GetCurrentProcess(). Initialize current_process to point to the
idle process (processes[0]) after setting up the idle process and before calling
InitScheduler().

Comment on lines 399 to +403
processes[slot].priority = (privilege == PROC_PRIV_SYSTEM) ? 10 : 100;
processes[slot].is_user_mode = (privilege != PROC_PRIV_SYSTEM);
processes[slot].weight = (privilege == PROC_PRIV_SYSTEM) ? 100 : 50;
processes[slot].cpu_time_accumulated = 0;
processes[slot].dynamic_priority_score = 0;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect priority initialization

The priority values (10 for system, 100 for user) exceed the valid range (0-3 for MLFQ). Also, the legacy scheduling fields are initialized but unused.

     processes[slot].stack = stack;
     processes[slot].privilege_level = privilege;
-    processes[slot].priority = (privilege == PROC_PRIV_SYSTEM) ? 10 : 100;
+    processes[slot].priority = (privilege == PROC_PRIV_SYSTEM) ? 0 : 1;
     processes[slot].is_user_mode = (privilege != PROC_PRIV_SYSTEM);
-    processes[slot].weight = (privilege == PROC_PRIV_SYSTEM) ? 100 : 50;
-    processes[slot].cpu_time_accumulated = 0;
-    processes[slot].dynamic_priority_score = 0;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
processes[slot].priority = (privilege == PROC_PRIV_SYSTEM) ? 10 : 100;
processes[slot].is_user_mode = (privilege != PROC_PRIV_SYSTEM);
processes[slot].weight = (privilege == PROC_PRIV_SYSTEM) ? 100 : 50;
processes[slot].cpu_time_accumulated = 0;
processes[slot].dynamic_priority_score = 0;
processes[slot].stack = stack;
processes[slot].privilege_level = privilege;
processes[slot].priority = (privilege == PROC_PRIV_SYSTEM) ? 0 : 1;
processes[slot].is_user_mode = (privilege != PROC_PRIV_SYSTEM);
🤖 Prompt for AI Agents
In Kernel/Process/Process.c around lines 399 to 403, the priority values
assigned (10 for system, 100 for user) exceed the valid MLFQ range of 0 to 3,
and legacy scheduling fields like weight, cpu_time_accumulated, and
dynamic_priority_score are initialized but unused. Fix this by setting priority
to valid values within 0-3, for example 0 for system and 3 for user, and remove
or comment out the initialization of the unused legacy scheduling fields to
clean up the code.

@assembler-0 assembler-0 merged commit 790bbdc into main Jul 10, 2025
1 check passed
This was referenced Jul 11, 2025
@coderabbitai coderabbitai bot mentioned this pull request Aug 11, 2025
@coderabbitai coderabbitai bot mentioned this pull request Aug 23, 2025
This was referenced Sep 1, 2025
@coderabbitai coderabbitai bot mentioned this pull request Sep 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant