Remove repeated --readonly options in documentation
[fio.git] / memory.c
index d3ef2cba9adda00bf9ab4f8d7bff9a7a47889665..4a9dc4af212d05feee7b360c9fd6dd08ea6200f4 100644 (file)
--- a/memory.c
+++ b/memory.c
@@ -5,7 +5,9 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <unistd.h>
+#ifndef FIO_NO_HAVE_SHM_H
 #include <sys/shm.h>
+#endif
 #include <sys/mman.h>
 
 #include "fio.h"
@@ -39,7 +41,7 @@ int fio_pin_memory(void)
        if (phys_mem) {
                if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
                        mlock_size = phys_mem - 128 * 1024 * 1024;
-                       log_info("fio: limiting mlocked memory to %lluMiB\n",
+                       log_info("fio: limiting mlocked memory to %lluMB\n",
                                                        mlock_size >> 20);
                }
        }
@@ -63,16 +65,20 @@ int fio_pin_memory(void)
 
 static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem)
 {
-       int flags = IPC_CREAT | SHM_R | SHM_W;
+       int flags = IPC_CREAT | S_IRUSR | S_IWUSR;
+
+       if (td->o.mem_type == MEM_SHMHUGE) {
+               unsigned long mask = td->o.hugepage_size - 1;
 
-       if (td->o.mem_type == MEM_SHMHUGE)
                flags |= SHM_HUGETLB;
+               total_mem = (total_mem + mask) & ~mask;
+       }
 
        td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
        dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
        if (td->shm_id < 0) {
                td_verror(td, errno, "shmget");
-               if (geteuid() != 0 && errno == ENOMEM)
+               if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM))
                        log_err("fio: you may need to run this job as root\n");
                if (td->o.mem_type == MEM_SHMHUGE) {
                        if (errno == EINVAL) {
@@ -111,12 +117,19 @@ static void free_mem_shm(struct thread_data *td)
        shmctl(td->shm_id, IPC_RMID, &sbuf);
 }
 
-static int alloc_mem_mmap(struct thread_data *td, unsigned int total_mem)
+static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
 {
        int flags = MAP_PRIVATE;
 
        td->mmapfd = 1;
 
+       if (td->o.mem_type == MEM_MMAPHUGE) {
+               unsigned long mask = td->o.hugepage_size - 1;
+
+               flags |= MAP_HUGETLB;
+               total_mem = (total_mem + mask) & ~mask;
+       }
+
        if (td->mmapfile) {
                td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
 
@@ -151,7 +164,7 @@ static int alloc_mem_mmap(struct thread_data *td, unsigned int total_mem)
        return 0;
 }
 
-static void free_mem_mmap(struct thread_data *td, unsigned int total_mem)
+static void free_mem_mmap(struct thread_data *td, size_t total_mem)
 {
        dprint(FD_MEM, "munmap %u %p\n", total_mem, td->orig_buffer);
        munmap(td->orig_buffer, td->orig_buffer_size);
@@ -162,7 +175,7 @@ static void free_mem_mmap(struct thread_data *td, unsigned int total_mem)
        }
 }
 
-static int alloc_mem_malloc(struct thread_data *td, unsigned int total_mem)
+static int alloc_mem_malloc(struct thread_data *td, size_t total_mem)
 {
        td->orig_buffer = malloc(total_mem);
        dprint(FD_MEM, "malloc %u %p\n", total_mem, td->orig_buffer);
@@ -177,19 +190,26 @@ static void free_mem_malloc(struct thread_data *td)
 }
 
 /*
- * Setup the buffer area we need for io.
+ * Set up the buffer area we need for io.
  */
 int allocate_io_mem(struct thread_data *td)
 {
-       unsigned int total_mem;
+       size_t total_mem;
        int ret = 0;
 
        if (td->io_ops->flags & FIO_NOIO)
                return 0;
 
        total_mem = td->orig_buffer_size;
-       if (td->o.odirect)
+
+       if (td->o.odirect || td->o.mem_align ||
+           (td->io_ops->flags & FIO_MEMALIGN)) {
                total_mem += page_mask;
+               if (td->o.mem_align && td->o.mem_align > page_size)
+                       total_mem += td->o.mem_align - page_size;
+       }
+
+       dprint(FD_MEM, "Alloc %lu for buffers\n", (size_t) total_mem);
 
        if (td->o.mem_type == MEM_MALLOC)
                ret = alloc_mem_malloc(td, total_mem);