Always make job use open() instead of dup(), even for shared files
[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;
6d86144d 39 log_info("fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
2f9ade3c
JA
40 }
41 }
42
43 pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
44 if (pinned_mem == MAP_FAILED) {
45 perror("malloc locked mem");
46 pinned_mem = NULL;
47 return 1;
48 }
49 if (mlock(pinned_mem, mlock_size) < 0) {
50 perror("mlock");
51 munmap(pinned_mem, mlock_size);
52 pinned_mem = NULL;
53 return 1;
54 }
55
56 return 0;
57}
58
b6f9676e 59static int alloc_mem_shm(struct thread_data *td)
2f9ade3c 60{
b6f9676e
JA
61 int flags = IPC_CREAT | SHM_R | SHM_W;
62
63 if (td->o.mem_type == MEM_SHMHUGE)
64 flags |= SHM_HUGETLB;
65
66 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags);
ee56ad50 67 dprint(FD_MEM, "shmget %zu, %d\n", td->orig_buffer_size, td->shm_id);
b6f9676e
JA
68 if (td->shm_id < 0) {
69 td_verror(td, errno, "shmget");
70 if (geteuid() != 0 && errno == ENOMEM)
71 log_err("fio: you may need to run this job as root\n");
886b878a 72 if (td->o.mem_type == MEM_SHMHUGE) {
d8602dd0
JA
73 if (errno == EINVAL)
74 log_err("fio: check that you have free huge pages and that hugepage-size is correct.\n");
75 else if (errno == ENOSYS)
76 log_err("fio: your system does not appear to support huge pages.\n");
c7173db6
JA
77 else if (errno == ENOMEM)
78 log_err("fio: no huge pages available, do you need to alocate some? See HOWTO.\n");
d8602dd0 79 }
b6f9676e
JA
80
81 return 1;
82 }
2f9ade3c 83
b6f9676e 84 td->orig_buffer = shmat(td->shm_id, NULL, 0);
ee56ad50 85 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
b6f9676e
JA
86 if (td->orig_buffer == (void *) -1) {
87 td_verror(td, errno, "shmat");
88 td->orig_buffer = NULL;
89 return 1;
90 }
91
92 return 0;
93}
94
95static int alloc_mem_mmap(struct thread_data *td)
96{
97 int flags = MAP_PRIVATE;
98
99 td->mmapfd = 0;
100
101 if (td->mmapfile) {
102 td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
103
104 if (td->mmapfd < 0) {
105 td_verror(td, errno, "open mmap file");
2f9ade3c
JA
106 td->orig_buffer = NULL;
107 return 1;
108 }
b6f9676e
JA
109 if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) {
110 td_verror(td, errno, "truncate mmap file");
2f9ade3c
JA
111 td->orig_buffer = NULL;
112 return 1;
113 }
b6f9676e
JA
114 } else
115 flags |= OS_MAP_ANON;
116
117 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, flags, td->mmapfd, 0);
ee56ad50
JA
118 dprint(FD_MEM, "mmap %zu/%d %p\n", td->orig_buffer_size, td->mmapfd,
119 td->orig_buffer);
b6f9676e
JA
120 if (td->orig_buffer == MAP_FAILED) {
121 td_verror(td, errno, "mmap");
122 td->orig_buffer = NULL;
123 if (td->mmapfd) {
124 close(td->mmapfd);
125 unlink(td->mmapfile);
126 }
127
128 return 1;
2f9ade3c
JA
129 }
130
131 return 0;
132}
133
b6f9676e
JA
134static int alloc_mem_malloc(struct thread_data *td)
135{
d87612ac
JA
136 unsigned int bsize = td->orig_buffer_size;
137
138 if (td->o.odirect)
139 bsize += page_mask;
140
141 td->orig_buffer = malloc(bsize);
ee56ad50 142 dprint(FD_MEM, "malloc %u %p\n", bsize, td->orig_buffer);
b6f9676e
JA
143 if (td->orig_buffer)
144 return 0;
145
146 return 1;
147}
148
149/*
150 * Setup the buffer area we need for io.
151 */
152int allocate_io_mem(struct thread_data *td)
153{
154 int ret = 0;
155
b4c5e1ac
JA
156 if (td->io_ops->flags & FIO_NOIO)
157 return 0;
158
b6f9676e
JA
159 if (td->o.mem_type == MEM_MALLOC)
160 ret = alloc_mem_malloc(td);
161 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
162 ret = alloc_mem_shm(td);
163 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
164 ret = alloc_mem_mmap(td);
165 else {
166 log_err("fio: bad mem type: %d\n", td->o.mem_type);
167 ret = 1;
168 }
169
3deb3101
JA
170 if (ret)
171 td_verror(td, ENOMEM, "iomem allocation");
172
b6f9676e
JA
173 return ret;
174}
175
2f9ade3c
JA
176void free_io_mem(struct thread_data *td)
177{
ee56ad50
JA
178 if (td->o.mem_type == MEM_MALLOC) {
179 dprint(FD_MEM, "free mem %p\n", td->orig_buffer);
2f9ade3c 180 free(td->orig_buffer);
ee56ad50 181 } else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) {
2f9ade3c
JA
182 struct shmid_ds sbuf;
183
ee56ad50 184 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id,td->orig_buffer);
2f9ade3c
JA
185 shmdt(td->orig_buffer);
186 shmctl(td->shm_id, IPC_RMID, &sbuf);
2dc1bbeb
JA
187 } else if (td->o.mem_type == MEM_MMAP ||
188 td->o.mem_type == MEM_MMAPHUGE) {
ee56ad50
JA
189 dprint(FD_MEM, "munmap %zu %p\n", td->orig_buffer_size,
190 td->orig_buffer);
2f9ade3c 191 munmap(td->orig_buffer, td->orig_buffer_size);
313cb206
JA
192 if (td->mmapfile) {
193 close(td->mmapfd);
194 unlink(td->mmapfile);
195 free(td->mmapfile);
d0bdaf49
JA
196 }
197 } else
2dc1bbeb 198 log_err("Bad memory type %u\n", td->o.mem_type);
2f9ade3c
JA
199
200 td->orig_buffer = NULL;
201}