sched.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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");
  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. 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);
  29. if (proc == &initial_proc_union.pcb)
  30. return;
  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. current_pcb->flags &= ~PROC_NEED_SCHED;
  48. struct process_control_block *proc = sched_cfs_dequeue();
  49. if (current_pcb->virtual_runtime >= proc->virtual_runtime) // 当前进程运行时间大于了下一进程的运行时间,进行切换
  50. {
  51. if (current_pcb->state = PROC_RUNNING) // 本次切换由于时间片到期引发,则再次加入就绪队列,否则交由其它功能模块进行管理
  52. sched_cfs_enqueue(current_pcb);
  53. if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
  54. {
  55. switch (proc->priority)
  56. {
  57. case 0:
  58. case 1:
  59. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = 4 / sched_cfs_ready_queue[proc_current_cpu_id].count;
  60. break;
  61. case 2:
  62. default:
  63. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = (4 / sched_cfs_ready_queue[proc_current_cpu_id].count) << 2;
  64. break;
  65. }
  66. }
  67. switch_proc(current_pcb, proc);
  68. }
  69. else // 不进行切换
  70. {
  71. // kdebug("not switch.");
  72. sched_cfs_enqueue(proc);
  73. if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
  74. {
  75. switch (proc->priority)
  76. {
  77. case 0:
  78. case 1:
  79. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = 4 / sched_cfs_ready_queue[proc_current_cpu_id].count;
  80. // sched_cfs_ready_queue.cpu_exec_proc_jiffies = 5;
  81. break;
  82. case 2:
  83. default:
  84. // sched_cfs_ready_queue.cpu_exec_proc_jiffies = 5;
  85. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies = (4 / sched_cfs_ready_queue[proc_current_cpu_id].count) << 2;
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. /**
  92. * @brief 当时钟中断到达时,更新时间片
  93. *
  94. */
  95. void sched_update_jiffies()
  96. {
  97. if(current_pcb->cpu_id == 0)
  98. return;
  99. switch (current_pcb->priority)
  100. {
  101. case 0:
  102. case 1:
  103. --sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies;
  104. ++current_pcb->virtual_runtime;
  105. break;
  106. case 2:
  107. default:
  108. sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies -= 2;
  109. current_pcb->virtual_runtime += 2;
  110. break;
  111. }
  112. // 时间片耗尽,标记可调度
  113. if (sched_cfs_ready_queue[proc_current_cpu_id].cpu_exec_proc_jiffies <= 0)
  114. current_pcb->flags |= PROC_NEED_SCHED;
  115. }
  116. /**
  117. * @brief 初始化进程调度器
  118. *
  119. */
  120. void sched_init()
  121. {
  122. memset(&sched_cfs_ready_queue, 0, sizeof(struct sched_queue_t) * MAX_CPU_NUM);
  123. for (int i = 0; i < MAX_CPU_NUM; ++i)
  124. {
  125. list_init(&sched_cfs_ready_queue[i].proc_queue.list);
  126. sched_cfs_ready_queue[i].count = 1; // 因为存在IDLE进程,因此为1
  127. sched_cfs_ready_queue[i].cpu_exec_proc_jiffies = 5;
  128. sched_cfs_ready_queue[i].proc_queue.virtual_runtime = 0x7fffffffffffffff;
  129. }
  130. }