| 1 | #include <string.h> |
| 2 | #include <errno.h> |
| 3 | #include <time.h> |
| 4 | #include <algorithm> |
| 5 | #include <boost/algorithm/string.hpp> |
| 6 | #include "misc_log_ex.h" |
| 7 | #include "timings.h" |
| 8 | |
| 9 | #define N_EXPECTED_FIELDS (8+11) |
| 10 | |
| 11 | TimingsDatabase::TimingsDatabase() |
| 12 | { |
| 13 | } |
| 14 | |
| 15 | TimingsDatabase::TimingsDatabase(const std::string &filename): |
| 16 | filename(filename) |
| 17 | { |
| 18 | load(); |
| 19 | } |
| 20 | |
| 21 | TimingsDatabase::~TimingsDatabase() |
| 22 | { |
| 23 | save(); |
| 24 | } |
| 25 | |
| 26 | bool TimingsDatabase::load() |
| 27 | { |
| 28 | instances.clear(); |
| 29 | |
| 30 | if (filename.empty()) |
| 31 | return true; |
| 32 | |
| 33 | FILE *f = fopen(filename.c_str(), "r"); |
| 34 | if (!f) |
| 35 | { |
| 36 | MDEBUG("Failed to load timings file " << filename << ": " << strerror(errno)); |
| 37 | return false; |
| 38 | } |
| 39 | while (1) |
| 40 | { |
| 41 | char s[4096]; |
| 42 | if (!fgets(s, sizeof(s), f)) |
| 43 | break; |
| 44 | char *tab = strchr(s, '\t'); |
| 45 | if (!tab) |
| 46 | { |
| 47 | MWARNING("Bad format: no tab found"); |
| 48 | continue; |
| 49 | } |
| 50 | const std::string name = std::string(s, tab - s); |
| 51 | std::vector<std::string> fields; |
| 52 | char *ptr = tab + 1; |
| 53 | boost::split(fields, ptr, boost::is_any_of(" ")); |
| 54 | if (fields.size() != N_EXPECTED_FIELDS) |
| 55 | { |
| 56 | MERROR("Bad format: wrong number of fields: got " << fields.size() << " expected " << N_EXPECTED_FIELDS); |
| 57 | continue; |
| 58 | } |
| 59 | |
| 60 | instance i; |
| 61 | |
| 62 | unsigned int idx = 0; |
| 63 | i.t = atoi(fields[idx++].c_str()); |
| 64 | i.npoints = atoi(fields[idx++].c_str()); |
| 65 | i.min = atof(fields[idx++].c_str()); |
| 66 | i.max = atof(fields[idx++].c_str()); |
| 67 | i.mean = atof(fields[idx++].c_str()); |
| 68 | i.median = atof(fields[idx++].c_str()); |
| 69 | i.stddev = atof(fields[idx++].c_str()); |
| 70 | i.npskew = atof(fields[idx++].c_str()); |
| 71 | i.deciles.reserve(11); |
| 72 | for (int n = 0; n < 11; ++n) |
| 73 | { |
| 74 | i.deciles.push_back(atoi(fields[idx++].c_str())); |
| 75 | } |
| 76 | instances.insert(std::make_pair(name, i)); |
| 77 | } |
| 78 | fclose(f); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | bool TimingsDatabase::save() |
| 83 | { |
| 84 | if (filename.empty()) |
| 85 | return true; |
| 86 | |
| 87 | FILE *f = fopen(filename.c_str(), "w"); |
| 88 | if (!f) |
| 89 | { |
| 90 | MERROR("Failed to write to file " << filename << ": " << strerror(errno)); |
| 91 | return false; |
| 92 | } |
| 93 | for (const auto &i: instances) |
| 94 | { |
| 95 | fprintf(f, "%s", i.first.c_str()); |
| 96 | fprintf(f, "\t%lu", (unsigned long)i.second.t); |
| 97 | fprintf(f, " %zu", i.second.npoints); |
| 98 | fprintf(f, " %f", i.second.min); |
| 99 | fprintf(f, " %f", i.second.max); |
| 100 | fprintf(f, " %f", i.second.mean); |
| 101 | fprintf(f, " %f", i.second.median); |
| 102 | fprintf(f, " %f", i.second.stddev); |
| 103 | fprintf(f, " %f", i.second.npskew); |
| 104 | for (uint64_t v: i.second.deciles) |
| 105 | fprintf(f, " %lu", (unsigned long)v); |
| 106 | fputc('\n', f); |
| 107 | } |
| 108 | fclose(f); |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | std::vector<TimingsDatabase::instance> TimingsDatabase::get(const char *name) const |
| 113 | { |
| 114 | std::vector<instance> ret; |
| 115 | auto range = instances.equal_range(name); |
| 116 | for (auto i = range.first; i != range.second; ++i) |
| 117 | ret.push_back(i->second); |
| 118 | std::sort(ret.begin(), ret.end(), [](const instance &e0, const instance &e1){ return e0.t < e1.t; }); |
| 119 | return ret; |
| 120 | } |
| 121 | |
| 122 | void TimingsDatabase::add(const char *name, const instance &i) |
| 123 | { |
| 124 | instances.insert(std::make_pair(name, i)); |
| 125 | } |