root/kern/vm/uw-vmstats.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. vmstats_inc
  2. vmstats_init
  3. _vmstats_inc
  4. _vmstats_init
  5. vmstats_print

   1 /* UW specific code - This won't be needed or used until assignment 3 */
   2 
   3 /* belongs in kern/vm/uw-vmstats.c */
   4 
   5 /* NOTE !!!!!! WARNING !!!!!
   6  * All of the functions whose names begin with '_'
   7  * assume that atomicity is ensured elsewhere
   8  * (i.e., outside of these routines) by acquiring stats_lock.
   9  * All of the functions whose names do not begin
  10  * with '_' ensure atomicity locally.
  11  */
  12 
  13 #include <types.h>
  14 #include <lib.h>
  15 #include <synch.h>
  16 #include <spl.h>
  17 #include <uw-vmstats.h>
  18 
  19 /* Counters for tracking statistics */
  20 static unsigned int stats_counts[VMSTAT_COUNT];
  21 
  22 struct spinlock stats_lock = SPINLOCK_INITIALIZER;
  23 
  24 /* Strings used in printing out the statistics */
  25 static const char *stats_names[] = {
  26  /*  0 */ "TLB Faults", 
  27  /*  1 */ "TLB Faults with Free",
  28  /*  2 */ "TLB Faults with Replace",
  29  /*  3 */ "TLB Invalidations",
  30  /*  4 */ "TLB Reloads",
  31  /*  5 */ "Page Faults (Zeroed)",
  32  /*  6 */ "Page Faults (Disk)",
  33  /*  7 */ "Page Faults from ELF",
  34  /*  8 */ "Page Faults from Swapfile",
  35  /*  9 */ "Swapfile Writes",
  36 };
  37 
  38 
  39 /* ---------------------------------------------------------------------- */
  40 /* Assumes vmstat_init has already been called */
  41 void
  42 vmstats_inc(unsigned int index)
  43 {
  44     spinlock_acquire(&stats_lock);
  45       _vmstats_inc(index);
  46     spinlock_release(&stats_lock);
  47 }
  48 
  49 /* ---------------------------------------------------------------------- */
  50 void
  51 vmstats_init(void)
  52 {
  53   /* Although the spinlock is initialized at declaration time we do it here
  54    * again in case we want use/reset these stats repeatedly without shutting down the kernel.
  55    */
  56   spinlock_init(&stats_lock);
  57 
  58   spinlock_acquire(&stats_lock);
  59     _vmstats_init();
  60   spinlock_release(&stats_lock);
  61 }
  62 
  63 /* ---------------------------------------------------------------------- */
  64 void
  65 _vmstats_inc(unsigned int index)
  66 {
  67   KASSERT(index < VMSTAT_COUNT);
  68   stats_counts[index]++;
  69 }
  70 
  71 /* ---------------------------------------------------------------------- */
  72 void
  73 _vmstats_init(void)
  74 {
  75   int i = 0;
  76 
  77   if (sizeof(stats_names) / sizeof(char *) != VMSTAT_COUNT) {
  78     kprintf("vmstats_init: number of stats_names = %d != VMSTAT_COUNT = %d\n",
  79       (sizeof(stats_names) / sizeof(char *)), VMSTAT_COUNT);
  80     panic("Should really fix this before proceeding\n");
  81   }
  82 
  83   for (i=0; i<VMSTAT_COUNT; i++) {
  84     stats_counts[i] = 0;
  85   }
  86 
  87 }
  88 
  89 /* ---------------------------------------------------------------------- */
  90 /* Assumes vmstat_init has already been called */
  91 /* NOTE: We do not grab the spinlock here because kprintf may block
  92  * and we can't block while holding a spinlock.
  93  * Just use this when there is only one thread remaining.
  94  */
  95 
  96 void
  97 vmstats_print(void)
  98 {
  99   int i = 0;
 100   int free_plus_replace = 0;
 101   int disk_plus_zeroed_plus_reload = 0;
 102   int tlb_faults = 0;
 103   int elf_plus_swap_reads = 0;
 104   int disk_reads = 0;
 105 
 106   kprintf("VMSTATS:\n");
 107   for (i=0; i<VMSTAT_COUNT; i++) {
 108     kprintf("VMSTAT %25s = %10d\n", stats_names[i], stats_counts[i]);
 109   }
 110 
 111   tlb_faults = stats_counts[VMSTAT_TLB_FAULT];
 112   free_plus_replace = stats_counts[VMSTAT_TLB_FAULT_FREE] + stats_counts[VMSTAT_TLB_FAULT_REPLACE];
 113   disk_plus_zeroed_plus_reload = stats_counts[VMSTAT_PAGE_FAULT_DISK] +
 114     stats_counts[VMSTAT_PAGE_FAULT_ZERO] + stats_counts[VMSTAT_TLB_RELOAD];
 115   elf_plus_swap_reads = stats_counts[VMSTAT_ELF_FILE_READ] + stats_counts[VMSTAT_SWAP_FILE_READ];
 116   disk_reads = stats_counts[VMSTAT_PAGE_FAULT_DISK];
 117 
 118   kprintf("VMSTAT TLB Faults with Free + TLB Faults with Replace = %d\n", free_plus_replace);
 119   if (tlb_faults != free_plus_replace) {
 120     kprintf("WARNING: TLB Faults (%d) != TLB Faults with Free + TLB Faults with Replace (%d)\n",
 121       tlb_faults, free_plus_replace); 
 122   }
 123 
 124   kprintf("VMSTAT TLB Reloads + Page Faults (Zeroed) + Page Faults (Disk) = %d\n",
 125     disk_plus_zeroed_plus_reload);
 126   if (tlb_faults != disk_plus_zeroed_plus_reload) {
 127     kprintf("WARNING: TLB Faults (%d) != TLB Reloads + Page Faults (Zeroed) + Page Faults (Disk) (%d)\n",
 128       tlb_faults, disk_plus_zeroed_plus_reload); 
 129   }
 130 
 131   kprintf("VMSTAT ELF File reads + Swapfile reads = %d\n", elf_plus_swap_reads);
 132   if (disk_reads != elf_plus_swap_reads) {
 133     kprintf("WARNING: ELF File reads + Swapfile reads != Page Faults (Disk) %d\n",
 134       elf_plus_swap_reads);
 135   }
 136 }
 137 /* ---------------------------------------------------------------------- */

/* [<][>][^][v][top][bottom][index][help] */