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"); | |
68 | if (errno == EINVAL && td->o.mem_type == MEM_SHMHUGE) | |
69 | log_err("fio: check that you have free huge pages and that hugepage-size is correct.\n"); | |
70 | ||
71 | return 1; | |
72 | } | |
2f9ade3c | 73 | |
b6f9676e JA |
74 | td->orig_buffer = shmat(td->shm_id, NULL, 0); |
75 | if (td->orig_buffer == (void *) -1) { | |
76 | td_verror(td, errno, "shmat"); | |
77 | td->orig_buffer = NULL; | |
78 | return 1; | |
79 | } | |
80 | ||
81 | return 0; | |
82 | } | |
83 | ||
84 | static int alloc_mem_mmap(struct thread_data *td) | |
85 | { | |
86 | int flags = MAP_PRIVATE; | |
87 | ||
88 | td->mmapfd = 0; | |
89 | ||
90 | if (td->mmapfile) { | |
91 | td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644); | |
92 | ||
93 | if (td->mmapfd < 0) { | |
94 | td_verror(td, errno, "open mmap file"); | |
2f9ade3c JA |
95 | td->orig_buffer = NULL; |
96 | return 1; | |
97 | } | |
b6f9676e JA |
98 | if (ftruncate(td->mmapfd, td->orig_buffer_size) < 0) { |
99 | td_verror(td, errno, "truncate mmap file"); | |
2f9ade3c JA |
100 | td->orig_buffer = NULL; |
101 | return 1; | |
102 | } | |
b6f9676e JA |
103 | } else |
104 | flags |= OS_MAP_ANON; | |
105 | ||
106 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, flags, td->mmapfd, 0); | |
107 | if (td->orig_buffer == MAP_FAILED) { | |
108 | td_verror(td, errno, "mmap"); | |
109 | td->orig_buffer = NULL; | |
110 | if (td->mmapfd) { | |
111 | close(td->mmapfd); | |
112 | unlink(td->mmapfile); | |
113 | } | |
114 | ||
115 | return 1; | |
2f9ade3c JA |
116 | } |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
b6f9676e JA |
121 | static int alloc_mem_malloc(struct thread_data *td) |
122 | { | |
123 | td->orig_buffer = malloc(td->orig_buffer_size); | |
124 | if (td->orig_buffer) | |
125 | return 0; | |
126 | ||
127 | return 1; | |
128 | } | |
129 | ||
130 | /* | |
131 | * Setup the buffer area we need for io. | |
132 | */ | |
133 | int allocate_io_mem(struct thread_data *td) | |
134 | { | |
135 | int ret = 0; | |
136 | ||
137 | if (td->o.mem_type == MEM_MALLOC) | |
138 | ret = alloc_mem_malloc(td); | |
139 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) | |
140 | ret = alloc_mem_shm(td); | |
141 | else if (td->o.mem_type == MEM_MMAP || td->o.mem_type == MEM_MMAPHUGE) | |
142 | ret = alloc_mem_mmap(td); | |
143 | else { | |
144 | log_err("fio: bad mem type: %d\n", td->o.mem_type); | |
145 | ret = 1; | |
146 | } | |
147 | ||
3deb3101 JA |
148 | if (ret) |
149 | td_verror(td, ENOMEM, "iomem allocation"); | |
150 | ||
b6f9676e JA |
151 | return ret; |
152 | } | |
153 | ||
2f9ade3c JA |
154 | void free_io_mem(struct thread_data *td) |
155 | { | |
2dc1bbeb | 156 | if (td->o.mem_type == MEM_MALLOC) |
2f9ade3c | 157 | free(td->orig_buffer); |
2dc1bbeb | 158 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) { |
2f9ade3c JA |
159 | struct shmid_ds sbuf; |
160 | ||
161 | shmdt(td->orig_buffer); | |
162 | shmctl(td->shm_id, IPC_RMID, &sbuf); | |
2dc1bbeb JA |
163 | } else if (td->o.mem_type == MEM_MMAP || |
164 | td->o.mem_type == MEM_MMAPHUGE) { | |
2f9ade3c | 165 | munmap(td->orig_buffer, td->orig_buffer_size); |
313cb206 JA |
166 | if (td->mmapfile) { |
167 | close(td->mmapfd); | |
168 | unlink(td->mmapfile); | |
169 | free(td->mmapfile); | |
d0bdaf49 JA |
170 | } |
171 | } else | |
2dc1bbeb | 172 | log_err("Bad memory type %u\n", td->o.mem_type); |
2f9ade3c JA |
173 | |
174 | td->orig_buffer = NULL; | |
175 | } |