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