Skip to content

分析当前调度策略,为 AxVisor 添加实时性 #352

Description

@ZCShou
No description provided.

Activity

  1. added theissue type on Jan 23, 2026
  2. Josen-B commented on Feb 6, 2026

    @Josen-B
    Contributor

    改进1:实现多队列调度(MQS)

    预期提升:40-60%(在高并发场景)

    实现思路:

    优势:

    • 不同优先级任务互不干扰

    • 高优先级任务响应快

    • 锁竞争减少

    MQS(多队列调度框架)
    ├── 实时队列(使用EDF调度)
    ├── 高优先级队列(使用RR调度)
    ├── 普通队列(使用CFS调度)
    └── 低优先级队列(使用FIFO调度)
    
    impl<T> HybridScheduler<T> {
        fn pick_next_task(&mut self) -> Option<Arc<HybridTask<T>>> {
            // 1. 实时队列
            if let Some(task) = self.realtime_queue.pop() {
                return Some(task);
            }
            
            // 2. 使用位图快速查找最高优先级
            if let Some(prio) = self.prio_bitmap.find_highest() {
                match prio {
                    0..=7 => {
                        // 高优先级队列
                        if let Some(task) = self.high_prio_queues[prio].pop_front() {
                            if self.high_prio_queues[prio].is_empty() {
                                self.prio_bitmap.clear(prio);
                            }
                            return Some(task);
                        }
                    }
                    8 => {
                        // 普通队列
                        if let Some((_, task)) = self.normal_queue.pop_first() {
                            if self.normal_queue.is_empty() {
                                self.prio_bitmap.clear(prio);
                            }
                            return Some(task);
                        }
                    }
                    9..=15 => {
                        // 低优先级队列
                        let idx = prio - 9;
                        if let Some(task) = self.low_prio_queues[idx].pop_front() {
                            if self.low_prio_queues[idx].is_empty() {
                                self.prio_bitmap.clear(prio);
                            }
                            return Some(task);
                        }
                    }
                    _ => {}
                }
            }
            
            None
        }
    }

    改进2:中断嵌套管理

    参考:FreeRTOS和Zephyr

    现状问题:

    • 当前ArceOS中断管理简单
    • 不支持中断优先级嵌套
    • 实时任务可能被低优先级中断阻塞

    预期提升:中断响应延迟降低50-70%

    /// 中断优先级
    #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
    pub enum IrqPriority {
        Critical = 0,      // 临界中断(不可嵌套)
        High = 1,           // 高优先级
        Medium = 2,         // 中优先级
        Low = 3,            // 低优先级
    }
    
    /// 中断嵌套控制器
    pub struct IrqNestingController {
        nesting_level: AtomicUsize,
        max_nesting: AtomicUsize,
        preemption_disabled: AtomicBool,
    }

    改进3:优先级位图优化

    参考:FreeRTOS和Zephyr

    预期提升:30-50%(调度查找延迟)

    se core::intrinsics::ctlz;
    
    pub struct PriorityBitmap {
        bitmap: AtomicUsize,
        num_levels: usize,
        level_size: usize,
    }
    
    impl PriorityBitmap {
        pub const fn new(num_levels: usize) -> Self {
            let level_size = (usize::BITS / num_levels).next_power_of_two();
            Self {
                bitmap: AtomicUsize::new(0),
                num_levels,
                level_size,
            }
        }
        
        /// 设置优先级位图
        pub fn set(&self, priority: usize) {
            self.bitmap.fetch_or(1usize << priority, Ordering::SeqCst);
        }
        
        /// 清除优先级位图
        pub fn clear(&self, priority: usize) {
            self.bitmap.fetch_and(!(1usize << priority), Ordering::SeqCst);
        }
        
        /// 查找最高优先级(使用CLZ指令)
        pub fn find_highest(&self) -> Option<usize> {
            let bitmap = self.bitmap.load(Ordering::Acquire);
            if bitmap == 0 {
                return None;
            }
            
            // 使用CPU的CLZ指令(Count Leading Zeros)
            // 复杂度:O(1)
            let leading_zeros = usize::BITS - bitmap.leading_zeros();
            Some(leading_zeros)
        }
        
        /// 查找最低优先级(使用CTZ指令)
        pub fn find_lowest(&self) -> Option<usize> {
            let bitmap = self.bitmap.load(Ordering::Acquire);
            if bitmap == 0 {
                return None;
            }
            
            // 使用CPU的CTZ指令(Count Trailing Zeros)
            // 复杂度:O(1)
            let trailing_zeros = bitmap.trailing_zeros();
            Some(trailing_zeros)
        }
    }

    改进4:实时调度器(EDF)

    预期提升:实时性提升100%+,吞吐量下降10-20%

    实现思路:

    优势:

    • 可保证实时任务截止时间
    • 预测性强
    pub struct EarliestDeadlineFirstScheduler<T> {
        ready_queue: BinaryHeap<Arc<EDFTask<T>>>,  // 最小堆
    }
    
    pub struct EDFTask<T> {
        inner: T,
        deadline: AtomicU64,
    }
    
    impl<T> BaseScheduler for EarliestDeadlineFirstScheduler<T> {
        fn pick_next_task(&mut self) -> Option<Arc<T>> {
            self.ready_queue.pop()  // 选择截止时间最早的任务
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions