From a6bc0f67f4a54ea2f94a55651aec1e2f2c71c32e Mon Sep 17 00:00:00 2001 From: ColumbusTech Date: Thu, 22 Nov 2018 22:12:43 +0300 Subject: [PATCH] Windows --- corinfo.h | 107 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 25 deletions(-) diff --git a/corinfo.h b/corinfo.h index da7fe8c..98ba10a 100644 --- a/corinfo.h +++ b/corinfo.h @@ -56,6 +56,7 @@ struct corinfo_cpu { uint32_t Count; //!< Number of CPU cores. uint32_t Frequency; //!< CPU frequency, in MHz. + uint32_t Family; uint32_t Model; @@ -215,7 +216,7 @@ static int __hdd_info(struct corinfo* info); int corinfo_GetInfo(struct corinfo* info) { if (info == NULL) return -1; - *info = (struct corinfo){0}; + *info = (struct corinfo) { 0 }; if (__cpu_info(info) == -1) return -1; if (__ram_info(info) == -1) return -1; @@ -228,13 +229,38 @@ static int __hdd_info(struct corinfo* info); #ifdef _WIN32 - int corinfo_GetInfo(struct corinfo* info) + int __cpu_info(struct corinfo* info) { - if (info == NULL) return -1; - __null_corinfo(info); - SYSTEM_INFO sys; - MEMORYSTATUSEX mem; + GetSystemInfo(&sys); + + info->Cpu.Count = sys.dwNumberOfProcessors; + + int cpu[4]; + + __cpuid(cpu, 0x00000000); + + for (int i = 0; i < 4; i++) + { + info->Cpu.Vendor[0 + i] = (cpu[1] >> (i * 8)) & 0xFF; + info->Cpu.Vendor[4 + i] = (cpu[3] >> (i * 8)) & 0xFF; + info->Cpu.Vendor[8 + i] = (cpu[2] >> (i * 8)) & 0xFF; + } + + __cpuid(cpu, 0x80000002); memcpy(info->Cpu.Name + 0, cpu, 16); + __cpuid(cpu, 0x80000003); memcpy(info->Cpu.Name + 16, cpu, 16); + __cpuid(cpu, 0x80000004); memcpy(info->Cpu.Name + 32, cpu, 16); + + __cpuid(cpu, 0x00000001); + info->Cpu.Family = ((cpu[0] >> 8) & 0xF) + ((cpu[0] >> 20) & 0xF); + info->Cpu.Model = ((cpu[0] >> 4) & 0xF) + (((cpu[0] >> 16) & 0xF) << 4); + info->Cpu.MMX = (cpu[3] >> 23) & 0x1; + info->Cpu.SSE = (cpu[3] >> 25) & 0x1; + info->Cpu.SSE2 = (cpu[3] >> 26) & 0x1; + info->Cpu.SSE3 = (cpu[2] >> 0) & 0x1; + info->Cpu.SSE41 = (cpu[2] >> 19) & 0x1; + info->Cpu.SSE42 = (cpu[2] >> 20) & 0x1; + info->Cpu.AVX = (cpu[2] >> 28) & 0x1; struct { @@ -246,33 +272,64 @@ static int __hdd_info(struct corinfo* info); ULONG CurrentIdleState; } PROCESSOR_POWER_INFORMATION, *PPI; - mem.dwLength = sizeof(mem); - - GetSystemInfo(&sys); - GlobalMemoryStatusEx(&mem); - BYTE* buf = (BYTE*)malloc(sizeof(PROCESSOR_POWER_INFORMATION) * 4); if (buf == NULL) return -1; - CallNtPowerInformation(ProcessorInformation, NULL, 0, buf, sizeof(PROCESSOR_POWER_INFORMATION) * sys.dwNumberOfProcessors); - PPI = buf; - int cpu[4]; - // CPUID 0x00 code returns in EAX maximum CPUID code and vendor string in EBX, EDX, ECX. - __cpuid(cpu, 0x00000000); - __vendor_string(cpu, info->VendorString); - __brand_string(cpu, info->BrandString); - __extensions(cpu, info); + if (PPI != NULL) info->Cpu.Frequency = PPI->MaxMhz; + if (buf) free(buf); - info->CpuCount = sys.dwNumberOfProcessors; - info->CpuFrequency = PPI->MaxMhz; + return 0; + } - info->RamSize = mem.ullTotalPhys / 1024; - info->RamFree = mem.ullAvailPhys / 1024; - info->RamUsage = mem.dwMemoryLoad; + int __ram_info(struct corinfo* info) + { + MEMORYSTATUSEX mem; + mem.dwLength = sizeof(MEMORYSTATUSEX); + GlobalMemoryStatusEx(&mem); - free(buf); + info->Ram.Total = mem.ullTotalPhys / 1024; + info->Ram.Free = mem.ullAvailPhys / 1024; + + if (info->Ram.Total != 0) + { + info->Ram.Usage = 100 - (info->Ram.Free / (float)info->Ram.Total) * 100; + } + + return 0; + } + + int __hdd_info(struct corinfo* info) + { + ULARGE_INTEGER free_bytes_avail; + ULARGE_INTEGER total_bytes; + ULARGE_INTEGER total_free_bytes; + + if (!GetDiskFreeSpaceEx(NULL, &free_bytes_avail, &total_bytes, &total_free_bytes)) + { + return -1; + } + + info->Hdd.Total = total_bytes.QuadPart / 1024; + info->Hdd.Free = free_bytes_avail.QuadPart / 1024; + + if (info->Hdd.Total != 0) + { + info->Hdd.Usage = 100 - (info->Hdd.Free / (double)info->Hdd.Total) * 100; + } + + return 0; + } + + int corinfo_GetInfo(struct corinfo* info) + { + if (info == NULL) return -1; + *info = (struct corinfo) { 0 }; + + if (__cpu_info(info) == -1) return -1; + if (__ram_info(info) == -1) return -1; + if (__hdd_info(info) == -1) return -1; return 0; }