2019-09-11 23:38:13 +08:00
|
|
|
#ifndef __EVENT_COUNTER_H
|
|
|
|
#define __EVENT_COUNTER_H
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
#include <cctype>
|
|
|
|
#ifndef _MSC_VER
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
2020-04-28 02:20:05 +08:00
|
|
|
#include <unistd.h>
|
2019-09-11 23:38:13 +08:00
|
|
|
#include <cinttypes>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <chrono>
|
|
|
|
#include <cstring>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iomanip>
|
|
|
|
#include <iostream>
|
|
|
|
#include <map>
|
|
|
|
#include <set>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "linux-perf-events.h"
|
|
|
|
#ifdef __linux__
|
|
|
|
#include <libgen.h>
|
|
|
|
#endif
|
2020-03-03 06:23:19 +08:00
|
|
|
|
|
|
|
#include "simdjson.h"
|
2019-09-11 23:38:13 +08:00
|
|
|
|
|
|
|
using std::string;
|
|
|
|
using std::vector;
|
|
|
|
using std::chrono::steady_clock;
|
|
|
|
using std::chrono::time_point;
|
|
|
|
using std::chrono::duration;
|
|
|
|
|
|
|
|
struct event_count {
|
|
|
|
duration<double> elapsed;
|
|
|
|
vector<unsigned long long> event_counts;
|
|
|
|
event_count() : elapsed(0), event_counts{0,0,0,0,0} {}
|
|
|
|
event_count(const duration<double> _elapsed, const vector<unsigned long long> _event_counts) : elapsed(_elapsed), event_counts(_event_counts) {}
|
|
|
|
event_count(const event_count& other): elapsed(other.elapsed), event_counts(other.event_counts) { }
|
|
|
|
|
|
|
|
// The types of counters (so we can read the getter more easily)
|
|
|
|
enum event_counter_types {
|
|
|
|
CPU_CYCLES,
|
|
|
|
INSTRUCTIONS,
|
|
|
|
BRANCH_MISSES,
|
|
|
|
CACHE_REFERENCES,
|
|
|
|
CACHE_MISSES
|
|
|
|
};
|
|
|
|
|
|
|
|
double elapsed_sec() const { return duration<double>(elapsed).count(); }
|
|
|
|
double elapsed_ns() const { return duration<double, std::nano>(elapsed).count(); }
|
2020-04-22 06:43:35 +08:00
|
|
|
double cycles() const { return static_cast<double>(event_counts[CPU_CYCLES]); }
|
|
|
|
double instructions() const { return static_cast<double>(event_counts[INSTRUCTIONS]); }
|
|
|
|
double branch_misses() const { return static_cast<double>(event_counts[BRANCH_MISSES]); }
|
|
|
|
double cache_references() const { return static_cast<double>(event_counts[CACHE_REFERENCES]); }
|
|
|
|
double cache_misses() const { return static_cast<double>(event_counts[CACHE_MISSES]); }
|
2019-09-11 23:38:13 +08:00
|
|
|
|
2020-02-07 23:41:45 +08:00
|
|
|
event_count& operator=(const event_count& other) {
|
2019-09-11 23:38:13 +08:00
|
|
|
this->elapsed = other.elapsed;
|
|
|
|
this->event_counts = other.event_counts;
|
|
|
|
return *this;
|
|
|
|
}
|
2020-02-07 23:41:45 +08:00
|
|
|
event_count operator+(const event_count& other) const {
|
2019-09-11 23:38:13 +08:00
|
|
|
return event_count(elapsed+other.elapsed, {
|
|
|
|
event_counts[0]+other.event_counts[0],
|
|
|
|
event_counts[1]+other.event_counts[1],
|
|
|
|
event_counts[2]+other.event_counts[2],
|
|
|
|
event_counts[3]+other.event_counts[3],
|
|
|
|
event_counts[4]+other.event_counts[4],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-07 23:41:45 +08:00
|
|
|
void operator+=(const event_count& other) {
|
2019-09-11 23:38:13 +08:00
|
|
|
*this = *this + other;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct event_aggregate {
|
|
|
|
int iterations = 0;
|
2020-04-23 23:36:52 +08:00
|
|
|
event_count total{};
|
|
|
|
event_count best{};
|
|
|
|
event_count worst{};
|
2019-09-11 23:38:13 +08:00
|
|
|
|
|
|
|
event_aggregate() {}
|
|
|
|
|
2020-02-07 23:41:45 +08:00
|
|
|
void operator<<(const event_count& other) {
|
2019-09-11 23:38:13 +08:00
|
|
|
if (iterations == 0 || other.elapsed < best.elapsed) {
|
|
|
|
best = other;
|
|
|
|
}
|
|
|
|
if (iterations == 0 || other.elapsed > worst.elapsed) {
|
|
|
|
worst = other;
|
|
|
|
}
|
|
|
|
iterations++;
|
|
|
|
total += other;
|
|
|
|
}
|
|
|
|
|
|
|
|
double elapsed_sec() const { return total.elapsed_sec() / iterations; }
|
|
|
|
double elapsed_ns() const { return total.elapsed_ns() / iterations; }
|
|
|
|
double cycles() const { return total.cycles() / iterations; }
|
|
|
|
double instructions() const { return total.instructions() / iterations; }
|
|
|
|
double branch_misses() const { return total.branch_misses() / iterations; }
|
|
|
|
double cache_references() const { return total.cache_references() / iterations; }
|
|
|
|
double cache_misses() const { return total.cache_misses() / iterations; }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct event_collector {
|
2020-04-23 23:36:52 +08:00
|
|
|
event_count count{};
|
|
|
|
time_point<steady_clock> start_clock{};
|
2019-09-11 23:38:13 +08:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
LinuxEvents<PERF_TYPE_HARDWARE> linux_events;
|
|
|
|
event_collector() : linux_events(vector<int>{
|
|
|
|
PERF_COUNT_HW_CPU_CYCLES,
|
|
|
|
PERF_COUNT_HW_INSTRUCTIONS,
|
|
|
|
PERF_COUNT_HW_BRANCH_MISSES,
|
|
|
|
PERF_COUNT_HW_CACHE_REFERENCES,
|
|
|
|
PERF_COUNT_HW_CACHE_MISSES
|
|
|
|
}) {}
|
|
|
|
bool has_events() {
|
|
|
|
return linux_events.is_working();
|
|
|
|
}
|
|
|
|
#else
|
2020-01-11 23:14:22 +08:00
|
|
|
event_collector() {}
|
2019-09-11 23:38:13 +08:00
|
|
|
bool has_events() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
really_inline void start() {
|
|
|
|
#if defined(__linux)
|
|
|
|
linux_events.start();
|
|
|
|
#endif
|
|
|
|
start_clock = steady_clock::now();
|
|
|
|
}
|
|
|
|
really_inline event_count& end() {
|
|
|
|
time_point<steady_clock> end_clock = steady_clock::now();
|
|
|
|
#if defined(__linux)
|
|
|
|
linux_events.end(count.event_counts);
|
|
|
|
#endif
|
|
|
|
count.elapsed = end_clock - start_clock;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-23 23:36:52 +08:00
|
|
|
#endif
|