OLD | NEW |
(Empty) | |
| 1 // Copyright 2015, Google, Inc. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 #include <assert.h> |
| 16 #include <fcntl.h> |
| 17 #include <inttypes.h> |
| 18 #include <stdint.h> |
| 19 #include <stdio.h> |
| 20 #include <stdlib.h> |
| 21 #include <string.h> |
| 22 #include <sys/mman.h> |
| 23 #include <unistd.h> |
| 24 |
| 25 const size_t page_size = 1 << 12; |
| 26 |
| 27 uint64_t get_physical_frame_num(uintptr_t virtual_addr) { |
| 28 int fd = open("/proc/self/pagemap", O_RDONLY); |
| 29 assert(fd >= 0); |
| 30 |
| 31 off_t pos = lseek(fd, (virtual_addr / page_size) * 8, SEEK_SET); |
| 32 assert(pos >= 0); |
| 33 uint64_t value; |
| 34 int got = read(fd, &value, 8); |
| 35 assert(got == 8); |
| 36 int rc = close(fd); |
| 37 assert(rc == 0); |
| 38 |
| 39 uint64_t frame_num = value & ((1ULL << 54) - 1); |
| 40 return frame_num; |
| 41 } |
| 42 |
| 43 int main(int argc, char **argv) { |
| 44 size_t page_num = 256; |
| 45 size_t sleep_sec = 30; |
| 46 |
| 47 int c; |
| 48 while ((c = getopt(argc, argv, "a:s:")) != -1) { |
| 49 switch (c) { |
| 50 case 'a': |
| 51 page_num = atoi(optarg); |
| 52 break; |
| 53 case 's': |
| 54 sleep_sec = atoi(optarg); |
| 55 break; |
| 56 default: |
| 57 fprintf(stderr, "Usage: %s [-a alloc_size] [-s sleep_sec]\n", |
| 58 argv[0]); |
| 59 exit(EXIT_FAILURE); |
| 60 } |
| 61 } |
| 62 |
| 63 fprintf(stderr, "using allocation size: %lu pages\n", page_num); |
| 64 fprintf(stderr, "using sleep interval: %lu seconds\n", sleep_sec); |
| 65 |
| 66 FILE *out = fopen("physmem_alloc_results", "w+"); |
| 67 if (out == NULL) { |
| 68 perror("open result file: "); |
| 69 exit(EXIT_FAILURE); |
| 70 } |
| 71 |
| 72 size_t chunk_size = page_num * page_size; |
| 73 size_t iter = 0; |
| 74 |
| 75 while (1) { |
| 76 fprintf(stderr, "iteration %lu ... \n", iter); |
| 77 iter++; |
| 78 |
| 79 char *chunk = (char *) mmap( |
| 80 NULL, chunk_size, PROT_READ | PROT_WRITE, |
| 81 MAP_POPULATE | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); |
| 82 assert(chunk != MAP_FAILED); |
| 83 |
| 84 for (size_t i = 0; i < page_num; i++) { |
| 85 char *va = chunk + page_size * i; |
| 86 uint64_t pfn = get_physical_frame_num((uintptr_t)va); |
| 87 fprintf(out, "0x%" PRIx64 " ", pfn); |
| 88 } |
| 89 |
| 90 fprintf(out, "\n"); |
| 91 fflush(out); |
| 92 int rc = munmap(chunk, chunk_size); |
| 93 assert(rc == 0); |
| 94 sleep(sleep_sec); |
| 95 } |
| 96 return 0; |
| 97 } |
OLD | NEW |