blob: 23a0d94b73e3d353b61044fe85149dfda4e59f55 [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{
Jens Axboeb931a9c2015-01-20 18:38:11 -070066#ifndef CONFIG_NO_SHM
Bruce Cran03e20d62011-01-02 20:14:54 +010067 int flags = IPC_CREAT | S_IRUSR | S_IWUSR;
Jens Axboeb6f96762007-04-17 19:48:46 +020068
Jens Axboea1242a22009-09-12 21:33:51 +020069 if (td->o.mem_type == MEM_SHMHUGE) {
70 unsigned long mask = td->o.hugepage_size - 1;
71
Jens Axboeb6f96762007-04-17 19:48:46 +020072 flags |= SHM_HUGETLB;
Jens Axboea1242a22009-09-12 21:33:51 +020073 total_mem = (total_mem + mask) & ~mask;
74 }
Jens Axboeb6f96762007-04-17 19:48:46 +020075
Jens Axboe829a6022009-07-01 09:00:52 +020076 td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
77 dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
Jens Axboeb6f96762007-04-17 19:48:46 +020078 if (td->shm_id < 0) {
79 td_verror(td, errno, "shmget");
Jens Axboeda7d79b2009-09-14 08:51:55 +020080 if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM))
Jens Axboeb6f96762007-04-17 19:48:46 +020081 log_err("fio: you may need to run this job as root\n");
Jens Axboe886b8782007-07-19 10:01:03 +020082 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010083 if (errno == EINVAL) {
84 log_err("fio: check that you have free huge"
85 " pages and that hugepage-size is"
86 " correct.\n");
87 } else if (errno == ENOSYS) {
88 log_err("fio: your system does not appear to"
89 " support huge pages.\n");
90 } else if (errno == ENOMEM) {
91 log_err("fio: no huge pages available, do you"
92 " need to alocate some? See HOWTO.\n");
93 }
Jens Axboed8602dd2007-07-19 10:00:05 +020094 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010095
Jens Axboeb6f96762007-04-17 19:48:46 +020096 return 1;
97 }
98
99 td->orig_buffer = shmat(td->shm_id, NULL, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +0100100 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200101 if (td->orig_buffer == (void *) -1) {
102 td_verror(td, errno, "shmat");
103 td->orig_buffer = NULL;
104 return 1;
105 }
106
107 return 0;
Jens Axboeb931a9c2015-01-20 18:38:11 -0700108#else
109 log_err("fio: shm not supported\n");
110 return 1;
111#endif
Jens Axboeb6f96762007-04-17 19:48:46 +0200112}
113
Jens Axboe829a6022009-07-01 09:00:52 +0200114static void free_mem_shm(struct thread_data *td)
115{
Jens Axboeb931a9c2015-01-20 18:38:11 -0700116#ifndef CONFIG_NO_SHM
Jens Axboe829a6022009-07-01 09:00:52 +0200117 struct shmid_ds sbuf;
118
119 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id, td->orig_buffer);
120 shmdt(td->orig_buffer);
121 shmctl(td->shm_id, IPC_RMID, &sbuf);
Jens Axboeb931a9c2015-01-20 18:38:11 -0700122#endif
Jens Axboe829a6022009-07-01 09:00:52 +0200123}
124
Jens Axboe0f805c02011-03-25 21:36:28 +0100125static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200126{
Shaohua Lid9759b12013-01-17 13:28:15 +0100127 int flags = 0;
Jens Axboeb6f96762007-04-17 19:48:46 +0200128
Jens Axboe5aa6d822014-10-30 08:40:47 -0600129 td->mmapfd = -1;
Jens Axboeb6f96762007-04-17 19:48:46 +0200130
Jens Axboed6dc02f2012-11-20 13:39:59 +0100131 if (td->o.mem_type == MEM_MMAPHUGE) {
132 unsigned long mask = td->o.hugepage_size - 1;
133
Shaohua Lid9759b12013-01-17 13:28:15 +0100134 /* TODO: make sure the file is a real hugetlbfs file */
Jens Axboe836fcc02013-01-24 09:08:45 -0700135 if (!td->o.mmapfile)
Shaohua Lid9759b12013-01-17 13:28:15 +0100136 flags |= MAP_HUGETLB;
Jens Axboed6dc02f2012-11-20 13:39:59 +0100137 total_mem = (total_mem + mask) & ~mask;
138 }
139
Jens Axboe83ea4222012-03-28 14:01:46 +0200140 if (td->o.mmapfile) {
141 td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
Jens Axboeb6f96762007-04-17 19:48:46 +0200142
143 if (td->mmapfd < 0) {
144 td_verror(td, errno, "open mmap file");
145 td->orig_buffer = NULL;
146 return 1;
147 }
Shaohua Lid9759b12013-01-17 13:28:15 +0100148 if (td->o.mem_type != MEM_MMAPHUGE &&
149 ftruncate(td->mmapfd, total_mem) < 0) {
Jens Axboeb6f96762007-04-17 19:48:46 +0200150 td_verror(td, errno, "truncate mmap file");
151 td->orig_buffer = NULL;
152 return 1;
153 }
Shaohua Lid9759b12013-01-17 13:28:15 +0100154 if (td->o.mem_type == MEM_MMAPHUGE)
155 flags |= MAP_SHARED;
156 else
157 flags |= MAP_PRIVATE;
Jens Axboeb6f96762007-04-17 19:48:46 +0200158 } else
Shaohua Lid9759b12013-01-17 13:28:15 +0100159 flags |= OS_MAP_ANON | MAP_PRIVATE;
Jens Axboeb6f96762007-04-17 19:48:46 +0200160
Jens Axboe829a6022009-07-01 09:00:52 +0200161 td->orig_buffer = mmap(NULL, total_mem, PROT_READ | PROT_WRITE, flags,
162 td->mmapfd, 0);
Jens Axboe4b91ee82013-02-25 10:18:33 +0100163 dprint(FD_MEM, "mmap %llu/%d %p\n", (unsigned long long) total_mem,
164 td->mmapfd, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200165 if (td->orig_buffer == MAP_FAILED) {
166 td_verror(td, errno, "mmap");
167 td->orig_buffer = NULL;
Jens Axboee0ba5712015-01-05 08:50:41 -0700168 if (td->mmapfd != 1 && td->mmapfd != -1) {
Jens Axboeb6f96762007-04-17 19:48:46 +0200169 close(td->mmapfd);
Jens Axboeb3493a72014-04-14 13:20:38 -0600170 if (td->o.mmapfile)
171 unlink(td->o.mmapfile);
Jens Axboeb6f96762007-04-17 19:48:46 +0200172 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100173
Jens Axboeb6f96762007-04-17 19:48:46 +0200174 return 1;
175 }
176
177 return 0;
178}
179
Jens Axboe0f805c02011-03-25 21:36:28 +0100180static void free_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200181{
Jens Axboe4b91ee82013-02-25 10:18:33 +0100182 dprint(FD_MEM, "munmap %llu %p\n", (unsigned long long) total_mem,
183 td->orig_buffer);
Jens Axboe829a6022009-07-01 09:00:52 +0200184 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe83ea4222012-03-28 14:01:46 +0200185 if (td->o.mmapfile) {
Jens Axboe5aa6d822014-10-30 08:40:47 -0600186 if (td->mmapfd != -1)
187 close(td->mmapfd);
Jens Axboe83ea4222012-03-28 14:01:46 +0200188 unlink(td->o.mmapfile);
189 free(td->o.mmapfile);
Jens Axboe829a6022009-07-01 09:00:52 +0200190 }
191}
Jens Axboed87612a2007-07-19 15:06:57 +0200192
Jens Axboe0f805c02011-03-25 21:36:28 +0100193static int alloc_mem_malloc(struct thread_data *td, size_t total_mem)
Jens Axboe829a6022009-07-01 09:00:52 +0200194{
195 td->orig_buffer = malloc(total_mem);
Jens Axboe4b91ee82013-02-25 10:18:33 +0100196 dprint(FD_MEM, "malloc %llu %p\n", (unsigned long long) total_mem,
197 td->orig_buffer);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100198
Jens Axboe829a6022009-07-01 09:00:52 +0200199 return td->orig_buffer == NULL;
200}
Jens Axboeb6f96762007-04-17 19:48:46 +0200201
Jens Axboe829a6022009-07-01 09:00:52 +0200202static void free_mem_malloc(struct thread_data *td)
203{
204 dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer);
205 free(td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200206}
207
Jens Axboe2f9ade32006-10-20 11:25:52 +0200208/*
Bruce Cran03e20d62011-01-02 20:14:54 +0100209 * Set up the buffer area we need for io.
Jens Axboe2f9ade32006-10-20 11:25:52 +0200210 */
211int allocate_io_mem(struct thread_data *td)
212{
Jens Axboe0f805c02011-03-25 21:36:28 +0100213 size_t total_mem;
Jens Axboeb6f96762007-04-17 19:48:46 +0200214 int ret = 0;
215
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200216 if (td->io_ops->flags & FIO_NOIO)
217 return 0;
218
Jens Axboe829a6022009-07-01 09:00:52 +0200219 total_mem = td->orig_buffer_size;
Jens Axboed529ee12009-07-01 10:33:03 +0200220
Chris Masond01612f2013-11-15 15:52:58 -0700221 if (td->o.odirect || td->o.mem_align || td->o.oatomic ||
Jens Axboeca7e0dd2010-10-28 08:52:13 -0600222 (td->io_ops->flags & FIO_MEMALIGN)) {
Jens Axboe829a6022009-07-01 09:00:52 +0200223 total_mem += page_mask;
Jens Axboed529ee12009-07-01 10:33:03 +0200224 if (td->o.mem_align && td->o.mem_align > page_size)
225 total_mem += td->o.mem_align - page_size;
226 }
Jens Axboe829a6022009-07-01 09:00:52 +0200227
Jens Axboe4b91ee82013-02-25 10:18:33 +0100228 dprint(FD_MEM, "Alloc %llu for buffers\n", (unsigned long long) total_mem);
Jens Axboe0f805c02011-03-25 21:36:28 +0100229
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100230 if (td->o.mem_type == MEM_MALLOC)
Jens Axboe829a6022009-07-01 09:00:52 +0200231 ret = alloc_mem_malloc(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200232 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200233 ret = alloc_mem_shm(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200234 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200235 ret = alloc_mem_mmap(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200236 else {
237 log_err("fio: bad mem type: %d\n", td->o.mem_type);
238 ret = 1;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200239 }
240
Jens Axboe3deb3102007-04-26 15:24:20 +0200241 if (ret)
242 td_verror(td, ENOMEM, "iomem allocation");
243
Jens Axboeb6f96762007-04-17 19:48:46 +0200244 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200245}
246
247void free_io_mem(struct thread_data *td)
248{
Jens Axboe829a6022009-07-01 09:00:52 +0200249 unsigned int total_mem;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200250
Jens Axboe829a6022009-07-01 09:00:52 +0200251 total_mem = td->orig_buffer_size;
Chris Masond01612f2013-11-15 15:52:58 -0700252 if (td->o.odirect || td->o.oatomic)
Jens Axboe829a6022009-07-01 09:00:52 +0200253 total_mem += page_mask;
254
255 if (td->o.mem_type == MEM_MALLOC)
256 free_mem_malloc(td);
257 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
258 free_mem_shm(td);
259 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
260 free_mem_mmap(td, total_mem);
261 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100262 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200263
264 td->orig_buffer = NULL;
Jens Axboe829a6022009-07-01 09:00:52 +0200265 td->orig_buffer_size = 0;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200266}