[PATCH] Add mem=mmap option to use anon mmap for buffer memory
authorJens Axboe <axboe@suse.de>
Fri, 18 Nov 2005 10:10:00 +0000 (11:10 +0100)
committerJens Axboe <axboe@suse.de>
Fri, 18 Nov 2005 10:10:00 +0000 (11:10 +0100)
README.fio
fio-ini.c
fio.c
fio.h

index ed9cae2eeb786de521ba8601afd53eda2e4875ee..f789bced378c56dda31caaf5925a08ed2aa425d9 100644 (file)
@@ -47,7 +47,7 @@ The <jobs> format is as follows:
        invalidate=x    Invalidate page cache for file prior to doing io
        sync=x          Use sync writes if x and writing
        mem=x           If x == malloc, use malloc for buffers. If x == shm,
-                       use shm for buffers.
+                       use shm for buffers. If x == mmap, use anon mmap.
        exitall         When one thread quits, terminate the others
        bwavgtime=x     Average bandwidth stats over x msec
        create_serialize=x      If 'x', serialize file creation.
index 55347ac4b834e1f9a4f3da8338ecfde2fdcbea95..4a8f4749b4fb10ec99f7b9994df840e73d8718eb 100644 (file)
--- a/fio-ini.c
+++ b/fio-ini.c
@@ -608,6 +608,11 @@ int parse_jobs_ini(char *file)
                                fgetpos(f, &off);
                                continue;
                        }
+                       if (!check_str(p, "mem", "mmap")) {
+                               td->mem_type = MEM_MMAP;
+                               fgetpos(f, &off);
+                               continue;
+                       }
                        if (!check_strset(p, "sequential")) {
                                td->sequential = 1;
                                fgetpos(f, &off);
diff --git a/fio.c b/fio.c
index c46b5ac386081c44490504475a1813f93148ec47..976e0a6284afa5bd963cfcf2297d84f5f6d53d68 100644 (file)
--- a/fio.c
+++ b/fio.c
@@ -1105,13 +1105,18 @@ static void cleanup_io_u(struct thread_data *td)
 
                shmdt(td->orig_buffer);
                shmctl(td->shm_id, IPC_RMID, &sbuf);
-       }
+       } else if (td->mem_type == MEM_MMAP)
+               munmap(td->orig_buffer, td->orig_buffer_size);
+       else
+               fprintf(stderr, "Bad memory type %d\n", td->mem_type);
+
+       td->orig_buffer = NULL;
 }
 
 static int init_io_u(struct thread_data *td)
 {
        struct io_u *io_u;
-       int i, max_units, mem_size;
+       int i, max_units;
        char *p;
 
        if (!td->use_aio)
@@ -1119,12 +1124,12 @@ static int init_io_u(struct thread_data *td)
        else
                max_units = td->aio_depth;
 
-       mem_size = td->max_bs * max_units + MASK;
+       td->orig_buffer_size = td->max_bs * max_units + MASK;
 
        if (td->mem_type == MEM_MALLOC)
-               td->orig_buffer = malloc(mem_size);
+               td->orig_buffer = malloc(td->orig_buffer_size);
        else if (td->mem_type == MEM_SHM) {
-               td->shm_id = shmget(IPC_PRIVATE, mem_size, IPC_CREAT | 0600);
+               td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, IPC_CREAT | 0600);
                if (td->shm_id < 0) {
                        td->error = errno;
                        perror("shmget");
@@ -1135,6 +1140,15 @@ static int init_io_u(struct thread_data *td)
                if (td->orig_buffer == (void *) -1) {
                        td->error = errno;
                        perror("shmat");
+                       td->orig_buffer = NULL;
+                       return 1;
+               }
+       } else if (td->mem_type == MEM_MMAP) {
+               td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
+               if (td->orig_buffer == MAP_FAILED) {
+                       td->error = errno;
+                       perror("mmap");
+                       td->orig_buffer = NULL;
                        return 1;
                }
        }
diff --git a/fio.h b/fio.h
index 6dfdc47324ea09f78effa375f17064c7f0e9b641..b8d39670e196d91d3619f72a41438885d37fd621 100644 (file)
--- a/fio.h
+++ b/fio.h
@@ -64,6 +64,7 @@ struct thread_data {
        void *mmap;
        pid_t pid;
        char *orig_buffer;
+       size_t orig_buffer_size;
        volatile int terminate;
        volatile int runstate;
        volatile int old_runstate;
@@ -179,11 +180,18 @@ enum {
        DDIR_WRITE,
 };
 
+/*
+ * What type of allocation to use for io buffers
+ */
 enum {
-       MEM_MALLOC,
-       MEM_SHM,
+       MEM_MALLOC,     /* ordinary malloc */
+       MEM_SHM,        /* use shared memory segments */
+       MEM_MMAP,       /* use anonynomous mmap */
 };
 
+/*
+ * The type of object we are working on
+ */
 enum {
        FIO_TYPE_FILE = 1,
        FIO_TYPE_BD,