-
Notifications
You must be signed in to change notification settings - Fork 164
/
helper.h
104 lines (86 loc) · 2.79 KB
/
helper.h
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* Copyright 2016, Victor van der Veen
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef __HELPER_H__
#define __HELPER_H__
#include <assert.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <algorithm>
#include <cmath>
#include <numeric>
#define G(x) (x << 30)
#define M(x) (x << 20)
#define K(x) (x << 10)
#define B_TO_ORDER(x) (ffs(x / 4096)-1)
#define KB_TO_ORDER(x) (ffs(x / 4)-1)
#define MB_TO_ORDER(x) (ffs(x * 256)-1)
#define ORDER_TO_B(x) ((1 << x) * 4096)
#define ORDER_TO_KB(x) ((1 << x) * 4)
#define ORDER_TO_MB(x) ((1 << x) / 256)
#define MAX_ORDER 10
#define BILLION 1000000000L
#define MILLION 1000000L
extern FILE *global_of;
static inline uint64_t get_ns(void) {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return BILLION * (uint64_t) t.tv_sec + (uint64_t) t.tv_nsec;
}
static inline uint64_t get_ms(void) {
struct timeval tv;
gettimeofday(&tv, NULL);
return MILLION * (uint64_t) tv.tv_sec + tv.tv_usec;
}
static int pagemap_fd = 0;
static bool got_pagemap = true;
static inline uintptr_t get_phys_addr(uintptr_t virtual_addr) {
if (!got_pagemap) return 0;
if (pagemap_fd == 0) {
pagemap_fd = open("/proc/self/pagemap", O_RDONLY);
if (pagemap_fd < 0) {
got_pagemap = false;
return 0;
}
}
uint64_t value;
off_t offset = (virtual_addr / PAGESIZE) * sizeof(value);
int got = pread(pagemap_fd, &value, sizeof(value), offset);
assert(got == 8);
// Check the "page present" flag.
if ((value & (1ULL << 63)) == 0) {
printf("page not present? virtual address: %p | value: %p\n", virtual_addr, value);
return 0;
}
uint64_t frame_num = (value & ((1ULL << 54) - 1));
return (frame_num * PAGESIZE) | (virtual_addr & (PAGESIZE-1));
}
static inline uint64_t compute_median(std::vector<uint64_t> &v) {
if (v.size() == 0) return 0;
std::vector<uint64_t> tmp = v;
size_t n = tmp.size() / 2;
std::nth_element(tmp.begin(), tmp.begin()+n, tmp.end());
return tmp[n];
}
static inline void print(const char *format, ...) {
va_list args;
va_start(args, format);
vfprintf(stdout, format, args);
if (global_of != NULL) vfprintf(global_of, format, args);
va_end(args);
}
#endif // __HELPER_H__