Print statistics at power off.
[pintos-anon] / src / threads / thread.c
index eb9646520de0e2b0b2af7c3254451ff5f86eefe1..afb2006bc1aa8a51ebffc002f80d29e766470e96 100644 (file)
@@ -41,6 +41,11 @@ struct kernel_thread_frame
     void *aux;                  /* Auxiliary data for function. */
   };
 
+/* Statistics. */
+static long long idle_ticks;    /* # of timer ticks spent idle. */
+static long long kernel_ticks;  /* # of timer ticks in kernel threads. */
+static long long user_ticks;    /* # of timer ticks in user programs. */
+
 static void kernel_thread (thread_func *, void *aux);
 
 static void idle (void *aux UNUSED);
@@ -86,6 +91,30 @@ thread_start (void)
   intr_enable ();
 }
 
+/* Called by the timer interrupt handler at each timer tick to
+   update statistics. */
+void
+thread_tick (void) 
+{
+  struct thread *t = thread_current ();
+  if (t == idle_thread)
+    idle_ticks++;
+#ifdef USERPROG
+  else if (t->pagedir != NULL)
+    user_ticks++;
+#endif
+  else
+    kernel_ticks++;
+}
+
+/* Prints thread statistics. */
+void
+thread_print_stats (void) 
+{
+  printf ("Thread: %lld idle ticks, %lld kernel ticks, %lld user ticks\n",
+          idle_ticks, kernel_ticks, user_ticks);
+}
+
 /* Creates a new kernel thread named NAME with the given initial
    PRIORITY, which executes FUNCTION passing AUX as the argument,
    and adds it to the ready queue.  If thread_start() has been
@@ -112,7 +141,7 @@ thread_create (const char *name, int priority,
   ASSERT (function != NULL);
 
   /* Allocate thread. */
-  t = palloc_get (PAL_ZERO);
+  t = palloc_get_page (PAL_ZERO);
   if (t == NULL)
     return TID_ERROR;
 
@@ -370,7 +399,7 @@ schedule_tail (struct thread *prev)
     {
       ASSERT (prev != cur);
       if (prev != initial_thread)
-        palloc_free (prev);
+        palloc_free_page (prev);
     }
 }