blob: c147229e27c67d4135bfe9c64be82f8dbe8e2c46 [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) {
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;
Jens Axboe6d861442007-03-15 09:22:23 +010036 log_info("fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
Jens Axboe2f9ade32006-10-20 11:25:52 +020037 }
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
Jens Axboeb6f96762007-04-17 19:48:46 +020056static int alloc_mem_shm(struct thread_data *td)
57{
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");
Jens Axboe886b8782007-07-19 10:01:03 +020068 if (td->o.mem_type == MEM_SHMHUGE) {
Jens Axboed8602dd2007-07-19 10:00:05 +020069 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");
Jens Axboec7173db2007-07-19 10:09:45 +020073 else if (errno == ENOMEM)
74 log_err("fio: no huge pages available, do you need to alocate some? See HOWTO.\n");
Jens Axboed8602dd2007-07-19 10:00:05 +020075 }
Jens Axboeb6f96762007-04-17 19:48:46 +020076
77 return 1;
78 }
79
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");
101 td->orig_buffer = NULL;
102 return 1;
103 }
104 if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) {
105 td_verror(td, errno, "truncate mmap file");
106 td->orig_buffer = NULL;
107 return 1;
108 }
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;
122 }
123
124 return 0;
125}
126
127static int alloc_mem_malloc(struct thread_data *td)
128{
Jens Axboed87612a2007-07-19 15:06:57 +0200129 unsigned int bsize = td->orig_buffer_size;
130
131 if (td->o.odirect)
132 bsize += page_mask;
133
134 td->orig_buffer = malloc(bsize);
Jens Axboeb6f96762007-04-17 19:48:46 +0200135 if (td->orig_buffer)
136 return 0;
137
138 return 1;
139}
140
Jens Axboe2f9ade32006-10-20 11:25:52 +0200141/*
142 * Setup the buffer area we need for io.
143 */
144int allocate_io_mem(struct thread_data *td)
145{
Jens Axboeb6f96762007-04-17 19:48:46 +0200146 int ret = 0;
147
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100148 if (td->o.mem_type == MEM_MALLOC)
Jens Axboeb6f96762007-04-17 19:48:46 +0200149 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;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200157 }
158
Jens Axboe3deb3102007-04-26 15:24:20 +0200159 if (ret)
160 td_verror(td, ENOMEM, "iomem allocation");
161
Jens Axboeb6f96762007-04-17 19:48:46 +0200162 return ret;
Jens Axboe2f9ade32006-10-20 11:25:52 +0200163}
164
165void free_io_mem(struct thread_data *td)
166{
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100167 if (td->o.mem_type == MEM_MALLOC)
Jens Axboe2f9ade32006-10-20 11:25:52 +0200168 free(td->orig_buffer);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100169 else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200170 struct shmid_ds sbuf;
171
172 shmdt(td->orig_buffer);
173 shmctl(td->shm_id, IPC_RMID, &sbuf);
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100174 } else if (td->o.mem_type == MEM_MMAP ||
175 td->o.mem_type == MEM_MMAPHUGE) {
Jens Axboe2f9ade32006-10-20 11:25:52 +0200176 munmap(td->orig_buffer, td->orig_buffer_size);
Jens Axboe313cb202006-12-21 09:50:00 +0100177 if (td->mmapfile) {
178 close(td->mmapfd);
179 unlink(td->mmapfile);
180 free(td->mmapfile);
Jens Axboed0bdaf42006-12-20 14:40:44 +0100181 }
182 } else
Jens Axboe2dc1bbe2007-03-15 15:01:33 +0100183 log_err("Bad memory type %u\n", td->o.mem_type);
Jens Axboe2f9ade32006-10-20 11:25:52 +0200184
185 td->orig_buffer = NULL;
186}