sched.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #include "sched.h"
  2. #include <common/kprint.h>
  3. struct sched_queue_t sched_cfs_ready_queue[MAX_CPU_NUM]; // 就绪队列
  4. /**
  5. * @brief 从就绪队列中取出PCB
  6. *
  7. * @return struct process_control_block*
  8. */
  9. struct process_control_block *sched_cfs_dequeue()
  10. {
  11. if (list_empty(&sched_cfs_ready_queue[proc_current_cpu_id].proc_queue.list))
  12. {
  13. // kdebug("list empty, count=%d", sched_cfs_ready_queue[proc_current_cpu_id].count);
  14. return &initial_proc_union.pcb;
  15. }
  16. struct process_control_block *proc = container_of(list_next(&sched_cfs_ready_queue[proc_current_cpu_id].proc_queue.list), struct process_control_block, list);
  17. list_del(&proc->list);
  18. --sched_cfs_ready_queue[proc_current_cpu_id].count;
  19. return proc;
  20. }
  21. /**
  22. * @brief 将PCB加入就绪队列
  23. *
  24. * @param pcb
  25. */
  26. void sched_cfs_enqueue(struct process_control_block *pcb)
  27. {
  28. if (pcb == initial_proc[proc_current_cpu_id])
  29. return;
  30. struct process_control_block *proc = container_of(list_next(&sched_cfs_ready_queue[proc_current_cpu_id].proc_queue.list), struct process_control_block, list);
  31. if ((list_empty(&sched_cfs_ready_queue[proc_current_cpu_id].proc_queue.list)) == 0)
  32. {
  33. while (proc->virtual_runtime < pcb->virtual_runtime)
  34. {
  35. proc = container_of(list_next(&proc->list), struct process_control_block, list);
  36. }
  37. }
  38. list_append(&proc->list, &pcb->list);
  39. ++sched_cfs_ready_queue[proc_current_cpu_id].count;
  40. }
  41. /**
  42. * @brief 调度函数
  43. *
  44. */
  45. void sched_cfs()
  46. {
  47. cli();
  48. current_pcb->flags &= ~PF_NEED_SCHED;
  49. struct process_control_block *proc = sched_cfs_dequeue();
  50. // kdebug("sched_cfs_ready_queue[proc_current_cpu_id].count = %d", sched_cfs_ready_queue[proc_current_cpu_id].count);
  51. if (current_pcb->virtual_runtime >= proc->virtual_runtime || current_pcb->state != PROC_RUNNING) // 当前进程运行时间大于了下一进程的运行时间,进行切换
  52. {
  53. if (current_pcb->state == PROC_RUNNING) // 本次切换由于时间片到期引发,则再次加入就绪队列,否则交由其它功能模块进行管理
  54. sched_cfs_enqueue(current_pcb);
  55. // kdebug("proc->pid=%d, count=%d", proc->pid, sched_cfs_ready_queue[proc_current_cpu_id].count);
  56. if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
  57. {
  58. switch (proc->priority)
  59. {
  60. case 0:
  61. case 1:
  62. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = 4 / sched_cfs_ready_queue[proc_current_cpu_id].count;
  63. break;
  64. case 2:
  65. default:
  66. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = (4 / sched_cfs_ready_queue[proc_current_cpu_id].count) << 2;
  67. break;
  68. }
  69. }
  70. // kdebug("before switch, next.rip = %#018lx\tnext->gs=%#018lx", proc->thread->rip, proc->thread->gs);
  71. process_switch_mm(proc);
  72. switch_proc(current_pcb, proc);
  73. }
  74. else // 不进行切换
  75. {
  76. // kdebug("not switch.");
  77. sched_cfs_enqueue(proc);
  78. if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
  79. {
  80. switch (proc->priority)
  81. {
  82. case 0:
  83. case 1:
  84. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = 4 / sched_cfs_ready_queue[proc_current_cpu_id].count;
  85. // sched_cfs_ready_queue.cpu_exec_proc_jiffies = 5;
  86. break;
  87. case 2:
  88. default:
  89. // sched_cfs_ready_queue.cpu_exec_proc_jiffies = 5;
  90. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = (4 / sched_cfs_ready_queue[proc_current_cpu_id].count) << 2;
  91. break;
  92. }
  93. }
  94. }
  95. sti();
  96. }
  97. /**
  98. * @brief 当时钟中断到达时,更新时间片
  99. *
  100. */
  101. void sched_update_jiffies()
  102. {
  103. // if (current_pcb->cpu_id == 0)
  104. // return;
  105. switch (current_pcb->priority)
  106. {
  107. case 0:
  108. case 1:
  109. --sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies;
  110. ++current_pcb->virtual_runtime;
  111. break;
  112. case 2:
  113. default:
  114. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies -= 2;
  115. current_pcb->virtual_runtime += 2;
  116. break;
  117. }
  118. // 时间片耗尽,标记可调度
  119. if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
  120. current_pcb->flags |= PF_NEED_SCHED;
  121. }
  122. /**
  123. * @brief 初始化进程调度器
  124. *
  125. */
  126. void sched_init()
  127. {
  128. memset(&sched_cfs_ready_queue, 0, sizeof(struct sched_queue_t) * MAX_CPU_NUM);
  129. for (int i = 0; i < MAX_CPU_NUM; ++i)
  130. {
  131. list_init(&sched_cfs_ready_queue[i].proc_queue.list);
  132. sched_cfs_ready_queue[i].count = 1; // 因为存在IDLE进程,因此为1
  133. sched_cfs_ready_queue[i].cpu_exec_proc_jiffies = 5;
  134. sched_cfs_ready_queue[i].proc_queue.virtual_runtime = 0x7fffffffffffffff;
  135. }
  136. }