Fix bad sign on td_verror()
[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
2f9ade3c
JA
13static void *pinned_mem;
14
15void fio_unpin_memory(void)
16{
17 if (pinned_mem) {
ee56ad50 18 dprint(FD_MEM, "unpinning %llu bytes\n", mlock_size);
2f9ade3c
JA
19 if (munlock(pinned_mem, mlock_size) < 0)
20 perror("munlock");
21 munmap(pinned_mem, mlock_size);
22 pinned_mem = NULL;
23 }
24}
25
26int fio_pin_memory(void)
27{
28 unsigned long long phys_mem;
29
30 if (!mlock_size)
31 return 0;
32
ee56ad50
JA
33 dprint(FD_MEM, "pinning %llu bytes\n", mlock_size);
34
2f9ade3c
JA
35 /*
36 * Don't allow mlock of more than real_mem-128MB
37 */
38 phys_mem = os_phys_mem();
39 if (phys_mem) {
40 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
41 mlock_size = phys_mem - 128 * 1024 * 1024;
b22989b9 42 log_info("fio: limiting mlocked memory to %lluMB\n",
5ec10eaa 43 mlock_size >> 20);
2f9ade3c
JA
44 }
45 }
46
5ec10eaa 47 pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE,
a55820db 48 MAP_PRIVATE | OS_MAP_ANON, -1, 0);
2f9ade3c
JA
49 if (pinned_mem == MAP_FAILED) {
50 perror("malloc locked mem");
51 pinned_mem = NULL;
52 return 1;
53 }
54 if (mlock(pinned_mem, mlock_size) < 0) {
55 perror("mlock");
56 munmap(pinned_mem, mlock_size);
57 pinned_mem = NULL;
58 return 1;
59 }
60
61 return 0;
62}
63
829a602c 64static int alloc_mem_shm(struct thread_data *td, unsigned int total_mem)
2f9ade3c 65{
b6f9676e
JA
66 int flags = IPC_CREAT | SHM_R | SHM_W;
67
a1242a20
JA
68 if (td->o.mem_type == MEM_SHMHUGE) {
69 unsigned long mask = td->o.hugepage_size - 1;
70
b6f9676e 71 flags |= SHM_HUGETLB;
a1242a20
JA
72 total_mem = (total_mem + mask) & ~mask;
73 }
b6f9676e 74
829a602c
JA
75 td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
76 dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
b6f9676e
JA
77 if (td->shm_id < 0) {
78 td_verror(td, errno, "shmget");
da7d79b0 79 if (geteuid() != 0 && (errno == ENOMEM || errno == EPERM))
b6f9676e 80 log_err("fio: you may need to run this job as root\n");
886b878a 81 if (td->o.mem_type == MEM_SHMHUGE) {
5ec10eaa
JA
82 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 }
d8602dd0 93 }
5ec10eaa 94
b6f9676e
JA
95 return 1;
96 }
2f9ade3c 97
b6f9676e 98 td->orig_buffer = shmat(td->shm_id, NULL, 0);
ee56ad50 99 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
b6f9676e
JA
100 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
829a602c
JA
109static 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
118static int alloc_mem_mmap(struct thread_data *td, unsigned int total_mem)
b6f9676e
JA
119{
120 int flags = MAP_PRIVATE;
121
a55820db 122 td->mmapfd = 1;
b6f9676e
JA
123
124 if (td->mmapfile) {
125 td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
126
127 if (td->mmapfd < 0) {
128 td_verror(td, errno, "open mmap file");
2f9ade3c
JA
129 td->orig_buffer = NULL;
130 return 1;
131 }
829a602c 132 if (ftruncate(td->mmapfd, total_mem) < 0) {
b6f9676e 133 td_verror(td, errno, "truncate mmap file");
2f9ade3c
JA
134 td->orig_buffer = NULL;
135 return 1;
136 }
b6f9676e
JA
137 } else
138 flags |= OS_MAP_ANON;
139
829a602c
JA
140 td->orig_buffer = mmap(NULL, total_mem, PROT_READ | PROT_WRITE, flags,
141 td->mmapfd, 0);
142 dprint(FD_MEM, "mmap %u/%d %p\n", total_mem, td->mmapfd,
143 td->orig_buffer);
b6f9676e
JA
144 if (td->orig_buffer == MAP_FAILED) {
145 td_verror(td, errno, "mmap");
146 td->orig_buffer = NULL;
147 if (td->mmapfd) {
148 close(td->mmapfd);
149 unlink(td->mmapfile);
150 }
5ec10eaa 151
b6f9676e 152 return 1;
2f9ade3c
JA
153 }
154
155 return 0;
156}
157
829a602c 158static void free_mem_mmap(struct thread_data *td, unsigned int total_mem)
b6f9676e 159{
829a602c
JA
160 dprint(FD_MEM, "munmap %u %p\n", total_mem, td->orig_buffer);
161 munmap(td->orig_buffer, td->orig_buffer_size);
162 if (td->mmapfile) {
163 close(td->mmapfd);
164 unlink(td->mmapfile);
165 free(td->mmapfile);
166 }
167}
d87612ac 168
829a602c
JA
169static int alloc_mem_malloc(struct thread_data *td, unsigned int total_mem)
170{
171 td->orig_buffer = malloc(total_mem);
172 dprint(FD_MEM, "malloc %u %p\n", total_mem, td->orig_buffer);
5ec10eaa 173
829a602c
JA
174 return td->orig_buffer == NULL;
175}
b6f9676e 176
829a602c
JA
177static void free_mem_malloc(struct thread_data *td)
178{
179 dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer);
180 free(td->orig_buffer);
b6f9676e
JA
181}
182
183/*
184 * Setup the buffer area we need for io.
185 */
186int allocate_io_mem(struct thread_data *td)
187{
829a602c 188 unsigned int total_mem;
b6f9676e
JA
189 int ret = 0;
190
b4c5e1ac
JA
191 if (td->io_ops->flags & FIO_NOIO)
192 return 0;
193
829a602c 194 total_mem = td->orig_buffer_size;
d529ee19
JA
195
196 if (td->o.odirect || td->o.mem_align) {
829a602c 197 total_mem += page_mask;
d529ee19
JA
198 if (td->o.mem_align && td->o.mem_align > page_size)
199 total_mem += td->o.mem_align - page_size;
200 }
829a602c 201
b6f9676e 202 if (td->o.mem_type == MEM_MALLOC)
829a602c 203 ret = alloc_mem_malloc(td, total_mem);
b6f9676e 204 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
829a602c 205 ret = alloc_mem_shm(td, total_mem);
b6f9676e 206 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
829a602c 207 ret = alloc_mem_mmap(td, total_mem);
b6f9676e
JA
208 else {
209 log_err("fio: bad mem type: %d\n", td->o.mem_type);
210 ret = 1;
211 }
212
3deb3101
JA
213 if (ret)
214 td_verror(td, ENOMEM, "iomem allocation");
215
b6f9676e
JA
216 return ret;
217}
218
2f9ade3c
JA
219void free_io_mem(struct thread_data *td)
220{
829a602c
JA
221 unsigned int total_mem;
222
223 total_mem = td->orig_buffer_size;
224 if (td->o.odirect)
225 total_mem += page_mask;
226
227 if (td->o.mem_type == MEM_MALLOC)
228 free_mem_malloc(td);
229 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
230 free_mem_shm(td);
231 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
232 free_mem_mmap(td, total_mem);
233 else
2dc1bbeb 234 log_err("Bad memory type %u\n", td->o.mem_type);
2f9ade3c
JA
235
236 td->orig_buffer = NULL;
829a602c 237 td->orig_buffer_size = 0;
2f9ade3c 238}