Merge branch 'master' into gfio
[fio.git] / memory.c
index 443d71d..e06cab2 100644 (file)
--- a/memory.c
+++ b/memory.c
 #include <sys/shm.h>
 #endif
 
-static void *pinned_mem;
-
-void fio_unpin_memory(void)
+void fio_unpin_memory(struct thread_data *td)
 {
-       if (pinned_mem) {
-               dprint(FD_MEM, "unpinning %llu bytes\n", mlock_size);
-               if (munlock(pinned_mem, mlock_size) < 0)
+       if (td->pinned_mem) {
+               dprint(FD_MEM, "unpinning %llu bytes\n", td->o.lockmem);
+               if (munlock(td->pinned_mem, td->o.lockmem) < 0)
                        perror("munlock");
-               munmap(pinned_mem, mlock_size);
-               pinned_mem = NULL;
+               munmap(td->pinned_mem, td->o.lockmem);
+               td->pinned_mem = NULL;
        }
 }
 
-int fio_pin_memory(void)
+int fio_pin_memory(struct thread_data *td)
 {
        unsigned long long phys_mem;
 
-       if (!mlock_size)
+       if (!td->o.lockmem)
                return 0;
 
-       dprint(FD_MEM, "pinning %llu bytes\n", mlock_size);
+       dprint(FD_MEM, "pinning %llu bytes\n", td->o.lockmem);
 
        /*
         * Don't allow mlock of more than real_mem-128MB
         */
        phys_mem = os_phys_mem();
        if (phys_mem) {
-               if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
-                       mlock_size = phys_mem - 128 * 1024 * 1024;
+               if ((td->o.lockmem + 128 * 1024 * 1024) > phys_mem) {
+                       td->o.lockmem = phys_mem - 128 * 1024 * 1024;
                        log_info("fio: limiting mlocked memory to %lluMB\n",
-                                                       mlock_size >> 20);
+                                                       td->o.lockmem >> 20);
                }
        }
 
-       pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE,
+       td->pinned_mem = mmap(NULL, td->o.lockmem, PROT_READ | PROT_WRITE,
                                MAP_PRIVATE | OS_MAP_ANON, -1, 0);
-       if (pinned_mem == MAP_FAILED) {
+       if (td->pinned_mem == MAP_FAILED) {
                perror("malloc locked mem");
-               pinned_mem = NULL;
+               td->pinned_mem = NULL;
                return 1;
        }
-       if (mlock(pinned_mem, mlock_size) < 0) {
+       if (mlock(td->pinned_mem, td->o.lockmem) < 0) {
                perror("mlock");
-               munmap(pinned_mem, mlock_size);
-               pinned_mem = NULL;
+               munmap(td->pinned_mem, td->o.lockmem);
+               td->pinned_mem = NULL;
                return 1;
        }
 
@@ -127,13 +125,13 @@ static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
                unsigned long mask = td->o.hugepage_size - 1;
 
                /* TODO: make sure the file is a real hugetlbfs file */
-               if (!td->mmapfile)
+               if (!td->o.mmapfile)
                        flags |= MAP_HUGETLB;
                total_mem = (total_mem + mask) & ~mask;
        }
 
-       if (td->mmapfile) {
-               td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
+       if (td->o.mmapfile) {
+               td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
 
                if (td->mmapfd < 0) {
                        td_verror(td, errno, "open mmap file");
@@ -162,7 +160,7 @@ static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
                td->orig_buffer = NULL;
                if (td->mmapfd) {
                        close(td->mmapfd);
-                       unlink(td->mmapfile);
+                       unlink(td->o.mmapfile);
                }
 
                return 1;
@@ -176,10 +174,10 @@ static void free_mem_mmap(struct thread_data *td, size_t total_mem)
        dprint(FD_MEM, "munmap %llu %p\n", (unsigned long long) total_mem,
                                                td->orig_buffer);
        munmap(td->orig_buffer, td->orig_buffer_size);
-       if (td->mmapfile) {
+       if (td->o.mmapfile) {
                close(td->mmapfd);
-               unlink(td->mmapfile);
-               free(td->mmapfile);
+               unlink(td->o.mmapfile);
+               free(td->o.mmapfile);
        }
 }