Improve comments.
[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 "threads/interrupt.h"
7 #include "threads/io.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 8254 Programmable Interval Timer (PIT) to
33    interrupt PIT_FREQ times per second, and registers the
34    corresponding interrupt. */
35 void
36 timer_init (void) 
37 {
38   /* 8254 input frequency divided by TIMER_FREQ, rounded to
39      nearest. */
40   uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
41
42   outb (0x43, 0x34);    /* CW: counter 0, LSB then MSB, mode 2, binary. */
43   outb (0x40, count & 0xff);
44   outb (0x40, count >> 8);
45
46   intr_register_ext (0x20, timer_interrupt, "8254 Timer");
47 }
48
49 /* Calibrates loops_per_tick, used to implement brief delays. */
50 void
51 timer_calibrate (void) 
52 {
53   unsigned high_bit, test_bit;
54
55   ASSERT (intr_get_level () == INTR_ON);
56   printf ("Calibrating timer...  ");
57
58   /* Approximate loops_per_tick as the largest power-of-two
59      still less than one timer tick. */
60   loops_per_tick = 1u << 10;
61   while (!too_many_loops (loops_per_tick << 1)) 
62     {
63       loops_per_tick <<= 1;
64       ASSERT (loops_per_tick != 0);
65     }
66
67   /* Refine the next 8 bits of loops_per_tick. */
68   high_bit = loops_per_tick;
69   for (test_bit = high_bit >> 1; test_bit != high_bit >> 10; test_bit >>= 1)
70     if (!too_many_loops (high_bit | test_bit))
71       loops_per_tick |= test_bit;
72
73   printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
74 }
75
76 /* Returns the number of timer ticks since the OS booted. */
77 int64_t
78 timer_ticks (void) 
79 {
80   enum intr_level old_level = intr_disable ();
81   int64_t t = ticks;
82   intr_set_level (old_level);
83   barrier ();
84   return t;
85 }
86
87 /* Returns the number of timer ticks elapsed since THEN, which
88    should be a value once returned by timer_ticks(). */
89 int64_t
90 timer_elapsed (int64_t then) 
91 {
92   return timer_ticks () - then;
93 }
94
95 /* Suspends execution for approximately TICKS timer ticks. */
96 void
97 timer_sleep (int64_t ticks) 
98 {
99   int64_t start = timer_ticks ();
100
101   ASSERT (intr_get_level () == INTR_ON);
102   while (timer_elapsed (start) < ticks) 
103     thread_yield ();
104 }
105
106 /* Suspends execution for approximately MS milliseconds. */
107 void
108 timer_msleep (int64_t ms) 
109 {
110   real_time_sleep (ms, 1000);
111 }
112
113 /* Suspends execution for approximately US microseconds. */
114 void
115 timer_usleep (int64_t us) 
116 {
117   real_time_sleep (us, 1000 * 1000);
118 }
119
120 /* Suspends execution for approximately NS nanoseconds. */
121 void
122 timer_nsleep (int64_t ns) 
123 {
124   real_time_sleep (ns, 1000 * 1000 * 1000);
125 }
126
127 /* Prints timer statistics. */
128 void
129 timer_print_stats (void) 
130 {
131   printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
132 }
133 \f
134 /* Timer interrupt handler. */
135 static void
136 timer_interrupt (struct intr_frame *args UNUSED)
137 {
138   ticks++;
139   thread_tick ();
140 }
141
142 /* Returns true if LOOPS iterations waits for more than one timer
143    tick, otherwise false. */
144 static bool
145 too_many_loops (unsigned loops) 
146 {
147   /* Wait for a timer tick. */
148   int64_t start = ticks;
149   while (ticks == start)
150     barrier ();
151
152   /* Run LOOPS loops. */
153   start = ticks;
154   busy_wait (loops);
155
156   /* If the tick count changed, we iterated too long. */
157   barrier ();
158   return start != ticks;
159 }
160
161 /* Iterates through a simple loop LOOPS times, for implementing
162    brief delays.
163
164    Marked NO_INLINE because code alignment can significantly
165    affect timings, so that if this function was inlined
166    differently in different places the results would be difficult
167    to predict. */
168 static void NO_INLINE
169 busy_wait (int64_t loops) 
170 {
171   while (loops-- > 0)
172     barrier ();
173 }
174
175 /* Sleep for approximately NUM/DENOM seconds. */
176 static void
177 real_time_sleep (int64_t num, int32_t denom) 
178 {
179   /* Convert NUM/DENOM seconds into timer ticks, rounding down.
180           
181         (NUM / DENOM) s          
182      ---------------------- = NUM * TIMER_FREQ / DENOM ticks. 
183      1 s / TIMER_FREQ ticks
184   */
185   int64_t ticks = num * TIMER_FREQ / denom;
186
187   ASSERT (intr_get_level () == INTR_ON);
188   if (ticks > 0)
189     {
190       /* We're waiting for at least one full timer tick.  Use
191          timer_sleep() because it will yield the CPU to other
192          processes. */                
193       timer_sleep (ticks); 
194     }
195   else 
196     {
197       /* Otherwise, use a busy-wait loop for more accurate
198          sub-tick timing.  We scale the numerator and denominator
199          down by 1000 to avoid the possibility of overflow. */
200       ASSERT (denom % 1000 == 0);
201       busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000)); 
202     }
203 }
204