⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bonnie.c

📁 磁盘的基准测试程序
💻 C
📖 第 1 页 / 共 2 页
字号:
      if (read(seek_control[0], seek_tickets, 1) != 1)	io_error("read ticket");      timestamp();      seeker_report[StartTime] = time_so_far();      /* loop until we read a 0 ticket back from our parent */      while(seek_tickets[0])      { /* until Mom says stop */        doseek((long) (random() % (size / Chunk)), fd,	  ((lseek_count++ % UpdateSeek) == 0));	if (read(seek_control[0], seek_tickets, 1) != 1)	  io_error("read ticket");      } /* until Mom says stop */      if (close(fd) == -1)        io_error("close after seek");      /* report to parent */      get_delta_t(Lseek);      seeker_report[EndTime] = time_so_far();      seeker_report[CPU] = delta[(int) Lseek][CPU];      if (write(seek_feedback[1], seeker_report, sizeof(seeker_report))          != sizeof(seeker_report))        io_error("pipe write");      exit(0);    } /* child process */  } /* for each seek proc */  /*   * Back in the parent; in an effort to ensure the children get an even   *  start, wait a few seconds for them to get scheduled, open their   *  files & so on.   */  close(seek_feedback[1]);  close(seek_control[0]);  sleep(5);  fprintf(stderr, "start 'em...");  if (write(seek_control[1], seek_tickets, sizeof(seek_tickets))       != sizeof(seek_tickets))    io_error("write tickets");    /* read back from children */  for (next = 0; next < SeekProcCount; next++)  { /* for each child */    if (read(seek_feedback[0], (char *) seeker_report, sizeof(seeker_report))        != sizeof(seeker_report))      io_error("pipe read");    /*     * each child writes back its CPU, start & end times.  The elapsed time      *  to do all the seeks is the time the first child started until the      *  time the last child stopped     */    delta[(int) Lseek][CPU] += seeker_report[CPU];    if (next == 0)    { /* first time */      first_start = seeker_report[StartTime];      last_stop = seeker_report[EndTime];    } /* first time */    else    { /* not first time */      first_start = (first_start < seeker_report[StartTime]) ?	first_start : seeker_report[StartTime];       last_stop = (last_stop > seeker_report[EndTime]) ?	last_stop : seeker_report[EndTime];     } /* not first time */    if (wait(&child) == -1)      io_error("wait");    fprintf(stderr, "done...");  } /* for each child */  fprintf(stderr, "\n");  delta[(int) Lseek][Elapsed] = last_stop - first_start;  if (html)    write_html(machine, size);  else    report(machine, size);  unlink(name);}static voidwrite_html(  char * machine,  off_t  size){  printf("<TR><TD>%s</TD><TD>%d</TD>", machine, size / (1024 * 1024));  printf("<TD>%d</TD><TD>%4.1f</TD><TD>%d</TD><TD>%4.1f</TD><TD>%d</TD><TD>%4.1f</TD>",    (int) (((double) size) / (delta[(int) Putc][Elapsed] * 1024.0)),    delta[(int) Putc][CPU] / delta[(int) Putc][Elapsed] * 100.0,    (int) (((double) size) / (delta[(int) FastWrite][Elapsed] * 1024.0)),    delta[(int) FastWrite][CPU] / delta[(int) FastWrite][Elapsed] * 100.0,    (int) (((double) size) / (delta[(int) ReWrite][Elapsed] * 1024.0)),    delta[(int) ReWrite][CPU] / delta[(int) ReWrite][Elapsed] * 100.0);  printf("<TD>%d</TD><TD>%4.1f</TD><TD>%d</TD><TD>%4.1f</TD>",    (int) (((double) size) / (delta[(int) Getc][Elapsed] * 1024.0)),    delta[(int) Getc][CPU] / delta[(int) Getc][Elapsed] * 100.0,    (int) (((double) size) / (delta[(int) FastRead][Elapsed] * 1024.0)),    delta[(int) FastRead][CPU] / delta[(int) FastRead][Elapsed] * 100.0);  printf("<TD>%5.1f</TD><TD>%4.1f</TD></TR>\n",    ((double) Seeks) / delta[(int) Lseek][Elapsed],    delta[(int) Lseek][CPU] / delta[(int) Lseek][Elapsed] * 100.0);}static voidreport(  char * machine,  off_t  size){  printf("              ");  printf(    "-------Sequential Output-------- ---Sequential Input-- --Random--\n");  printf("              ");  printf(    "-Per Char- --Block--- -Rewrite-- -Per Char- --Block--- --Seeks---\n");  printf("Machine    MB ");  printf("K/sec %%CPU K/sec %%CPU K/sec %%CPU K/sec %%CPU K/sec ");  printf("%%CPU  /sec %%CPU\n");  printf("%-8.8s %4d ", machine, size / (1024 * 1024));  printf("%5d %4.1f %5d %4.1f %5d %4.1f ",    (int) (((double) size) / (delta[(int) Putc][Elapsed] * 1024.0)),    delta[(int) Putc][CPU] / delta[(int) Putc][Elapsed] * 100.0,    (int) (((double) size) / (delta[(int) FastWrite][Elapsed] * 1024.0)),    delta[(int) FastWrite][CPU] / delta[(int) FastWrite][Elapsed] * 100.0,    (int) (((double) size) / (delta[(int) ReWrite][Elapsed] * 1024.0)),    delta[(int) ReWrite][CPU] / delta[(int) ReWrite][Elapsed] * 100.0);  printf("%5d %4.1f %5d %4.1f ",    (int) (((double) size) / (delta[(int) Getc][Elapsed] * 1024.0)),    delta[(int) Getc][CPU] / delta[(int) Getc][Elapsed] * 100.0,    (int) (((double) size) / (delta[(int) FastRead][Elapsed] * 1024.0)),    delta[(int) FastRead][CPU] / delta[(int) FastRead][Elapsed] * 100.0);  printf("%5.1f %4.1f\n",    ((double) Seeks) / delta[(int) Lseek][Elapsed],    delta[(int) Lseek][CPU] / delta[(int) Lseek][Elapsed] * 100.0);}static voidnewfile(  char *   name,  int *    fd,  FILE * * stream,  int      create){  if (create)  { /* create from scratch */    if (unlink(name) == -1 && *fd != -1)      io_error("unlink");    *fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0777);  } /* create from scratch */  else    *fd = open(name, O_RDWR, 0777);  if (*fd == -1)    io_error(name);  *stream = fdopen(*fd, "r+");  if (*stream == NULL)    io_error("fdopen");}static voidusage(){  fprintf(stderr,    "usage: Bonnie [-d scratch-dir] [-s size-in-Mb] [-html] [-m machine-label]\n");  exit(1);}static voidtimestamp(){  last_timestamp = time_so_far();  last_cpustamp = cpu_so_far();}static void get_delta_t(test)  tests_t test;{  int which = (int) test;  delta[which][Elapsed] = time_so_far() - last_timestamp;  delta[which][CPU] = cpu_so_far() - last_cpustamp;}static double cpu_so_far(){#if defined(SysV)  struct tms tms;  if (times(&tms) == -1)    io_error("times");  return ((double) tms.tms_utime) / ((double) sysconf(_SC_CLK_TCK)) +    ((double) tms.tms_stime) / ((double) sysconf(_SC_CLK_TCK));#else  struct rusage rusage;  getrusage(RUSAGE_SELF, &rusage);  return    ((double) rusage.ru_utime.tv_sec) +      (((double) rusage.ru_utime.tv_usec) / 1000000.0) +        ((double) rusage.ru_stime.tv_sec) +          (((double) rusage.ru_stime.tv_usec) / 1000000.0);#endif}static doubletime_so_far(){#if defined(SysV)  int        val;  struct tms tms;  if ((val = times(&tms)) == -1)    io_error("times");  return ((double) val) / ((double) sysconf(_SC_CLK_TCK));#else  struct timeval tp;  if (gettimeofday(&tp, (struct timezone *) NULL) == -1)    io_error("gettimeofday");  return ((double) (tp.tv_sec - basetime)) +    (((double) tp.tv_usec) / 1000000.0);#endif}static voidio_error(char * message){  char buf[Chunk];  sprintf(buf, "Bonnie: drastic I/O error (%s)", message);  perror(buf);  exit(1);}/* * Do a typical-of-something random I/O.  Any serious application that *  has a random I/O bottleneck is going to be smart enough to operate *  in a page mode, and not stupidly pull individual words out at *  odd offsets.   * The 'where' argument is used as a chunk number * To keep the cache from getting too clever, some pages must be updated.   *  However an application that updated each of many random pages that  *  it looked at is hard to imagine.   * However, it would be wrong to put the update percentage in as a *  parameter - the effect is too nonlinear. */static void doseek(  off_t where,  int   fd,  int   update){  int   buf[Chunk / IntSize];  off_t probe;  off_t size;  probe = where * Chunk;  if (lseek(fd, probe, 0) != probe)    io_error("lseek in doseek");  if ((size = read(fd, (char *) buf, Chunk)) == -1)    io_error("read in doseek");  /* every so often, update a block */  if (update)  { /* update this block */    /* touch a word */    buf[((int) random() % (size/IntSize - 2)) + 1]--;    if (lseek(fd, (long) probe, 0) != probe)      io_error("lseek in doseek update");    if (write(fd, (char *) buf, size) == -1)      io_error("write in doseek");  } /* update this block */}  #if defined(SysV)static char randseed[32];static voidsrandom(int seed){  sprintf(randseed, "%06d", seed);}static longrandom(){  return nrand48(randseed);}#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -