Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 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 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 11 | static void *pinned_mem; |
| 12 | |
| 13 | void 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 | |
| 23 | int 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; |
Jens Axboe | 6d86144 | 2007-03-15 09:22:23 +0100 | [diff] [blame] | 37 | log_info("fio: limiting mlocked memory to %lluMiB\n", mlock_size >> 20); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 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 | */ |
| 60 | int allocate_io_mem(struct thread_data *td) |
| 61 | { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 62 | if (td->o.mem_type == MEM_MALLOC) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 63 | td->orig_buffer = malloc(td->orig_buffer_size); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 64 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) { |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 65 | int flags = IPC_CREAT | SHM_R | SHM_W; |
| 66 | |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 67 | if (td->o.mem_type == MEM_SHMHUGE) |
Jens Axboe | 74b025b | 2006-12-19 15:18:14 +0100 | [diff] [blame] | 68 | flags |= SHM_HUGETLB; |
| 69 | |
| 70 | td->shm_id = shmget(IPC_PRIVATE, td->orig_buffer_size, flags); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 71 | if (td->shm_id < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 72 | td_verror(td, errno, "shmget"); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 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) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 79 | td_verror(td, errno, "shmat"); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 80 | perror("shmat"); |
| 81 | td->orig_buffer = NULL; |
| 82 | return 1; |
| 83 | } |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 84 | } else if (td->o.mem_type == MEM_MMAP || |
| 85 | td->o.mem_type == MEM_MMAPHUGE) { |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 86 | int flags = MAP_PRIVATE; |
| 87 | |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 88 | td->mmapfd = 0; |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 89 | |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 90 | if (td->mmapfile) { |
| 91 | td->mmapfd = open(td->mmapfile, O_RDWR|O_CREAT, 0644); |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 92 | |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 93 | if (td->mmapfd < 0) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 94 | td_verror(td, errno, "open"); |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 95 | perror("open mmap file"); |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 96 | td->orig_buffer = NULL; |
| 97 | return 1; |
| 98 | } |
| 99 | } else |
| 100 | flags |= OS_MAP_ANON; |
| 101 | |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 102 | td->orig_buffer = mmap(NULL, td->orig_buffer_size, PROT_READ | PROT_WRITE, flags, td->mmapfd, 0); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 103 | if (td->orig_buffer == MAP_FAILED) { |
Jens Axboe | e1161c3 | 2007-02-22 19:36:48 +0100 | [diff] [blame] | 104 | td_verror(td, errno, "mmap"); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 105 | perror("mmap"); |
| 106 | td->orig_buffer = NULL; |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 107 | if (td->mmapfd) { |
| 108 | close(td->mmapfd); |
| 109 | unlink(td->mmapfile); |
| 110 | } |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 111 | |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | void free_io_mem(struct thread_data *td) |
| 120 | { |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 121 | if (td->o.mem_type == MEM_MALLOC) |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 122 | free(td->orig_buffer); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 123 | else if (td->o.mem_type == MEM_SHM || td->o.mem_type == MEM_SHMHUGE) { |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 124 | struct shmid_ds sbuf; |
| 125 | |
| 126 | shmdt(td->orig_buffer); |
| 127 | shmctl(td->shm_id, IPC_RMID, &sbuf); |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 128 | } else if (td->o.mem_type == MEM_MMAP || |
| 129 | td->o.mem_type == MEM_MMAPHUGE) { |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 130 | munmap(td->orig_buffer, td->orig_buffer_size); |
Jens Axboe | 313cb20 | 2006-12-21 09:50:00 +0100 | [diff] [blame] | 131 | if (td->mmapfile) { |
| 132 | close(td->mmapfd); |
| 133 | unlink(td->mmapfile); |
| 134 | free(td->mmapfile); |
Jens Axboe | d0bdaf4 | 2006-12-20 14:40:44 +0100 | [diff] [blame] | 135 | } |
| 136 | } else |
Jens Axboe | 2dc1bbe | 2007-03-15 15:01:33 +0100 | [diff] [blame^] | 137 | log_err("Bad memory type %u\n", td->o.mem_type); |
Jens Axboe | 2f9ade3 | 2006-10-20 11:25:52 +0200 | [diff] [blame] | 138 | |
| 139 | td->orig_buffer = NULL; |
| 140 | } |