Merge branch 'master' into gfio
[fio.git] / memory.c
CommitLineData
2f9ade3c
JA
1/*
2 * Memory helpers
3 */
5921e80c
JA
4#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
2f9ade3c
JA
7#include <unistd.h>
8#include <sys/shm.h>
9#include <sys/mman.h>
10
11#include "fio.h"
2f9ade3c 12
1b79a070 13void fio_unpin_memory(struct thread_data *td)
2f9ade3c 14{
1b79a070
JA
15 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)
2f9ade3c 18 perror("munlock");
1b79a070
JA
19 munmap(td->pinned_mem, td->o.lockmem);
20 td->pinned_mem = NULL;
2f9ade3c
JA
21 }
22}
23
1b79a070 24int fio_pin_memory(struct thread_data *td)
2f9ade3c
JA
25{
26 unsigned long long phys_mem;
27
1b79a070 28 if (!td->o.lockmem)
2f9ade3c
JA
29 return 0;
30
1b79a070 31 dprint(FD_MEM, "pinning %llu bytes\n", td->o.lockmem);
ee56ad50 32
2f9ade3c
JA
33 /*
34 * Don't allow mlock of more than real_mem-128MB
35 */
36 phys_mem = os_phys_mem();
37 if (phys_mem) {
1b79a070
JA
38 if ((td->o.lockmem + 128 * 1024 * 1024) > phys_mem) {
39 td->o.lockmem = phys_mem - 128 * 1024 * 1024;
b22989b9 40 log_info("fio: limiting mlocked memory to %lluMB\n",
1b79a070 41 td->o.lockmem >> 20);
2f9ade3c
JA
42 }
43 }
44
1b79a070 45 td->pinned_mem = mmap(NULL, td->o.lockmem, PROT_READ | PROT_WRITE,
a55820db 46 MAP_PRIVATE | OS_MAP_ANON, -1, 0);
1b79a070 47 if (td->pinned_mem == MAP_FAILED) {
2f9ade3c 48 perror("malloc locked mem");
1b79a070 49 td->pinned_mem = NULL;
2f9ade3c
JA
50 return 1;
51 }
1b79a070 52 if (mlock(td->pinned_mem, td->o.lockmem) < 0) {
2f9ade3c 53 perror("mlock");
1b79a070
JA
54 munmap(td->pinned_mem, td->o.lockmem);
55 td->pinned_mem = NULL;
2f9ade3c
JA
56 return 1;
57 }
58
59 return 0;
60}
61
829a602c 62static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem)
2f9ade3c 63{
03e20d68 64 int flags = IPC_CREAT | S_IRUSR | S_IWUSR;
b6f9676e 65
a1242a20
JA
66 if (td->o.mem_type == MEM_SHMHUGE) {
67 unsigned long mask = td->o.hugepage_size - 1;
68
b6f9676e 69 flags |= SHM_HUGETLB;
a1242a20
JA
70 total_mem = (total_mem + mask) & ~mask;
71 }
b6f9676e 72
829a602c
JA
73 td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
74 dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
b6f9676e
JA
75 if (td->shm_id < 0) {
76 td_verror(td, errno, "shmget");
da7d79b0 77 if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM))
b6f9676e 78 log_err("fio: you may need to run this job as root\n");
886b878a 79 if (td->o.mem_type == MEM_SHMHUGE) {
5ec10eaa
JA
80 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 }
d8602dd0 91 }
5ec10eaa 92
b6f9676e
JA
93 return 1;
94 }
2f9ade3c 95
b6f9676e 96 td->orig_buffer = shmat(td->shm_id, NULL, 0);
ee56ad50 97 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
b6f9676e
JA
98 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
829a602c
JA
107static 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
0f805c00 116static int alloc_mem_mmap(struct thread_data *td, size_t total_mem)
b6f9676e
JA
117{
118 int flags = MAP_PRIVATE;
119
a55820db 120 td->mmapfd = 1;
b6f9676e 121
83ea422a
JA
122 if (td->o.mmapfile) {
123 td->mmapfd = open(td->o.mmapfile, O_RDWR|O_CREAT, 0644);
b6f9676e
JA
124
125 if (td->mmapfd < 0) {
126 td_verror(td, errno, "open mmap file");
2f9ade3c
JA
127 td->orig_buffer = NULL;
128 return 1;
129 }
829a602c 130 if (ftruncate(td->mmapfd, total_mem) < 0) {
b6f9676e 131 td_verror(td, errno, "truncate mmap file");
2f9ade3c
JA
132 td->orig_buffer = NULL;
133 return 1;
134 }
b6f9676e
JA
135 } else
136 flags |= OS_MAP_ANON;
137
829a602c
JA
138 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);
b6f9676e
JA
142 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);
83ea422a 147 unlink(td->o.mmapfile);
b6f9676e 148 }
5ec10eaa 149
b6f9676e 150 return 1;
2f9ade3c
JA
151 }
152
153 return 0;
154}
155
0f805c00 156static void free_mem_mmap(struct thread_data *td, size_t total_mem)
b6f9676e 157{
829a602c
JA
158 dprint(FD_MEM, "munmap %u %p\n", total_mem, td->orig_buffer);
159 munmap(td->orig_buffer, td->orig_buffer_size);
83ea422a 160 if (td->o.mmapfile) {
829a602c 161 close(td->mmapfd);
83ea422a
JA
162 unlink(td->o.mmapfile);
163 free(td->o.mmapfile);
829a602c
JA
164 }
165}
d87612ac 166
0f805c00 167static int alloc_mem_malloc(struct thread_data *td, size_t total_mem)
829a602c
JA
168{
169 td->orig_buffer = malloc(total_mem);
170 dprint(FD_MEM, "malloc %u %p\n", total_mem, td->orig_buffer);
5ec10eaa 171
829a602c
JA
172 return td->orig_buffer == NULL;
173}
b6f9676e 174
829a602c
JA
175static 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);
b6f9676e
JA
179}
180
181/*
03e20d68 182 * Set up the buffer area we need for io.
b6f9676e
JA
183 */
184int allocate_io_mem(struct thread_data *td)
185{
0f805c00 186 size_t total_mem;
b6f9676e
JA
187 int ret = 0;
188
b4c5e1ac
JA
189 if (td->io_ops->flags & FIO_NOIO)
190 return 0;
191
829a602c 192 total_mem = td->orig_buffer_size;
d529ee19 193
ca7e0ddb
JA
194 if (td->o.odirect || td->o.mem_align ||
195 (td->io_ops->flags & FIO_MEMALIGN)) {
829a602c 196 total_mem += page_mask;
d529ee19
JA
197 if (td->o.mem_align && td->o.mem_align > page_size)
198 total_mem += td->o.mem_align - page_size;
199 }
829a602c 200
0f805c00
JA
201 dprint(FD_MEM, "Alloc %lu for buffers\n", (size_t) total_mem);
202
b6f9676e 203 if (td->o.mem_type == MEM_MALLOC)
829a602c 204 ret = alloc_mem_malloc(td, total_mem);
b6f9676e 205 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
829a602c 206 ret = alloc_mem_shm(td, total_mem);
b6f9676e 207 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
829a602c 208 ret = alloc_mem_mmap(td, total_mem);
b6f9676e
JA
209 else {
210 log_err("fio: bad mem type: %d\n", td->o.mem_type);
211 ret = 1;
212 }
213
3deb3101
JA
214 if (ret)
215 td_verror(td, ENOMEM, "iomem allocation");
216
b6f9676e
JA
217 return ret;
218}
219
2f9ade3c
JA
220void free_io_mem(struct thread_data *td)
221{
829a602c
JA
222 unsigned int total_mem;
223
224 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
2dc1bbeb 235 log_err("Bad memory type %u\n", td->o.mem_type);
2f9ade3c
JA
236
237 td->orig_buffer = NULL;
829a602c 238 td->orig_buffer_size = 0;
2f9ade3c 239}