From 1a2d4053843d10610531b3b0ec9ef8a6a481bc68 Mon Sep 17 00:00:00 2001 From: houmkh <100781004+houmkh@users.noreply.github.com> Date: Wed, 31 Aug 2022 07:59:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86=E5=BD=93=E5=89=8D=E7=9A=84sched=5Fcfs?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E5=81=9A=E4=B8=80=E4=B8=AA=E5=8C=85=E8=A3=B9?= =?UTF-8?q?=EF=BC=8C=E5=A5=97=E4=B8=80=E5=B1=82sched().=20(#32)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 将当前的sched_cfs函数做一个包裹,套一层sched(). * 用sched()包裹sched_cfs(),用sched_enqueue()包裹sched_cfs_enqueue() * 用sched()包裹sched_cfs(),用sched_enqueue()包裹sched_cfs_enqueue(),并替换 * bugfix: 修正拼写错误导致的编译错误 * 修正拼写错误 Co-authored-by: fslongjin --- kernel/common/mutex.c | 2 +- kernel/common/semaphore.c | 4 ++-- kernel/common/wait_queue.c | 6 +++--- kernel/driver/disk/ahci/ahci.c | 4 ++-- kernel/driver/interrupt/apic/apic.c | 2 +- kernel/exception/trap.c | 30 ++++++++++++++--------------- kernel/process/process.c | 6 +++--- kernel/sched/sched.c | 19 ++++++++++++++++++ kernel/sched/sched.h | 12 ++++++++++++ kernel/time/sleep.c | 2 +- 10 files changed, 59 insertions(+), 28 deletions(-) diff --git a/kernel/common/mutex.c b/kernel/common/mutex.c index a60009d0..a67e4695 100644 --- a/kernel/common/mutex.c +++ b/kernel/common/mutex.c @@ -17,7 +17,7 @@ void mutex_init(mutex_t *lock) static void __mutex_sleep() { current_pcb->state = PROC_UNINTERRUPTIBLE; - sched_cfs(); + sched(); } static void __mutex_acquire(mutex_t *lock) diff --git a/kernel/common/semaphore.c b/kernel/common/semaphore.c index f2f68d64..1f2edceb 100644 --- a/kernel/common/semaphore.c +++ b/kernel/common/semaphore.c @@ -18,7 +18,7 @@ void semaphore_down(semaphore_t *sema) list_append(&sema->wait_queue.wait_list, &wait.wait_list); // 执行调度 - sched_cfs(); + sched(); } } @@ -35,7 +35,7 @@ void semaphore_up(semaphore_t *sema) list_del(&wq->wait_list); wq->pcb->state = PROC_RUNNING; - sched_cfs_enqueue(wq->pcb); + sched_enqueue(wq->pcb); // 当前进程缺少需要的资源,立即标为需要被调度 current_pcb->flags |= PF_NEED_SCHED; diff --git a/kernel/common/wait_queue.c b/kernel/common/wait_queue.c index 86ee7a4e..ec7387ac 100644 --- a/kernel/common/wait_queue.c +++ b/kernel/common/wait_queue.c @@ -28,7 +28,7 @@ void wait_queue_sleep_on(wait_queue_node_t *wait_queue_head) current_pcb->state = PROC_UNINTERRUPTIBLE; list_append(&wait_queue_head->wait_list, &wait->wait_list); - sched_cfs(); + sched(); } /** @@ -44,7 +44,7 @@ void wait_queue_sleep_on_unlock(wait_queue_node_t *wait_queue_head, current_pcb->state = PROC_UNINTERRUPTIBLE; list_append(&wait_queue_head->wait_list, &wait->wait_list); spin_unlock((spinlock_t *)lock); - sched_cfs(); + sched(); } /** @@ -59,7 +59,7 @@ void wait_queue_sleep_on_interriptible(wait_queue_node_t *wait_queue_head) current_pcb->state = PROC_INTERRUPTIBLE; list_append(&wait_queue_head->wait_list, &wait->wait_list); - sched_cfs(); + sched(); } /** diff --git a/kernel/driver/disk/ahci/ahci.c b/kernel/driver/disk/ahci/ahci.c index b4f8e100..284c4b61 100644 --- a/kernel/driver/disk/ahci/ahci.c +++ b/kernel/driver/disk/ahci/ahci.c @@ -284,7 +284,7 @@ static bool ahci_read(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_t port->ci = 1 << slot; // Issue command current_pcb->flags |= PF_NEED_SCHED; - sched_cfs(); + sched(); int retval = AHCI_SUCCESS; // Wait for completion while (1) @@ -364,7 +364,7 @@ static bool ahci_write(HBA_PORT *port, uint32_t startl, uint32_t starth, uint32_ port->ci = 1; // Issue command current_pcb->flags |= PF_NEED_SCHED; - sched_cfs(); + sched(); int retval = AHCI_SUCCESS; while (1) diff --git a/kernel/driver/interrupt/apic/apic.c b/kernel/driver/interrupt/apic/apic.c index f77461db..eff58f7e 100644 --- a/kernel/driver/interrupt/apic/apic.c +++ b/kernel/driver/interrupt/apic/apic.c @@ -510,7 +510,7 @@ void do_IRQ(struct pt_regs *rsp, ul number) if (current_pcb->flags & PF_NEED_SCHED) { io_mfence(); - sched_cfs(); + sched(); } } diff --git a/kernel/exception/trap.c b/kernel/exception/trap.c index 1415f624..9c6e435f 100644 --- a/kernel/exception/trap.c +++ b/kernel/exception/trap.c @@ -12,7 +12,7 @@ void do_divide_error(struct pt_regs *regs, unsigned long error_code) kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\t pid=%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid); traceback(regs); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 1 #DB 调试异常 @@ -59,7 +59,7 @@ void do_overflow(struct pt_regs *regs, unsigned long error_code) printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 5 #BR 越界异常 @@ -79,7 +79,7 @@ void do_undefined_opcode(struct pt_regs *regs, unsigned long error_code) kerror("do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d, pid:%ld", error_code, regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid); traceback(regs); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 7 #NM 设备异常(FPU不存在) @@ -89,7 +89,7 @@ void do_dev_not_avaliable(struct pt_regs *regs, unsigned long error_code) kerror("do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 8 #DF 双重错误 @@ -101,7 +101,7 @@ void do_double_fault(struct pt_regs *regs, unsigned long error_code) printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); traceback(regs); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 9 协处理器越界(保留) @@ -111,7 +111,7 @@ void do_coprocessor_segment_overrun(struct pt_regs *regs, unsigned long error_co kerror("do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 10 #TS 无效的TSS段 @@ -142,7 +142,7 @@ void do_invalid_TSS(struct pt_regs *regs, unsigned long error_code) printk("\n"); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 11 #NP 段不存在 @@ -152,7 +152,7 @@ void do_segment_not_exists(struct pt_regs *regs, unsigned long error_code) kerror("do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 12 #SS SS段错误 @@ -162,7 +162,7 @@ void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code) kerror("do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 13 #GP 通用保护性异常 @@ -187,7 +187,7 @@ void do_general_protection(struct pt_regs *regs, unsigned long error_code) printk_color(RED, BLACK, "Segment Selector Index:%#010x\n", error_code & 0xfff8); traceback(regs); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 14 #PF 页故障 @@ -226,7 +226,7 @@ void do_page_fault(struct pt_regs *regs, unsigned long error_code) traceback(regs); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 15 Intel保留,请勿使用 @@ -248,7 +248,7 @@ void do_alignment_check(struct pt_regs *regs, unsigned long error_code) kerror("do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 18 #MC 机器检测 @@ -258,7 +258,7 @@ void do_machine_check(struct pt_regs *regs, unsigned long error_code) kerror("do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 19 #XM SIMD浮点异常 @@ -268,7 +268,7 @@ void do_SIMD_exception(struct pt_regs *regs, unsigned long error_code) kerror("do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 20 #VE 虚拟化异常 @@ -278,7 +278,7 @@ void do_virtualization_exception(struct pt_regs *regs, unsigned long error_code) kerror("do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip, proc_current_cpu_id); current_pcb->state = PROC_STOPPED; - sched_cfs(); + sched(); } // 21-21 Intel保留,请勿使用 diff --git a/kernel/process/process.c b/kernel/process/process.c index b6974c53..67a764f8 100644 --- a/kernel/process/process.c +++ b/kernel/process/process.c @@ -546,7 +546,7 @@ ul process_do_exit(ul code) sti(); process_exit_notify(); - sched_cfs(); + sched(); while (1) pause(); @@ -801,7 +801,7 @@ struct process_control_block *process_get_pcb(long pid) void process_wakeup(struct process_control_block *pcb) { pcb->state = PROC_RUNNING; - sched_cfs_enqueue(pcb); + sched_enqueue(pcb); } /** @@ -812,7 +812,7 @@ void process_wakeup(struct process_control_block *pcb) void process_wakeup_immediately(struct process_control_block *pcb) { pcb->state = PROC_RUNNING; - sched_cfs_enqueue(pcb); + sched_enqueue(pcb); // 将当前进程标志为需要调度,缩短新进程被wakeup的时间 current_pcb->flags |= PF_NEED_SCHED; } diff --git a/kernel/sched/sched.c b/kernel/sched/sched.c index ce28b508..142562ea 100644 --- a/kernel/sched/sched.c +++ b/kernel/sched/sched.c @@ -47,6 +47,16 @@ void sched_cfs_enqueue(struct process_control_block *pcb) ++sched_cfs_ready_queue[proc_current_cpu_id].count; } +/** + * @brief 包裹shced_cfs_enqueue(),将PCB加入就绪队列 + * + * @param pcb + */ +void sched_enqueue(struct process_control_block *pcb) +{ + sched_cfs_enqueue(pcb); +} + /** * @brief 调度函数 * @@ -118,6 +128,15 @@ void sched_cfs() sti(); } +/** + * @brief 包裹sched_cfs(),调度函数 + * + */ +void sched() +{ + sched_cfs(); +} + /** * @brief 当时钟中断到达时,更新时间片 * diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 371ba820..8159ae72 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -20,6 +20,12 @@ extern struct sched_queue_t sched_cfs_ready_queue[MAX_CPU_NUM]; // 就绪队列 */ void sched_cfs(); +/** + * @brief 包裹sched_cfs(),调度函数 + * + */ +void sched(); + /** * @brief 将PCB加入就绪队列 * @@ -27,6 +33,12 @@ void sched_cfs(); */ void sched_cfs_enqueue(struct process_control_block *pcb); +/** + * @brief 包裹sched_enqueue(),将PCB加入就绪队列 + * + * @param pcb + */ +void sched_enqueue(struct process_control_block *pcb); /** * @brief 从就绪队列中取出PCB diff --git a/kernel/time/sleep.c b/kernel/time/sleep.c index 05be1685..10f3bedc 100644 --- a/kernel/time/sleep.c +++ b/kernel/time/sleep.c @@ -57,7 +57,7 @@ int nanosleep(const struct timespec *rqtp, struct timespec *rmtp) current_pcb->state = PROC_INTERRUPTIBLE; current_pcb->flags |= PF_NEED_SCHED; - sched_cfs(); + sched(); // todo: 增加信号唤醒的功能后,设置rmtp