schedule the highest priority thread,overP1
[monosproject.git] / src / devices / timer.c
blob8ff0ceacb21416dfe434ae5c7c0f6168da4bb258
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"
11 /* See [8254] for hardware details of the 8254 timer chip. */
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
20 /* Number of timer ticks since OS booted. */
21 static int64_t ticks;
23 /* Number of loops per timer tick.
24 Initialized by timer_calibrate(). */
25 static unsigned loops_per_tick;
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);
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)
38 /* 8254 input frequency divided by TIMER_FREQ, rounded to
39 nearest. */
40 uint16_t count = (1193180 + TIMER_FREQ / 2) / TIMER_FREQ;
42 outb (0x43, 0x34); /* CW: counter 0, LSB then MSB, mode 2, binary. */
43 outb (0x40, count & 0xff);
44 outb (0x40, count >> 8);
46 intr_register_ext (0x20, timer_interrupt, "8254 Timer");
49 /* Calibrates loops_per_tick, used to implement brief delays. */
50 void
51 timer_calibrate (void)
53 unsigned high_bit, test_bit;
55 ASSERT (intr_get_level () == INTR_ON);
56 printf ("Calibrating timer... ");
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))
63 loops_per_tick <<= 1;
64 ASSERT (loops_per_tick != 0);
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;
73 printf ("%'"PRIu64" loops/s.\n", (uint64_t) loops_per_tick * TIMER_FREQ);
76 /* Returns the number of timer ticks since the OS booted. */
77 int64_t
78 timer_ticks (void)
80 enum intr_level old_level = intr_disable ();
81 int64_t t = ticks;
82 intr_set_level (old_level);
83 barrier ();
84 return t;
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)
92 return timer_ticks () - then;
95 /* Suspends execution for approximately TICKS timer ticks. */
96 void
97 timer_sleep (int64_t ts)
99 int64_t deadline = ts + timer_ticks();
100 thread_wait(thread_current(),deadline);
102 while(deadline > timer_ticks()){
103 thread_wait(thread_current(),deadline);
109 /* Suspends execution for approximately MS milliseconds. */
110 void
111 timer_msleep (int64_t ms)
113 real_time_sleep (ms, 1000);
116 /* Suspends execution for approximately US microseconds. */
117 void
118 timer_usleep (int64_t us)
120 real_time_sleep (us, 1000 * 1000);
123 /* Suspends execution for approximately NS nanoseconds. */
124 void
125 timer_nsleep (int64_t ns)
127 real_time_sleep (ns, 1000 * 1000 * 1000);
130 /* Prints timer statistics. */
131 void
132 timer_print_stats (void)
134 printf ("Timer: %"PRId64" ticks\n", timer_ticks ());
137 /* Timer interrupt handler. */
138 static void
139 timer_interrupt (struct intr_frame *args UNUSED)
141 ticks++;
142 thread_tick ();
145 /* Returns true if LOOPS iterations waits for more than one timer
146 tick, otherwise false. */
147 static bool
148 too_many_loops (unsigned loops)
150 /* Wait for a timer tick. */
151 int64_t start = ticks;
152 while (ticks == start)
153 barrier ();
155 /* Run LOOPS loops. */
156 start = ticks;
157 busy_wait (loops);
159 /* If the tick count changed, we iterated too long. */
160 barrier ();
161 return start != ticks;
164 /* Iterates through a simple loop LOOPS times, for implementing
165 brief delays.
167 Marked NO_INLINE because code alignment can significantly
168 affect timings, so that if this function was inlined
169 differently in different places the results would be difficult
170 to predict. */
171 static void NO_INLINE
172 busy_wait (int64_t loops)
174 while (loops-- > 0)
175 barrier ();
178 /* Sleep for approximately NUM/DENOM seconds. */
179 static void
180 real_time_sleep (int64_t num, int32_t denom)
182 /* Convert NUM/DENOM seconds into timer ticks, rounding down.
184 (NUM / DENOM) s
185 ---------------------- = NUM * TIMER_FREQ / DENOM ticks.
186 1 s / TIMER_FREQ ticks
188 int64_t ticks = num * TIMER_FREQ / denom;
190 ASSERT (intr_get_level () == INTR_ON);
191 if (ticks > 0)
193 /* We're waiting for at least one full timer tick. Use
194 timer_sleep() because it will yield the CPU to other
195 processes. */
196 timer_sleep (ticks);
198 else
200 /* Otherwise, use a busy-wait loop for more accurate
201 sub-tick timing. We scale the numerator and denominator
202 down by 1000 to avoid the possibility of overflow. */
203 ASSERT (denom % 1000 == 0);
204 busy_wait (loops_per_tick * num / 1000 * TIMER_FREQ / (denom / 1000));