Commit | Line | Data |
---|---|---|
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 |
10 | static void *pinned_mem; |
11 | ||
12 | void 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 | ||
22 | int 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 | 56 | static 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 | ||
90 | static 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 |
127 | static 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 | */ | |
144 | int allocate_io_mem(struct thread_data *td) | |
145 | { | |
146 | int ret = 0; | |
147 | ||
b4c5e1ac JA |
148 | if (td->io_ops->flags & FIO_NOIO) |
149 | return 0; | |
150 | ||
b6f9676e JA |
151 | if (td->o.mem_type == MEM_MALLOC) |
152 | ret = alloc_mem_malloc(td); | |
153 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) | |
154 | ret = alloc_mem_shm(td); | |
155 | else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE) | |
156 | ret = alloc_mem_mmap(td); | |
157 | else { | |
158 | log_err("fio: bad mem type: %d\n", td->o.mem_type); | |
159 | ret = 1; | |
160 | } | |
161 | ||
3deb3101 JA |
162 | if (ret) |
163 | td_verror(td, ENOMEM, "iomem allocation"); | |
164 | ||
b6f9676e JA |
165 | return ret; |
166 | } | |
167 | ||
2f9ade3c JA |
168 | void free_io_mem(struct thread_data *td) |
169 | { | |
2dc1bbeb | 170 | if (td->o.mem_type == MEM_MALLOC) |
2f9ade3c | 171 | free(td->orig_buffer); |
2dc1bbeb | 172 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) { |
2f9ade3c JA |
173 | struct shmid_ds sbuf; |
174 | ||
175 | shmdt(td->orig_buffer); | |
176 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
2dc1bbeb JA |
177 | } else if (td->o.mem_type == MEM_MMAP || |
178 | td->o.mem_type == MEM_MMAPHUGE) { | |
2f9ade3c | 179 | munmap(td->orig_buffer, td->orig_buffer_size); |
313cb206 JA |
180 | if (td->mmapfile) { |
181 | close(td->mmapfd); | |
182 | unlink(td->mmapfile); | |
183 | free(td->mmapfile); | |
d0bdaf49 JA |
184 | } |
185 | } else | |
2dc1bbeb | 186 | log_err("Bad memory type %u\n", td->o.mem_type); |
2f9ade3c JA |
187 | |
188 | td->orig_buffer = NULL; | |
189 | } |