Add tera/peta suffixes
[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
68 if (td->o.mem_type == MEM_SHMHUGE)
69 flags |= SHM_HUGETLB;
70
829a602c
JA
71 td->shm_id = shmget(IPC_PRIVATE, total_mem, flags);
72 dprint(FD_MEM, "shmget %u, %d\n", total_mem, td->shm_id);
b6f9676e
JA
73 if (td->shm_id < 0) {
74 td_verror(td, errno, "shmget");
75 if (geteuid() != 0 && errno == ENOMEM)
76 log_err("fio: you may need to run this job as root\n");
886b878a 77 if (td->o.mem_type == MEM_SHMHUGE) {
5ec10eaa
JA
78 if (errno == EINVAL) {
79 log_err("fio: check that you have free huge"
80 " pages and that hugepage-size is"
81 " correct.\n");
82 } else if (errno == ENOSYS) {
83 log_err("fio: your system does not appear to"
84 " support huge pages.\n");
85 } else if (errno == ENOMEM) {
86 log_err("fio: no huge pages available, do you"
87 " need to alocate some? See HOWTO.\n");
88 }
d8602dd0 89 }
5ec10eaa 90
b6f9676e
JA
91 return 1;
92 }
2f9ade3c 93
b6f9676e 94 td->orig_buffer = shmat(td->shm_id, NULL, 0);
ee56ad50 95 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
b6f9676e
JA
96 if (td->orig_buffer == (void *) -1) {
97 td_verror(td, errno, "shmat");
98 td->orig_buffer = NULL;
99 return 1;
100 }
101
102 return 0;
103}
104
829a602c
JA
105static void free_mem_shm(struct thread_data *td)
106{
107 struct shmid_ds sbuf;
108
109 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id, td->orig_buffer);
110 shmdt(td->orig_buffer);
111 shmctl(td->shm_id, IPC_RMID, &sbuf);
112}
113
114static int alloc_mem_mmap(struct thread_data *td, unsigned int total_mem)
b6f9676e
JA
115{
116 int flags = MAP_PRIVATE;
117
a55820db 118 td->mmapfd = 1;
b6f9676e
JA
119
120 if (td->mmapfile) {
121 td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
122
123 if (td->mmapfd < 0) {
124 td_verror(td, errno, "open mmap file");
2f9ade3c
JA
125 td->orig_buffer = NULL;
126 return 1;
127 }
829a602c 128 if (ftruncate(td->mmapfd, total_mem) < 0) {
b6f9676e 129 td_verror(td, errno, "truncate mmap file");
2f9ade3c
JA
130 td->orig_buffer = NULL;
131 return 1;
132 }
b6f9676e
JA
133 } else
134 flags |= OS_MAP_ANON;
135
829a602c
JA
136 td->orig_buffer = mmap(NULL, total_mem, PROT_READ | PROT_WRITE, flags,
137 td->mmapfd, 0);
138 dprint(FD_MEM, "mmap %u/%d %p\n", total_mem, td->mmapfd,
139 td->orig_buffer);
b6f9676e
JA
140 if (td->orig_buffer == MAP_FAILED) {
141 td_verror(td, errno, "mmap");
142 td->orig_buffer = NULL;
143 if (td->mmapfd) {
144 close(td->mmapfd);
145 unlink(td->mmapfile);
146 }
5ec10eaa 147
b6f9676e 148 return 1;
2f9ade3c
JA
149 }
150
151 return 0;
152}
153
829a602c 154static void free_mem_mmap(struct thread_data *td, unsigned int total_mem)
b6f9676e 155{
829a602c
JA
156 dprint(FD_MEM, "munmap %u %p\n", total_mem, td->orig_buffer);
157 munmap(td->orig_buffer, td->orig_buffer_size);
158 if (td->mmapfile) {
159 close(td->mmapfd);
160 unlink(td->mmapfile);
161 free(td->mmapfile);
162 }
163}
d87612ac 164
829a602c
JA
165static int alloc_mem_malloc(struct thread_data *td, unsigned int total_mem)
166{
167 td->orig_buffer = malloc(total_mem);
168 dprint(FD_MEM, "malloc %u %p\n", total_mem, td->orig_buffer);
5ec10eaa 169
829a602c
JA
170 return td->orig_buffer == NULL;
171}
b6f9676e 172
829a602c
JA
173static void free_mem_malloc(struct thread_data *td)
174{
175 dprint(FD_MEM, "free malloc mem %p\n", td->orig_buffer);
176 free(td->orig_buffer);
b6f9676e
JA
177}
178
179/*
180 * Setup the buffer area we need for io.
181 */
182int allocate_io_mem(struct thread_data *td)
183{
829a602c 184 unsigned int total_mem;
b6f9676e
JA
185 int ret = 0;
186
b4c5e1ac
JA
187 if (td->io_ops->flags & FIO_NOIO)
188 return 0;
189
829a602c 190 total_mem = td->orig_buffer_size;
d529ee19
JA
191
192 if (td->o.odirect || td->o.mem_align) {
829a602c 193 total_mem += page_mask;
d529ee19
JA
194 if (td->o.mem_align && td->o.mem_align > page_size)
195 total_mem += td->o.mem_align - page_size;
196 }
829a602c 197
b6f9676e 198 if (td->o.mem_type == MEM_MALLOC)
829a602c 199 ret = alloc_mem_malloc(td, total_mem);
b6f9676e 200 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
829a602c 201 ret = alloc_mem_shm(td, total_mem);
b6f9676e 202 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
829a602c 203 ret = alloc_mem_mmap(td, total_mem);
b6f9676e
JA
204 else {
205 log_err("fio: bad mem type: %d\n", td->o.mem_type);
206 ret = 1;
207 }
208
3deb3101
JA
209 if (ret)
210 td_verror(td, ENOMEM, "iomem allocation");
211
b6f9676e
JA
212 return ret;
213}
214
2f9ade3c
JA
215void free_io_mem(struct thread_data *td)
216{
829a602c
JA
217 unsigned int total_mem;
218
219 total_mem = td->orig_buffer_size;
220 if (td->o.odirect)
221 total_mem += page_mask;
222
223 if (td->o.mem_type == MEM_MALLOC)
224 free_mem_malloc(td);
225 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
226 free_mem_shm(td);
227 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
228 free_mem_mmap(td, total_mem);
229 else
2dc1bbeb 230 log_err("Bad memory type %u\n", td->o.mem_type);
2f9ade3c
JA
231
232 td->orig_buffer = NULL;
829a602c 233 td->orig_buffer_size = 0;
2f9ade3c 234}