clock.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <types.h>
  30. #include <lib.h>
  31. #include <cpu.h>
  32. #include <wchan.h>
  33. #include <clock.h>
  34. #include <thread.h>
  35. #include <lamebus/ltimer.h>
  36. #include <current.h>
  37. /*
  38. * Time handling.
  39. *
  40. * This is pretty primitive. A real kernel will typically have some
  41. * kind of support for scheduling callbacks to happen at specific
  42. * points in the future, usually with more resolution that one second.
  43. *
  44. * A real kernel also has to maintain the time of day; in OS/161 we
  45. * skimp on that because we have a known-good hardware clock.
  46. */
  47. /*
  48. * Timing constants. These should be tuned along with any work done on
  49. * the scheduler.
  50. */
  51. #define SCHEDULE_HARDCLOCKS 4 /* Reschedule every 4 hardclocks. */
  52. #define MIGRATE_HARDCLOCKS 16 /* Migrate every 16 hardclocks. */
  53. /*
  54. * Once a second, everything waiting on lbolt is awakened by CPU 0.
  55. */
  56. static struct wchan *lbolt;
  57. /*
  58. * Once every LT_GRANULARITY usec, everything on minibolt is awakenened by CPU 0
  59. */
  60. static struct wchan *minibolt;
  61. /*
  62. * number of minibolts per second
  63. */
  64. #define MINI_PER_SECOND (1000000/LT_GRANULARITY);
  65. /*
  66. * minibolt countdown
  67. */
  68. static int minicount;
  69. /*
  70. * Setup.
  71. */
  72. void
  73. hardclock_bootstrap(void)
  74. {
  75. lbolt = wchan_create("lbolt");
  76. if (lbolt == NULL) {
  77. panic("Couldn't create lbolt\n");
  78. }
  79. minibolt = wchan_create("minibolt");
  80. if (minibolt == NULL) {
  81. panic("Couldn't create minibolt\n");
  82. }
  83. minicount = MINI_PER_SECOND;
  84. /* we assume MINI_PER_SECOND > 0 */
  85. KASSERT(minicount > 0);
  86. }
  87. /*
  88. * This is called once every every LT_GRANULARITY usec, on one processor,
  89. * by the timer code.
  90. */
  91. void
  92. timerclock(void)
  93. {
  94. /* Broadcast on minibolt */
  95. wchan_wakeall(minibolt);
  96. /* Broadcast on lbolt if a second has elapsed */
  97. if (--minicount <= 0) {
  98. minicount = MINI_PER_SECOND;
  99. wchan_wakeall(lbolt);
  100. }
  101. }
  102. /*
  103. * This is called HZ times a second (on each processor) by the timer
  104. * code.
  105. */
  106. void
  107. hardclock(void)
  108. {
  109. /*
  110. * Collect statistics here as desired.
  111. */
  112. curcpu->c_hardclocks++;
  113. if ((curcpu->c_hardclocks % SCHEDULE_HARDCLOCKS) == 0) {
  114. schedule();
  115. }
  116. if ((curcpu->c_hardclocks % MIGRATE_HARDCLOCKS) == 0) {
  117. thread_consider_migration();
  118. }
  119. thread_yield();
  120. }
  121. /*
  122. * Suspend execution for n seconds.
  123. */
  124. void
  125. clocksleep(int num_secs)
  126. {
  127. while (num_secs > 0) {
  128. wchan_lock(lbolt);
  129. wchan_sleep(lbolt);
  130. num_secs--;
  131. }
  132. }
  133. /*
  134. * Suspend execution for num_ticks timer ticks.
  135. * (one tick every LT_GRANULARITY usec)
  136. */
  137. void
  138. clocknap(int num_ticks)
  139. {
  140. while (num_ticks > 0) {
  141. wchan_lock(minibolt);
  142. wchan_sleep(minibolt);
  143. num_ticks--;
  144. }
  145. }