blob: 257914b153a1f37469b3e42071a78a601901f5cc [file] [log] [blame]
Jens Axboe2f9ade32006-10-20 11:25:52 +02001/*
2 * Memory helpers
3 */
4#include <unistd.h>
5#include <sys/shm.h>
6#include <sys/mman.h>
7
8#include "fio.h"
Jens Axboe2f9ade32006-10-20 11:25:52 +02009
Jens Axboe2f9ade32006-10-20 11:25:52 +020010static void *pinned_mem;
11
12void fio_unpin_memory(void)
13{
14 if (pinned_mem) {
Jens Axboeee56ad52008-02-01 10:30:20 +010015 dprint(FD_MEM, "unpinning %llu bytes\n", mlock_size);
Jens Axboe2f9ade32006-10-20 11:25:52 +020016 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
Jens Axboeee56ad52008-02-01 10:30:20 +010030 dprint(FD_MEM, "pinning %llu bytes\n", mlock_size);
31
Jens Axboe2f9ade32006-10-20 11:25:52 +020032 /*
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;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010039 log_info("fio: limiting mlocked memory to %lluMiB\n",
40 mlock_size >> 20);
Jens Axboe2f9ade32006-10-20 11:25:52 +020041 }
42 }
43
Jens Axboe5ec10ea2008-03-06 15:42:00 +010044 pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE,
45 MAP_PRIVATE | OS_MAP_ANON, 0, 0);
Jens Axboe2f9ade32006-10-20 11:25:52 +020046 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
Jens Axboeb6f96762007-04-17 19:48:46 +020061static int alloc_mem_shm(struct thread_data *td)
62{
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);
Jens Axboeee56ad52008-02-01 10:30:20 +010069 dprint(FD_MEM, "shmget %zu, %d\n", td->orig_buffer_size, td->shm_id);
Jens Axboeb6f96762007-04-17 19:48:46 +020070 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");
Jens Axboe886b8782007-07-19 10:01:03 +020074 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010075 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 }
Jens Axboed8602dd2007-07-19 10:00:05 +020086 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010087
Jens Axboeb6f96762007-04-17 19:48:46 +020088 return 1;
89 }
90
91 td->orig_buffer = shmat(td->shm_id, NULL, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +010092 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +020093 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");
113 td->orig_buffer = NULL;
114 return 1;
115 }
116 if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) {
117 td_verror(td, errno, "truncate mmap file");
118 td->orig_buffer = NULL;
119 return 1;
120 }
121 } else
122 flags |= OS_MAP_ANON;
123
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100124 td->orig_buffer = mmap(NULL, td->orig_buffer_size,
125 PROT_READ | PROT_WRITE, flags, td->mmapfd, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +0100126 dprint(FD_MEM, "mmap %zu/%d %p\n", td->orig_buffer_size, td->mmapfd,
127 td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200128 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 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100135
Jens Axboeb6f96762007-04-17 19:48:46 +0200136 return 1;
137 }
138
139 return 0;
140}
141
142static int alloc_mem_malloc(struct thread_data *td)
143{
Jens Axboed87612a2007-07-19 15:06:57 +0200144 unsigned int bsize = td->orig_buffer_size;
145
146 if (td->o.odirect)
147 bsize += page_mask;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100148
Jens Axboed87612a2007-07-19 15:06:57 +0200149 td->orig_buffer = malloc(bsize);
Jens Axboeee56ad52008-02-01 10:30:20 +0100150 dprint(FD_MEM, "malloc %u %p\n", bsize, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200151 if (td->orig_buffer)
152 return 0;
153
154 return 1;
155}
156
Jens Axboe2f9ade32006-10-20 11:25:52 +0200157/*
158 * Setup the buffer area we need for io.
159 */
160int allocate_io_mem(struct thread_data *td)
161{
Jens Axboeb6f96762007-04-17 19:48:46 +0200162 int ret = 0;
163
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200164 if (td->io_ops->flags & FIO_NOIO)
165 return 0;
166
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100167 if (td->o.mem_type == MEM_MALLOC)
Jens Axboeb6f96762007-04-17 19:48:46 +0200168 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;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200176 }
177
Jens Axboe3deb3102007-04-26 15:24:20 +0200178 if (ret)
179 td_verror(td, ENOMEM, "iomem allocation");
180
Jens Axboeb6f96762007-04-17 19:48:46 +0200181 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200182}
183
184void free_io_mem(struct thread_data *td)
185{
Jens Axboeee56ad52008-02-01 10:30:20 +0100186 if (td->o.mem_type == MEM_MALLOC) {
187 dprint(FD_MEM, "free mem %p\n", td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200188 free(td->orig_buffer);
Jens Axboeee56ad52008-02-01 10:30:20 +0100189 } else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200190 struct shmid_ds sbuf;
191
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100192 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id,
193 td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200194 shmdt(td->orig_buffer);
195 shmctl(td->shm_id, IPC_RMID, &sbuf);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100196 } else if (td->o.mem_type == MEM_MMAP ||
197 td->o.mem_type == MEM_MMAPHUGE) {
Jens Axboeee56ad52008-02-01 10:30:20 +0100198 dprint(FD_MEM, "munmap %zu %p\n", td->orig_buffer_size,
199 td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200200 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe313cb202006-12-21 09:50:00 +0100201 if (td->mmapfile) {
202 close(td->mmapfd);
203 unlink(td->mmapfile);
204 free(td->mmapfile);
Jens Axboed0bdaf42006-12-20 14:40:44 +0100205 }
206 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100207 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200208
209 td->orig_buffer = NULL;
210}