blob: e969221f36f86a8a1c74b502152a97e75f81c219 [file] [log] [blame]
Jens Axboe2f9ade32006-10-20 11:25:52 +02001/*
2 * Memory helpers
3 */
Jens Axboe5921e802008-05-30 15:02:38 +02004#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
Jens Axboe2f9ade32006-10-20 11:25:52 +02007#include <unistd.h>
Aaron Carrollec5c6b12012-11-21 10:39:00 +01008#ifndef FIO_NO_HAVE_SHM_H
Jens Axboe2f9ade32006-10-20 11:25:52 +02009#include <sys/shm.h>
Aaron Carrollec5c6b12012-11-21 10:39:00 +010010#endif
Jens Axboe2f9ade32006-10-20 11:25:52 +020011#include <sys/mman.h>
12
13#include "fio.h"
Jens Axboe2f9ade32006-10-20 11:25:52 +020014
Jens Axboe1b79a072012-03-28 20:50:15 +020015void fio_unpin_memory(struct thread_data *td)
Jens Axboe2f9ade32006-10-20 11:25:52 +020016{
Jens Axboe1b79a072012-03-28 20:50:15 +020017 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 Axboe2f9ade32006-10-20 11:25:52 +020020 perror("munlock");
Jens Axboe1b79a072012-03-28 20:50:15 +020021 munmap(td->pinned_mem, td->o.lockmem);
22 td->pinned_mem = NULL;
Jens Axboe2f9ade32006-10-20 11:25:52 +020023 }
24}
25
Jens Axboe1b79a072012-03-28 20:50:15 +020026int fio_pin_memory(struct thread_data *td)
Jens Axboe2f9ade32006-10-20 11:25:52 +020027{
28 unsigned long long phys_mem;
29
Jens Axboe1b79a072012-03-28 20:50:15 +020030 if (!td->o.lockmem)
Jens Axboe2f9ade32006-10-20 11:25:52 +020031 return 0;
32
Jens Axboe1b79a072012-03-28 20:50:15 +020033 dprint(FD_MEM, "pinning %llu bytes\n", td->o.lockmem);
Jens Axboeee56ad52008-02-01 10:30:20 +010034
Jens Axboe2f9ade32006-10-20 11:25:52 +020035 /*
36 * Don't allow mlock of more than real_mem-128MB
37 */
38 phys_mem = os_phys_mem();
39 if (phys_mem) {
Jens Axboe1b79a072012-03-28 20:50:15 +020040 if ((td->o.lockmem + 128 * 1024 * 1024) > phys_mem) {
41 td->o.lockmem = phys_mem - 128 * 1024 * 1024;
Jens Axboeb22989b2009-07-17 22:29:23 +020042 log_info("fio: limiting mlocked memory to %lluMB\n",
Jens Axboe1b79a072012-03-28 20:50:15 +020043 td->o.lockmem >> 20);
Jens Axboe2f9ade32006-10-20 11:25:52 +020044 }
45 }
46
Jens Axboe1b79a072012-03-28 20:50:15 +020047 td->pinned_mem = mmap(NULL, td->o.lockmem, PROT_READ | PROT_WRITE,
Jens Axboea55820d2008-06-04 20:37:18 +020048 MAP_PRIVATE | OS_MAP_ANON, -1, 0);
Jens Axboe1b79a072012-03-28 20:50:15 +020049 if (td->pinned_mem == MAP_FAILED) {
Jens Axboe2f9ade32006-10-20 11:25:52 +020050 perror("malloc locked mem");
Jens Axboe1b79a072012-03-28 20:50:15 +020051 td->pinned_mem = NULL;
Jens Axboe2f9ade32006-10-20 11:25:52 +020052 return 1;
53 }
Jens Axboe1b79a072012-03-28 20:50:15 +020054 if (mlock(td->pinned_mem, td->o.lockmem) < 0) {
Jens Axboe2f9ade32006-10-20 11:25:52 +020055 perror("mlock");
Jens Axboe1b79a072012-03-28 20:50:15 +020056 munmap(td->pinned_mem, td->o.lockmem);
57 td->pinned_mem = NULL;
Jens Axboe2f9ade32006-10-20 11:25:52 +020058 return 1;
59 }
60
61 return 0;
62}
63
Jens Axboe829a6022009-07-01 09:00:52 +020064static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +020065{
Bruce Cran03e20d62011-01-02 20:14:54 +010066 int flags = IPC_CREAT | S_IRUSR | S_IWUSR;
Jens Axboeb6f96762007-04-17 19:48:46 +020067
Jens Axboea1242a22009-09-12 21:33:51 +020068 if (td->o.mem_type == MEM_SHMHUGE) {
69 unsigned long mask = td->o.hugepage_size - 1;
70
Jens Axboeb6f96762007-04-17 19:48:46 +020071 flags |= SHM_HUGETLB;
Jens Axboea1242a22009-09-12 21:33:51 +020072 total_mem = (total_mem + mask) & ~mask;
73 }
Jens Axboeb6f96762007-04-17 19:48:46 +020074
Jens Axboe829a6022009-07-01 09:00:52 +020075 td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
76 dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
Jens Axboeb6f96762007-04-17 19:48:46 +020077 if (td->shm_id < 0) {
78 td_verror(td, errno, "shmget");
Jens Axboeda7d79b2009-09-14 08:51:55 +020079 if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM))
Jens Axboeb6f96762007-04-17 19:48:46 +020080 log_err("fio: you may need to run this job as root\n");
Jens Axboe886b8782007-07-19 10:01:03 +020081 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010082 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 Axboed8602dd2007-07-19 10:00:05 +020093 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010094
Jens Axboeb6f96762007-04-17 19:48:46 +020095 return 1;
96 }
97
98 td->orig_buffer = shmat(td->shm_id, NULL, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +010099 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200100 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 Axboe829a6022009-07-01 09:00:52 +0200109static 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 Axboe0f805c02011-03-25 21:36:28 +0100118static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200119{
Shaohua Lid9759b12013-01-17 13:28:15 +0100120 int flags = 0;
Jens Axboeb6f96762007-04-17 19:48:46 +0200121
Jens Axboea55820d2008-06-04 20:37:18 +0200122 td->mmapfd = 1;
Jens Axboeb6f96762007-04-17 19:48:46 +0200123
Jens Axboed6dc02f2012-11-20 13:39:59 +0100124 if (td->o.mem_type == MEM_MMAPHUGE) {
125 unsigned long mask = td->o.hugepage_size - 1;
126
Shaohua Lid9759b12013-01-17 13:28:15 +0100127 /* TODO: make sure the file is a real hugetlbfs file */
Jens Axboe836fcc02013-01-24 09:08:45 -0700128 if (!td->o.mmapfile)
Shaohua Lid9759b12013-01-17 13:28:15 +0100129 flags |= MAP_HUGETLB;
Jens Axboed6dc02f2012-11-20 13:39:59 +0100130 total_mem = (total_mem + mask) & ~mask;
131 }
132
Jens Axboe83ea4222012-03-28 14:01:46 +0200133 if (td->o.mmapfile) {
134 td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
Jens Axboeb6f96762007-04-17 19:48:46 +0200135
136 if (td->mmapfd < 0) {
137 td_verror(td, errno, "open mmap file");
138 td->orig_buffer = NULL;
139 return 1;
140 }
Shaohua Lid9759b12013-01-17 13:28:15 +0100141 if (td->o.mem_type != MEM_MMAPHUGE &&
142 ftruncate(td->mmapfd, total_mem) < 0) {
Jens Axboeb6f96762007-04-17 19:48:46 +0200143 td_verror(td, errno, "truncate mmap file");
144 td->orig_buffer = NULL;
145 return 1;
146 }
Shaohua Lid9759b12013-01-17 13:28:15 +0100147 if (td->o.mem_type == MEM_MMAPHUGE)
148 flags |= MAP_SHARED;
149 else
150 flags |= MAP_PRIVATE;
Jens Axboeb6f96762007-04-17 19:48:46 +0200151 } else
Shaohua Lid9759b12013-01-17 13:28:15 +0100152 flags |= OS_MAP_ANON | MAP_PRIVATE;
Jens Axboeb6f96762007-04-17 19:48:46 +0200153
Jens Axboe829a6022009-07-01 09:00:52 +0200154 td->orig_buffer = mmap(NULL, total_mem, PROT_READ | PROT_WRITE, flags,
155 td->mmapfd, 0);
156 dprint(FD_MEM, "mmap %u/%d %p\n", total_mem, td->mmapfd,
157 td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200158 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);
Jens Axboe83ea4222012-03-28 14:01:46 +0200163 unlink(td->o.mmapfile);
Jens Axboeb6f96762007-04-17 19:48:46 +0200164 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100165
Jens Axboeb6f96762007-04-17 19:48:46 +0200166 return 1;
167 }
168
169 return 0;
170}
171
Jens Axboe0f805c02011-03-25 21:36:28 +0100172static void free_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200173{
Jens Axboe829a6022009-07-01 09:00:52 +0200174 dprint(FD_MEM, "munmap %u %p\n", total_mem, td->orig_buffer);
175 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe83ea4222012-03-28 14:01:46 +0200176 if (td->o.mmapfile) {
Jens Axboe829a6022009-07-01 09:00:52 +0200177 close(td->mmapfd);
Jens Axboe83ea4222012-03-28 14:01:46 +0200178 unlink(td->o.mmapfile);
179 free(td->o.mmapfile);
Jens Axboe829a6022009-07-01 09:00:52 +0200180 }
181}
Jens Axboed87612a2007-07-19 15:06:57 +0200182
Jens Axboe0f805c02011-03-25 21:36:28 +0100183static int alloc_mem_malloc(struct thread_data *td, size_t total_mem)
Jens Axboe829a6022009-07-01 09:00:52 +0200184{
185 td->orig_buffer = malloc(total_mem);
186 dprint(FD_MEM, "malloc %u %p\n", total_mem, td->orig_buffer);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100187
Jens Axboe829a6022009-07-01 09:00:52 +0200188 return td->orig_buffer == NULL;
189}
Jens Axboeb6f96762007-04-17 19:48:46 +0200190
Jens Axboe829a6022009-07-01 09:00:52 +0200191static void free_mem_malloc(struct thread_data *td)
192{
193 dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer);
194 free(td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200195}
196
Jens Axboe2f9ade32006-10-20 11:25:52 +0200197/*
Bruce Cran03e20d62011-01-02 20:14:54 +0100198 * Set up the buffer area we need for io.
Jens Axboe2f9ade32006-10-20 11:25:52 +0200199 */
200int allocate_io_mem(struct thread_data *td)
201{
Jens Axboe0f805c02011-03-25 21:36:28 +0100202 size_t total_mem;
Jens Axboeb6f96762007-04-17 19:48:46 +0200203 int ret = 0;
204
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200205 if (td->io_ops->flags & FIO_NOIO)
206 return 0;
207
Jens Axboe829a6022009-07-01 09:00:52 +0200208 total_mem = td->orig_buffer_size;
Jens Axboed529ee12009-07-01 10:33:03 +0200209
Jens Axboeca7e0dd2010-10-28 08:52:13 -0600210 if (td->o.odirect || td->o.mem_align ||
211 (td->io_ops->flags & FIO_MEMALIGN)) {
Jens Axboe829a6022009-07-01 09:00:52 +0200212 total_mem += page_mask;
Jens Axboed529ee12009-07-01 10:33:03 +0200213 if (td->o.mem_align && td->o.mem_align > page_size)
214 total_mem += td->o.mem_align - page_size;
215 }
Jens Axboe829a6022009-07-01 09:00:52 +0200216
Jens Axboe0f805c02011-03-25 21:36:28 +0100217 dprint(FD_MEM, "Alloc %lu for buffers\n", (size_t) total_mem);
218
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100219 if (td->o.mem_type == MEM_MALLOC)
Jens Axboe829a6022009-07-01 09:00:52 +0200220 ret = alloc_mem_malloc(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200221 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200222 ret = alloc_mem_shm(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200223 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200224 ret = alloc_mem_mmap(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200225 else {
226 log_err("fio: bad mem type: %d\n", td->o.mem_type);
227 ret = 1;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200228 }
229
Jens Axboe3deb3102007-04-26 15:24:20 +0200230 if (ret)
231 td_verror(td, ENOMEM, "iomem allocation");
232
Jens Axboeb6f96762007-04-17 19:48:46 +0200233 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200234}
235
236void free_io_mem(struct thread_data *td)
237{
Jens Axboe829a6022009-07-01 09:00:52 +0200238 unsigned int total_mem;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200239
Jens Axboe829a6022009-07-01 09:00:52 +0200240 total_mem = td->orig_buffer_size;
241 if (td->o.odirect)
242 total_mem += page_mask;
243
244 if (td->o.mem_type == MEM_MALLOC)
245 free_mem_malloc(td);
246 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
247 free_mem_shm(td);
248 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
249 free_mem_mmap(td, total_mem);
250 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100251 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200252
253 td->orig_buffer = NULL;
Jens Axboe829a6022009-07-01 09:00:52 +0200254 td->orig_buffer_size = 0;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200255}