summarylogtreecommitdiffstats
path: root/overlay.diff
blob: 6cf28ef29fdfeb7bc733a48e73c0d58022097177 (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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
--- a/src/Cafe/HW/Latte/Core/LatteOverlay.cpp
+++ b/src/Cafe/HW/Latte/Core/LatteOverlay.cpp
@@ -12,30 +12,16 @@
 #include "imgui/imgui_extension.h"
 
 #include "input/InputManager.h"
+#include "util/SystemInfo/SystemInfo.h"
 
 #include <cinttypes>
 
-#if BOOST_OS_WINDOWS
-#include <Psapi.h>
-#include <winternl.h>
-#pragma comment(lib, "ntdll.lib")
-#endif
-
 struct OverlayStats
 {
 	OverlayStats() {};
 
 	int processor_count = 1;
-
-	// cemu cpu stats
-	uint64_t last_cpu{}, kernel{}, user{};
-
-	// global cpu stats
-	struct ProcessorTime
-	{
-		uint64_t idle{}, kernel{}, user{};
-	};
-
+	ProcessorTime processor_time_cemu;
 	std::vector<ProcessorTime> processor_times;
 
 	double fps{};
@@ -562,83 +548,52 @@ void LatteOverlay_render(bool pad_view)
 	}
 }
 
-
 void LatteOverlay_init()
 {
-#if BOOST_OS_WINDOWS
-	SYSTEM_INFO sys_info;
-	GetSystemInfo(&sys_info);
-	g_state.processor_count = sys_info.dwNumberOfProcessors;
+	g_state.processor_count = GetProcessorCount();
 
 	g_state.processor_times.resize(g_state.processor_count);
 	g_state.cpu_per_core.resize(g_state.processor_count);
-#else
-	g_state.processor_count = 1;
-#endif
 }
 
-void LatteOverlay_updateStats(double fps, sint32 drawcalls)
+static void UpdateStats_CemuCpu()
 {
-	if (GetConfig().overlay.position == ScreenPosition::kDisabled)
-		return;
+	ProcessorTime now;
+	QueryProcTime(now);
+	
+	double cpu = ProcessorTime::Compare(g_state.processor_time_cemu, now);
+	cpu /= g_state.processor_count;
+	
+	g_state.cpu_usage = cpu * 100;
+	g_state.processor_time_cemu = now;
+}
 
-	g_state.fps = fps;
-	g_state.draw_calls_per_frame = drawcalls;
+static void UpdateStats_CpuPerCore()
+{
+	std::vector<ProcessorTime> now(g_state.processor_count);
+	QueryCoreTimes(g_state.processor_count, now);
 
-#if BOOST_OS_WINDOWS
-	// update cemu cpu
-	FILETIME ftime, fkernel, fuser;
-	LARGE_INTEGER now, kernel, user;
-	GetSystemTimeAsFileTime(&ftime);
-	now.LowPart = ftime.dwLowDateTime;
-	now.HighPart = ftime.dwHighDateTime;
-
-	GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &fkernel, &fuser);
-	kernel.LowPart = fkernel.dwLowDateTime;
-	kernel.HighPart = fkernel.dwHighDateTime;
-
-	user.LowPart = fuser.dwLowDateTime;
-	user.HighPart = fuser.dwHighDateTime;
-
-	double percent = (kernel.QuadPart - g_state.kernel) + (user.QuadPart - g_state.user);
-	percent /= (now.QuadPart - g_state.last_cpu);
-	percent /= g_state.processor_count;
-	g_state.cpu_usage = percent * 100;
-	g_state.last_cpu = now.QuadPart;
-	g_state.user = user.QuadPart;
-	g_state.kernel = kernel.QuadPart;
-
-	// update cpu per core
-	std::vector<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> sppi(g_state.processor_count);
-	if (NT_SUCCESS(NtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi.data(), sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * g_state.processor_count, nullptr)))
+	for (int32_t i = 0; i < g_state.processor_count; ++i)
 	{
-		for (sint32 i = 0; i < g_state.processor_count; ++i)
-		{
-			const uint64 kernel_diff = sppi[i].KernelTime.QuadPart - g_state.processor_times[i].kernel;
-			const uint64 user_diff = sppi[i].UserTime.QuadPart - g_state.processor_times[i].user;
-			const uint64 idle_diff = sppi[i].IdleTime.QuadPart - g_state.processor_times[i].idle;
-
-			const auto total = kernel_diff + user_diff; // kernel time already includes idletime
-			const double cpu = total == 0 ? 0 : (1.0 - ((double)idle_diff / total)) * 100.0;
+		double cpu = ProcessorTime::Compare(g_state.processor_times[i], now[i]);
 
-			g_state.cpu_per_core[i] = cpu;
-			//total_cpu += cpu;
+		g_state.cpu_per_core[i] = cpu * 100;
+		g_state.processor_times[i] = now[i];
+	}
+}
 
-			g_state.processor_times[i].idle = sppi[i].IdleTime.QuadPart;
-			g_state.processor_times[i].kernel = sppi[i].KernelTime.QuadPart;
-			g_state.processor_times[i].user = sppi[i].UserTime.QuadPart;
-		}
+void LatteOverlay_updateStats(double fps, sint32 drawcalls)
+{
+	if (GetConfig().overlay.position == ScreenPosition::kDisabled)
+		return;
 
-		//total_cpu /= g_state.processor_count;
-		//g_state.cpu_usage = total_cpu;
-	}
+	g_state.fps = fps;
+	g_state.draw_calls_per_frame = drawcalls;
+	UpdateStats_CemuCpu();
+	UpdateStats_CpuPerCore();
 
 	// update ram
-	PROCESS_MEMORY_COUNTERS pmc{};
-	pmc.cb = sizeof(pmc);
-	GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
-	g_state.ram_usage = (pmc.WorkingSetSize / 1000) / 1000;
-#endif
+	g_state.ram_usage = (QueryRamUsage() / 1000) / 1000;
 
 	// update vram
 	g_renderer->GetVRAMInfo(g_state.vramUsage, g_state.vramTotal);
--- a/src/Cafe/HW/Latte/Core/LattePerformanceMonitor.cpp
+++ b/src/Cafe/HW/Latte/Core/LattePerformanceMonitor.cpp
@@ -98,7 +98,10 @@ void LattePerformanceMonitor_frameEnd()
 		performanceMonitor.cycle[nextCycleIndex].recompilerLeaveCount = 0;
 		performanceMonitor.cycle[nextCycleIndex].threadLeaveCount = 0;
 		performanceMonitor.cycleIndex = nextCycleIndex;
-		
+
+		// next update in 1 second
+		performanceMonitor.cycle[performanceMonitor.cycleIndex].lastUpdate = GetTickCount();
+
 		if (isFirstUpdate)
 		{
 			LatteOverlay_updateStats(0.0, 0);
@@ -109,8 +112,6 @@ void LattePerformanceMonitor_frameEnd()
 			LatteOverlay_updateStats(fps, drawCallCounter / elapsedFrames);
 			gui_updateWindowTitles(false, false, fps);
 		}
-		// next update in 1 second
-		performanceMonitor.cycle[performanceMonitor.cycleIndex].lastUpdate = GetTickCount();
 
 		// prevent hibernation and screen saver/monitor off
 		#if BOOST_OS_WINDOWS
@@ -124,4 +125,4 @@ void LattePerformanceMonitor_frameBegin()
 {
 	performanceMonitor.vk.numDrawBarriersPerFrame.reset();
 	performanceMonitor.vk.numBeginRenderpassPerFrame.reset();
-}
\ No newline at end of file
+}
--- a/src/util/CMakeLists.txt
+++ b/src/util/CMakeLists.txt
@@ -16,8 +16,6 @@ add_library(CemuUtil
   DXGIWrapper/DXGIWrapper.h
   EventService.h
   Fiber/Fiber.h
-  Fiber/FiberUnix.cpp
-  Fiber/FiberWin.cpp
   helpers/ClassWrapper.h
   helpers/ConcurrentQueue.h
   helpers/enum_array.hpp
@@ -50,8 +48,8 @@ add_library(CemuUtil
   math/vector2.h
   math/vector3.h
   MemMapper/MemMapper.h
-  MemMapper/MemMapperUnix.cpp
-  MemMapper/MemMapperWin.cpp
+  SystemInfo/SystemInfo.cpp
+  SystemInfo/SystemInfo.h
   ThreadPool/ThreadPool.cpp
   ThreadPool/ThreadPool.h
   tinyxml2/tinyxml2.cpp
@@ -71,6 +69,23 @@ add_library(CemuUtil
   Zir/Passes/ZpIRRegisterAllocator.cpp
 )
 
+if(WIN32)
+	target_sources(CemuUtil PRIVATE Fiber/FiberWin.cpp)
+	target_sources(CemuUtil PRIVATE MemMapper/MemMapperWin.cpp)
+	target_sources(CemuUtil PRIVATE SystemInfo/SystemInfoWin.cpp)
+elseif(UNIX)
+	target_sources(CemuUtil PRIVATE Fiber/FiberUnix.cpp)
+	target_sources(CemuUtil PRIVATE MemMapper/MemMapperUnix.cpp)
+	target_sources(CemuUtil PRIVATE SystemInfo/SystemInfoUnix.cpp)
+	if(NOT APPLE)
+		target_sources(CemuUtil PRIVATE SystemInfo/SystemInfoLinux.cpp)
+	else()
+		target_sources(CemuUtil PRIVATE SystemInfo/SystemInfoMac.cpp)
+	endif()
+else()
+	target_sources(CemuUtil PRIVATE SystemInfo/SystemInfoStub.cpp)
+endif()
+
 set_property(TARGET CemuUtil PROPERTY MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
 
 target_include_directories(CemuUtil PUBLIC "../")
--- a/src/util/Fiber/FiberUnix.cpp
+++ b/src/util/Fiber/FiberUnix.cpp
@@ -1,5 +1,4 @@
 #include "Fiber.h"
-#if BOOST_OS_LINUX || BOOST_OS_MACOS
 #include <ucontext.h>
 
 thread_local Fiber* sCurrentFiber{};
@@ -52,5 +51,3 @@ void* Fiber::GetFiberPrivateData()
 {
 	return sCurrentFiber->m_privateData;
 }
-
-#endif
\ No newline at end of file
--- a/src/util/Fiber/FiberWin.cpp
+++ b/src/util/Fiber/FiberWin.cpp
@@ -1,5 +1,4 @@
 #include "Fiber.h"
-#if BOOST_OS_WINDOWS
 #include <Windows.h>
 
 thread_local Fiber* sCurrentFiber{};
@@ -39,5 +38,3 @@ void* Fiber::GetFiberPrivateData()
 {
 	return sCurrentFiber->m_privateData;
 }
-
-#endif
\ No newline at end of file
--- a/src/util/MemMapper/MemMapperUnix.cpp
+++ b/src/util/MemMapper/MemMapperUnix.cpp
@@ -1,6 +1,5 @@
 #include "util/MemMapper/MemMapper.h"
 
-#if BOOST_OS_LINUX || BOOST_OS_MACOS
 #include <unistd.h>
 #include <sys/mman.h>
 
@@ -65,5 +64,3 @@ namespace MemMapper
 	}
 
 };
-
-#endif
\ No newline at end of file
--- a/src/util/MemMapper/MemMapperWin.cpp
+++ b/src/util/MemMapper/MemMapperWin.cpp
@@ -1,7 +1,5 @@
 #include "util/MemMapper/MemMapper.h"
 
-#if BOOST_OS_WINDOWS
-
 #include <Windows.h>
 
 namespace MemMapper
@@ -63,5 +61,3 @@ namespace MemMapper
 	}
 
 };
-
-#endif
\ No newline at end of file
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfo.cpp
@@ -0,0 +1,34 @@
+#include "util/SystemInfo/SystemInfo.h"
+
+uint64 ProcessorTime::work()
+{
+	return user + kernel;
+}
+
+uint64 ProcessorTime::total()
+{
+	return idle + user + kernel;
+}
+
+double ProcessorTime::Compare(ProcessorTime &last, ProcessorTime &now)
+{
+	auto dwork = now.work() - last.work();
+	auto dtotal = now.total() - last.total();
+
+	return (double)dwork / dtotal;
+}
+
+uint32 GetProcessorCount()
+{
+	return std::thread::hardware_concurrency();
+}
+
+void QueryProcTime(ProcessorTime &out)
+{
+	uint64 now, user, kernel;
+	QueryProcTime(now, user, kernel);
+
+	out.idle = now - (user + kernel);
+	out.kernel = kernel;
+	out.user = user;
+}
\ No newline at end of file
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfo.h
@@ -0,0 +1,17 @@
+#pragma once
+
+struct ProcessorTime
+{
+	uint64 idle{}, kernel{}, user{};
+	
+	uint64 work();
+	uint64 total();
+
+	static double Compare(ProcessorTime &last, ProcessorTime &now);
+};
+
+uint32 GetProcessorCount();
+uint64 QueryRamUsage();
+void QueryProcTime(uint64 &out_now, uint64 &out_user, uint64 &out_kernel);
+void QueryProcTime(ProcessorTime &out);
+void QueryCoreTimes(uint32 count, std::vector<ProcessorTime>& out);
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfoLinux.cpp
@@ -0,0 +1,48 @@
+#include "util/SystemInfo/SystemInfo.h"
+
+#include <unistd.h>
+
+uint64 QueryRamUsage()
+{
+	static long page_size = sysconf(_SC_PAGESIZE);
+	if (page_size == -1)
+	{
+		return 0;
+	}
+
+	std::ifstream file("/proc/self/statm");
+	if (file)
+	{
+		file.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
+		uint64 pages;
+		file >> pages;
+
+		return pages * page_size;
+	}
+	return 0;
+}
+
+void QueryCoreTimes(uint32 count, std::vector<ProcessorTime>& out)
+{
+	std::ifstream file("/proc/stat");
+	if (file)
+	{
+		file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
+
+		for (auto i = 0; i < out.size(); ++i)
+		{
+			uint64 user, nice, kernel, idle;
+			file.ignore(std::numeric_limits<std::streamsize>::max(), ' ');
+			file >> user >> nice >> kernel >> idle;
+			file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
+
+			out[i].idle = idle;
+			out[i].kernel = kernel;
+			out[i].user = user + nice;
+		}
+	}
+	else
+	{
+		for (auto i = 0; i < count; ++i) out[i] = { };
+	}
+}
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfoMac.cpp
@@ -0,0 +1,62 @@
+#include "util/SystemInfo/SystemInfo.h"
+
+#include <unistd.h>
+#include <sys/resource.h>
+#include <sys/sysctl.h>
+#include <sys/types.h>
+#include <mach/mach.h>
+
+#include <mach/processor_info.h>
+#include <mach/mach_host.h>
+#include <mach/kern_return.h>
+
+// borrowed from https://en.wikichip.org/wiki/resident_set_size#OS_X
+uint64 QueryRamUsage()
+{
+	mach_task_basic_info info;
+	mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
+	if (task_info(mach_task_self(), MACH_TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS)
+		return info.resident_size;
+	return 0;
+}
+
+// apple official documentation is non-existsent.
+// based on https://github.com/giampaolo/psutil/blob/master/psutil/_psutil_osx.c#L623
+void QueryCoreTimes(uint32 count, std::vector<ProcessorTime>& out)
+{
+	// initialize default
+	for (auto i = 0; i < out.size(); ++i)
+	{
+		out[i] = {};
+	}
+
+	natural_t cpu_count;
+	processor_info_array_t info_array;
+	mach_msg_type_number_t info_count;
+	kern_return_t error;
+
+	mach_port_t host_port = mach_host_self();
+	error = host_processor_info(host_port, PROCESSOR_CPU_LOAD_INFO, &cpu_count, &info_array, &info_count);
+	mach_port_deallocate(mach_task_self(), host_port);
+
+	if (error != KERN_SUCCESS)
+		return;
+
+	processor_cpu_load_info_data_t* cpuLoad = (processor_cpu_load_info_data_t*) info_array;
+
+	for (auto i = 0; i < cpu_count; ++i)
+	{
+		uint64 system = cpuLoad[i].cpu_ticks[CPU_STATE_SYSTEM];
+		uint64 user = cpuLoad[i].cpu_ticks[CPU_STATE_USER] + cpuLoad[i].cpu_ticks[CPU_STATE_NICE];
+		uint64 idle = cpuLoad[i].cpu_ticks[CPU_STATE_IDLE];
+
+		out[i].idle = idle;
+		out[i].kernel = system;
+		out[i].user = user;
+	}
+
+	int ret = vm_deallocate(mach_task_self(), (vm_address_t) info_array,
+						info_count * sizeof(int));
+	if (ret != KERN_SUCCESS)
+		cemuLog_force("vm_deallocate() failed");
+}
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfoStub.cpp
@@ -0,0 +1,21 @@
+#include "util/SystemInfo/SystemInfo.h"
+
+uint64 QueryRamUsage()
+{
+	return 0;
+}
+
+void QueryProcTime(uint64 &out_now, uint64 &out_user, uint64 &out_kernel)
+{
+	out_now = 0;
+	out_user = 0;
+	out_kernel = 0;
+}
+
+void QueryCoreTimes(uint32 count, std::vector<ProcessorTime>& out)
+{
+	for (auto i = 0; i < out.size(); ++i)
+	{
+		out[i] = { };
+	}
+}
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfoUnix.cpp
@@ -0,0 +1,15 @@
+#include "util/SystemInfo/SystemInfo.h"
+
+#include <sys/times.h>
+
+void QueryProcTime(uint64 &out_now, uint64 &out_user, uint64 &out_kernel)
+{
+	struct tms time_info;
+	clock_t clock_now = times(&time_info);
+	clock_t clock_user = time_info.tms_utime;
+	clock_t clock_kernel = time_info.tms_stime;
+	out_now = static_cast<uint64>(clock_now);
+	out_user = static_cast<uint64>(clock_user);
+	out_kernel = static_cast<uint64>(clock_kernel);
+}
+
--- /dev/null
+++ b/src/util/SystemInfo/SystemInfoWin.cpp
@@ -0,0 +1,68 @@
+#include "util/SystemInfo/SystemInfo.h"
+
+#include <Psapi.h>
+#include <winternl.h>
+#pragma comment(lib, "ntdll.lib")
+
+uint64 QueryRamUsage()
+{
+	PROCESS_MEMORY_COUNTERS pmc{};
+	pmc.cb = sizeof(pmc);
+	if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)))
+	{
+		return pmc.WorkingSetSize;
+	}
+	else
+	{
+		return 0;
+	}
+}
+
+void QueryProcTime(uint64 &out_now, uint64 &out_user, uint64 &out_kernel)
+{
+	FILETIME ftime, fkernel, fuser;
+	LARGE_INTEGER now, kernel, user;
+	GetSystemTimeAsFileTime(&ftime);
+	now.LowPart = ftime.dwLowDateTime;
+	now.HighPart = ftime.dwHighDateTime;
+
+	if (GetProcessTimes(GetCurrentProcess(), &ftime, &ftime, &fkernel, &fuser))
+	{	
+		kernel.LowPart = fkernel.dwLowDateTime;
+		kernel.HighPart = fkernel.dwHighDateTime;
+
+		user.LowPart = fuser.dwLowDateTime;
+		user.HighPart = fuser.dwHighDateTime;
+
+		out_now = now.QuadPart;
+		out_user = user.QuadPart;
+		out_kernel = kernel.QuadPart;
+	}
+	else
+	{
+		out_now = 0;
+		out_user = 0;
+		out_kernel = 0;
+	}
+}
+
+void QueryCoreTimes(uint32 count, std::vector<ProcessorTime>& out)
+{
+	std::vector<SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION> sppi(count);
+	if (NT_SUCCESS(NtQuerySystemInformation(SystemProcessorPerformanceInformation, sppi.data(), sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * count, nullptr)))
+	{
+		for (auto i = 0; i < out.size(); ++i)
+		{
+			out[i].idle = sppi[i].IdleTime.QuadPart;			
+			out[i].kernel = sppi[i].KernelTime.QuadPart;
+			out[i].user = sppi[i].UserTime.QuadPart;
+		}
+	}
+	else
+	{
+		for (auto i = 0; i < count; ++i)
+		{
+			out[i] = { };
+		}
+	}
+}