Skip to content
Browse files

added options

  • Loading branch information...
1 parent 2752f3a commit a0a418f273e2e9d31f6b93e317c7f5cc48651ba1 @fceller committed Sep 27, 2011
Showing with 66 additions and 39 deletions.
  1. +66 −39 msync-bench.c
View
105 msync-bench.c
@@ -8,21 +8,51 @@
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
+#include <stdbool.h>
-#define DO_MSYNC
-#define SPARSE_FILE 1
+static bool NoMsync = false;
+static bool OnlyFinalMsync = false;
+static bool SparseFile = true;
+static bool NoDots = false;
int main (int argc, char* argv[]) {
int i;
+ int pos = 1;
- if (argc != 4) {
- printf("usage: %s <file> <block-size> <iterations>\n", argv[0]);
+ while (pos < argc) {
+ if (strcmp(argv[pos], "--no-sparse-file") == 0) {
+ printf("not using sparse file\n");
+ SparseFile = false;
+ }
+ else if (strcmp(argv[pos], "--only-final-msync") == 0) {
+ printf("only final msync\n");
+ OnlyFinalMsync = true;
+ }
+ else if (strcmp(argv[pos], "--no-msync") == 0) {
+ printf("no msync at all\n");
+ NoMsync = true;
+ }
+ else if (strcmp(argv[pos], "--no-dots") == 0) {
+ printf("not printing dots\n");
+ NoDots = true;
+ }
+ else {
+ break;
+ }
+
+ ++pos;
+ }
+
+ if (argc - pos != 3) {
+ printf("usage: %s [options] <file> <block-size> <iterations>\n\n"
+ "options are: --no-sparse-file, --only-final-msync, --no-msync, --no-dots\n",
+ argv[0]);
exit(EXIT_FAILURE);
}
- char const* filename = argv[1];
- int block = atoi(argv[2]);
- int count = atoi(argv[3]);
+ char const* filename = argv[pos];
+ int block = atoi(argv[pos + 1]);
+ int count = atoi(argv[pos + 2]);
int fd = open(filename, O_CREAT | O_EXCL | O_RDWR, S_IRUSR | S_IWUSR);
@@ -31,36 +61,33 @@ int main (int argc, char* argv[]) {
exit(EXIT_FAILURE);
}
-#ifdef SPARSE_FILE
-
- off_t offset = lseek(fd, block * count - 1, SEEK_SET);
-
- if (offset == (off_t) -1) {
- printf("cannot seek %s: %s\n", filename, strerror(errno));
- exit(EXIT_FAILURE);
- }
-
- char zero = 0;
- ssize_t res = write(fd, &zero, 1);
+ if (SparseFile) {
+ off_t offset = lseek(fd, block * count - 1, SEEK_SET);
- if (res < 0) {
- printf("cannot fill %s: %s\n", filename, strerror(errno));
- exit(EXIT_FAILURE);
- }
-
-#else
+ if (offset == (off_t) -1) {
+ printf("cannot seek %s: %s\n", filename, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
- for (i = 0; i < block * count; ++i) {
- ssize_t res = write(fd, &i, 1);
+ char zero = 0;
+ ssize_t res = write(fd, &zero, 1);
if (res < 0) {
printf("cannot fill %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
+ }
+ }
+ else {
+ for (i = 0; i < block * count; ++i) {
+ ssize_t res = write(fd, &i, 1);
+
+ if (res < 0) {
+ printf("cannot fill %s: %s\n", filename, strerror(errno));
+ exit(EXIT_FAILURE);
+ }
}
}
-#endif
-
// memory map the data
intptr_t g = getpagesize();
size_t ms = ((block * count + g - 1) / g) * g;
@@ -95,22 +122,22 @@ int main (int argc, char* argv[]) {
char* begin = (char*)( (p / g) * g );
char* end = (char*)( ((p + (intptr_t) block + g - 1) / g) * g );
-#ifdef DO_MSYNC
+ if (! NoMsync && ! OnlyFinalMsync) {
+ ssize_t res = msync(begin, end - begin, MS_SYNC);
- ssize_t res = msync(begin, end - begin, MS_SYNC);
-
- if (res < 0) {
- printf("failed to msync: %s\n", strerror(errno));
- exit(EXIT_FAILURE);
+ if (res < 0) {
+ printf("failed to msync: %s\n", strerror(errno));
+ exit(EXIT_FAILURE);
+ }
}
-#endif
-
- fputc('.', stdout);
- fflush(stdout);
+ if (! NoDots) {
+ fputc('.', stdout);
+ fflush(stdout);
+ }
}
- {
+ if (! NoMsync) {
intptr_t p = (intptr_t) data;
char* begin = (char*)( (p / g) * g );
char* end = (char*)( ((p + (intptr_t) (count * block) + g - 1) / g) * g );

0 comments on commit a0a418f

Please sign in to comment.
Something went wrong with that request. Please try again.