-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.cpp
133 lines (110 loc) · 3.09 KB
/
common.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
128
129
130
131
132
133
/**
* Generate IPV4 and IPV6 5-tuples to standard output
* gen at the best effort.
* Need the VIP:port list
*/
#include "common.h"
#include <csignal>
#include <cstdarg>
#include <gperftools/profiler.h>
#include "input/input_types.h"
int Clocker::currentLevel = 0;
list<Counter> Counter::counters;
TeeOstream tos;
Clocker global("root", &tos);
//int stick_this_thread_to_core(int core_id) {
// long num_cores = sysconf(_SC_NPROCESSORS_ONLN);
// if (core_id < 0 || core_id >= num_cores) return EINVAL;
//
// cpu_set_t cpuset;
// CPU_ZERO(&cpuset);
// CPU_SET(core_id, &cpuset);
//
// pthread_t current_thread = pthread_self();
// return pthread_setaffinity_np(current_thread, sizeof(cpu_set_t), &cpuset);
//}
static pthread_mutex_t printf_mutex;
void sync_printf(const char *format, ...) {
va_list args;
va_start(args, format);
pthread_mutex_lock(&printf_mutex);
vprintf(format, args);
pthread_mutex_unlock(&printf_mutex);
va_end(args);
}
void printStackTrace(int sig) {
void *array[10];
size_t size;
// get void*'s for all entries on the stack
size = backtrace(array, 10);
// print out all the frames to stderr
fprintf(stderr, "Error: signal %d:\n", sig);
backtrace_symbols_fd(array, size, STDERR_FILENO);
exit(1);
}
void sigHandler(int sig) {
ProfilerStop();
for (auto &c:Counter::counters) {
c.lap();
}
printStackTrace(sig);
exit(0);
}
void registerSigHandler() {
signal(SIGINT, sigHandler);
signal(SIGSEGV, sigHandler);
signal(SIGABRT, sigHandler);
signal(SIGFPE, sigHandler);
signal(SIGILL, sigHandler);
signal(SIGTERM, sigHandler);
}
void commonInit() {
system("mkdir -p ../dist/logs/");
srand(1);
InputBase::setSeed(1);
registerSigHandler();
// ProfilerStart("./validity.pprof");
pthread_mutex_init(&printf_mutex, NULL);
}
//! convert a 64-bit Integer to human-readable format in K/M/G. e.g, 102400 is converted to "100K".
std::string human(uint64_t word) {
std::stringstream ss;
if (word <= 1024) { ss << word; }
else if (word <= 10240) { ss << std::setprecision(2) << word * 1.0 / 1024 << "K"; }
else if (word <= 1048576) { ss << word / 1024 << "K"; }
else if (word <= 10485760) { ss << word * 1.0 / 1048576 << "M"; }
else if (word <= (1048576 << 10)) { ss << word / 1048576 << "M"; }
else { ss << word * 1.0 / (1 << 30) << "G"; }
std::string s;
ss >> s;
return s;
}
//! split a c-style string with delimineter chara.
std::vector<std::string> split(const char *str, char deli) {
std::istringstream ss(str);
std::string token;
std::vector<std::string> ret;
while (std::getline(ss, token, deli)) {
if (token.size() >= 1) ret.push_back(token);
}
return ret;
}
string Counter::pad() const {
if (!Clocker::currentLevel) return "";
ostringstream oss;
for (int i = 0; i < Clocker::currentLevel; ++i) oss << "| ";
oss << " ";
return oss.str();
}
void printCurrentDateAndTime() {
// Declaring argument for time()
time_t tt;
// Declaring variable to store return value of
// localtime()
struct tm *ti;
// Applying time()
time(&tt);
// Using localtime()
ti = localtime(&tt);
cout << asctime(ti);
}