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
|
#include <string.h>
#include <error.h>
#include <time.h>
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include "misc_log_ex.h"
#include "timings.h"
#define N_EXPECTED_FIELDS (8+11)
TimingsDatabase::TimingsDatabase()
{
}
TimingsDatabase::TimingsDatabase(const std::string &filename):
filename(filename)
{
load();
}
TimingsDatabase::~TimingsDatabase()
{
save();
}
bool TimingsDatabase::load()
{
instances.clear();
if (filename.empty())
return true;
FILE *f = fopen(filename.c_str(), "r");
if (!f)
{
MDEBUG("Failed to load timings file " << filename << ": " << strerror(errno));
return false;
}
while (1)
{
char s[4096];
if (!fgets(s, sizeof(s), f))
break;
char *tab = strchr(s, '\t');
if (!tab)
{
MWARNING("Bad format: no tab found");
continue;
}
const std::string name = std::string(s, tab - s);
std::vector<std::string> fields;
char *ptr = tab + 1;
boost::split(fields, ptr, boost::is_any_of(" "));
if (fields.size() != N_EXPECTED_FIELDS)
{
MERROR("Bad format: wrong number of fields: got " << fields.size() << " expected " << N_EXPECTED_FIELDS);
continue;
}
instance i;
unsigned int idx = 0;
i.t = atoi(fields[idx++].c_str());
i.npoints = atoi(fields[idx++].c_str());
i.min = atof(fields[idx++].c_str());
i.max = atof(fields[idx++].c_str());
i.mean = atof(fields[idx++].c_str());
i.median = atof(fields[idx++].c_str());
i.stddev = atof(fields[idx++].c_str());
i.npskew = atof(fields[idx++].c_str());
i.deciles.reserve(11);
for (int n = 0; n < 11; ++n)
{
i.deciles.push_back(atoi(fields[idx++].c_str()));
}
instances.insert(std::make_pair(name, i));
}
fclose(f);
return true;
}
bool TimingsDatabase::save()
{
if (filename.empty())
return true;
FILE *f = fopen(filename.c_str(), "w");
if (!f)
{
MERROR("Failed to write to file " << filename << ": " << strerror(errno));
return false;
}
for (const auto &i: instances)
{
fprintf(f, "%s", i.first.c_str());
fprintf(f, "\t%lu", (unsigned long)i.second.t);
fprintf(f, " %zu", i.second.npoints);
fprintf(f, " %f", i.second.min);
fprintf(f, " %f", i.second.max);
fprintf(f, " %f", i.second.mean);
fprintf(f, " %f", i.second.median);
fprintf(f, " %f", i.second.stddev);
fprintf(f, " %f", i.second.npskew);
for (uint64_t v: i.second.deciles)
fprintf(f, " %lu", (unsigned long)v);
fputc('\n', f);
}
fclose(f);
return true;
}
std::vector<TimingsDatabase::instance> TimingsDatabase::get(const char *name) const
{
std::vector<instance> ret;
auto range = instances.equal_range(name);
for (auto i = range.first; i != range.second; ++i)
ret.push_back(i->second);
std::sort(ret.begin(), ret.end(), [](const instance &e0, const instance &e1){ return e0.t < e1.t; });
return ret;
}
void TimingsDatabase::add(const char *name, const instance &i)
{
instances.insert(std::make_pair(name, i));
}
|