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