From b717d07d839ce204cf6c50e541b4ff1150ca9c77 Mon Sep 17 00:00:00 2001
From: adilger <adilger>
Date: Fri, 4 Oct 2002 05:20:52 +0000
Subject: [PATCH] Program to create/destroy many files.

---
 lustre/tests/.cvsignore      |   1 +
 lustre/tests/Makefile.am     |   4 +-
 lustre/tests/createdestroy.c | 108 +++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 lustre/tests/createdestroy.c

diff --git a/lustre/tests/.cvsignore b/lustre/tests/.cvsignore
index 1acdc7b951..f2ce02895a 100644
--- a/lustre/tests/.cvsignore
+++ b/lustre/tests/.cvsignore
@@ -20,3 +20,4 @@ fsx
 test_brw
 newfile
 openclose
+createdestroy
diff --git a/lustre/tests/Makefile.am b/lustre/tests/Makefile.am
index aa5663b897..9646b90097 100644
--- a/lustre/tests/Makefile.am
+++ b/lustre/tests/Makefile.am
@@ -24,7 +24,8 @@ noinst_SCRIPTS = fs.sh intent-test.sh intent-test2.sh leak_finder.pl \
 pkglib_SCRIPTS = common.sh
 pkgcfg_DATA = lustre.cfg
 noinst_PROGRAMS = openunlink testreq truncate directio openme writeme mcreate
-noinst_PROGRAMS += munlink tchmod toexcl fsx test_brw openclose #ldaptest 
+noinst_PROGRAMS += munlink tchmod toexcl fsx test_brw openclose createdestroy
+# noinst_PROGRAMS += ldaptest 
 
 # ldaptest_SOURCES = ldaptest.c
 tchmod_SOURCES = tchmod.c
@@ -40,5 +41,6 @@ writeme_SOURCES = writeme.c
 fsx_SOURCES = fsx.c
 test_brw_SOURCES = test_brw.c
 openclose_SOURCES = openclose.c
+createdestroy_SOURCES = createdestroy.c
 
 include $(top_srcdir)/Rules
diff --git a/lustre/tests/createdestroy.c b/lustre/tests/createdestroy.c
new file mode 100644
index 0000000000..93ecb11a26
--- /dev/null
+++ b/lustre/tests/createdestroy.c
@@ -0,0 +1,108 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <string.h>
+#include <sys/wait.h>
+
+int main(int argc, char *argv[])
+{
+        char filename[1024];
+        unsigned long count, i;
+	int thread = 0;
+	int threads = 0;
+	int rc;
+
+        if (argc < 3 || argc > 4) {
+                fprintf(stderr, "usage: %s <filename> <iterations> [threads]\n",
+			argv[0]);
+                exit(1);
+        }
+
+        count = strtoul(argv[2], NULL, 0);
+	if (argc == 4)
+		threads = strtoul(argv[3], NULL, 0);
+
+        for (i = 1; i <= threads; i++) {
+                rc = fork();
+                if (rc < 0) {
+                        fprintf(stderr, "error: %s: #%ld - %s\n", argv[0], i,
+                                strerror(rc = errno));
+                        break;
+                } else if (rc == 0) {
+                        thread = i;
+                        argv[2] = "--device";
+                        break;
+                } else
+                        printf("%s: thread #%ld (PID %d) started\n",
+                               argv[0], i, rc);
+                rc = 0;
+        }
+
+        if (threads && thread == 0) {	/* parent process */
+                int live_threads = threads;
+
+                while (live_threads > 0) {
+                        int status;
+                        pid_t ret;
+
+                        ret = waitpid(0, &status, 0);
+                        if (ret == 0) {
+                                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 {
+		for (i = 0; i < count; i++) {
+			if (threads)
+				sprintf(filename, "%s-%d-%ld",
+					argv[1], thread, i);
+			else
+				sprintf(filename, "%s-%ld", argv[1], i);
+
+			rc = mknod(filename, S_IFREG, 0);
+			if (rc < 0) {
+				fprintf(stderr, "mknod(%s): %s\n",
+					filename, strerror(errno));
+				rc = errno;
+				break;
+			}
+			if (unlink(filename) < 0) {
+				fprintf(stderr, "unlink(%s): %s\n",
+					filename, strerror(errno));
+				rc = errno;
+				break;
+			}
+		}
+		if (threads)
+			printf("Thread %d done: rc = %d\n", thread, rc);
+		else
+			printf("Done: rc = %d\n", rc);
+	}
+        return rc;
+}
-- 
GitLab