forked from DylanZA/netbench
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.h
117 lines (97 loc) · 2.66 KB
/
util.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
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
#include <pthread.h>
#include <string.h>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <boost/program_options.hpp>
float logTime();
void setVerbose();
bool isVerbose();
#define likely(x) __builtin_expect((x),1)
#define unlikely(x) __builtin_expect((x),0)
namespace {
template <typename... T>
inline std::string strcat(const T&... vals) {
std::stringstream ss;
(ss << ... << vals) << "";
return ss.str();
}
template <typename... T>
inline void die(const T&... vals) {
throw std::runtime_error(strcat(vals...));
}
template <typename... T>
inline void log(const T&... vals) {
std::stringstream ss;
ss << "[" << std::fixed << std::setw(9) << std::setprecision(3) << logTime()
<< "] " << strcat(vals...) << "\n";
auto str = ss.str();
fwrite(str.data(), 1, str.size(), stderr);
}
template <typename... T>
inline void _vlog(const T&... vals) {
if (likely(!isVerbose()))
return;
log(vals...);
}
#define vlog(...) \
_vlog(__FILE__, ":", __LINE__, ':', __FUNCTION__, ' ', __VA_ARGS__)
inline std::string leftpad(std::string x, size_t n) {
if (x.size() >= n)
return x;
return std::string(n - x.size(), ' ') + x;
}
struct InterruptedException : std::exception {};
template <typename TResult, typename... T>
inline TResult checkedErrno(TResult res, const T&... vals) {
static_assert(std::is_integral_v<TResult>, "result type should be integral!");
if (res < 0) {
int64_t error = res == -1 ? errno : -res;
if (error == EINTR) {
throw InterruptedException{};
}
if (error == ETIME) {
return -ETIME; // probably fine
}
die(strcat(vals...),
": failed with ",
res,
" err=",
strerror(error),
"(",
error,
")");
}
return res;
}
template <class T>
inline void doSetSockOpt(int fd, int t, int l, T val) {
checkedErrno(setsockopt(fd, t, l, &val, sizeof(val)), "doSetSockOpt");
}
template <class FN>
inline auto wrapThread(std::string name, FN&& fn) {
return [name, f = std::move(fn)]() mutable {
if (name.size()) {
pthread_setname_np(pthread_self(), name.c_str());
}
try {
f();
} catch (std::exception const& e) {
log("uncaught thread exception: ", e.what());
fflush(stderr);
std::terminate();
}
};
}
} // namespace
boost::program_options::variables_map simpleParse(
boost::program_options::options_description desc,
std::vector<std::string> const& splits);
void checkHugePages(int count);
std::string hexdump(void const* p, size_t n);
void runWorkload(unsigned int outer, unsigned int inner);