1 #include "devices/timer.h"
6 #include "devices/pit.h"
7 #include "threads/interrupt.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
11 /* See [8254] for hardware details of the 8254 timer chip. */
14 #error 8254 timer requires TIMER_FREQ >= 19
17 #error TIMER_FREQ <= 1000 recommended
20 /* Number of timer ticks since OS booted. */
23 /* Number of loops per timer tick.
24 Initialized by timer_calibrate(). */
25 static unsigned loops_per_tick;
27 static intr_handler_func timer_interrupt;
28 static bool too_many_loops (unsigned loops);
29 static void busy_wait (int64_t loops);
30 static void real_time_sleep (int64_t num, int32_t denom);
32 /* Sets up the timer to interrupt TIMER_FREQ times per second,
33 and registers the corresponding interrupt. */
37 pit_configure_channel (0, 2, TIMER_FREQ);
38 intr_register_ext (0x20, timer_interrupt, "8254 Timer");
41 /* Calibrates loops_per_tick, used to implement brief delays. */
43 timer_calibrate (void)
45 unsigned high_bit, test_bit;
47 ASSERT (intr_get_level () == INTR_ON);
48 printf ("Calibrating timer... ");
50 /* Approximate loops_per_tick as the largest power-of-two
51 still less than one timer tick. */
52 loops_per_tick = 1u << 10;
53 while (!too_many_loops (loops_per_tick << 1))
56 ASSERT (loops_per_tick != 0);
59 /* Refine the next 8 bits of loops_per_tick. */
60 high_bit = loops_per_tick;
61 for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
62 if (!too_many_loops (high_bit | test_bit))
63 loops_per_tick |= test_bit;
65 printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
68 /* Returns the number of timer ticks since the OS booted. */
72 enum intr_level old_level = intr_disable ();
74 intr_set_level (old_level);
79 /* Returns the number of timer ticks elapsed since THEN, which
80 should be a value once returned by timer_ticks(). */
82 timer_elapsed (int64_t then)
84 return timer_ticks () - then;
87 /* Suspends execution for approximately TICKS timer ticks. */
89 timer_sleep (int64_t ticks)
91 int64_t start = timer_ticks ();
93 ASSERT (intr_get_level () == INTR_ON);
94 while (timer_elapsed (start) < ticks)
98 /* Suspends execution for approximately MS milliseconds. */
100 timer_msleep (int64_t ms)
102 real_time_sleep (ms, 1000);
105 /* Suspends execution for approximately US microseconds. */
107 timer_usleep (int64_t us)
109 real_time_sleep (us, 1000 * 1000);
112 /* Suspends execution for approximately NS nanoseconds. */
114 timer_nsleep (int64_t ns)
116 real_time_sleep (ns, 1000 * 1000 * 1000);
119 /* Prints timer statistics. */
121 timer_print_stats (void)
123 printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
126 /* Timer interrupt handler. */
128 timer_interrupt (struct intr_frame *args UNUSED)
134 /* Returns true if LOOPS iterations waits for more than one timer
135 tick, otherwise false. */
137 too_many_loops (unsigned loops)
139 /* Wait for a timer tick. */
140 int64_t start = ticks;
141 while (ticks == start)
144 /* Run LOOPS loops. */
148 /* If the tick count changed, we iterated too long. */
150 return start != ticks;
153 /* Iterates through a simple loop LOOPS times, for implementing
156 Marked NO_INLINE because code alignment can significantly
157 affect timings, so that if this function was inlined
158 differently in different places the results would be difficult
160 static void NO_INLINE
161 busy_wait (int64_t loops)
167 /* Sleep for approximately NUM/DENOM seconds. */
169 real_time_sleep (int64_t num, int32_t denom)
171 /* Convert NUM/DENOM seconds into timer ticks, rounding down.
174 ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
175 1 s / TIMER_FREQ ticks
177 int64_t ticks = num * TIMER_FREQ / denom;
179 ASSERT (intr_get_level () == INTR_ON);
182 /* We're waiting for at least one full timer tick. Use
183 timer_sleep() because it will yield the CPU to other
189 /* Otherwise, use a busy-wait loop for more accurate
190 sub-tick timing. We scale the numerator and denominator
191 down by 1000 to avoid the possibility of overflow. */
192 ASSERT (denom % 1000 == 0);
193 busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000));