Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- ** Time a command
- */
- #include <stdio.h>
- #include <signal.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/times.h>
- #include <fcntl.h>
- #define Chunk (16384)
- #define IntSize (sizeof(int))
- static void newfile();
- static void io_error();
- main(argc, argv)
- char **argv;
- {
- struct {
- long user;
- long sys;
- long childuser;
- long childsys;
- } buffer;
- long before, after;
- extern long times();
- char name[Chunk];
- int fd;
- int bufindex;
- int buf[Chunk / IntSize];
- FILE * stream;
- off_t words;
- off_t size = 15;
- char * dir;
- int chars[256];
- int next;
- dir = ".";
- sprintf(name, "%s/Bonnie.%d", dir, getpid());
- size *= (1024 * 1024);
- size = Chunk * (size / Chunk);
- fprintf(stderr, "File '%s', size: %ld\n", name, size);
- fprintf(stderr, "Writing with putc()...");
- newfile(name, &fd, &stream, 1);
- before = times(&buffer);
- for (words = 0; words < size; words++)
- if (putc(words & 0x7f, stream) == EOF)
- io_error("putc");
- /*
- * note that we always close the file before measuring time, in an
- * effort to force as much of the I/O out as we can
- */
- if (fclose(stream) == -1)
- io_error("fclose after putc");
- after = times(&buffer);
- printt(" ", (after-before));
- /* Now read & rewrite it using block I/O. Dirty one word in each block */
- newfile(name, &fd, &stream, 0);
- if (lseek(fd, (off_t) 0, 0) == (off_t) -1)
- io_error("lseek(2) before rewrite");
- fprintf(stderr, "Rewriting...");
- bufindex = 0;
- before = times(&buffer);
- if ((words = read(fd, (char *) buf, Chunk)) == -1)
- io_error("rewrite read");
- while (words == Chunk)
- { /* while we can read a block */
- if (bufindex == Chunk / IntSize)
- bufindex = 0;
- buf[bufindex++]++;
- if (lseek(fd, (off_t) -words, 1) == -1)
- io_error("relative lseek(2)");
- if (write(fd, (char *) buf, words) == -1)
- io_error("re write(2)");
- if ((words = read(fd, (char *) buf, Chunk)) == -1)
- io_error("rwrite read");
- } /* while we can read a block */
- if (close(fd) == -1)
- io_error("close after rewrite");
- after = times(&buffer);
- printt(" ", (after-before));
- /* Write the whole file from scratch, again, with block I/O */
- newfile(name, &fd, &stream, 1);
- fprintf(stderr, "Writing intelligently...");
- for (words = 0; words < Chunk / IntSize; words++)
- buf[words] = 0;
- before = times(&buffer);
- for (words = bufindex = 0; words < (size / Chunk); words++)
- { /* for each word */
- if (bufindex == (Chunk / IntSize))
- bufindex = 0;
- buf[bufindex++]++;
- if (write(fd, (char *) buf, Chunk) == -1)
- io_error("write(2)");
- } /* for each word */
- if (close(fd) == -1)
- io_error("close after fast write");
- after = times(&buffer);
- printt(" ", (after-before));
- /* read them all back with getc() */
- newfile(name, &fd, &stream, 0);
- for (words = 0; words < 256; words++)
- chars[words] = 0;
- fprintf(stderr, "Reading with getc()...");
- before = times(&buffer);
- for (words = 0; words < size; words++)
- { /* for each byte */
- if ((next = getc(stream)) == EOF)
- io_error("getc(3)");
- /* just to fool optimizers */
- chars[next]++;
- } /* for each byte */
- if (fclose(stream) == -1)
- io_error("fclose after getc");
- after = times(&buffer);
- printt(" ", (after-before));
- /* use the frequency count */
- for (words = 0; words < 256; words++)
- sprintf((char *) buf, "%d", chars[words]);
- /* Now suck it in, Chunk at a time, as fast as we can */
- newfile(name, &fd, &stream, 0);
- if (lseek(fd, (off_t) 0, 0) == -1)
- io_error("lseek before read");
- fprintf(stderr, "Reading intelligently...");
- before = times(&buffer);
- do
- { /* per block */
- if ((words = read(fd, (char *) buf, Chunk)) == -1)
- io_error("read(2)");
- chars[buf[abs(buf[0]) % (Chunk / IntSize)] & 0x7f]++;
- } /* per block */
- while (words);
- if (close(fd) == -1)
- io_error("close after read");
- after = times(&buffer);
- printt(" ", (after-before));
- }
- char quant[] = { 6, 10, 10, 6, 10, 6, 10, 10, 10 };
- char *pad = "000 ";
- char *sep = "\0\0.\0:\0:\0\0";
- char *nsep = "\0\0.\0 \0 \0\0";
- printt(s, a)
- char *s;
- long a;
- {
- register i;
- char digit[9];
- char c;
- int nonzero;
- for(i=0; i<9; i++) {
- digit[i] = a % quant[i];
- a /= quant[i];
- }
- fprintf(stderr,s);
- nonzero = 0;
- while(--i>0) {
- c = digit[i]!=0 ? digit[i]+'0':
- nonzero ? '0':
- pad[i];
- if (c != '\0')
- putc (c, stderr);
- nonzero |= digit[i];
- c = nonzero?sep[i]:nsep[i];
- if (c != '\0')
- putc (c, stderr);
- }
- fprintf(stderr,"\n");
- }
- static void
- newfile(name,fd,stream,create)
- char * name;
- int * fd;
- FILE * * stream;
- int create;
- {
- if (create)
- { /* create from scratch */
- unlink(name);
- *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 void
- io_error(message)
- char * message;
- {
- char buf[Chunk];
- sprintf(buf, "Bonnie: drastic I/O error (%s)", message);
- perror(buf);
- exit(1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement