blob: 4ca85d53033e1a8c3564f1e427e92f65b4beaeee (
plain)
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
|
#include "stop_watch.h"
#include <iostream>
#include <iomanip>
#include <err.h>
#include <x86intrin.h>
double Subtract(const struct timespec &first, const struct timespec &second) {
return static_cast<double>(first.tv_sec - second.tv_sec) + static_cast<double>(first.tv_nsec - second.tv_nsec) / 1000000000.0;
}
StopWatch::StopWatch(const std::string &label, float divide) : label_(label), divide_(divide) {
if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &started_))
err(1, "Failed to read CLOCK_PROCESS_CPUTIME_ID");
tsc_ = __rdtsc();
}
StopWatch::~StopWatch() {
uint64_t tsc_now = __rdtsc();
if (tsc_now < tsc_) {
std::cerr << "Warning: tsc went down, no core affinity?" << std::endl;
tsc_now = tsc_;
}
struct timespec stopped;
if (-1 == clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &stopped))
err(1, "Failed to read CLOCK_PROCESS_CPUTIME_ID");
std::cout << label_ << '\t' << std::setw(9) << std::setprecision(5) << (Subtract(stopped, started_) / divide_) << '\t' << std::setw(11) << (tsc_now - tsc_) << std::endl;
}
|