Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Memory helpers |
| 3 | */ |
Jens Axboe | 5921e80 | 2008-05-30 15:02:38 +0200 | [diff] [blame] | 4 | #include <sys/types.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <fcntl.h> |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 7 | #include <unistd.h> |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 8 | #include <sys/mman.h> |
| 9 | |
| 10 | #include "fio.h" |
Oleg | a5e0ee1 | 2013-03-12 00:06:53 -0700 | [diff] [blame] | 11 | #ifndef FIO_NO_HAVE_SHM_H |
| 12 | #include <sys/shm.h> |
| 13 | #endif |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 14 | |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 15 | void fio_unpin_memory(struct thread_data *td) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 16 | { |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 17 | if (td->pinned_mem) { |
| 18 | dprint(FD_MEM, "unpinning %llu bytes\n", td->o.lockmem); |
| 19 | if (munlock(td->pinned_mem, td->o.lockmem) < 0) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 20 | perror("munlock"); |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 21 | munmap(td->pinned_mem, td->o.lockmem); |
| 22 | td->pinned_mem = NULL; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 26 | int fio_pin_memory(struct thread_data *td) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 27 | { |
| 28 | unsigned long long phys_mem; |
| 29 | |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 30 | if (!td->o.lockmem) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 31 | return 0; |
| 32 | |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 33 | dprint(FD_MEM, "pinning %llu bytes\n", td->o.lockmem); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 34 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 35 | /* |
| 36 | * Don't allow mlock of more than real_mem-128MB |
| 37 | */ |
| 38 | phys_mem = os_phys_mem(); |
| 39 | if (phys_mem) { |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 40 | if ((td->o.lockmem + 128 * 1024 * 1024) > phys_mem) { |
| 41 | td->o.lockmem = phys_mem - 128 * 1024 * 1024; |
Jens Axboe | b22989b | 2009-07-17 22:29:23 +0200 | [diff] [blame] | 42 | log_info("fio: limiting mlocked memory to %lluMB\n", |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 43 | td->o.lockmem >> 20); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 47 | td->pinned_mem = mmap(NULL, td->o.lockmem, PROT_READ | PROT_WRITE, |
Jens Axboe | a55820d | 2008-06-04 20:37:18 +0200 | [diff] [blame] | 48 | MAP_PRIVATE | OS_MAP_ANON, -1, 0); |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 49 | if (td->pinned_mem == MAP_FAILED) { |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 50 | perror("malloc locked mem"); |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 51 | td->pinned_mem = NULL; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 52 | return 1; |
| 53 | } |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 54 | if (mlock(td->pinned_mem, td->o.lockmem) < 0) { |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 55 | perror("mlock"); |
Jens Axboe | 9a3f110 | 2012-03-28 20:50:15 +0200 | [diff] [blame^] | 56 | munmap(td->pinned_mem, td->o.lockmem); |
| 57 | td->pinned_mem = NULL; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 58 | return 1; |
| 59 | } |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 64 | static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem) |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 65 | { |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 66 | int flags = IPC_CREAT | S_IRUSR | S_IWUSR; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 67 | |
Jens Axboe | a1242a2 | 2009-09-12 21:33:51 +0200 | [diff] [blame] | 68 | if (td->o.mem_type == MEM_SHMHUGE) { |
| 69 | unsigned long mask = td->o.hugepage_size - 1; |
| 70 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 71 | flags |= SHM_HUGETLB; |
Jens Axboe | a1242a2 | 2009-09-12 21:33:51 +0200 | [diff] [blame] | 72 | total_mem = (total_mem + mask) & ~mask; |
| 73 | } |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 74 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 75 | td->shm_id = shmget(IPC_PRIVATE, total_mem, flags); |
| 76 | dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 77 | if (td->shm_id < 0) { |
| 78 | td_verror(td, errno, "shmget"); |
Jens Axboe | da7d79b | 2009-09-14 08:51:55 +0200 | [diff] [blame] | 79 | if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM)) |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 80 | log_err("fio: you may need to run this job as root\n"); |
Jens Axboe | 886b878 | 2007-07-19 10:01:03 +0200 | [diff] [blame] | 81 | if (td->o.mem_type == MEM_SHMHUGE) { |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 82 | if (errno == EINVAL) { |
| 83 | log_err("fio: check that you have free huge" |
| 84 | " pages and that hugepage-size is" |
| 85 | " correct.\n"); |
| 86 | } else if (errno == ENOSYS) { |
| 87 | log_err("fio: your system does not appear to" |
| 88 | " support huge pages.\n"); |
| 89 | } else if (errno == ENOMEM) { |
| 90 | log_err("fio: no huge pages available, do you" |
| 91 | " need to alocate some? See HOWTO.\n"); |
| 92 | } |
Jens Axboe | d8602dd | 2007-07-19 10:00:05 +0200 | [diff] [blame] | 93 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 94 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 95 | return 1; |
| 96 | } |
| 97 | |
| 98 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 99 | dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 100 | if (td->orig_buffer == (void *) -1) { |
| 101 | td_verror(td, errno, "shmat"); |
| 102 | td->orig_buffer = NULL; |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 109 | static void free_mem_shm(struct thread_data *td) |
| 110 | { |
| 111 | struct shmid_ds sbuf; |
| 112 | |
| 113 | dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id, td->orig_buffer); |
| 114 | shmdt(td->orig_buffer); |
| 115 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
| 116 | } |
| 117 | |
Jens Axboe | 0f805c0 | 2011-03-25 21:36:28 +0100 | [diff] [blame] | 118 | static int alloc_mem_mmap(struct thread_data *td, size_t total_mem) |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 119 | { |
Shaohua Li | d9759b1 | 2013-01-17 13:28:15 +0100 | [diff] [blame] | 120 | int flags = 0; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 121 | |
Jens Axboe | a55820d | 2008-06-04 20:37:18 +0200 | [diff] [blame] | 122 | td->mmapfd = 1; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 123 | |
Jens Axboe | d6dc02f | 2012-11-20 13:39:59 +0100 | [diff] [blame] | 124 | if (td->o.mem_type == MEM_MMAPHUGE) { |
| 125 | unsigned long mask = td->o.hugepage_size - 1; |
| 126 | |
Shaohua Li | d9759b1 | 2013-01-17 13:28:15 +0100 | [diff] [blame] | 127 | /* TODO: make sure the file is a real hugetlbfs file */ |
| 128 | if (!td->mmapfile) |
| 129 | flags |= MAP_HUGETLB; |
Jens Axboe | d6dc02f | 2012-11-20 13:39:59 +0100 | [diff] [blame] | 130 | total_mem = (total_mem + mask) & ~mask; |
| 131 | } |
| 132 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 133 | if (td->mmapfile) { |
| 134 | td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644); |
| 135 | |
| 136 | if (td->mmapfd < 0) { |
| 137 | td_verror(td, errno, "open mmap file"); |
| 138 | td->orig_buffer = NULL; |
| 139 | return 1; |
| 140 | } |
Shaohua Li | d9759b1 | 2013-01-17 13:28:15 +0100 | [diff] [blame] | 141 | if (td->o.mem_type != MEM_MMAPHUGE && |
| 142 | ftruncate(td->mmapfd, total_mem) < 0) { |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 143 | td_verror(td, errno, "truncate mmap file"); |
| 144 | td->orig_buffer = NULL; |
| 145 | return 1; |
| 146 | } |
Shaohua Li | d9759b1 | 2013-01-17 13:28:15 +0100 | [diff] [blame] | 147 | if (td->o.mem_type == MEM_MMAPHUGE) |
| 148 | flags |= MAP_SHARED; |
| 149 | else |
| 150 | flags |= MAP_PRIVATE; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 151 | } else |
Shaohua Li | d9759b1 | 2013-01-17 13:28:15 +0100 | [diff] [blame] | 152 | flags |= OS_MAP_ANON | MAP_PRIVATE; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 153 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 154 | td->orig_buffer = mmap(NULL, total_mem, PROT_READ | PROT_WRITE, flags, |
| 155 | td->mmapfd, 0); |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 156 | dprint(FD_MEM, "mmap %llu/%d %p\n", (unsigned long long) total_mem, |
| 157 | td->mmapfd, td->orig_buffer); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 158 | if (td->orig_buffer == MAP_FAILED) { |
| 159 | td_verror(td, errno, "mmap"); |
| 160 | td->orig_buffer = NULL; |
| 161 | if (td->mmapfd) { |
| 162 | close(td->mmapfd); |
| 163 | unlink(td->mmapfile); |
| 164 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 165 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 166 | return 1; |
| 167 | } |
| 168 | |
| 169 | return 0; |
| 170 | } |
| 171 | |
Jens Axboe | 0f805c0 | 2011-03-25 21:36:28 +0100 | [diff] [blame] | 172 | static void free_mem_mmap(struct thread_data *td, size_t total_mem) |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 173 | { |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 174 | dprint(FD_MEM, "munmap %llu %p\n", (unsigned long long) total_mem, |
| 175 | td->orig_buffer); |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 176 | munmap(td->orig_buffer, td->orig_buffer_size); |
| 177 | if (td->mmapfile) { |
| 178 | close(td->mmapfd); |
| 179 | unlink(td->mmapfile); |
| 180 | free(td->mmapfile); |
| 181 | } |
| 182 | } |
Jens Axboe | d87612a | 2007-07-19 15:06:57 +0200 | [diff] [blame] | 183 | |
Jens Axboe | 0f805c0 | 2011-03-25 21:36:28 +0100 | [diff] [blame] | 184 | static int alloc_mem_malloc(struct thread_data *td, size_t total_mem) |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 185 | { |
| 186 | td->orig_buffer = malloc(total_mem); |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 187 | dprint(FD_MEM, "malloc %llu %p\n", (unsigned long long) total_mem, |
| 188 | td->orig_buffer); |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 189 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 190 | return td->orig_buffer == NULL; |
| 191 | } |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 192 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 193 | static void free_mem_malloc(struct thread_data *td) |
| 194 | { |
| 195 | dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer); |
| 196 | free(td->orig_buffer); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 199 | /* |
Bruce Cran | 03e20d6 | 2011-01-02 20:14:54 +0100 | [diff] [blame] | 200 | * Set up the buffer area we need for io. |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 201 | */ |
| 202 | int allocate_io_mem(struct thread_data *td) |
| 203 | { |
Jens Axboe | 0f805c0 | 2011-03-25 21:36:28 +0100 | [diff] [blame] | 204 | size_t total_mem; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 205 | int ret = 0; |
| 206 | |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 207 | if (td->io_ops->flags & FIO_NOIO) |
| 208 | return 0; |
| 209 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 210 | total_mem = td->orig_buffer_size; |
Jens Axboe | d529ee1 | 2009-07-01 10:33:03 +0200 | [diff] [blame] | 211 | |
Jens Axboe | ca7e0dd | 2010-10-28 08:52:13 -0600 | [diff] [blame] | 212 | if (td->o.odirect || td->o.mem_align || |
| 213 | (td->io_ops->flags & FIO_MEMALIGN)) { |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 214 | total_mem += page_mask; |
Jens Axboe | d529ee1 | 2009-07-01 10:33:03 +0200 | [diff] [blame] | 215 | if (td->o.mem_align && td->o.mem_align > page_size) |
| 216 | total_mem += td->o.mem_align - page_size; |
| 217 | } |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 218 | |
Jens Axboe | 4b91ee8 | 2013-02-25 10:18:33 +0100 | [diff] [blame] | 219 | dprint(FD_MEM, "Alloc %llu for buffers\n", (unsigned long long) total_mem); |
Jens Axboe | 0f805c0 | 2011-03-25 21:36:28 +0100 | [diff] [blame] | 220 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 221 | if (td->o.mem_type == MEM_MALLOC) |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 222 | ret = alloc_mem_malloc(td, total_mem); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 223 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 224 | ret = alloc_mem_shm(td, total_mem); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 225 | else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE) |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 226 | ret = alloc_mem_mmap(td, total_mem); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 227 | else { |
| 228 | log_err("fio: bad mem type: %d\n", td->o.mem_type); |
| 229 | ret = 1; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 230 | } |
| 231 | |
Jens Axboe | 3deb310 | 2007-04-26 15:24:20 +0200 | [diff] [blame] | 232 | if (ret) |
| 233 | td_verror(td, ENOMEM, "iomem allocation"); |
| 234 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 235 | return ret; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | void free_io_mem(struct thread_data *td) |
| 239 | { |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 240 | unsigned int total_mem; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 241 | |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 242 | total_mem = td->orig_buffer_size; |
| 243 | if (td->o.odirect) |
| 244 | total_mem += page_mask; |
| 245 | |
| 246 | if (td->o.mem_type == MEM_MALLOC) |
| 247 | free_mem_malloc(td); |
| 248 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) |
| 249 | free_mem_shm(td); |
| 250 | else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE) |
| 251 | free_mem_mmap(td, total_mem); |
| 252 | else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 253 | log_err("Bad memory type %u\n", td->o.mem_type); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 254 | |
| 255 | td->orig_buffer = NULL; |
Jens Axboe | 829a602 | 2009-07-01 09:00:52 +0200 | [diff] [blame] | 256 | td->orig_buffer_size = 0; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 257 | } |