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