Skip to content
Snippets Groups Projects
Commit 62da0849 authored by Andreas Dilger's avatar Andreas Dilger
Browse files

Add a "threads" parameter too openclose, so you can easily launch lots of

threads.
parent 49923e09
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <sys/wait.h>
#ifndef O_DIRECT #ifndef O_DIRECT
#define O_DIRECT 040000 /* direct disk access hint */ #define O_DIRECT 040000 /* direct disk access hint */
...@@ -13,45 +14,117 @@ ...@@ -13,45 +14,117 @@
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
char *filename; char filename[1024];
unsigned long count, i; unsigned long count, i;
int thread = 0;
int threads = 0;
int rc;
int fd; int fd;
if (argc != 3) { if (argc < 3 || argc > 4) {
fprintf(stderr, "usage: %s <filename> <iterations>\n", argv[0]); fprintf(stderr, "usage: %s <filename> <iterations> [threads]\n",
argv[0]);
exit(1); exit(1);
} }
filename = argv[1];
count = strtoul(argv[2], NULL, 0); count = strtoul(argv[2], NULL, 0);
if (argc == 4)
threads = strtoul(argv[3], NULL, 0);
fd = open(filename, O_RDWR|O_CREAT, 0644); for (i = 1; i <= threads; i++) {
if (fd < 0) { rc = fork();
fprintf(stderr, "open(%s, O_CREAT): %s\n", filename, if (rc < 0) {
strerror(errno)); fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
exit(1); strerror(rc = errno));
} break;
if (close(fd) < 0) { } else if (rc == 0) {
fprintf(stderr, "close(): %s\n", strerror(errno)); thread = i;
exit(1); argv[2] = "--device";
break;
} else
printf("%s: thread #%ld (PID %d) started\n",
argv[0], i, rc);
rc = 0;
} }
for (i = 0; i < count; i++) { if (threads && thread == 0) { /* parent process */
fd = open(filename, O_RDONLY|O_LARGEFILE|O_DIRECT); int live_threads = threads;
if (fd < 0) {
fprintf(stderr, "open(%s, O_RDONLY): %s\n", filename, while (live_threads > 0) {
strerror(errno)); int status;
exit(1); pid_t ret;
}
if (close(fd) < 0) { ret = waitpid(0, &status, 0);
fprintf(stderr, "close(): %s\n", strerror(errno)); if (ret == 0) {
exit(1); continue;
}
if (ret < 0) {
fprintf(stderr, "error: %s: wait - %s\n",
argv[0], strerror(errno));
if (!rc)
rc = errno;
} else {
/*
* This is a hack. We _should_ be able to use
* WIFEXITED(status) to see if there was an
* error, but it appears to be broken and it
* always returns 1 (OK). See wait(2).
*/
int err = WEXITSTATUS(status);
if (err || WIFSIGNALED(status))
fprintf(stderr,
"%s: PID %d had rc=%d\n",
argv[0], ret, err);
if (!rc)
rc = err;
live_threads--;
}
} }
} } else {
if (unlink(filename) < 0) { if (threads)
fprintf(stderr, "unlink(%s): %s\n", filename, strerror(errno)); sprintf(filename, "%s-%d", argv[1], thread);
exit(1); else
} strcpy(filename, argv[1]);
printf("Done.\n");
return 0; fd = open(filename, O_RDWR|O_CREAT, 0644);
if (fd < 0) {
fprintf(stderr, "open(%s, O_CREAT): %s\n", filename,
strerror(errno));
exit(errno);
}
if (close(fd) < 0) {
fprintf(stderr, "close(): %s\n", strerror(errno));
rc = errno;
goto unlink;
}
for (i = 0; i < count; i++) {
fd = open(filename, O_RDWR|O_LARGEFILE|O_DIRECT);
if (fd < 0) {
fprintf(stderr, "open(%s, O_RDWR): %s\n",
filename, strerror(errno));
rc = errno;
break;
}
if (close(fd) < 0) {
fprintf(stderr, "close(): %s\n",
strerror(errno));
rc = errno;
break;
}
}
unlink:
if (unlink(filename) < 0) {
fprintf(stderr, "unlink(%s): %s\n", filename,
strerror(errno));
rc = errno;
}
if (threads)
printf("Thread %d done: rc = %d\n", thread, rc);
else
printf("Done: rc = %d\n", rc);
}
return rc;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment