aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/system.cpp
blob: c3658bda014e8be3a5f4d80afb4873aa43c70653 (plain) (blame)
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zencore/system.h"

#include <zencore/compactbinarybuilder.h>
#include <zencore/except.h>
#include <zencore/memory/memory.h>
#include <zencore/string.h>

#include <mutex>

#if ZEN_PLATFORM_WINDOWS
#	include <zencore/windows.h>

ZEN_THIRD_PARTY_INCLUDES_START
#	include <iphlpapi.h>
#	include <winsock2.h>
#	include <pdh.h>
#	pragma comment(lib, "pdh.lib")
ZEN_THIRD_PARTY_INCLUDES_END
#elif ZEN_PLATFORM_LINUX
#	include <sys/utsname.h>
#	include <unistd.h>
#elif ZEN_PLATFORM_MAC
#	include <unistd.h>
#	include <mach/mach.h>
#	include <mach/mach_host.h>
#	include <sys/types.h>
#	include <sys/sysctl.h>
#endif

namespace zen {

using namespace std::literals;

int g_FakeCpuCount = 0;

void
SetCpuCountForReporting(int FakeCpuCount)
{
	g_FakeCpuCount = FakeCpuCount;
}

#if ZEN_PLATFORM_WINDOWS
std::string
GetMachineName()
{
	WCHAR NameBuffer[256];
	DWORD dwNameSize = 255;
	BOOL  Success	 = GetComputerNameW(NameBuffer, &dwNameSize);

	if (Success)
	{
		return WideToUtf8(NameBuffer);
	}

	return {};
}

SystemMetrics
GetSystemMetrics()
{
	SYSTEM_INFO SysInfo{};
	GetSystemInfo(&SysInfo);

	SystemMetrics Metrics;

	Metrics.LogicalProcessorCount = SysInfo.dwNumberOfProcessors;

	// Determine physical core count

	{
		DWORD BufferSize = 0;
		BOOL  Result	 = GetLogicalProcessorInformationEx(RelationAll, nullptr, &BufferSize);
		if (int32_t Error = GetLastError(); Error != ERROR_INSUFFICIENT_BUFFER)
		{
			ThrowSystemError(Error, "Failed to get buffer size for logical processor information");
		}

		PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX Buffer = (PSYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX)Memory::Alloc(BufferSize);

		Result = GetLogicalProcessorInformationEx(RelationAll, Buffer, &BufferSize);
		if (!Result)
		{
			Memory::Free(Buffer);
			throw std::runtime_error("Failed to get logical processor information");
		}

		DWORD ProcessorPkgCount		= 0;
		DWORD ProcessorCoreCount	= 0;
		DWORD LogicalProcessorCount = 0;

		BYTE*		Ptr = reinterpret_cast<BYTE*>(Buffer);
		BYTE* const End = Ptr + BufferSize;
		while (Ptr < End)
		{
			const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX& Slpi = *reinterpret_cast<const SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX*>(Ptr);
			if (Slpi.Relationship == RelationProcessorCore)
			{
				++ProcessorCoreCount;

				// Count logical processors (threads) across all processor groups for this core.
				// Each core entry lists one GROUP_AFFINITY per group it spans; each set bit
				// in the Mask represents one logical processor (HyperThreading sibling).
				for (WORD g = 0; g < Slpi.Processor.GroupCount; ++g)
				{
					LogicalProcessorCount += static_cast<DWORD>(__popcnt64(Slpi.Processor.GroupMask[g].Mask));
				}
			}
			else if (Slpi.Relationship == RelationProcessorPackage)
			{
				++ProcessorPkgCount;
			}
			Ptr += Slpi.Size;
		}

		Metrics.CoreCount			  = ProcessorCoreCount;
		Metrics.CpuCount			  = ProcessorPkgCount;
		Metrics.LogicalProcessorCount = LogicalProcessorCount;

		Memory::Free(Buffer);
	}

	// Query memory status

	{
		MEMORYSTATUSEX MemStatus{.dwLength = sizeof(MEMORYSTATUSEX)};
		GlobalMemoryStatusEx(&MemStatus);

		Metrics.SystemMemoryMiB		  = MemStatus.ullTotalPhys / 1024 / 1024;
		Metrics.AvailSystemMemoryMiB  = MemStatus.ullAvailPhys / 1024 / 1024;
		Metrics.VirtualMemoryMiB	  = MemStatus.ullTotalVirtual / 1024 / 1024;
		Metrics.AvailVirtualMemoryMiB = MemStatus.ullAvailVirtual / 1024 / 1024;
		Metrics.PageFileMiB			  = MemStatus.ullTotalPageFile / 1024 / 1024;
		Metrics.AvailPageFileMiB	  = MemStatus.ullAvailPageFile / 1024 / 1024;
	}

	return Metrics;
}
#elif ZEN_PLATFORM_LINUX
std::string
GetMachineName()
{
	static char Result[256] = "";
	if (!Result[0])
	{
		struct utsname name;
		const char*	   SysName = name.nodename;
		if (uname(&name))
		{
			SysName = "Unix Computer";
		}

		strcpy(Result, SysName);
	}
	return Result;
}

SystemMetrics
GetSystemMetrics()
{
	SystemMetrics Metrics;

	// Get processor information
	long NumProcessors = sysconf(_SC_NPROCESSORS_ONLN);
	if (NumProcessors > 0)
	{
		Metrics.LogicalProcessorCount = static_cast<uint32_t>(NumProcessors);
	}

	// On Linux, we approximate core count from logical processor count
	// A more accurate implementation would parse /proc/cpuinfo
	Metrics.CoreCount = Metrics.LogicalProcessorCount;
	Metrics.CpuCount  = 1;	// Default to 1 CPU package

	// Parse /proc/cpuinfo for more accurate core/CPU info
	if (FILE* CpuInfo = fopen("/proc/cpuinfo", "r"))
	{
		char Line[1024];
		int	 PhysicalIds = 0;
		int	 CoreIds	 = 0;

		while (fgets(Line, sizeof(Line), CpuInfo))
		{
			if (strncmp(Line, "physical id", 11) == 0)
			{
				int Id;
				if (sscanf(Line, "physical id\t: %d", &Id) == 1)
				{
					if (Id + 1 > PhysicalIds)
					{
						PhysicalIds = Id + 1;
					}
				}
			}
			else if (strncmp(Line, "cpu cores", 9) == 0)
			{
				sscanf(Line, "cpu cores\t: %d", &CoreIds);
			}
		}
		fclose(CpuInfo);

		if (PhysicalIds > 0)
		{
			Metrics.CpuCount = PhysicalIds;
		}
		if (CoreIds > 0)
		{
			Metrics.CoreCount = CoreIds * Metrics.CpuCount;
		}
	}

	// Get memory information
	long Pages		= sysconf(_SC_PHYS_PAGES);
	long PageSize	= sysconf(_SC_PAGE_SIZE);
	long AvailPages = sysconf(_SC_AVPHYS_PAGES);

	if (Pages > 0 && PageSize > 0)
	{
		Metrics.SystemMemoryMiB		 = (Pages * PageSize) / 1024 / 1024;
		Metrics.AvailSystemMemoryMiB = (AvailPages * PageSize) / 1024 / 1024;
	}

	// Virtual memory is not directly available via sysconf
	// Set to system memory as a reasonable default
	Metrics.VirtualMemoryMiB	  = Metrics.SystemMemoryMiB;
	Metrics.AvailVirtualMemoryMiB = Metrics.AvailSystemMemoryMiB;

	// Parse /proc/meminfo for swap/page file information
	Metrics.PageFileMiB		 = 0;
	Metrics.AvailPageFileMiB = 0;

	if (FILE* MemInfo = fopen("/proc/meminfo", "r"))
	{
		char Line[256];
		long SwapTotal = 0;
		long SwapFree  = 0;

		while (fgets(Line, sizeof(Line), MemInfo))
		{
			if (strncmp(Line, "SwapTotal:", 10) == 0)
			{
				sscanf(Line, "SwapTotal: %ld kB", &SwapTotal);
			}
			else if (strncmp(Line, "SwapFree:", 9) == 0)
			{
				sscanf(Line, "SwapFree: %ld kB", &SwapFree);
			}
		}
		fclose(MemInfo);

		if (SwapTotal > 0)
		{
			Metrics.PageFileMiB		 = SwapTotal / 1024;
			Metrics.AvailPageFileMiB = SwapFree / 1024;
		}
	}

	return Metrics;
}
#elif ZEN_PLATFORM_MAC
std::string
GetMachineName()
{
	static char Result[256] = "";

	if (!Result[0])
	{
		gethostname(Result, sizeof(Result));
	}
	return Result;
}

SystemMetrics
GetSystemMetrics()
{
	SystemMetrics Metrics;

	// Get processor information
	size_t Size = sizeof(Metrics.LogicalProcessorCount);
	sysctlbyname("hw.logicalcpu", &Metrics.LogicalProcessorCount, &Size, nullptr, 0);

	uint32_t PhysicalCpu = 0;
	Size				 = sizeof(PhysicalCpu);
	sysctlbyname("hw.physicalcpu", &PhysicalCpu, &Size, nullptr, 0);
	Metrics.CoreCount = PhysicalCpu;

	uint32_t Packages = 0;
	Size			  = sizeof(Packages);
	sysctlbyname("hw.packages", &Packages, &Size, nullptr, 0);
	Metrics.CpuCount = Packages > 0 ? Packages : 1;

	// Get memory information
	uint64_t MemSize = 0;
	Size			 = sizeof(MemSize);
	sysctlbyname("hw.memsize", &MemSize, &Size, nullptr, 0);
	Metrics.SystemMemoryMiB = MemSize / 1024 / 1024;

	// Get available memory using vm_stat
	vm_size_t PageSize = 0;
	host_page_size(mach_host_self(), &PageSize);

	vm_statistics64_data_t VmStats;
	mach_msg_type_number_t InfoCount = sizeof(VmStats) / sizeof(natural_t);
	host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t)&VmStats, &InfoCount);

	uint64_t FreeMemory			 = (uint64_t)(VmStats.free_count + VmStats.inactive_count) * PageSize;
	Metrics.AvailSystemMemoryMiB = FreeMemory / 1024 / 1024;

	// Virtual memory on macOS is essentially unlimited, set to a large value
	Metrics.VirtualMemoryMiB	  = Metrics.SystemMemoryMiB * 16;
	Metrics.AvailVirtualMemoryMiB = Metrics.VirtualMemoryMiB;

	// macOS doesn't use traditional page files, use swap space
	xsw_usage SwapUsage;
	Size = sizeof(SwapUsage);
	sysctlbyname("vm.swapusage", &SwapUsage, &Size, nullptr, 0);
	Metrics.PageFileMiB		 = SwapUsage.xsu_total / 1024 / 1024;
	Metrics.AvailPageFileMiB = (SwapUsage.xsu_total - SwapUsage.xsu_used) / 1024 / 1024;

	return Metrics;
}
#else
#	error "Unknown platform"
#endif

ExtendedSystemMetrics
ApplyReportingOverrides(ExtendedSystemMetrics Metrics)
{
	if (g_FakeCpuCount)
	{
		Metrics.CoreCount			  = g_FakeCpuCount;
		Metrics.LogicalProcessorCount = g_FakeCpuCount;
	}
	return Metrics;
}

SystemMetrics
GetSystemMetricsForReporting()
{
	SystemMetrics Sm = GetSystemMetrics();

	if (g_FakeCpuCount)
	{
		Sm.CoreCount			 = g_FakeCpuCount;
		Sm.LogicalProcessorCount = g_FakeCpuCount;
	}

	return Sm;
}

///////////////////////////////////////////////////////////////////////////
// SystemMetricsTracker
///////////////////////////////////////////////////////////////////////////

// Per-platform CPU sampling helper. Called with m_Mutex held.

#if ZEN_PLATFORM_WINDOWS || ZEN_PLATFORM_LINUX

// Samples CPU usage by reading /proc/stat. Used natively on Linux and as a
// Wine fallback on Windows (where /proc/stat is accessible via the Z: drive).
struct ProcStatCpuSampler
{
	const char*	  Path		  = "/proc/stat";
	unsigned long PrevUser	  = 0;
	unsigned long PrevNice	  = 0;
	unsigned long PrevSystem  = 0;
	unsigned long PrevIdle	  = 0;
	unsigned long PrevIoWait  = 0;
	unsigned long PrevIrq	  = 0;
	unsigned long PrevSoftIrq = 0;

	explicit ProcStatCpuSampler(const char* InPath = "/proc/stat") : Path(InPath) {}

	float Sample()
	{
		float CpuUsage = 0.0f;

		if (FILE* Stat = fopen(Path, "r"))
		{
			char		  Line[256];
			unsigned long User, Nice, System, Idle, IoWait, Irq, SoftIrq;

			if (fgets(Line, sizeof(Line), Stat))
			{
				if (sscanf(Line, "cpu %lu %lu %lu %lu %lu %lu %lu", &User, &Nice, &System, &Idle, &IoWait, &Irq, &SoftIrq) == 7)
				{
					unsigned long TotalDelta = (User + Nice + System + Idle + IoWait + Irq + SoftIrq) -
											   (PrevUser + PrevNice + PrevSystem + PrevIdle + PrevIoWait + PrevIrq + PrevSoftIrq);
					unsigned long IdleDelta = Idle - PrevIdle;

					if (TotalDelta > 0)
					{
						CpuUsage = 100.0f * (TotalDelta - IdleDelta) / TotalDelta;
					}

					PrevUser	= User;
					PrevNice	= Nice;
					PrevSystem	= System;
					PrevIdle	= Idle;
					PrevIoWait	= IoWait;
					PrevIrq		= Irq;
					PrevSoftIrq = SoftIrq;
				}
			}
			fclose(Stat);
		}

		return CpuUsage;
	}
};

#endif

#if ZEN_PLATFORM_WINDOWS

struct CpuSampler
{
	PDH_HQUERY		   QueryHandle		 = nullptr;
	PDH_HCOUNTER	   CounterHandle	 = nullptr;
	bool			   HasPreviousSample = false;
	bool			   IsWine			 = false;
	ProcStatCpuSampler ProcStat{"Z:\\proc\\stat"};

	CpuSampler()
	{
		IsWine = zen::windows::IsRunningOnWine();

		if (!IsWine)
		{
			if (PdhOpenQueryW(nullptr, 0, &QueryHandle) == ERROR_SUCCESS)
			{
				if (PdhAddEnglishCounterW(QueryHandle, L"\\Processor(_Total)\\% Processor Time", 0, &CounterHandle) != ERROR_SUCCESS)
				{
					CounterHandle = nullptr;
				}
			}
		}
	}

	~CpuSampler()
	{
		if (QueryHandle)
		{
			PdhCloseQuery(QueryHandle);
		}
	}

	float Sample()
	{
		if (IsWine)
		{
			return ProcStat.Sample();
		}

		if (!QueryHandle || !CounterHandle)
		{
			return 0.0f;
		}

		PdhCollectQueryData(QueryHandle);

		if (!HasPreviousSample)
		{
			HasPreviousSample = true;
			return 0.0f;
		}

		PDH_FMT_COUNTERVALUE CounterValue;
		if (PdhGetFormattedCounterValue(CounterHandle, PDH_FMT_DOUBLE, nullptr, &CounterValue) == ERROR_SUCCESS)
		{
			return static_cast<float>(CounterValue.doubleValue);
		}

		return 0.0f;
	}
};

#elif ZEN_PLATFORM_LINUX

struct CpuSampler
{
	ProcStatCpuSampler ProcStat;

	float Sample() { return ProcStat.Sample(); }
};

#elif ZEN_PLATFORM_MAC

struct CpuSampler
{
	unsigned long PrevTotalTicks = 0;
	unsigned long PrevIdleTicks	 = 0;

	float Sample()
	{
		float CpuUsage = 0.0f;

		host_cpu_load_info_data_t CpuLoad;
		mach_msg_type_number_t	  Count = sizeof(CpuLoad) / sizeof(natural_t);
		if (host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)&CpuLoad, &Count) == KERN_SUCCESS)
		{
			unsigned long TotalTicks = 0;
			for (int i = 0; i < CPU_STATE_MAX; ++i)
			{
				TotalTicks += CpuLoad.cpu_ticks[i];
			}
			unsigned long IdleTicks = CpuLoad.cpu_ticks[CPU_STATE_IDLE];

			unsigned long TotalDelta = TotalTicks - PrevTotalTicks;
			unsigned long IdleDelta	 = IdleTicks - PrevIdleTicks;

			if (TotalDelta > 0 && PrevTotalTicks > 0)
			{
				CpuUsage = 100.0f * (TotalDelta - IdleDelta) / TotalDelta;
			}

			PrevTotalTicks = TotalTicks;
			PrevIdleTicks  = IdleTicks;
		}

		return CpuUsage;
	}
};

#endif

struct SystemMetricsTracker::Impl
{
	using Clock = std::chrono::steady_clock;

	std::mutex				  Mutex;
	CpuSampler				  Sampler;
	float					  CachedCpuPercent = 0.0f;
	Clock::time_point		  NextSampleTime   = Clock::now();
	std::chrono::milliseconds MinInterval;

	explicit Impl(std::chrono::milliseconds InMinInterval) : MinInterval(InMinInterval) {}

	float SampleCpu()
	{
		const auto Now = Clock::now();
		if (Now >= NextSampleTime)
		{
			CachedCpuPercent = Sampler.Sample();
			NextSampleTime	 = Now + MinInterval;
		}
		return CachedCpuPercent;
	}
};

SystemMetricsTracker::SystemMetricsTracker(std::chrono::milliseconds MinInterval) : m_Impl(std::make_unique<Impl>(MinInterval))
{
}

SystemMetricsTracker::~SystemMetricsTracker() = default;

ExtendedSystemMetrics
SystemMetricsTracker::Query()
{
	ExtendedSystemMetrics Metrics;
	static_cast<SystemMetrics&>(Metrics) = GetSystemMetrics();

	std::lock_guard Lock(m_Impl->Mutex);
	Metrics.CpuUsagePercent = m_Impl->SampleCpu();
	return Metrics;
}

///////////////////////////////////////////////////////////////////////////

std::string_view
GetOperatingSystemName()
{
	return ZEN_PLATFORM_NAME;
}

std::string_view
GetCpuName()
{
#if ZEN_ARCH_X64
	return "x64"sv;
#elif ZEN_ARCH_ARM64
	return "arm64"sv;
#endif
}

void
Describe(const SystemMetrics& Metrics, CbWriter& Writer)
{
	Writer << "cpu_count" << Metrics.CpuCount << "core_count" << Metrics.CoreCount << "lp_count" << Metrics.LogicalProcessorCount
		   << "total_memory_mb" << Metrics.SystemMemoryMiB << "avail_memory_mb" << Metrics.AvailSystemMemoryMiB << "total_virtual_mb"
		   << Metrics.VirtualMemoryMiB << "avail_virtual_mb" << Metrics.AvailVirtualMemoryMiB << "total_pagefile_mb" << Metrics.PageFileMiB
		   << "avail_pagefile_mb" << Metrics.AvailPageFileMiB;
}

void
Describe(const ExtendedSystemMetrics& Metrics, CbWriter& Writer)
{
	Describe(static_cast<const SystemMetrics&>(Metrics), Writer);
	Writer << "cpu_usage_percent" << Metrics.CpuUsagePercent;
}

}  // namespace zen