a83a588d6884dc271bca00909148c0348a0b986a
[pintos-anon] / src / devices / timer.c
1 #include "devices/timer.h"
2 #include <debug.h>
3 #include <inttypes.h>
4 #include <round.h>
5 #include <stdio.h>
6 #include "devices/pit.h"
7 #include "threads/interrupt.h"
8 #include "threads/synch.h"
9 #include "threads/thread.h"
10   
11 /* See [8254] for hardware details of the 8254 timer chip. */
12
13 #if TIMER_FREQ < 19
14 #error 8254 timer requires TIMER_FREQ >= 19
15 #endif
16 #if TIMER_FREQ > 1000
17 #error TIMER_FREQ <= 1000 recommended
18 #endif
19
20 /* Number of timer ticks since OS booted. */
21 static int64_t ticks;
22
23 /* Number of loops per timer tick.
24    Initialized by timer_calibrate(). */
25 static unsigned loops_per_tick;
26
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);
31
32 /* Sets up the timer to interrupt TIMER_FREQ times per second,
33    and registers the corresponding interrupt. */
34 void
35 timer_init (void) 
36 {
37   pit_configure_channel (0, 2, TIMER_FREQ);
38   intr_register_ext (0x20, timer_interrupt, "8254 Timer");
39 }
40
41 /* Calibrates loops_per_tick, used to implement brief delays. */
42 void
43 timer_calibrate (void) 
44 {
45   unsigned high_bit, test_bit;
46
47   ASSERT (intr_get_level () == INTR_ON);
48   printf ("Calibrating timer...  ");
49
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)) 
54     {
55       loops_per_tick <<= 1;
56       ASSERT (loops_per_tick != 0);
57     }
58
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;
64
65   printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
66 }
67
68 /* Returns the number of timer ticks since the OS booted. */
69 int64_t
70 timer_ticks (void) 
71 {
72   enum intr_level old_level = intr_disable ();
73   int64_t t = ticks;
74   intr_set_level (old_level);
75   barrier ();
76   return t;
77 }
78
79 /* Returns the number of timer ticks elapsed since THEN, which
80    should be a value once returned by timer_ticks(). */
81 int64_t
82 timer_elapsed (int64_t then) 
83 {
84   return timer_ticks () - then;
85 }
86
87 /* Suspends execution for approximately TICKS timer ticks. */
88 void
89 timer_sleep (int64_t ticks) 
90 {
91   int64_t start = timer_ticks ();
92
93   ASSERT (intr_get_level () == INTR_ON);
94   while (timer_elapsed (start) < ticks) 
95     thread_yield ();
96 }
97
98 /* Suspends execution for approximately MS milliseconds. */
99 void
100 timer_msleep (int64_t ms) 
101 {
102   real_time_sleep (ms, 1000);
103 }
104
105 /* Suspends execution for approximately US microseconds. */
106 void
107 timer_usleep (int64_t us) 
108 {
109   real_time_sleep (us, 1000 * 1000);
110 }
111
112 /* Suspends execution for approximately NS nanoseconds. */
113 void
114 timer_nsleep (int64_t ns) 
115 {
116   real_time_sleep (ns, 1000 * 1000 * 1000);
117 }
118
119 /* Prints timer statistics. */
120 void
121 timer_print_stats (void) 
122 {
123   printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
124 }
125 \f
126 /* Timer interrupt handler. */
127 static void
128 timer_interrupt (struct intr_frame *args UNUSED)
129 {
130   ticks++;
131   thread_tick ();
132 }
133
134 /* Returns true if LOOPS iterations waits for more than one timer
135    tick, otherwise false. */
136 static bool
137 too_many_loops (unsigned loops) 
138 {
139   /* Wait for a timer tick. */
140   int64_t start = ticks;
141   while (ticks == start)
142     barrier ();
143
144   /* Run LOOPS loops. */
145   start = ticks;
146   busy_wait (loops);
147
148   /* If the tick count changed, we iterated too long. */
149   barrier ();
150   return start != ticks;
151 }
152
153 /* Iterates through a simple loop LOOPS times, for implementing
154    brief delays.
155
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
159    to predict. */
160 static void NO_INLINE
161 busy_wait (int64_t loops) 
162 {
163   while (loops-- > 0)
164     barrier ();
165 }
166
167 /* Sleep for approximately NUM/DENOM seconds. */
168 static void
169 real_time_sleep (int64_t num, int32_t denom) 
170 {
171   /* Convert NUM/DENOM seconds into timer ticks, rounding down.
172           
173         (NUM / DENOM) s          
174      ---------------------- = NUM * TIMER_FREQ / DENOM ticks. 
175      1 s / TIMER_FREQ ticks
176   */
177   int64_t ticks = num * TIMER_FREQ / denom;
178
179   ASSERT (intr_get_level () == INTR_ON);
180   if (ticks > 0)
181     {
182       /* We're waiting for at least one full timer tick.  Use
183          timer_sleep() because it will yield the CPU to other
184          processes. */                
185       timer_sleep (ticks); 
186     }
187   else 
188     {
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)); 
194     }
195 }
196