smalloc: change to a bitmap allocator
[fio.git] / memory.c
CommitLineData
2f9ade3c
JA
1/*
2 * Memory helpers
3 */
4#include <unistd.h>
5#include <sys/shm.h>
6#include <sys/mman.h>
7
8#include "fio.h"
2f9ade3c 9
2f9ade3c
JA
10static void *pinned_mem;
11
12void fio_unpin_memory(void)
13{
14 if (pinned_mem) {
ee56ad50 15 dprint(FD_MEM, "unpinning %llu bytes\n", mlock_size);
2f9ade3c
JA
16 if (munlock(pinned_mem, mlock_size) < 0)
17 perror("munlock");
18 munmap(pinned_mem, mlock_size);
19 pinned_mem = NULL;
20 }
21}
22
23int fio_pin_memory(void)
24{
25 unsigned long long phys_mem;
26
27 if (!mlock_size)
28 return 0;
29
ee56ad50
JA
30 dprint(FD_MEM, "pinning %llu bytes\n", mlock_size);
31
2f9ade3c
JA
32 /*
33 * Don't allow mlock of more than real_mem-128MB
34 */
35 phys_mem = os_phys_mem();
36 if (phys_mem) {
37 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
38 mlock_size = phys_mem - 128 * 1024 * 1024;
5ec10eaa
JA
39 log_info("fio: limiting mlocked memory to %lluMiB\n",
40 mlock_size >> 20);
2f9ade3c
JA
41 }
42 }
43
5ec10eaa
JA
44 pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE,
45 MAP_PRIVATE | OS_MAP_ANON, 0, 0);
2f9ade3c
JA
46 if (pinned_mem == MAP_FAILED) {
47 perror("malloc locked mem");
48 pinned_mem = NULL;
49 return 1;
50 }
51 if (mlock(pinned_mem, mlock_size) < 0) {
52 perror("mlock");
53 munmap(pinned_mem, mlock_size);
54 pinned_mem = NULL;
55 return 1;
56 }
57
58 return 0;
59}
60
b6f9676e 61static int alloc_mem_shm(struct thread_data *td)
2f9ade3c 62{
b6f9676e
JA
63 int flags = IPC_CREAT | SHM_R | SHM_W;
64
65 if (td->o.mem_type == MEM_SHMHUGE)
66 flags |= SHM_HUGETLB;
67
68 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags);
ee56ad50 69 dprint(FD_MEM, "shmget %zu, %d\n", td->orig_buffer_size, td->shm_id);
b6f9676e
JA
70 if (td->shm_id < 0) {
71 td_verror(td, errno, "shmget");
72 if (geteuid() != 0 && errno == ENOMEM)
73 log_err("fio: you may need to run this job as root\n");
886b878a 74 if (td->o.mem_type == MEM_SHMHUGE) {
5ec10eaa
JA
75 if (errno == EINVAL) {
76 log_err("fio: check that you have free huge"
77 " pages and that hugepage-size is"
78 " correct.\n");
79 } else if (errno == ENOSYS) {
80 log_err("fio: your system does not appear to"
81 " support huge pages.\n");
82 } else if (errno == ENOMEM) {
83 log_err("fio: no huge pages available, do you"
84 " need to alocate some? See HOWTO.\n");
85 }
d8602dd0 86 }
5ec10eaa 87
b6f9676e
JA
88 return 1;
89 }
2f9ade3c 90
b6f9676e 91 td->orig_buffer = shmat(td->shm_id, NULL, 0);
ee56ad50 92 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
b6f9676e
JA
93 if (td->orig_buffer == (void *) -1) {
94 td_verror(td, errno, "shmat");
95 td->orig_buffer = NULL;
96 return 1;
97 }
98
99 return 0;
100}
101
102static int alloc_mem_mmap(struct thread_data *td)
103{
104 int flags = MAP_PRIVATE;
105
106 td->mmapfd = 0;
107
108 if (td->mmapfile) {
109 td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
110
111 if (td->mmapfd < 0) {
112 td_verror(td, errno, "open mmap file");
2f9ade3c
JA
113 td->orig_buffer = NULL;
114 return 1;
115 }
b6f9676e
JA
116 if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) {
117 td_verror(td, errno, "truncate mmap file");
2f9ade3c
JA
118 td->orig_buffer = NULL;
119 return 1;
120 }
b6f9676e
JA
121 } else
122 flags |= OS_MAP_ANON;
123
5ec10eaa
JA
124 td->orig_buffer = mmap(NULL, td->orig_buffer_size,
125 PROT_READ | PROT_WRITE, flags, td->mmapfd, 0);
ee56ad50
JA
126 dprint(FD_MEM, "mmap %zu/%d %p\n", td->orig_buffer_size, td->mmapfd,
127 td->orig_buffer);
b6f9676e
JA
128 if (td->orig_buffer == MAP_FAILED) {
129 td_verror(td, errno, "mmap");
130 td->orig_buffer = NULL;
131 if (td->mmapfd) {
132 close(td->mmapfd);
133 unlink(td->mmapfile);
134 }
5ec10eaa 135
b6f9676e 136 return 1;
2f9ade3c
JA
137 }
138
139 return 0;
140}
141
b6f9676e
JA
142static int alloc_mem_malloc(struct thread_data *td)
143{
d87612ac
JA
144 unsigned int bsize = td->orig_buffer_size;
145
146 if (td->o.odirect)
147 bsize += page_mask;
5ec10eaa 148
d87612ac 149 td->orig_buffer = malloc(bsize);
ee56ad50 150 dprint(FD_MEM, "malloc %u %p\n", bsize, td->orig_buffer);
b6f9676e
JA
151 if (td->orig_buffer)
152 return 0;
153
154 return 1;
155}
156
157/*
158 * Setup the buffer area we need for io.
159 */
160int allocate_io_mem(struct thread_data *td)
161{
162 int ret = 0;
163
b4c5e1ac
JA
164 if (td->io_ops->flags & FIO_NOIO)
165 return 0;
166
b6f9676e
JA
167 if (td->o.mem_type == MEM_MALLOC)
168 ret = alloc_mem_malloc(td);
169 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
170 ret = alloc_mem_shm(td);
171 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
172 ret = alloc_mem_mmap(td);
173 else {
174 log_err("fio: bad mem type: %d\n", td->o.mem_type);
175 ret = 1;
176 }
177
3deb3101
JA
178 if (ret)
179 td_verror(td, ENOMEM, "iomem allocation");
180
b6f9676e
JA
181 return ret;
182}
183
2f9ade3c
JA
184void free_io_mem(struct thread_data *td)
185{
ee56ad50
JA
186 if (td->o.mem_type == MEM_MALLOC) {
187 dprint(FD_MEM, "free mem %p\n", td->orig_buffer);
2f9ade3c 188 free(td->orig_buffer);
ee56ad50 189 } else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) {
2f9ade3c
JA
190 struct shmid_ds sbuf;
191
5ec10eaa
JA
192 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id,
193 td->orig_buffer);
2f9ade3c
JA
194 shmdt(td->orig_buffer);
195 shmctl(td->shm_id, IPC_RMID, &sbuf);
2dc1bbeb
JA
196 } else if (td->o.mem_type == MEM_MMAP ||
197 td->o.mem_type == MEM_MMAPHUGE) {
ee56ad50
JA
198 dprint(FD_MEM, "munmap %zu %p\n", td->orig_buffer_size,
199 td->orig_buffer);
2f9ade3c 200 munmap(td->orig_buffer, td->orig_buffer_size);
313cb206
JA
201 if (td->mmapfile) {
202 close(td->mmapfd);
203 unlink(td->mmapfile);
204 free(td->mmapfile);
d0bdaf49
JA
205 }
206 } else
2dc1bbeb 207 log_err("Bad memory type %u\n", td->o.mem_type);
2f9ade3c
JA
208
209 td->orig_buffer = NULL;
210}