forked from DylanZA/netbench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.cpp
127 lines (115 loc) · 2.99 KB
/
util.cpp
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "util.h"
#include <atomic>
#include <fcntl.h>
#include <stdio.h>
#include <sys/syscall.h>
#include <sys/types.h>
namespace po = boost::program_options;
float logTime() {
using namespace std::chrono;
static auto base = steady_clock::now();
return float(duration_cast<milliseconds>((steady_clock::now() - base))
.count()) /
1000.0;
}
bool verb = false;
void setVerbose() {
verb = true;
}
bool isVerbose() {
return verb;
}
boost::program_options::variables_map simpleParse(
boost::program_options::options_description desc,
std::vector<std::string> const& splits) {
desc.add_options()("help", "produce help message");
boost::program_options::variables_map vm;
{
std::vector<char const*> split_chars;
for (auto const& s : splits) {
split_chars.push_back(s.c_str());
}
po::store(
po::parse_command_line(split_chars.size(), split_chars.data(), desc),
vm);
}
po::notify(vm);
if (vm.count("help")) {
std::cerr << "parsing: ";
for (auto const& s : splits) {
std::cerr << s << " ";
}
std::cerr << "\n";
std::cerr << desc << "\n";
exit(1);
}
return vm;
}
void checkHugePages(int count) {
static int already_checked = 0;
count = already_checked = (already_checked + count);
int file = open("/proc/sys/vm/nr_hugepages", O_RDWR);
if (file < 0) {
die("unable to open /proc/sys/vm/nr_hugepages");
}
char buff[128];
buff[sizeof(buff) - 1] = '\0';
int res = read(file, &buff, sizeof(buff) - 1);
if (res <= 0) {
die("unable to read number of pages");
}
int val;
if (sscanf(buff, "%d", &val) != 1) {
die("unable to parse:", buff);
}
vlog("have ", val, " huge pages available");
if (val < count) {
int n = snprintf(buff, sizeof(buff), "%d\n", count);
res = pwrite(file, buff, n, 0);
if (res != n) {
log("unable to write ",
buff,
" len=",
n,
" to /proc/sys/vm/nr_hugepages, "
"this might not work res=",
res);
}
}
close(file);
}
std::string hexdump(void const* p, size_t n) {
std::stringstream ss;
ss << "[size=" << n << "] ";
ss << std::setfill('0') << std::setw(2) << std::hex << std::uppercase;
char const* p2 = (char const*)p;
for (size_t i = 0; i < n; i++) {
ss << (int)p2[i];
}
return ss.str();
}
std::atomic<int> gInt{0};
void runWorkload(unsigned int outer, unsigned int inner) {
for (uint32_t i = 0; i < outer; i++) {
size_t loops = 0;
auto start = std::chrono::steady_clock::now();
auto end = start + std::chrono::microseconds(inner);
syscall(99999999);
do {
loops++;
#ifdef __i386__
__builtin_ia32_pause();
#elif __arm__
asm volatile("yield");.
#endif
} while (std::chrono::steady_clock::now() < end);
syscall(99999999);
vlog(
"took ",
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::steady_clock::now() - start)
.count(),
"us loops=",
loops);
}
}