Skip to content

Conversation

@sparkzky
Copy link
Collaborator

@sparkzky sparkzky commented Nov 29, 2025

初步的cpu多核心上的负载均衡

目前DragonOS启动时会有两个CPU核心,但是之前进程只会调度到第一个核心上运行,第二个核心空转,现在在调度进程之前加入核心选择,进程将调度到当前负载较低的核心上执行

核心选择策略:

优先级从高到低:
1. 负载均衡未启用 → 返回当前CPU
2. 单核系统 → 返回当前CPU
3. IDLE策略任务 → 找最空闲CPU
4. 当前CPU负载 < 原CPU负载 → 当前CPU(缓存热度)
5. 原CPU负载 ≤ 2 → 原CPU(缓存亲和性)
6. 当前CPU负载 ≤ 1 → 当前CPU
7. 默认 → 找负载最低的CPU

Copilot AI review requested due to automatic review settings November 29, 2025 13:52
@github-actions github-actions bot added the enhancement New feature or request label Nov 29, 2025
Copilot finished reviewing on behalf of sparkzky November 29, 2025 13:54
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements initial multi-core CPU load balancing for the DragonOS scheduler. The feature enables tasks to be distributed across multiple CPU cores based on load conditions, with a priority-based selection strategy that balances performance (cache locality) against load distribution.

Key changes include:

  • Added a new load balancer module with CPU selection logic for task wakeup
  • Integrated load balancing into the scheduler tick and process wakeup paths
  • Added test programs to verify load distribution across CPUs

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
kernel/src/sched/load_balance.rs New module implementing CPU selection strategy and load balancing logic
kernel/src/sched/mod.rs Integrated load balancer into scheduler tick, added nr_running_lockless() method and migration flag handling
kernel/src/process/mod.rs Updated process wakeup to use load balancer for CPU selection with migration support
kernel/src/smp/mod.rs Enabled load balancing after SMP initialization
kernel/src/smp/core.rs Updated documentation comment style from @brief to ##
user/apps/c_unitest/test_smp_balance.c Simple test program to verify multi-core load balancing
user/apps/c_unitest/test_load_balance.c Comprehensive test suite for load balancing functionality

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

pthread_t threads[NUM_WORKERS];
int thread_ids[NUM_WORKERS];
int i;
int cpu_usage[2] = {0}; /* 假设最多2个CPU */
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

Array size mismatch: cpu_usage is declared with size 2 but later code accesses indices up to 16 (lines 160-162, 166-169). This will cause buffer overflow if final_cpu >= 2. The array size should be at least 16 to match the loop bounds.

Suggested change
int cpu_usage[2] = {0}; /* 假设最多2个CPU */
int cpu_usage[16] = {0}; /* 支持最多16个CPU */

Copilot uses AI. Check for mistakes.
result = 1;
}

// 这个测例会报错,先注释掉
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

[nitpick] Corrected spelling of '测例' (test case) to '测试用例' for consistency with standard terminology.

Suggested change
// 这个测例会报错,先注释掉
// 这个测试用例会报错,先注释掉

Copilot uses AI. Check for mistakes.

use super::{cpu_rq, CpuRunQueue, DequeueFlag, EnqueueFlag, SchedPolicy};

/// ## 负载均衡间隔(单位:jiffies),执行一次负载均衡检查
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment says 'execute one load balance check' but doesn't clarify what happens after the interval. Consider rephrasing to '每隔100个jiffies执行一次负载均衡检查' (execute load balance check every 100 jiffies) for clarity.

Suggested change
/// ## 负载均衡间隔(单位:jiffies),执行一次负载均衡检查
/// ## 负载均衡间隔(单位:jiffies),每隔100个jiffies执行一次负载均衡检查

Copilot uses AI. Check for mistakes.
drop(guard);
// TODO:处理负载均衡

//todo 检查并执行负载均衡(只检测不均衡,migrate_tasks 内部是空的)
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

The TODO comment contains important implementation status (that migrate_tasks is empty), but uses lowercase 'todo' instead of the standard 'TODO:' format. For consistency with other TODOs in the codebase, use 'TODO:' prefix.

Suggested change
//todo 检查并执行负载均衡(只检测不均衡,migrate_tasks 内部是空的)
// TODO: 检查并执行负载均衡(只检测不均衡,migrate_tasks 内部是空的)

Copilot uses AI. Check for mistakes.
}

// 如果原CPU负载不高,保持缓存亲和性
if prev_load <= 2 {
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

[nitpick] Magic number 2 used for load threshold without explanation. Consider defining a named constant like CACHE_HOT_LOAD_THRESHOLD to make the cache affinity strategy more explicit.

Copilot uses AI. Check for mistakes.
}

// 如果当前CPU负载低,直接使用当前cpu即可
if current_load <= 1 {
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

[nitpick] Magic number 1 used for load threshold without explanation. Consider defining a named constant like IDLE_LOAD_THRESHOLD to clarify when a CPU is considered idle enough for immediate use.

Copilot uses AI. Check for mistakes.
let current_load = Self::get_rq_load_lockless(&current_rq);

// 如果当前CPU负载很高,不主动拉取任务
if current_load > 2 {
Copy link

Copilot AI Nov 29, 2025

Choose a reason for hiding this comment

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

[nitpick] Magic number 2 appears again for load threshold. This should use the same named constant as suggested for line 103 to maintain consistency in the load balancing strategy.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant