X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fdevices%2Ftimer.c;h=ecf6c5aacdfee97c6577c8b60912475ef99e4e77;hb=64f0952217e8b1a53461f5a98a070cb2da266171;hp=4adca8989cf1192612a0c009d187f999c2d5e25a;hpb=f8cae751ac2163f5fe73d5b1372c93ada35dc9e3;p=pintos-anon diff --git a/src/devices/timer.c b/src/devices/timer.c index 4adca89..ecf6c5a 100644 --- a/src/devices/timer.c +++ b/src/devices/timer.c @@ -1,18 +1,31 @@ -#include "timer.h" -#include "debug.h" -#include "interrupt.h" -#include "io.h" +#include "devices/timer.h" +#include +#include +#include +#include +#include "threads/interrupt.h" +#include "threads/io.h" +#include "threads/thread.h" -static volatile uint64_t ticks; +/* See [8254] for hardware details of the 8254 timer chip. */ -static void -irq20_timer (struct intr_frame *args UNUSED) -{ - ticks++; - intr_yield_on_return (); -} +#if TIMER_FREQ < 19 +#error 8254 timer requires TIMER_FREQ >= 19 +#endif +#if TIMER_FREQ > 1000 +#error TIMER_FREQ <= 1000 recommended +#endif + +/* Number of timer ticks that a process gets before being + preempted. */ +#define TIME_SLICE 1 + +/* Number of timer ticks since OS booted. */ +static volatile int64_t ticks; + +static intr_handler_func timer_interrupt; -/* Sets up the 8254 Programmable Interrupt Timer (PIT) to +/* Sets up the 8254 Programmable Interval Timer (PIT) to interrupt PIT_FREQ times per second, and registers the corresponding interrupt. */ void @@ -26,28 +39,76 @@ timer_init (void) outb (0x40, count & 0xff); outb (0x40, count >> 8); - intr_register (0x20, 0, IF_OFF, irq20_timer); + intr_register (0x20, 0, INTR_OFF, timer_interrupt, "8254 Timer"); } -uint64_t +/* Returns the number of timer ticks since the OS booted. */ +int64_t timer_ticks (void) { - enum if_level old_level = intr_disable (); - uint64_t t = ticks; + enum intr_level old_level = intr_disable (); + int64_t t = ticks; intr_set_level (old_level); return t; } -uint64_t -timer_elapsed (uint64_t then) +/* Returns the number of timer ticks elapsed since THEN, which + should be a value once returned by timer_ticks(). */ +int64_t +timer_elapsed (int64_t then) { - uint64_t now = timer_ticks (); - return now > then ? now - then : ((uint64_t) -1 - then) + now + 1; + return timer_ticks () - then; } -/* Suspends execution for at least DURATION ticks. */ +/* Suspends execution for approximately TICKS timer ticks. */ void -timer_wait_until (uint64_t duration) +timer_sleep (int64_t ticks) { - /* FIXME */ + int64_t start = timer_ticks (); + + ASSERT (intr_get_level () == INTR_ON); + while (timer_elapsed (start) < ticks) + thread_yield (); +} + +/* Returns MS milliseconds in timer ticks, rounding up. */ +int64_t +timer_ms2ticks (int64_t ms) +{ + /* MS / 1000 s + ------------------------ = MS * TIMER_FREQ / 1000 ticks. + (1 / TIMER_FREQ) ticks/s + */ + return DIV_ROUND_UP (ms * TIMER_FREQ, 1000); +} + +/* Returns US microseconds in timer ticks, rounding up. */ +int64_t +timer_us2ticks (int64_t us) +{ + return DIV_ROUND_UP (us * TIMER_FREQ, 1000000); +} + +/* Returns NS nanoseconds in timer ticks, rounding up. */ +int64_t +timer_ns2ticks (int64_t ns) +{ + return DIV_ROUND_UP (ns * TIMER_FREQ, 1000000000); +} + +/* Prints timer statistics. */ +void +timer_print_stats (void) +{ + printf ("Timer: %"PRId64" ticks\n", ticks); +} + +/* Timer interrupt handler. */ +static void +timer_interrupt (struct intr_frame *args UNUSED) +{ + ticks++; + thread_tick (); + if (ticks % TIME_SLICE == 0) + intr_yield_on_return (); }