Advertisement
Guest User

Untitled

a guest
Jan 24th, 2016
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.74 KB | None | 0 0
  1. /*
  2. **      Time a command
  3. */
  4.  
  5. #include        <stdio.h>
  6. #include        <signal.h>
  7. #include        <errno.h>
  8. #include        <sys/types.h>
  9. #include        <sys/times.h>
  10. #include <fcntl.h>
  11.  
  12.  
  13. #define Chunk (16384)
  14. #define IntSize (sizeof(int))
  15.  
  16.  
  17. static void   newfile();
  18. static void   io_error();
  19.  
  20. main(argc, argv)
  21. char **argv;
  22. {
  23.         struct {
  24.                 long user;
  25.                 long sys;
  26.                 long childuser;
  27.                 long childsys;
  28.         } buffer;
  29.  
  30.         long    before, after;
  31.         extern long times();
  32.   char   name[Chunk];
  33.   int    fd;
  34.   int    bufindex;
  35.   int    buf[Chunk / IntSize];
  36.   FILE * stream;
  37.   off_t  words;
  38.   off_t  size   = 15;
  39.   char * dir;
  40.   int    chars[256];
  41.   int    next;
  42.  
  43.  
  44.   dir = ".";
  45.  
  46.   sprintf(name, "%s/Bonnie.%d", dir, getpid());
  47.  
  48.   size *= (1024 * 1024);
  49.   size = Chunk * (size / Chunk);
  50.   fprintf(stderr, "File '%s', size: %ld\n", name, size);
  51.  
  52.   fprintf(stderr, "Writing with putc()...");
  53.   newfile(name, &fd, &stream, 1);
  54.  
  55.  
  56.         before = times(&buffer);
  57.   for (words = 0; words < size; words++)
  58.     if (putc(words & 0x7f, stream) == EOF)
  59.       io_error("putc");
  60.  
  61.   /*
  62.    * note that we always close the file before measuring time, in an
  63.    *  effort to force as much of the I/O out as we can
  64.    */
  65.   if (fclose(stream) == -1)
  66.     io_error("fclose after putc");
  67.  
  68.  
  69.         after = times(&buffer);
  70.  
  71.         printt(" ", (after-before));
  72.  
  73.   /* Now read & rewrite it using block I/O.  Dirty one word in each block */
  74.   newfile(name, &fd, &stream, 0);
  75.   if (lseek(fd, (off_t) 0, 0) == (off_t) -1)
  76.     io_error("lseek(2) before rewrite");
  77.   fprintf(stderr, "Rewriting...");
  78.   bufindex = 0;
  79.         before = times(&buffer);
  80.   if ((words = read(fd, (char *) buf, Chunk)) == -1)
  81.     io_error("rewrite read");
  82.   while (words == Chunk)
  83.   { /* while we can read a block */
  84.     if (bufindex == Chunk / IntSize)
  85.       bufindex = 0;
  86.     buf[bufindex++]++;
  87.     if (lseek(fd, (off_t) -words, 1) == -1)
  88.       io_error("relative lseek(2)");
  89.     if (write(fd, (char *) buf, words) == -1)
  90.       io_error("re write(2)");
  91.     if ((words = read(fd, (char *) buf, Chunk)) == -1)
  92.       io_error("rwrite read");
  93.   } /* while we can read a block */
  94.   if (close(fd) == -1)
  95.     io_error("close after rewrite");
  96.  
  97.         after = times(&buffer);
  98.  
  99.         printt(" ", (after-before));
  100.  
  101.  
  102.   /* Write the whole file from scratch, again, with block I/O */
  103.   newfile(name, &fd, &stream, 1);
  104.   fprintf(stderr, "Writing intelligently...");
  105.   for (words = 0; words < Chunk / IntSize; words++)
  106.     buf[words] = 0;
  107.         before = times(&buffer);
  108.   for (words = bufindex = 0; words < (size / Chunk); words++)
  109.   { /* for each word */
  110.     if (bufindex == (Chunk / IntSize))
  111.       bufindex = 0;
  112.     buf[bufindex++]++;
  113.     if (write(fd, (char *) buf, Chunk) == -1)
  114.       io_error("write(2)");
  115.   } /* for each word */
  116.   if (close(fd) == -1)
  117.     io_error("close after fast write");
  118.  
  119.         after = times(&buffer);
  120.  
  121.         printt(" ", (after-before));
  122.  
  123.  
  124.   /* read them all back with getc() */
  125.   newfile(name, &fd, &stream, 0);
  126.   for (words = 0; words < 256; words++)
  127.     chars[words] = 0;
  128.   fprintf(stderr, "Reading with getc()...");
  129.         before = times(&buffer);
  130.   for (words = 0; words < size; words++)
  131.   { /* for each byte */
  132.     if ((next = getc(stream)) == EOF)
  133.       io_error("getc(3)");
  134.  
  135.     /* just to fool optimizers */
  136.     chars[next]++;
  137.   } /* for each byte */
  138.   if (fclose(stream) == -1)
  139.     io_error("fclose after getc");
  140.  
  141.         after = times(&buffer);
  142.  
  143.         printt(" ", (after-before));
  144.  
  145.  
  146.   /* use the frequency count */
  147.   for (words = 0; words < 256; words++)
  148.     sprintf((char *) buf, "%d", chars[words]);
  149.  
  150.   /* Now suck it in, Chunk at a time, as fast as we can */
  151.   newfile(name, &fd, &stream, 0);
  152.   if (lseek(fd, (off_t) 0, 0) == -1)
  153.     io_error("lseek before read");
  154.   fprintf(stderr, "Reading intelligently...");
  155.         before = times(&buffer);
  156.   do
  157.   { /* per block */
  158.     if ((words = read(fd, (char *) buf, Chunk)) == -1)
  159.       io_error("read(2)");
  160.     chars[buf[abs(buf[0]) % (Chunk / IntSize)] & 0x7f]++;
  161.   } /* per block */
  162.   while (words);
  163.   if (close(fd) == -1)
  164.     io_error("close after read");
  165.  
  166.         after = times(&buffer);
  167.  
  168.         printt(" ", (after-before));
  169.  
  170.  
  171. }
  172.  
  173. char quant[] = { 6, 10, 10, 6, 10, 6, 10, 10, 10 };
  174. char *pad  = "000      ";
  175. char *sep  = "\0\0.\0:\0:\0\0";
  176. char *nsep = "\0\0.\0 \0 \0\0";
  177.  
  178. printt(s, a)
  179. char *s;
  180. long a;
  181. {
  182.         register i;
  183.         char    digit[9];
  184.         char    c;
  185.         int     nonzero;
  186.  
  187.         for(i=0; i<9; i++) {
  188.                 digit[i] = a % quant[i];
  189.                 a /= quant[i];
  190.         }
  191.         fprintf(stderr,s);
  192.         nonzero = 0;
  193.         while(--i>0) {
  194.                 c = digit[i]!=0 ? digit[i]+'0':
  195.                     nonzero ? '0':
  196.                     pad[i];
  197.                 if (c != '\0')
  198.                         putc (c, stderr);
  199.                 nonzero |= digit[i];
  200.                 c = nonzero?sep[i]:nsep[i];
  201.                 if (c != '\0')
  202.                         putc (c, stderr);
  203.         }
  204.         fprintf(stderr,"\n");
  205. }
  206.  
  207. static void
  208. newfile(name,fd,stream,create)
  209.   char *   name;
  210.   int *    fd;
  211.   FILE * * stream;
  212.   int      create;
  213. {
  214.   if (create)
  215.   { /* create from scratch */
  216.     unlink(name);
  217.     *fd = open(name, O_RDWR | O_CREAT | O_EXCL, 0777);
  218.   } /* create from scratch */
  219.   else
  220.     *fd = open(name, O_RDWR, 0777);
  221.  
  222.   if (*fd == -1)
  223.     io_error(name);
  224.   *stream = fdopen(*fd, "r+");
  225.   if (*stream == NULL)
  226.     io_error("fdopen");
  227. }
  228.  
  229. static void
  230. io_error(message)
  231.   char * message;
  232. {
  233.   char buf[Chunk];
  234.  
  235.   sprintf(buf, "Bonnie: drastic I/O error (%s)", message);
  236.   perror(buf);
  237.   exit(1);
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement