Add some more options for commands
[fio.git] / memory.c
... / ...
CommitLineData
1/*
2 * Memory helpers
3 */
4#include <unistd.h>
5#include <sys/shm.h>
6#include <sys/mman.h>
7
8#include "fio.h"
9#include "os.h"
10
11static void *pinned_mem;
12
13void fio_unpin_memory(void)
14{
15 if (pinned_mem) {
16 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
30 /*
31 * Don't allow mlock of more than real_mem-128MB
32 */
33 phys_mem = os_phys_mem();
34 if (phys_mem) {
35 if ((mlock_size + 128 * 1024 * 1024) > phys_mem) {
36 mlock_size = phys_mem - 128 * 1024 * 1024;
37 log_info("fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20);
38 }
39 }
40
41 pinned_mem = mmap(NULL, mlock_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | OS_MAP_ANON, 0, 0);
42 if (pinned_mem == MAP_FAILED) {
43 perror("malloc locked mem");
44 pinned_mem = NULL;
45 return 1;
46 }
47 if (mlock(pinned_mem, mlock_size) < 0) {
48 perror("mlock");
49 munmap(pinned_mem, mlock_size);
50 pinned_mem = NULL;
51 return 1;
52 }
53
54 return 0;
55}
56
57/*
58 * Setup the buffer area we need for io.
59 */
60int allocate_io_mem(struct thread_data *td)
61{
62 if (td->mem_type == MEM_MALLOC)
63 td->orig_buffer = malloc(td->orig_buffer_size);
64 else if (td->mem_type == MEM_SHM || td->mem_type == MEM_SHMHUGE) {
65 int flags = IPC_CREAT | SHM_R | SHM_W;
66
67 if (td->mem_type == MEM_SHMHUGE)
68 flags |= SHM_HUGETLB;
69
70 td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags);
71 if (td->shm_id < 0) {
72 td_verror(td, errno, "shmget");
73 perror("shmget");
74 return 1;
75 }
76
77 td->orig_buffer = shmat(td->shm_id, NULL, 0);
78 if (td->orig_buffer == (void *) -1) {
79 td_verror(td, errno, "shmat");
80 perror("shmat");
81 td->orig_buffer = NULL;
82 return 1;
83 }
84 } else if (td->mem_type == MEM_MMAP || td->mem_type == MEM_MMAPHUGE) {
85 int flags = MAP_PRIVATE;
86
87 td->mmapfd = 0;
88
89 if (td->mmapfile) {
90 td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644);
91
92 if (td->mmapfd < 0) {
93 td_verror(td, errno, "open");
94 perror("open mmap file");
95 td->orig_buffer = NULL;
96 return 1;
97 }
98 } else
99 flags |= OS_MAP_ANON;
100
101 td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, flags, td->mmapfd, 0);
102 if (td->orig_buffer == MAP_FAILED) {
103 td_verror(td, errno, "mmap");
104 perror("mmap");
105 td->orig_buffer = NULL;
106 if (td->mmapfd) {
107 close(td->mmapfd);
108 unlink(td->mmapfile);
109 }
110
111 return 1;
112 }
113 }
114
115 return 0;
116}
117
118void free_io_mem(struct thread_data *td)
119{
120 if (td->mem_type == MEM_MALLOC)
121 free(td->orig_buffer);
122 else if (td->mem_type == MEM_SHM || td->mem_type == MEM_SHMHUGE) {
123 struct shmid_ds sbuf;
124
125 shmdt(td->orig_buffer);
126 shmctl(td->shm_id, IPC_RMID, &sbuf);
127 } else if (td->mem_type == MEM_MMAP || td->mem_type == MEM_MMAPHUGE) {
128 munmap(td->orig_buffer, td->orig_buffer_size);
129 if (td->mmapfile) {
130 close(td->mmapfd);
131 unlink(td->mmapfile);
132 free(td->mmapfile);
133 }
134 } else
135 log_err("Bad memory type %u\n", td->mem_type);
136
137 td->orig_buffer = NULL;
138}