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