c97667f56bfcd5d9bd70862c7ff3ca2047f8f706
[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 static void real_time_delay (int64_t num, int32_t denom);
32
33 /* Sets up the timer to interrupt TIMER_FREQ times per second,
34    and registers the corresponding interrupt. */
35 void
36 timer_init (void) 
37 {
38   pit_configure_channel (0, 2, TIMER_FREQ);
39   intr_register_ext (0x20, timer_interrupt, "8254 Timer");
40 }
41
42 /* Calibrates loops_per_tick, used to implement brief delays. */
43 void
44 timer_calibrate (void) 
45 {
46   unsigned high_bit, test_bit;
47
48   ASSERT (intr_get_level () == INTR_ON);
49   printf ("Calibrating timer...  ");
50
51   /* Approximate loops_per_tick as the largest power-of-two
52      still less than one timer tick. */
53   loops_per_tick = 1u << 10;
54   while (!too_many_loops (loops_per_tick << 1)) 
55     {
56       loops_per_tick <<= 1;
57       ASSERT (loops_per_tick != 0);
58     }
59
60   /* Refine the next 8 bits of loops_per_tick. */
61   high_bit = loops_per_tick;
62   for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
63     if (!too_many_loops (high_bit | test_bit))
64       loops_per_tick |= test_bit;
65
66   printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
67 }
68
69 /* Returns the number of timer ticks since the OS booted. */
70 int64_t
71 timer_ticks (void) 
72 {
73   enum intr_level old_level = intr_disable ();
74   int64_t t = ticks;
75   intr_set_level (old_level);
76   barrier ();
77   return t;
78 }
79
80 /* Returns the number of timer ticks elapsed since THEN, which
81    should be a value once returned by timer_ticks(). */
82 int64_t
83 timer_elapsed (int64_t then) 
84 {
85   return timer_ticks () - then;
86 }
87
88 /* Sleeps for approximately TICKS timer ticks.  Interrupts must
89    be turned on. */
90 void
91 timer_sleep (int64_t ticks) 
92 {
93   int64_t start = timer_ticks ();
94
95   ASSERT (intr_get_level () == INTR_ON);
96   while (timer_elapsed (start) < ticks) 
97     thread_yield ();
98 }
99
100 /* Sleeps for approximately MS milliseconds.  Interrupts must be
101    turned on. */
102 void
103 timer_msleep (int64_t ms) 
104 {
105   real_time_sleep (ms, 1000);
106 }
107
108 /* Sleeps for approximately US microseconds.  Interrupts must be
109    turned on. */
110 void
111 timer_usleep (int64_t us) 
112 {
113   real_time_sleep (us, 1000 * 1000);
114 }
115
116 /* Sleeps for approximately NS nanoseconds.  Interrupts must be
117    turned on. */
118 void
119 timer_nsleep (int64_t ns) 
120 {
121   real_time_sleep (ns, 1000 * 1000 * 1000);
122 }
123
124 /* Busy-waits for approximately MS milliseconds.  Interrupts need
125    not be turned on.
126
127    Busy waiting wastes CPU cycles, and busy waiting with
128    interrupts off for the interval between timer ticks or longer
129    will cause timer ticks to be lost.  Thus, use timer_msleep()
130    instead if interrupts are enabled. */
131 void
132 timer_mdelay (int64_t ms) 
133 {
134   real_time_delay (ms, 1000);
135 }
136
137 /* Sleeps for approximately US microseconds.  Interrupts need not
138    be turned on.
139
140    Busy waiting wastes CPU cycles, and busy waiting with
141    interrupts off for the interval between timer ticks or longer
142    will cause timer ticks to be lost.  Thus, use timer_usleep()
143    instead if interrupts are enabled. */
144 void
145 timer_udelay (int64_t us) 
146 {
147   real_time_delay (us, 1000 * 1000);
148 }
149
150 /* Sleeps execution for approximately NS nanoseconds.  Interrupts
151    need not be turned on.
152
153    Busy waiting wastes CPU cycles, and busy waiting with
154    interrupts off for the interval between timer ticks or longer
155    will cause timer ticks to be lost.  Thus, use timer_nsleep()
156    instead if interrupts are enabled.*/
157 void
158 timer_ndelay (int64_t ns) 
159 {
160   real_time_delay (ns, 1000 * 1000 * 1000);
161 }
162
163 /* Prints timer statistics. */
164 void
165 timer_print_stats (void) 
166 {
167   printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
168 }
169 \f
170 /* Timer interrupt handler. */
171 static void
172 timer_interrupt (struct intr_frame *args UNUSED)
173 {
174   ticks++;
175   thread_tick ();
176 }
177
178 /* Returns true if LOOPS iterations waits for more than one timer
179    tick, otherwise false. */
180 static bool
181 too_many_loops (unsigned loops) 
182 {
183   /* Wait for a timer tick. */
184   int64_t start = ticks;
185   while (ticks == start)
186     barrier ();
187
188   /* Run LOOPS loops. */
189   start = ticks;
190   busy_wait (loops);
191
192   /* If the tick count changed, we iterated too long. */
193   barrier ();
194   return start != ticks;
195 }
196
197 /* Iterates through a simple loop LOOPS times, for implementing
198    brief delays.
199
200    Marked NO_INLINE because code alignment can significantly
201    affect timings, so that if this function was inlined
202    differently in different places the results would be difficult
203    to predict. */
204 static void NO_INLINE
205 busy_wait (int64_t loops) 
206 {
207   while (loops-- > 0)
208     barrier ();
209 }
210
211 /* Sleep for approximately NUM/DENOM seconds. */
212 static void
213 real_time_sleep (int64_t num, int32_t denom) 
214 {
215   /* Convert NUM/DENOM seconds into timer ticks, rounding down.
216           
217         (NUM / DENOM) s          
218      ---------------------- = NUM * TIMER_FREQ / DENOM ticks. 
219      1 s / TIMER_FREQ ticks
220   */
221   int64_t ticks = num * TIMER_FREQ / denom;
222
223   ASSERT (intr_get_level () == INTR_ON);
224   if (ticks > 0)
225     {
226       /* We're waiting for at least one full timer tick.  Use
227          timer_sleep() because it will yield the CPU to other
228          processes. */                
229       timer_sleep (ticks); 
230     }
231   else 
232     {
233       /* Otherwise, use a busy-wait loop for more accurate
234          sub-tick timing. */
235       real_time_delay (num, denom); 
236     }
237 }
238
239 /* Busy-wait for approximately NUM/DENOM seconds. */
240 static void
241 real_time_delay (int64_t num, int32_t denom)
242 {
243   /* Scale the numerator and denominator down by 1000 to avoid
244      the possibility of overflow. */
245   ASSERT (denom % 1000 == 0);
246   busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000)); 
247 }