This repository has been archived by the owner on Dec 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathStats.h
88 lines (74 loc) · 2.07 KB
/
Stats.h
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
#pragma once
#include "containers/cancel_flag.h"
#include <atomic>
#include <functional>
#include <thread>
#include <wx/string.h>
namespace ch = std::chrono;
struct StatsUpdateData {
#define DEFINE_STAT(name, type) type name;
DEFINE_STAT(cpu, float)
DEFINE_STAT(ram, size_t)
DEFINE_STAT(read, size_t)
DEFINE_STAT(write, size_t)
DEFINE_STAT(provide, size_t)
DEFINE_STAT(download, size_t)
DEFINE_STAT(latency, float)
DEFINE_STAT(threads, int)
#undef DEFINE_STAT
};
class Stats {
public:
Stats() = delete;
Stats(const Stats&) = delete;
Stats& operator=(const Stats&) = delete;
static inline const wxString GetReadableSize(size_t size) {
if (!size) {
return "0 B";
}
static constexpr const char* suffix[] = { "B", "KB", "MB", "GB", "TB", "PB", "EB" };
int i = 0;
auto sizeD = (double)size;
while (sizeD >= 1024) {
sizeD /= 1024;
i++;
}
return wxString::Format("%.*f %s", 2 - (int)floor(log10(sizeD)), sizeD, suffix[i]);
}
template<class Callback, class ...Args>
static inline void StartUpdateThread(ch::milliseconds refreshRate, Callback&& updateCallback, Args&&... args) {
if (ThreadRunning) {
return;
}
RefreshRate = refreshRate;
std::thread([=]() {
ThreadRunning = true;
while (!Flag.cancelled()) {
UpdateData();
if (Flag.cancelled()) {
break;
}
if (!updateCallback(Data, args...)) {
break;
}
std::this_thread::sleep_for(RefreshRate);
}
ThreadRunning = false;
}).detach();
}
static inline void StopUpdateThread() {
Flag.cancel();
}
static inline std::atomic_uint64_t ProvideCount = 0; // std::atomic_uint_fast64_t is the same in msvc
static inline std::atomic_uint64_t FileReadCount = 0;
static inline std::atomic_uint64_t FileWriteCount = 0;
static inline std::atomic_uint64_t DownloadCount = 0;
static inline std::atomic_uint64_t LatOpCount = 0;
static inline std::atomic_uint64_t LatNsCount = 0;
private:
static inline StatsUpdateData Data;
static inline ch::milliseconds RefreshRate;
static inline bool ThreadRunning = false;
static inline cancel_flag Flag;
static void UpdateData();
};