blob: 899a9e50914dba216075557e59829bafeee46891 [file] [log] [blame]
Jens Axboe2f9ade32006-10-20 11:25:52 +02001/*
2 * Memory helpers
3 */
Jens Axboe5921e802008-05-30 15:02:38 +02004#include <sys/types.h>
5#include <sys/stat.h>
6#include <fcntl.h>
Jens Axboe2f9ade32006-10-20 11:25:52 +02007#include <unistd.h>
8#include <sys/shm.h>
9#include <sys/mman.h>
10
11#include "fio.h"
Jens Axboe2f9ade32006-10-20 11:25:52 +020012
Jens Axboe2f9ade32006-10-20 11:25:52 +020013static void *pinned_mem;
14
15void fio_unpin_memory(void)
16{
17 if (pinned_mem) {
Jens Axboeee56ad52008-02-01 10:30:20 +010018 dprint(FD_MEM, "unpinning %llu bytes\n", mlock_size);
Jens Axboe2f9ade32006-10-20 11:25:52 +020019 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
Jens Axboeee56ad52008-02-01 10:30:20 +010033 dprint(FD_MEM, "pinning %llu bytes\n", mlock_size);
34
Jens Axboe2f9ade32006-10-20 11:25:52 +020035 /*
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;
Jens Axboe5ec10ea2008-03-06 15:42:00 +010042 log_info("fio: limiting mlocked memory to %lluMiB\n",
43 mlock_size >> 20);
Jens Axboe2f9ade32006-10-20 11:25:52 +020044 }
45 }
46
Jens Axboe5ec10ea2008-03-06 15:42:00 +010047 pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE,
Jens Axboea55820d2008-06-04 20:37:18 +020048 MAP_PRIVATE | OS_MAP_ANON, -1, 0);
Jens Axboe2f9ade32006-10-20 11:25:52 +020049 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
Jens Axboeb6f96762007-04-17 19:48:46 +020064static int alloc_mem_shm(struct thread_data *td)
65{
66 int flags = IPC_CREAT | SHM_R | SHM_W;
67
68 if (td->o.mem_type == MEM_SHMHUGE)
69 flags |= SHM_HUGETLB;
70
71 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags);
Jens Axboeee56ad52008-02-01 10:30:20 +010072 dprint(FD_MEM, "shmget %zu, %d\n", td->orig_buffer_size, td->shm_id);
Jens Axboeb6f96762007-04-17 19:48:46 +020073 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");
Jens Axboe886b8782007-07-19 10:01:03 +020077 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe5ec10ea2008-03-06 15:42:00 +010078 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 }
Jens Axboed8602dd2007-07-19 10:00:05 +020089 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +010090
Jens Axboeb6f96762007-04-17 19:48:46 +020091 return 1;
92 }
93
94 td->orig_buffer = shmat(td->shm_id, NULL, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +010095 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +020096 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
105static int alloc_mem_mmap(struct thread_data *td)
106{
107 int flags = MAP_PRIVATE;
108
Jens Axboea55820d2008-06-04 20:37:18 +0200109 td->mmapfd = 1;
Jens Axboeb6f96762007-04-17 19:48:46 +0200110
111 if (td->mmapfile) {
112 td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
113
114 if (td->mmapfd < 0) {
115 td_verror(td, errno, "open mmap file");
116 td->orig_buffer = NULL;
117 return 1;
118 }
119 if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) {
120 td_verror(td, errno, "truncate mmap file");
121 td->orig_buffer = NULL;
122 return 1;
123 }
124 } else
125 flags |= OS_MAP_ANON;
126
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100127 td->orig_buffer = mmap(NULL, td->orig_buffer_size,
128 PROT_READ | PROT_WRITE, flags, td->mmapfd, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +0100129 dprint(FD_MEM, "mmap %zu/%d %p\n", td->orig_buffer_size, td->mmapfd,
130 td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200131 if (td->orig_buffer == MAP_FAILED) {
132 td_verror(td, errno, "mmap");
133 td->orig_buffer = NULL;
134 if (td->mmapfd) {
135 close(td->mmapfd);
136 unlink(td->mmapfile);
137 }
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100138
Jens Axboeb6f96762007-04-17 19:48:46 +0200139 return 1;
140 }
141
142 return 0;
143}
144
145static int alloc_mem_malloc(struct thread_data *td)
146{
Jens Axboed87612a2007-07-19 15:06:57 +0200147 unsigned int bsize = td->orig_buffer_size;
148
149 if (td->o.odirect)
150 bsize += page_mask;
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100151
Jens Axboed87612a2007-07-19 15:06:57 +0200152 td->orig_buffer = malloc(bsize);
Jens Axboeee56ad52008-02-01 10:30:20 +0100153 dprint(FD_MEM, "malloc %u %p\n", bsize, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200154 if (td->orig_buffer)
155 return 0;
156
157 return 1;
158}
159
Jens Axboe2f9ade32006-10-20 11:25:52 +0200160/*
161 * Setup the buffer area we need for io.
162 */
163int allocate_io_mem(struct thread_data *td)
164{
Jens Axboeb6f96762007-04-17 19:48:46 +0200165 int ret = 0;
166
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200167 if (td->io_ops->flags & FIO_NOIO)
168 return 0;
169
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100170 if (td->o.mem_type == MEM_MALLOC)
Jens Axboeb6f96762007-04-17 19:48:46 +0200171 ret = alloc_mem_malloc(td);
172 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE)
173 ret = alloc_mem_shm(td);
174 else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE)
175 ret = alloc_mem_mmap(td);
176 else {
177 log_err("fio: bad mem type: %d\n", td->o.mem_type);
178 ret = 1;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200179 }
180
Jens Axboe3deb3102007-04-26 15:24:20 +0200181 if (ret)
182 td_verror(td, ENOMEM, "iomem allocation");
183
Jens Axboeb6f96762007-04-17 19:48:46 +0200184 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200185}
186
187void free_io_mem(struct thread_data *td)
188{
Jens Axboeee56ad52008-02-01 10:30:20 +0100189 if (td->o.mem_type == MEM_MALLOC) {
190 dprint(FD_MEM, "free mem %p\n", td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200191 free(td->orig_buffer);
Jens Axboeee56ad52008-02-01 10:30:20 +0100192 } else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200193 struct shmid_ds sbuf;
194
Jens Axboe5ec10ea2008-03-06 15:42:00 +0100195 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id,
196 td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200197 shmdt(td->orig_buffer);
198 shmctl(td->shm_id, IPC_RMID, &sbuf);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100199 } else if (td->o.mem_type == MEM_MMAP ||
200 td->o.mem_type == MEM_MMAPHUGE) {
Jens Axboeee56ad52008-02-01 10:30:20 +0100201 dprint(FD_MEM, "munmap %zu %p\n", td->orig_buffer_size,
202 td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200203 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe313cb202006-12-21 09:50:00 +0100204 if (td->mmapfile) {
205 close(td->mmapfd);
206 unlink(td->mmapfile);
207 free(td->mmapfile);
Jens Axboed0bdaf42006-12-20 14:40:44 +0100208 }
209 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100210 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200211
212 td->orig_buffer = NULL;
213}