sched.c 5.4 KB

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