Print statistics at power off.
[pintos-anon] / src / devices / timer.c
index 951c2a6d0b17d5ce375f0a7246c5da2714a92dbf..8f47c810ffc8af5a1c6804933fad710929450318 100644 (file)
@@ -1,8 +1,11 @@
 #include "devices/timer.h"
 #include <debug.h>
+#include <inttypes.h>
 #include <round.h>
+#include <stdio.h>
 #include "threads/interrupt.h"
 #include "threads/io.h"
+#include "threads/thread.h"
   
 #if TIMER_FREQ < 19
 #error 8254 timer requires TIMER_FREQ >= 19
@@ -20,7 +23,7 @@ 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
@@ -55,31 +58,47 @@ timer_elapsed (int64_t then)
   return timer_ticks () - then;
 }
 
-/* Suspends execution for approximately MS milliseconds. */
+/* Suspends execution for approximately TICKS timer ticks. */
 void
-timer_msleep (int64_t ms) 
+timer_sleep (int64_t ticks) 
 {
-  int64_t ticks = (int64_t) DIV_ROUND_UP (ms * TIMER_FREQ, 1000);
   int64_t start = timer_ticks ();
 
   while (timer_elapsed (start) < ticks) 
-    continue;
+    if (intr_get_level () == INTR_ON)
+      thread_yield ();
 }
 
-/* Suspends execution for approximately US microseconds.
-   Note: this is ridiculously inaccurate. */
-void
-timer_usleep (int64_t us) 
+/* 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) 
 {
-  timer_msleep (us / 1000 + 1);
+  return DIV_ROUND_UP (ns * TIMER_FREQ, 1000000000);
 }
 
-/* Suspends execution for approximately NS nanoseconds.
-   Note: this is ridiculously inaccurate. */
+/* Prints timer statistics. */
 void
-timer_nsleep (int64_t ns
+timer_print_stats (void
 {
-  timer_msleep (ns / 1000000 + 1);
+  printf ("Timer: %"PRId64" ticks.\n", ticks);
 }
 \f
 /* Timer interrupt handler. */
@@ -87,6 +106,7 @@ static void
 timer_interrupt (struct intr_frame *args UNUSED)
 {
   ticks++;
+  thread_tick ();
   if (ticks % TIME_SLICE == 0)
     intr_yield_on_return ();
 }