blob: f97749bc72c657f7b0d9f3739853dd7039797280 [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>
8#include <sys/shm.h>
9#include <sys/mman.h>
10
11#include "fio.h"
Jens Axboe2f9ade32006-10-20 11:25:52 +020012
Jens Axboe1b79a072012-03-28 20:50:15 +020013void fio_unpin_memory(struct thread_data *td)
Jens Axboe2f9ade32006-10-20 11:25:52 +020014{
Jens Axboe1b79a072012-03-28 20:50:15 +020015 if (td->pinned_mem) {
16 dprint(FD_MEM, "unpinning %llu bytes\n", td->o.lockmem);
17 if (munlock(td->pinned_mem, td->o.lockmem) < 0)
Jens Axboe2f9ade32006-10-20 11:25:52 +020018 perror("munlock");
Jens Axboe1b79a072012-03-28 20:50:15 +020019 munmap(td->pinned_mem, td->o.lockmem);
20 td->pinned_mem = NULL;
Jens Axboe2f9ade32006-10-20 11:25:52 +020021 }
22}
23
Jens Axboe1b79a072012-03-28 20:50:15 +020024int fio_pin_memory(struct thread_data *td)
Jens Axboe2f9ade32006-10-20 11:25:52 +020025{
26 unsigned long long phys_mem;
27
Jens Axboe1b79a072012-03-28 20:50:15 +020028 if (!td->o.lockmem)
Jens Axboe2f9ade32006-10-20 11:25:52 +020029 return 0;
30
Jens Axboe1b79a072012-03-28 20:50:15 +020031 dprint(FD_MEM, "pinning %llu bytes\n", td->o.lockmem);
Jens Axboeee56ad52008-02-01 10:30:20 +010032
Jens Axboe2f9ade32006-10-20 11:25:52 +020033 /*
34 * Don't allow mlock of more than real_mem-128MB
35 */
36 phys_mem = os_phys_mem();
37 if (phys_mem) {
Jens Axboe1b79a072012-03-28 20:50:15 +020038 if ((td->o.lockmem + 128 * 1024 * 1024) > phys_mem) {
39 td->o.lockmem = phys_mem - 128 * 1024 * 1024;
Jens Axboeb22989b2009-07-17 22:29:23 +020040 log_info("fio: limiting mlocked memory to %lluMB\n",
Jens Axboe1b79a072012-03-28 20:50:15 +020041 td->o.lockmem >> 20);
Jens Axboe2f9ade32006-10-20 11:25:52 +020042 }
43 }
44
Jens Axboe1b79a072012-03-28 20:50:15 +020045 td->pinned_mem = mmap(NULL, td->o.lockmem, PROT_READ | PROT_WRITE,
Jens Axboea55820d2008-06-04 20:37:18 +020046 MAP_PRIVATE | OS_MAP_ANON, -1, 0);
Jens Axboe1b79a072012-03-28 20:50:15 +020047 if (td->pinned_mem == MAP_FAILED) {
Jens Axboe2f9ade32006-10-20 11:25:52 +020048 perror("malloc locked mem");
Jens Axboe1b79a072012-03-28 20:50:15 +020049 td->pinned_mem = NULL;
Jens Axboe2f9ade32006-10-20 11:25:52 +020050 return 1;
51 }
Jens Axboe1b79a072012-03-28 20:50:15 +020052 if (mlock(td->pinned_mem, td->o.lockmem) < 0) {
Jens Axboe2f9ade32006-10-20 11:25:52 +020053 perror("mlock");
Jens Axboe1b79a072012-03-28 20:50:15 +020054 munmap(td->pinned_mem, td->o.lockmem);
55 td->pinned_mem = NULL;
Jens Axboe2f9ade32006-10-20 11:25:52 +020056 return 1;
57 }
58
59 return 0;
60}
61
Jens Axboe829a6022009-07-01 09:00:52 +020062static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +020063{
Bruce Cran03e20d62011-01-02 20:14:54 +010064 int flags = IPC_CREAT | S_IRUSR | S_IWUSR;
Jens Axboeb6f96762007-04-17 19:48:46 +020065
Jens Axboea1242a22009-09-12 21:33:51 +020066 if (td->o.mem_type == MEM_SHMHUGE) {
67 unsigned long mask = td->o.hugepage_size - 1;
68
Jens Axboeb6f96762007-04-17 19:48:46 +020069 flags |= SHM_HUGETLB;
Jens Axboea1242a22009-09-12 21:33:51 +020070 total_mem = (total_mem + mask) & ~mask;
71 }
Jens Axboeb6f96762007-04-17 19:48:46 +020072
Jens Axboe829a6022009-07-01 09:00:52 +020073 td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
74 dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
Jens Axboeb6f96762007-04-17 19:48:46 +020075 if (td->shm_id < 0) {
76 td_verror(td, errno, "shmget");
Jens Axboeda7d79b2009-09-14 08:51:55 +020077 if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM))
Jens Axboeb6f96762007-04-17 19:48:46 +020078 log_err("fio: you may need to run this job as root\n");
Jens Axboe886b8782007-07-19 10:01:03 +020079 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010080 if (errno == EINVAL) {
81 log_err("fio: check that you have free huge"
82 " pages and that hugepage-size is"
83 " correct.\n");
84 } else if (errno == ENOSYS) {
85 log_err("fio: your system does not appear to"
86 " support huge pages.\n");
87 } else if (errno == ENOMEM) {
88 log_err("fio: no huge pages available, do you"
89 " need to alocate some? See HOWTO.\n");
90 }
Jens Axboed8602dd2007-07-19 10:00:05 +020091 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010092
Jens Axboeb6f96762007-04-17 19:48:46 +020093 return 1;
94 }
95
96 td->orig_buffer = shmat(td->shm_id, NULL, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +010097 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +020098 if (td->orig_buffer == (void *) -1) {
99 td_verror(td, errno, "shmat");
100 td->orig_buffer = NULL;
101 return 1;
102 }
103
104 return 0;
105}
106
Jens Axboe829a6022009-07-01 09:00:52 +0200107static void free_mem_shm(struct thread_data *td)
108{
109 struct shmid_ds sbuf;
110
111 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id, td->orig_buffer);
112 shmdt(td->orig_buffer);
113 shmctl(td->shm_id, IPC_RMID, &sbuf);
114}
115
Jens Axboe0f805c02011-03-25 21:36:28 +0100116static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200117{
118 int flags = MAP_PRIVATE;
119
Jens Axboea55820d2008-06-04 20:37:18 +0200120 td->mmapfd = 1;
Jens Axboeb6f96762007-04-17 19:48:46 +0200121
Jens Axboe83ea4222012-03-28 14:01:46 +0200122 if (td->o.mmapfile) {
123 td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
Jens Axboeb6f96762007-04-17 19:48:46 +0200124
125 if (td->mmapfd < 0) {
126 td_verror(td, errno, "open mmap file");
127 td->orig_buffer = NULL;
128 return 1;
129 }
Jens Axboe829a6022009-07-01 09:00:52 +0200130 if (ftruncate(td->mmapfd, total_mem) < 0) {
Jens Axboeb6f96762007-04-17 19:48:46 +0200131 td_verror(td, errno, "truncate mmap file");
132 td->orig_buffer = NULL;
133 return 1;
134 }
135 } else
136 flags |= OS_MAP_ANON;
137
Jens Axboe829a6022009-07-01 09:00:52 +0200138 td->orig_buffer = mmap(NULL, total_mem, PROT_READ | PROT_WRITE, flags,
139 td->mmapfd, 0);
140 dprint(FD_MEM, "mmap %u/%d %p\n", total_mem, td->mmapfd,
141 td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200142 if (td->orig_buffer == MAP_FAILED) {
143 td_verror(td, errno, "mmap");
144 td->orig_buffer = NULL;
145 if (td->mmapfd) {
146 close(td->mmapfd);
Jens Axboe83ea4222012-03-28 14:01:46 +0200147 unlink(td->o.mmapfile);
Jens Axboeb6f96762007-04-17 19:48:46 +0200148 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100149
Jens Axboeb6f96762007-04-17 19:48:46 +0200150 return 1;
151 }
152
153 return 0;
154}
155
Jens Axboe0f805c02011-03-25 21:36:28 +0100156static void free_mem_mmap(struct thread_data *td, size_t total_mem)
Jens Axboeb6f96762007-04-17 19:48:46 +0200157{
Jens Axboe829a6022009-07-01 09:00:52 +0200158 dprint(FD_MEM, "munmap %u %p\n", total_mem, td->orig_buffer);
159 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe83ea4222012-03-28 14:01:46 +0200160 if (td->o.mmapfile) {
Jens Axboe829a6022009-07-01 09:00:52 +0200161 close(td->mmapfd);
Jens Axboe83ea4222012-03-28 14:01:46 +0200162 unlink(td->o.mmapfile);
163 free(td->o.mmapfile);
Jens Axboe829a6022009-07-01 09:00:52 +0200164 }
165}
Jens Axboed87612a2007-07-19 15:06:57 +0200166
Jens Axboe0f805c02011-03-25 21:36:28 +0100167static int alloc_mem_malloc(struct thread_data *td, size_t total_mem)
Jens Axboe829a6022009-07-01 09:00:52 +0200168{
169 td->orig_buffer = malloc(total_mem);
170 dprint(FD_MEM, "malloc %u %p\n", total_mem, td->orig_buffer);
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100171
Jens Axboe829a6022009-07-01 09:00:52 +0200172 return td->orig_buffer == NULL;
173}
Jens Axboeb6f96762007-04-17 19:48:46 +0200174
Jens Axboe829a6022009-07-01 09:00:52 +0200175static void free_mem_malloc(struct thread_data *td)
176{
177 dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer);
178 free(td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200179}
180
Jens Axboe2f9ade32006-10-20 11:25:52 +0200181/*
Bruce Cran03e20d62011-01-02 20:14:54 +0100182 * Set up the buffer area we need for io.
Jens Axboe2f9ade32006-10-20 11:25:52 +0200183 */
184int allocate_io_mem(struct thread_data *td)
185{
Jens Axboe0f805c02011-03-25 21:36:28 +0100186 size_t total_mem;
Jens Axboeb6f96762007-04-17 19:48:46 +0200187 int ret = 0;
188
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200189 if (td->io_ops->flags & FIO_NOIO)
190 return 0;
191
Jens Axboe829a6022009-07-01 09:00:52 +0200192 total_mem = td->orig_buffer_size;
Jens Axboed529ee12009-07-01 10:33:03 +0200193
Jens Axboeca7e0dd2010-10-28 08:52:13 -0600194 if (td->o.odirect || td->o.mem_align ||
195 (td->io_ops->flags & FIO_MEMALIGN)) {
Jens Axboe829a6022009-07-01 09:00:52 +0200196 total_mem += page_mask;
Jens Axboed529ee12009-07-01 10:33:03 +0200197 if (td->o.mem_align && td->o.mem_align > page_size)
198 total_mem += td->o.mem_align - page_size;
199 }
Jens Axboe829a6022009-07-01 09:00:52 +0200200
Jens Axboe0f805c02011-03-25 21:36:28 +0100201 dprint(FD_MEM, "Alloc %lu for buffers\n", (size_t) total_mem);
202
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100203 if (td->o.mem_type == MEM_MALLOC)
Jens Axboe829a6022009-07-01 09:00:52 +0200204 ret = alloc_mem_malloc(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200205 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200206 ret = alloc_mem_shm(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200207 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
Jens Axboe829a6022009-07-01 09:00:52 +0200208 ret = alloc_mem_mmap(td, total_mem);
Jens Axboeb6f96762007-04-17 19:48:46 +0200209 else {
210 log_err("fio: bad mem type: %d\n", td->o.mem_type);
211 ret = 1;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200212 }
213
Jens Axboe3deb3102007-04-26 15:24:20 +0200214 if (ret)
215 td_verror(td, ENOMEM, "iomem allocation");
216
Jens Axboeb6f96762007-04-17 19:48:46 +0200217 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200218}
219
220void free_io_mem(struct thread_data *td)
221{
Jens Axboe829a6022009-07-01 09:00:52 +0200222 unsigned int total_mem;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200223
Jens Axboe829a6022009-07-01 09:00:52 +0200224 total_mem = td->orig_buffer_size;
225 if (td->o.odirect)
226 total_mem += page_mask;
227
228 if (td->o.mem_type == MEM_MALLOC)
229 free_mem_malloc(td);
230 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
231 free_mem_shm(td);
232 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
233 free_mem_mmap(td, total_mem);
234 else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100235 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200236
237 td->orig_buffer = NULL;
Jens Axboe829a6022009-07-01 09:00:52 +0200238 td->orig_buffer_size = 0;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200239}