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 | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 64 | static int alloc_mem_shm(struct thread_data *td) |
| 65 | { |
| 66 | int flags = IPC_CREAT | SHM_R | SHM_W; |
| 67 | |
| 68 | if (td->o.mem_type == MEM_SHMHUGE) |
| 69 | flags |= SHM_HUGETLB; |
| 70 | |
| 71 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 72 | dprint(FD_MEM, "shmget %zu, %d\n", td->orig_buffer_size, 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 | |
| 105 | static int alloc_mem_mmap(struct thread_data *td) |
| 106 | { |
| 107 | int flags = MAP_PRIVATE; |
| 108 | |
Jens Axboe | a55820d | 2008-06-04 20:37:18 +0200 | [diff] [blame] | 109 | td->mmapfd = 1; |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 110 | |
| 111 | if (td->mmapfile) { |
| 112 | td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644); |
| 113 | |
| 114 | if (td->mmapfd < 0) { |
| 115 | td_verror(td, errno, "open mmap file"); |
| 116 | td->orig_buffer = NULL; |
| 117 | return 1; |
| 118 | } |
| 119 | if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) { |
| 120 | td_verror(td, errno, "truncate mmap file"); |
| 121 | td->orig_buffer = NULL; |
| 122 | return 1; |
| 123 | } |
| 124 | } else |
| 125 | flags |= OS_MAP_ANON; |
| 126 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 127 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, |
| 128 | PROT_READ | PROT_WRITE, flags, td->mmapfd, 0); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 129 | dprint(FD_MEM, "mmap %zu/%d %p\n", td->orig_buffer_size, td->mmapfd, |
| 130 | td->orig_buffer); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 131 | if (td->orig_buffer == MAP_FAILED) { |
| 132 | td_verror(td, errno, "mmap"); |
| 133 | td->orig_buffer = NULL; |
| 134 | if (td->mmapfd) { |
| 135 | close(td->mmapfd); |
| 136 | unlink(td->mmapfile); |
| 137 | } |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 138 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 139 | return 1; |
| 140 | } |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static int alloc_mem_malloc(struct thread_data *td) |
| 146 | { |
Jens Axboe | d87612a | 2007-07-19 15:06:57 +0200 | [diff] [blame] | 147 | unsigned int bsize = td->orig_buffer_size; |
| 148 | |
| 149 | if (td->o.odirect) |
| 150 | bsize += page_mask; |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 151 | |
Jens Axboe | d87612a | 2007-07-19 15:06:57 +0200 | [diff] [blame] | 152 | td->orig_buffer = malloc(bsize); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 153 | dprint(FD_MEM, "malloc %u %p\n", bsize, td->orig_buffer); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 154 | if (td->orig_buffer) |
| 155 | return 0; |
| 156 | |
| 157 | return 1; |
| 158 | } |
| 159 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 160 | /* |
| 161 | * Setup the buffer area we need for io. |
| 162 | */ |
| 163 | int allocate_io_mem(struct thread_data *td) |
| 164 | { |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 165 | int ret = 0; |
| 166 | |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame] | 167 | if (td->io_ops->flags & FIO_NOIO) |
| 168 | return 0; |
| 169 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 170 | if (td->o.mem_type == MEM_MALLOC) |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 171 | ret = alloc_mem_malloc(td); |
| 172 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) |
| 173 | ret = alloc_mem_shm(td); |
| 174 | else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE) |
| 175 | ret = alloc_mem_mmap(td); |
| 176 | else { |
| 177 | log_err("fio: bad mem type: %d\n", td->o.mem_type); |
| 178 | ret = 1; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 179 | } |
| 180 | |
Jens Axboe | 3deb310 | 2007-04-26 15:24:20 +0200 | [diff] [blame] | 181 | if (ret) |
| 182 | td_verror(td, ENOMEM, "iomem allocation"); |
| 183 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 184 | return ret; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | void free_io_mem(struct thread_data *td) |
| 188 | { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 189 | if (td->o.mem_type == MEM_MALLOC) { |
| 190 | dprint(FD_MEM, "free mem %p\n", td->orig_buffer); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 191 | free(td->orig_buffer); |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 192 | } else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) { |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 193 | struct shmid_ds sbuf; |
| 194 | |
Jens Axboe | 5ec10ea | 2008-03-06 15:42:00 +0100 | [diff] [blame] | 195 | dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id, |
| 196 | td->orig_buffer); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 197 | shmdt(td->orig_buffer); |
| 198 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 199 | } else if (td->o.mem_type == MEM_MMAP || |
| 200 | td->o.mem_type == MEM_MMAPHUGE) { |
Jens Axboe | ee56ad5 | 2008-02-01 10:30:20 +0100 | [diff] [blame] | 201 | dprint(FD_MEM, "munmap %zu %p\n", td->orig_buffer_size, |
| 202 | td->orig_buffer); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 203 | munmap(td->orig_buffer, td->orig_buffer_size); |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 204 | if (td->mmapfile) { |
| 205 | close(td->mmapfd); |
| 206 | unlink(td->mmapfile); |
| 207 | free(td->mmapfile); |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 208 | } |
| 209 | } else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 210 | log_err("Bad memory type %u\n", td->o.mem_type); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 211 | |
| 212 | td->orig_buffer = NULL; |
| 213 | } |