This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bench_cpu_kswapd_anon_rand.cc
80 lines (65 loc) · 2.47 KB
/
bench_cpu_kswapd_anon_rand.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <stdlib.h>
#include <sys/mman.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <cstdlib>
#include <ctime>
#include "rdmemusg.h"
#include "pattern.h"
#include "pinning.h"
// returns a random unsigned long with upto 45-bits of pseudo-randomness
unsigned long random_number(unsigned long min, unsigned long max) {
int rand_hi = rand();
int rand_mi = rand();
int rand_lo = rand();
unsigned long rand_45 = (((unsigned long)rand_hi) << 30) ^ (((unsigned long)rand_mi) << 15) ^ rand_lo;
return rand_45 % max;
}
int main(int argc, char **argv) {
set_cpu(0);
if (argc < 3) {
std::cout << "Usage: ./prog num_gb kswapd_pid" << std::endl;
return -1;
}
size_t gbs = atoi(argv[1]);
size_t kswapd = atoi(argv[2]);
uint64_t vma_size = (uint64_t)gbs << 30;
char* start_addr = (char*)mmap(NULL, vma_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
if (start_addr == MAP_FAILED) {
std::cerr << "mmap failed: " << strerror(errno) << std::endl;
exit(1);
}
unsigned long long num_pages_mapped = vma_size / PAGE_SIZE;
unsigned long* total_jiffies = (unsigned long*)malloc(num_pages_mapped * sizeof(unsigned long));
unsigned long* pid_jiffies = (unsigned long*)malloc(num_pages_mapped * sizeof(unsigned long));
// touch all pages
for(unsigned long i = 0; i < num_pages_mapped; i++) {
total_jiffies[i] = 0;
pid_jiffies[i] = 0;
}
// lock this results array itself so itself is not swapped out
if(mlock((const void*) total_jiffies, num_pages_mapped * sizeof(double))) {
std::cerr << "mlock failed: " << strerror(errno) << std::endl;
exit(1);
}
if(mlock((const void*) pid_jiffies, num_pages_mapped * sizeof(double))) {
std::cerr << "mlock failed: " << strerror(errno) << std::endl;
exit(1);
}
// seed the rng
srand((int)time(0));
// touch all the pages now
char* addr = start_addr;
for (unsigned long num_touched = 0; num_touched < num_pages_mapped; num_touched++) {
*addr = 'X';
total_jiffies[num_touched] = get_uptime_jiffies();
pid_jiffies[num_touched] = get_pid_jiffies(kswapd);
addr = ((char*)start_addr) + random_number(0, vma_size);
//printf("%lx\n", (unsigned long)addr);
}
for (unsigned int i = 0; i < num_pages_mapped; i++) {
std::cout << total_jiffies[i] << " " << pid_jiffies[i] << std::endl;
}
}