blob: be1fd24c8037623864333680c268770785f0e10b [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 Axboe6d861442007-03-15 09:22:23 +010039 log_info("fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
Jens Axboe2f9ade32006-10-20 11:25:52 +020040 }
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
Jens Axboeb6f96762007-04-17 19:48:46 +020059static int alloc_mem_shm(struct thread_data *td)
60{
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);
Jens Axboeee56ad52008-02-01 10:30:20 +010067 dprint(FD_MEM, "shmget %zu, %d\n", td->orig_buffer_size, td->shm_id);
Jens Axboeb6f96762007-04-17 19:48:46 +020068 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");
Jens Axboe886b8782007-07-19 10:01:03 +020072 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboed8602dd2007-07-19 10:00:05 +020073 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");
Jens Axboec7173db2007-07-19 10:09:45 +020077 else if (errno == ENOMEM)
78 log_err("fio: no huge pages available, do you need to alocate some? See HOWTO.\n");
Jens Axboed8602dd2007-07-19 10:00:05 +020079 }
Jens Axboeb6f96762007-04-17 19:48:46 +020080
81 return 1;
82 }
83
84 td->orig_buffer = shmat(td->shm_id, NULL, 0);
Jens Axboeee56ad52008-02-01 10:30:20 +010085 dprint(FD_MEM, "shmat %d, %p\n", td->shm_id, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +020086 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");
106 td->orig_buffer = NULL;
107 return 1;
108 }
109 if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) {
110 td_verror(td, errno, "truncate mmap file");
111 td->orig_buffer = NULL;
112 return 1;
113 }
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);
Jens Axboeee56ad52008-02-01 10:30:20 +0100118 dprint(FD_MEM, "mmap %zu/%d %p\n", td->orig_buffer_size, td->mmapfd,
119 td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200120 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;
129 }
130
131 return 0;
132}
133
134static int alloc_mem_malloc(struct thread_data *td)
135{
Jens Axboed87612a2007-07-19 15:06:57 +0200136 unsigned int bsize = td->orig_buffer_size;
137
138 if (td->o.odirect)
139 bsize += page_mask;
140
141 td->orig_buffer = malloc(bsize);
Jens Axboeee56ad52008-02-01 10:30:20 +0100142 dprint(FD_MEM, "malloc %u %p\n", bsize, td->orig_buffer);
Jens Axboeb6f96762007-04-17 19:48:46 +0200143 if (td->orig_buffer)
144 return 0;
145
146 return 1;
147}
148
Jens Axboe2f9ade32006-10-20 11:25:52 +0200149/*
150 * Setup the buffer area we need for io.
151 */
152int allocate_io_mem(struct thread_data *td)
153{
Jens Axboeb6f96762007-04-17 19:48:46 +0200154 int ret = 0;
155
Jens Axboeb4c5e1a2007-10-25 18:44:45 +0200156 if (td->io_ops->flags & FIO_NOIO)
157 return 0;
158
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100159 if (td->o.mem_type == MEM_MALLOC)
Jens Axboeb6f96762007-04-17 19:48:46 +0200160 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;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200168 }
169
Jens Axboe3deb3102007-04-26 15:24:20 +0200170 if (ret)
171 td_verror(td, ENOMEM, "iomem allocation");
172
Jens Axboeb6f96762007-04-17 19:48:46 +0200173 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200174}
175
176void free_io_mem(struct thread_data *td)
177{
Jens Axboeee56ad52008-02-01 10:30:20 +0100178 if (td->o.mem_type == MEM_MALLOC) {
179 dprint(FD_MEM, "free mem %p\n", td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200180 free(td->orig_buffer);
Jens Axboeee56ad52008-02-01 10:30:20 +0100181 } else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200182 struct shmid_ds sbuf;
183
Jens Axboeee56ad52008-02-01 10:30:20 +0100184 dprint(FD_MEM, "shmdt/ctl %d %p\n", td->shm_id,td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200185 shmdt(td->orig_buffer);
186 shmctl(td->shm_id, IPC_RMID, &sbuf);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100187 } else if (td->o.mem_type == MEM_MMAP ||
188 td->o.mem_type == MEM_MMAPHUGE) {
Jens Axboeee56ad52008-02-01 10:30:20 +0100189 dprint(FD_MEM, "munmap %zu %p\n", td->orig_buffer_size,
190 td->orig_buffer);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200191 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe313cb202006-12-21 09:50:00 +0100192 if (td->mmapfile) {
193 close(td->mmapfd);
194 unlink(td->mmapfile);
195 free(td->mmapfile);
Jens Axboed0bdaf42006-12-20 14:40:44 +0100196 }
197 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100198 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200199
200 td->orig_buffer = NULL;
201}