/* time_io.c  check how much is cached in ram             */
/*            assumed pre-existing data file  time_io.dat */
/*            created by running  time_io_init            */
#include <stdio.h>
#include <time.h>
int main()
{
  FILE * handle;
  int i;
  int j;
  double cpu;
  char buf[1000000];
  int check;
  int n = 10000; /* number of reads on 10MB file for buf1*/
  int k = 1000;  /* number of bytes read per read */

  printf("time_io.c 10MB file, read 1KB, 10KB, 100Kb, 1MB \n");
  handle = fopen("time_io.dat","rb");
  printf("On rebooted machine, first read \n");
  cpu = (double)clock()/(double)CLOCKS_PER_SEC;
  for(i=0; i<n; i++)
  {
    check = fread(buf, k, 1, handle);
    if(check != buf[1]) printf("check failed \n");
  }
  cpu = (double)clock()/(double)CLOCKS_PER_SEC - cpu;
  fclose(handle);
  printf("first read time %g seconds \n", cpu);

  for(n=10000; n>=10; n=n/10)
  {
    printf("more reads, cached? consistent? \n");

    for(j=2; j<10; j++)
    {
      handle = fopen("time_io.dat","rb");
      cpu = (double)clock()/(double)CLOCKS_PER_SEC;
      for(i=0; i<n; i++)
      {
        check = fread(buf, k, 1, handle);
        if(check != buf[1]) printf("check failed \n");
      }
      cpu = (double)clock()/(double)CLOCKS_PER_SEC - cpu;
      fclose(handle);
      printf("%d read time %g seconds for %dKB block \n", j, cpu, k/1000);
    }
    k = k*10;
  }
  return 0;
}








