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