1
1
#include " monitor.h"
2
2
3
+ #include < memory/encoders/json.h>
4
+ #include < utils/utils.h>
5
+
3
6
void ResourceMonitor::Enable ()
4
7
{
5
8
resmonTimesTable.clear ();
@@ -53,4 +56,85 @@ void ResourceMonitor::StopTime(std::string plugin_id, std::string key)
53
56
if (resmonTempTables[plugin_id].find (key) == resmonTempTables[plugin_id].end ()) return ;
54
57
55
58
RecordTime (plugin_id, key, float (std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now () - resmonTempTables[plugin_id][key]).count ()) / 1000.0 );
59
+ }
60
+
61
+ std::string ResourceMonitor::GenerateJSONPerformance (std::string plugin_id)
62
+ {
63
+ rapidjson::Document doc (rapidjson::kArrayType );
64
+
65
+ rapidjson::Document v1 = encoders::json::FromString (" {\" args\" : {\" name\" : \" Swiftly\" },\" cat\" : \" __metadata\" ,\" name\" : \" process_name\" ,\" ph\" : \" M\" ,\" pid\" : 1,\" tid\" : 1,\" ts\" : 0}" );
66
+ doc.PushBack (v1, doc.GetAllocator ());
67
+ rapidjson::Document v2 = encoders::json::FromString (" {\" args\" :{\" name\" :\" Swiftly Main\" },\" cat\" :\" __metadata\" ,\" name\" :\" thread_name\" ,\" ph\" :\" M\" ,\" pid\" :1,\" tid\" :1,\" ts\" :0}" );
68
+ doc.PushBack (v2, doc.GetAllocator ());
69
+ rapidjson::Document v3 = encoders::json::FromString (" {\" args\" :{\" name\" :\" Swiftly Profiler\" },\" cat\" :\" __metadata\" ,\" name\" :\" thread_name\" ,\" ph\" :\" M\" ,\" pid\" :1,\" tid\" :2,\" ts\" :0}" );
70
+ doc.PushBack (v3, doc.GetAllocator ());
71
+
72
+ uint64_t t = 0 ;
73
+ auto timings = GetResmonTimeTables ();
74
+ if (plugin_id == " " ) {
75
+ for (auto it = timings.begin (); it != timings.end (); ++it) {
76
+ for (auto it2 = it->second .begin (); it2 != it->second .end (); ++it2) {
77
+ for (float time : it2->second ) {
78
+ uint64_t uint_time = (uint64_t )(time * 1000 .0f );
79
+
80
+ rapidjson::Value val (rapidjson::kObjectType );
81
+ rapidjson::Value val2 (rapidjson::kObjectType );
82
+
83
+ t++;
84
+
85
+ std::string event_name = string_format (" %s [%s]" , it2->first .c_str (), it->first .c_str ());
86
+
87
+ val.AddMember (" name" , rapidjson::Value ().SetString (event_name.c_str (), doc.GetAllocator ()), doc.GetAllocator ());
88
+ val.AddMember (" ph" , rapidjson::Value ().SetString (" B" , doc.GetAllocator ()), doc.GetAllocator ());
89
+ val.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
90
+ val.AddMember (" pid" , rapidjson::Value ().SetInt (1 ), doc.GetAllocator ());
91
+ val.AddMember (" ts" , rapidjson::Value ().SetUint64 (t), doc.GetAllocator ());
92
+
93
+ doc.PushBack (val, doc.GetAllocator ());
94
+
95
+ t += uint_time;
96
+
97
+ val2.AddMember (" name" , rapidjson::Value ().SetString (event_name.c_str (), doc.GetAllocator ()), doc.GetAllocator ());
98
+ val2.AddMember (" ph" , rapidjson::Value ().SetString (" E" , doc.GetAllocator ()), doc.GetAllocator ());
99
+ val2.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
100
+ val2.AddMember (" pid" , rapidjson::Value ().SetInt (1 ), doc.GetAllocator ());
101
+ val2.AddMember (" ts" , rapidjson::Value ().SetUint64 (t), doc.GetAllocator ());
102
+
103
+ doc.PushBack (val2, doc.GetAllocator ());
104
+ }
105
+ }
106
+ }
107
+ }
108
+ else {
109
+ for (auto it2 = timings[plugin_id].begin (); it2 != timings[plugin_id].end (); ++it2) {
110
+ for (float time : it2->second ) {
111
+ uint64_t uint_time = (uint64_t )(time * 1000 .0f );
112
+
113
+ rapidjson::Value val (rapidjson::kObjectType );
114
+ rapidjson::Value val2 (rapidjson::kObjectType );
115
+
116
+ t++;
117
+
118
+ val.AddMember (" name" , rapidjson::Value ().SetString (it2->first .c_str (), doc.GetAllocator ()), doc.GetAllocator ());
119
+ val.AddMember (" ph" , rapidjson::Value ().SetString (" B" , doc.GetAllocator ()), doc.GetAllocator ());
120
+ val.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
121
+ val.AddMember (" pid" , rapidjson::Value ().SetInt (1 ), doc.GetAllocator ());
122
+ val.AddMember (" ts" , rapidjson::Value ().SetUint64 (t), doc.GetAllocator ());
123
+
124
+ doc.PushBack (val, doc.GetAllocator ());
125
+
126
+ t += uint_time;
127
+
128
+ val2.AddMember (" name" , rapidjson::Value ().SetString (it2->first .c_str (), doc.GetAllocator ()), doc.GetAllocator ());
129
+ val2.AddMember (" ph" , rapidjson::Value ().SetString (" E" , doc.GetAllocator ()), doc.GetAllocator ());
130
+ val2.AddMember (" tid" , rapidjson::Value ().SetInt (2 ), doc.GetAllocator ());
131
+ val2.AddMember (" pid" , rapidjson::Value ().SetInt (1 ), doc.GetAllocator ());
132
+ val2.AddMember (" ts" , rapidjson::Value ().SetUint64 (t), doc.GetAllocator ());
133
+
134
+ doc.PushBack (val2, doc.GetAllocator ());
135
+ }
136
+ }
137
+ }
138
+
139
+ return std::string (" {\" traceEvents\" :" ) + encoders::json::ToString (doc) + " }" ;
56
140
}
0 commit comments