Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Memory helpers |
| 3 | */ |
| 4 | #include <unistd.h> |
| 5 | #include <sys/shm.h> |
| 6 | #include <sys/mman.h> |
| 7 | |
| 8 | #include "fio.h" |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 9 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 10 | static void *pinned_mem; |
| 11 | |
| 12 | void fio_unpin_memory(void) |
| 13 | { |
| 14 | if (pinned_mem) { |
| 15 | if (munlock(pinned_mem, mlock_size) < 0) |
| 16 | perror("munlock"); |
| 17 | munmap(pinned_mem, mlock_size); |
| 18 | pinned_mem = NULL; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | int fio_pin_memory(void) |
| 23 | { |
| 24 | unsigned long long phys_mem; |
| 25 | |
| 26 | if (!mlock_size) |
| 27 | return 0; |
| 28 | |
| 29 | /* |
| 30 | * Don't allow mlock of more than real_mem-128MB |
| 31 | */ |
| 32 | phys_mem = os_phys_mem(); |
| 33 | if (phys_mem) { |
| 34 | if ((mlock_size + 128 * 1024 * 1024) > phys_mem) { |
| 35 | mlock_size = phys_mem - 128 * 1024 * 1024; |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 36 | log_info("fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0); |
| 41 | if (pinned_mem == MAP_FAILED) { |
| 42 | perror("malloc locked mem"); |
| 43 | pinned_mem = NULL; |
| 44 | return 1; |
| 45 | } |
| 46 | if (mlock(pinned_mem, mlock_size) < 0) { |
| 47 | perror("mlock"); |
| 48 | munmap(pinned_mem, mlock_size); |
| 49 | pinned_mem = NULL; |
| 50 | return 1; |
| 51 | } |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 56 | static int alloc_mem_shm(struct thread_data *td) |
| 57 | { |
| 58 | int flags = IPC_CREAT | SHM_R | SHM_W; |
| 59 | |
| 60 | if (td->o.mem_type == MEM_SHMHUGE) |
| 61 | flags |= SHM_HUGETLB; |
| 62 | |
| 63 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags); |
| 64 | if (td->shm_id < 0) { |
| 65 | td_verror(td, errno, "shmget"); |
| 66 | if (geteuid() != 0 && errno == ENOMEM) |
| 67 | 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] | 68 | if (td->o.mem_type == MEM_SHMHUGE) { |
Jens Axboe | d8602dd | 2007-07-19 10:00:05 +0200 | [diff] [blame] | 69 | if (errno == EINVAL) |
| 70 | log_err("fio: check that you have free huge pages and that hugepage-size is correct.\n"); |
| 71 | else if (errno == ENOSYS) |
| 72 | log_err("fio: your system does not appear to support huge pages.\n"); |
Jens Axboe | c7173db | 2007-07-19 10:09:45 +0200 | [diff] [blame] | 73 | else if (errno == ENOMEM) |
| 74 | log_err("fio: no huge pages available, do you need to alocate some? See HOWTO.\n"); |
Jens Axboe | d8602dd | 2007-07-19 10:00:05 +0200 | [diff] [blame] | 75 | } |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 76 | |
| 77 | return 1; |
| 78 | } |
| 79 | |
| 80 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
| 81 | if (td->orig_buffer == (void *) -1) { |
| 82 | td_verror(td, errno, "shmat"); |
| 83 | td->orig_buffer = NULL; |
| 84 | return 1; |
| 85 | } |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int alloc_mem_mmap(struct thread_data *td) |
| 91 | { |
| 92 | int flags = MAP_PRIVATE; |
| 93 | |
| 94 | td->mmapfd = 0; |
| 95 | |
| 96 | if (td->mmapfile) { |
| 97 | td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644); |
| 98 | |
| 99 | if (td->mmapfd < 0) { |
| 100 | td_verror(td, errno, "open mmap file"); |
| 101 | td->orig_buffer = NULL; |
| 102 | return 1; |
| 103 | } |
| 104 | if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) { |
| 105 | td_verror(td, errno, "truncate mmap file"); |
| 106 | td->orig_buffer = NULL; |
| 107 | return 1; |
| 108 | } |
| 109 | } else |
| 110 | flags |= OS_MAP_ANON; |
| 111 | |
| 112 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, flags, td->mmapfd, 0); |
| 113 | if (td->orig_buffer == MAP_FAILED) { |
| 114 | td_verror(td, errno, "mmap"); |
| 115 | td->orig_buffer = NULL; |
| 116 | if (td->mmapfd) { |
| 117 | close(td->mmapfd); |
| 118 | unlink(td->mmapfile); |
| 119 | } |
| 120 | |
| 121 | return 1; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static int alloc_mem_malloc(struct thread_data *td) |
| 128 | { |
Jens Axboe | d87612a | 2007-07-19 15:06:57 +0200 | [diff] [blame] | 129 | unsigned int bsize = td->orig_buffer_size; |
| 130 | |
| 131 | if (td->o.odirect) |
| 132 | bsize += page_mask; |
| 133 | |
| 134 | td->orig_buffer = malloc(bsize); |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 135 | if (td->orig_buffer) |
| 136 | return 0; |
| 137 | |
| 138 | return 1; |
| 139 | } |
| 140 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 141 | /* |
| 142 | * Setup the buffer area we need for io. |
| 143 | */ |
| 144 | int allocate_io_mem(struct thread_data *td) |
| 145 | { |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 146 | int ret = 0; |
| 147 | |
Jens Axboe | b4c5e1a | 2007-10-25 18:44:45 +0200 | [diff] [blame^] | 148 | if (td->io_ops->flags & FIO_NOIO) |
| 149 | return 0; |
| 150 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 151 | if (td->o.mem_type == MEM_MALLOC) |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 152 | ret = alloc_mem_malloc(td); |
| 153 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) |
| 154 | ret = alloc_mem_shm(td); |
| 155 | else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE) |
| 156 | ret = alloc_mem_mmap(td); |
| 157 | else { |
| 158 | log_err("fio: bad mem type: %d\n", td->o.mem_type); |
| 159 | ret = 1; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Jens Axboe | 3deb310 | 2007-04-26 15:24:20 +0200 | [diff] [blame] | 162 | if (ret) |
| 163 | td_verror(td, ENOMEM, "iomem allocation"); |
| 164 | |
Jens Axboe | b6f9676 | 2007-04-17 19:48:46 +0200 | [diff] [blame] | 165 | return ret; |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | void free_io_mem(struct thread_data *td) |
| 169 | { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 170 | if (td->o.mem_type == MEM_MALLOC) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 171 | free(td->orig_buffer); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 172 | 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] | 173 | struct shmid_ds sbuf; |
| 174 | |
| 175 | shmdt(td->orig_buffer); |
| 176 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 177 | } else if (td->o.mem_type == MEM_MMAP || |
| 178 | td->o.mem_type == MEM_MMAPHUGE) { |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 179 | munmap(td->orig_buffer, td->orig_buffer_size); |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 180 | if (td->mmapfile) { |
| 181 | close(td->mmapfd); |
| 182 | unlink(td->mmapfile); |
| 183 | free(td->mmapfile); |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 184 | } |
| 185 | } else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame] | 186 | log_err("Bad memory type %u\n", td->o.mem_type); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 187 | |
| 188 | td->orig_buffer = NULL; |
| 189 | } |