forked from RiecoinTeam/rieMiner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stats.hpp
56 lines (51 loc) · 2.37 KB
/
Stats.hpp
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
// (c) 2017-2020 Pttn (https://github.com/Pttn/rieMiner)
#ifndef HEADER_Stats_hpp
#define HEADER_Stats_hpp
#include "tools.hpp"
// "Raw" and immutable stats, and tools to analyze them
class Stats {
std::vector<uint64_t> _counts;
double _duration;
public:
Stats(const std::vector<uint64_t> &counts, const double &duration) : _counts(counts), _duration(duration) {};
std::vector<uint64_t> counts() const {return _counts;}
uint64_t count(const uint64_t i) const {return i < _counts.size() ? _counts[i] : 0;}
double duration() const {return _duration;}
double cps() const {return _duration > 0. ? static_cast<double>(_counts[0])/_duration : 0.;}
double r() const {return _counts[1] > 0 ? static_cast<double>(_counts[0])/static_cast<double>(_counts[1]) : 0.;}
double estimatedAverageTimeToFindBlock(const uint64_t primeCountTarget) const {return cps() != 0. ? std::pow(r(), primeCountTarget)/cps() : 0.;}
std::string formattedCounts(const uint64_t = 0) const;
std::string formattedRates(const uint64_t = 0) const;
std::string formattedRatios() const;
static std::string formattedTime(const double &time);
static std::string formattedClockTimeNow();
static std::string formattedDuration(const double &duration);
};
// Allows the miner to update and get stats
constexpr uint32_t countsRecentEntries(5);
class StatManager {
struct Counts {
std::vector<uint64_t> counts;
std::chrono::time_point<std::chrono::steady_clock> startTp;
Counts() : counts(std::vector<uint64_t>()) {};
Counts(const uint64_t tupleSize) : counts(tupleSize + 1, 0), startTp(std::chrono::steady_clock::now()) {}
};
uint64_t _tupleSize, _nBlocks, _countsRecentEntryPos;
Counts _countsSinceStart;
uint64_t _shares, _rejectedShares;
std::vector<Counts> _countsRecent; // Stores separately the counts for the last countsRecentEntries blocks
std::mutex _countsLock;
public:
StatManager() : _shares(0ULL), _rejectedShares(0ULL) {}
void start(const uint64_t);
void newBlock();
void addCounts(const std::vector<uint64_t>&);
void incrementShares() {_shares++;}
void incrementRejectedShares() {_rejectedShares++;}
double timeSinceStart() const {return timeSince(_countsSinceStart.startTp);}
double averageBlockTime() const {return _nBlocks > 0ULL ? timeSinceStart()/static_cast<double>(_nBlocks) : 0.;}
Stats stats(const bool) const;
uint64_t shares() {return _shares;}
uint64_t rejectedShares() {return _rejectedShares;}
};
#endif