stats.h

main 128 lines · 3.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1// tfs stats system made by BBarwik
2
3#ifndef OTCLIENT_STATS_H
4#define OTCLIENT_STATS_H
5
6#include <list>
7#include <atomic>
8#include <mutex>
9#include <chrono>
10#include <unordered_map>
11#include <set>
12
13// NOT THREAD SAFE
14
15enum StatsTypes{
16 STATS_FIRST = 0,
17 STATS_GENERAL = STATS_FIRST,
18 STATS_MAIN,
19 STATS_RENDER,
20 STATS_DISPATCHER,
21 STATS_LUA,
22 STATS_LUACALLBACK,
23 STATS_PACKETS,
24 STATS_LAST = STATS_PACKETS
25};
26
27struct Stat {
28 Stat(uint64_t _executionTime, const std::string& _description, const std::string& _extraDescription) :
29 executionTime(_executionTime), description(_description), extraDescription(_extraDescription) {};
30 uint64_t executionTime = 0;
31 std::string description;
32 std::string extraDescription;
33};
34
35struct StatsData {
36 StatsData(uint32_t _calls, uint64_t _executionTime, const std::string& _extraInfo) :
37 calls(_calls), executionTime(_executionTime), extraInfo(_extraInfo) {}
38 uint32_t calls = 0;
39 uint64_t executionTime = 0;
40 std::string extraInfo;
41};
42
43using StatsMap = std::unordered_map<std::string, StatsData>;
44using StatsList = std::list<Stat*>;
45
46class UIWidget;
47
48class Stats {
49public:
50 void add(int type, Stat* stats);
51
52 std::string get(int type, int limit, bool pretty);
53 void clear(int type);
54
55 void clearAll();
56
57 std::string getSlow(int type, int limit, unsigned int minTime, bool pretty);
58 void clearSlow(int type);
59
60 int types() { return STATS_LAST + 1; }
61
62 int64_t getSleepTime() {
63 return m_sleepTime;
64 }
65 void resetSleepTime() {
66 m_sleepTime = 0;
67 }
68
69 int64_t m_sleepTime = 0;
70
71 void addWidget(UIWidget* widget);
72 void removeWidget(UIWidget* widget);
73 std::string getWidgetsInfo(int limit, bool pretty);
74
75 inline void addTexture() { createdTextures += 1; }
76 inline void removeTexture() { destroyedTextures += 1; }
77
78 inline void addThing() { createdThings += 1; }
79 inline void removeThing() { destroyedThings += 1; }
80
81 inline void addCreature() { createdCreatures += 1; }
82 inline void removeCreature() { destroyedCreatures += 1; }
83
84private:
85 struct {
86 StatsMap data;
87 StatsList slow;
88 int64_t start = 0;
89 } stats[STATS_LAST + 1];
90
91 std::set<UIWidget*> widgets;
92 int createdWidgets = 0;
93 int destroyedWidgets = 0;
94 int createdTextures = 0;
95 int destroyedTextures = 0;
96 int createdThings = 0;
97 int destroyedThings = 0;
98 int createdCreatures = 0;
99 int destroyedCreatures = 0;
100 std::mutex m_mutex;
101};
102
103extern Stats g_stats;
104
105class AutoStat {
106public:
107 AutoStat(int type, const std::string& description, const std::string& extraDescription = "") :
108 m_type(type), m_stat(new Stat(0, description, extraDescription)), m_timePoint(std::chrono::high_resolution_clock::now()) {}
109
110 ~AutoStat() {
111 m_stat->executionTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - m_timePoint).count();
112 m_stat->executionTime -= m_minusTime;
113 g_stats.add(m_type, m_stat);
114 }
115
116 AutoStat(const AutoStat&) = delete;
117 AutoStat & operator=(const AutoStat&) = delete;
118
119private:
120 int m_type;
121 Stat* m_stat;
122
123protected:
124 uint64_t m_minusTime = 0;
125 std::chrono::high_resolution_clock::time_point m_timePoint;
126};
127
128#endif
Top