blob: 8c06d94d85e7a48ecfb5d8681a8780658f567f6c [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>
Jens Axboe2f9ade32006-10-20 11:25:52 +02008#include <sys/mman.h>
9
10#include "fio.h"
Olega5e0ee12013-03-12 00:06:53 -070011#ifndef FIO_NO_HAVE_SHM_H
12#include <sys/shm.h>
13#endif
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);
Jens Axboe4b91ee82013-02-25 10:18:33 +0100156 dprint(FD_MEM, "mmap %llu/%d %p\n", (unsigned long long) total_mem,
157 td->mmapfd, 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;
Jens Axboeb3493a72014-04-14 13:20:38 -0600161 if (td->mmapfd != 1) {
Jens Axboeb6f96762007-04-17 19:48:46 +0200162 close(td->mmapfd);
Jens Axboeb3493a72014-04-14 13:20:38 -0600163 if (td->o.mmapfile)
164 unlink(td->o.mmapfile);
Jens Axboeb6f96762007-04-17 19:48:46 +0200165 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100166
Jens Axboeb6f96762007-04-17 19:48:46 +0200167 return 1;
168 }
169
170 return 0;
171}
172
Jens Axboe0f805c02011-03-25 21:36:28 +0100173static void free_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200174{
Jens Axboe4b91ee82013-02-25 10:18:33 +0100175 dprint(FD_MEM, "munmap %llu %p\n", (unsigned long long) total_mem,
176 td->orig_buffer);
Jens Axboe829a6022009-07-01 09:00:52 +0200177 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe83ea4222012-03-28 14:01:46 +0200178 if (td->o.mmapfile) {
Jens Axboe829a6022009-07-01 09:00:52 +0200179 close(td->mmapfd);
Jens Axboe83ea4222012-03-28 14:01:46 +0200180 unlink(td->o.mmapfile);
181 free(td->o.mmapfile);
Jens Axboe829a6022009-07-01 09:00:52 +0200182 }
183}
Jens Axboed87612a2007-07-19 15:06:57 +0200184
Jens Axboe0f805c02011-03-25 21:36:28 +0100185static int alloc_mem_malloc(struct thread_data *td, size_t total_mem)
Jens Axboe829a6022009-07-01 09:00:52 +0200186{
187 td->orig_buffer = malloc(total_mem);
Jens Axboe4b91ee82013-02-25 10:18:33 +0100188 dprint(FD_MEM, "malloc %llu %p\n", (unsigned long long) total_mem,
189 td->orig_buffer);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100190
Jens Axboe829a6022009-07-01 09:00:52 +0200191 return td->orig_buffer == NULL;
192}
Jens Axboeb6f96762007-04-17 19:48:46 +0200193
Jens Axboe829a6022009-07-01 09:00:52 +0200194static void free_mem_malloc(struct thread_data *td)
195{
196 dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer);
197 free(td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200198}
199
Jens Axboe2f9ade32006-10-20 11:25:52 +0200200/*
Bruce Cran03e20d62011-01-02 20:14:54 +0100201 * Set up the buffer area we need for io.
Jens Axboe2f9ade32006-10-20 11:25:52 +0200202 */
203int allocate_io_mem(struct thread_data *td)
204{
Jens Axboe0f805c02011-03-25 21:36:28 +0100205 size_t total_mem;
Jens Axboeb6f96762007-04-17 19:48:46 +0200206 int ret = 0;
207
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200208 if (td->io_ops->flags & FIO_NOIO)
209 return 0;
210
Jens Axboe829a6022009-07-01 09:00:52 +0200211 total_mem = td->orig_buffer_size;
Jens Axboed529ee12009-07-01 10:33:03 +0200212
Chris Masond01612f2013-11-15 15:52:58 -0700213 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
Jens Axboeca7e0dd2010-10-28 08:52:13 -0600214 (td->io_ops->flags & FIO_MEMALIGN)) {
Jens Axboe829a6022009-07-01 09:00:52 +0200215 total_mem += page_mask;
Jens Axboed529ee12009-07-01 10:33:03 +0200216 if (td->o.mem_align && td->o.mem_align > page_size)
217 total_mem += td->o.mem_align - page_size;
218 }
Jens Axboe829a6022009-07-01 09:00:52 +0200219
Jens Axboe4b91ee82013-02-25 10:18:33 +0100220 dprint(FD_MEM, "Alloc %llu for buffers\n", (unsigned long long) total_mem);
Jens Axboe0f805c02011-03-25 21:36:28 +0100221
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100222 if (td->o.mem_type == MEM_MALLOC)
Jens Axboe829a6022009-07-01 09:00:52 +0200223 ret = alloc_mem_malloc(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200224 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200225 ret = alloc_mem_shm(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200226 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200227 ret = alloc_mem_mmap(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200228 else {
229 log_err("fio: bad mem type: %d\n", td->o.mem_type);
230 ret = 1;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200231 }
232
Jens Axboe3deb3102007-04-26 15:24:20 +0200233 if (ret)
234 td_verror(td, ENOMEM, "iomem allocation");
235
Jens Axboeb6f96762007-04-17 19:48:46 +0200236 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200237}
238
239void free_io_mem(struct thread_data *td)
240{
Jens Axboe829a6022009-07-01 09:00:52 +0200241 unsigned int total_mem;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200242
Jens Axboe829a6022009-07-01 09:00:52 +0200243 total_mem = td->orig_buffer_size;
Chris Masond01612f2013-11-15 15:52:58 -0700244 if (td->o.odirect || td->o.oatomic)
Jens Axboe829a6022009-07-01 09:00:52 +0200245 total_mem += page_mask;
246
247 if (td->o.mem_type == MEM_MALLOC)
248 free_mem_malloc(td);
249 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
250 free_mem_shm(td);
251 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
252 free_mem_mmap(td, total_mem);
253 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100254 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200255
256 td->orig_buffer = NULL;
Jens Axboe829a6022009-07-01 09:00:52 +0200257 td->orig_buffer_size = 0;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200258}