123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- // Copyright 2015 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "base/system/sys_info.h"
- #include <stddef.h>
- #include <stdint.h>
- #include <windows.h>
- #include <limits>
- #include "base/check.h"
- #include "base/files/file_path.h"
- #include "base/notreached.h"
- #include "base/numerics/safe_conversions.h"
- #include "base/process/process_metrics.h"
- #include "base/strings/string_util.h"
- #include "base/strings/stringprintf.h"
- #include "base/strings/sys_string_conversions.h"
- #include "base/strings/utf_string_conversions.h"
- #include "base/threading/scoped_blocking_call.h"
- #include "base/win/registry.h"
- #include "base/win/windows_version.h"
- namespace {
- uint64_t AmountOfMemory(DWORDLONG MEMORYSTATUSEX::*memory_field) {
- MEMORYSTATUSEX memory_info;
- memory_info.dwLength = sizeof(memory_info);
- if (!GlobalMemoryStatusEx(&memory_info)) {
- NOTREACHED();
- return 0;
- }
- return memory_info.*memory_field;
- }
- bool GetDiskSpaceInfo(const base::FilePath& path,
- int64_t* available_bytes,
- int64_t* total_bytes) {
- ULARGE_INTEGER available;
- ULARGE_INTEGER total;
- ULARGE_INTEGER free;
- if (!GetDiskFreeSpaceExW(path.value().c_str(), &available, &total, &free))
- return false;
- if (available_bytes) {
- *available_bytes = static_cast<int64_t>(available.QuadPart);
- if (*available_bytes < 0)
- *available_bytes = std::numeric_limits<int64_t>::max();
- }
- if (total_bytes) {
- *total_bytes = static_cast<int64_t>(total.QuadPart);
- if (*total_bytes < 0)
- *total_bytes = std::numeric_limits<int64_t>::max();
- }
- return true;
- }
- } // namespace
- namespace base {
- // static
- int SysInfo::NumberOfProcessors() {
- return win::OSInfo::GetInstance()->processors();
- }
- // static
- uint64_t SysInfo::AmountOfPhysicalMemoryImpl() {
- return AmountOfMemory(&MEMORYSTATUSEX::ullTotalPhys);
- }
- // static
- uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
- SystemMemoryInfoKB info;
- if (!GetSystemMemoryInfo(&info))
- return 0;
- return checked_cast<uint64_t>(info.avail_phys) * 1024;
- }
- // static
- uint64_t SysInfo::AmountOfVirtualMemory() {
- return AmountOfMemory(&MEMORYSTATUSEX::ullTotalVirtual);
- }
- // static
- int64_t SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
- base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
- base::BlockingType::MAY_BLOCK);
- int64_t available;
- if (!GetDiskSpaceInfo(path, &available, nullptr))
- return -1;
- return available;
- }
- // static
- int64_t SysInfo::AmountOfTotalDiskSpace(const FilePath& path) {
- base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
- base::BlockingType::MAY_BLOCK);
- int64_t total;
- if (!GetDiskSpaceInfo(path, nullptr, &total))
- return -1;
- return total;
- }
- std::string SysInfo::OperatingSystemName() {
- return "Windows NT";
- }
- // static
- std::string SysInfo::OperatingSystemVersion() {
- win::OSInfo* os_info = win::OSInfo::GetInstance();
- win::OSInfo::VersionNumber version_number = os_info->version_number();
- std::string version(StringPrintf("%d.%d.%d", version_number.major,
- version_number.minor, version_number.build));
- win::OSInfo::ServicePack service_pack = os_info->service_pack();
- if (service_pack.major != 0) {
- version += StringPrintf(" SP%d", service_pack.major);
- if (service_pack.minor != 0)
- version += StringPrintf(".%d", service_pack.minor);
- }
- return version;
- }
- // TODO: Implement OperatingSystemVersionComplete, which would include
- // patchlevel/service pack number.
- // See chrome/browser/feedback/feedback_util.h, FeedbackUtil::SetOSVersion.
- // static
- std::string SysInfo::OperatingSystemArchitecture() {
- win::OSInfo::WindowsArchitecture arch = win::OSInfo::GetArchitecture();
- switch (arch) {
- case win::OSInfo::X86_ARCHITECTURE:
- return "x86";
- case win::OSInfo::X64_ARCHITECTURE:
- return "x86_64";
- case win::OSInfo::IA64_ARCHITECTURE:
- return "ia64";
- case win::OSInfo::ARM64_ARCHITECTURE:
- return "arm64";
- default:
- return "";
- }
- }
- // static
- std::string SysInfo::CPUModelName() {
- return win::OSInfo::GetInstance()->processor_model_name();
- }
- // static
- size_t SysInfo::VMAllocationGranularity() {
- return win::OSInfo::GetInstance()->allocation_granularity();
- }
- // static
- void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
- int32_t* minor_version,
- int32_t* bugfix_version) {
- win::OSInfo* os_info = win::OSInfo::GetInstance();
- *major_version = static_cast<int32_t>(os_info->version_number().major);
- *minor_version = static_cast<int32_t>(os_info->version_number().minor);
- *bugfix_version = 0;
- }
- // static
- std::string ReadHardwareInfoFromRegistry(const wchar_t* reg_value_name) {
- // On some systems or VMs, the system information and some of the below
- // locations may be missing info. Attempt to find the info from the below
- // registry keys in the order provided.
- static const wchar_t* const kSystemInfoRegKeyPaths[] = {
- L"HARDWARE\\DESCRIPTION\\System\\BIOS",
- L"SYSTEM\\CurrentControlSet\\Control\\SystemInformation",
- L"SYSTEM\\HardwareConfig\\Current",
- };
- std::wstring value;
- for (const wchar_t* system_info_reg_key_path : kSystemInfoRegKeyPaths) {
- base::win::RegKey system_information_key;
- if (system_information_key.Open(HKEY_LOCAL_MACHINE,
- system_info_reg_key_path,
- KEY_READ) == ERROR_SUCCESS) {
- if ((system_information_key.ReadValue(reg_value_name, &value) ==
- ERROR_SUCCESS) &&
- !value.empty()) {
- break;
- }
- }
- }
- return base::SysWideToUTF8(value);
- }
- // static
- SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
- HardwareInfo info = {ReadHardwareInfoFromRegistry(L"SystemManufacturer"),
- SysInfo::HardwareModelName()};
- return info;
- }
- // static
- std::string SysInfo::HardwareModelName() {
- return ReadHardwareInfoFromRegistry(L"SystemProductName");
- }
- } // namespace base
|