1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/sys_info.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/process/process_metrics.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/threading/thread_restrictions.h"
18 #include "base/win/windows_version.h"
22 int64_t AmountOfMemory(DWORDLONG MEMORYSTATUSEX::*memory_field) {
23 MEMORYSTATUSEX memory_info;
24 memory_info.dwLength = sizeof(memory_info);
25 if (!GlobalMemoryStatusEx(&memory_info)) {
30 int64_t rv = static_cast<int64_t>(memory_info.*memory_field);
31 return rv < 0 ? std::numeric_limits<int64_t>::max() : rv;
34 bool GetDiskSpaceInfo(const base::FilePath& path,
35 int64_t* available_bytes,
36 int64_t* total_bytes) {
37 ULARGE_INTEGER available;
40 if (!GetDiskFreeSpaceExW(path.value().c_str(), &available, &total, &free))
43 if (available_bytes) {
44 *available_bytes = static_cast<int64_t>(available.QuadPart);
45 if (*available_bytes < 0)
46 *available_bytes = std::numeric_limits<int64_t>::max();
49 *total_bytes = static_cast<int64_t>(total.QuadPart);
51 *total_bytes = std::numeric_limits<int64_t>::max();
61 int SysInfo::NumberOfProcessors() {
62 return win::OSInfo::GetInstance()->processors();
66 int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
67 return AmountOfMemory(&MEMORYSTATUSEX::ullTotalPhys);
71 int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
72 SystemMemoryInfoKB info;
73 if (!GetSystemMemoryInfo(&info))
75 return static_cast<int64_t>(info.avail_phys) * 1024;
79 int64_t SysInfo::AmountOfVirtualMemory() {
80 return AmountOfMemory(&MEMORYSTATUSEX::ullTotalVirtual);
84 int64_t SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
85 AssertBlockingAllowed();
88 if (!GetDiskSpaceInfo(path, &available, nullptr))
94 int64_t SysInfo::AmountOfTotalDiskSpace(const FilePath& path) {
95 AssertBlockingAllowed();
98 if (!GetDiskSpaceInfo(path, nullptr, &total))
103 std::string SysInfo::OperatingSystemName() {
108 std::string SysInfo::OperatingSystemVersion() {
109 win::OSInfo* os_info = win::OSInfo::GetInstance();
110 win::OSInfo::VersionNumber version_number = os_info->version_number();
111 std::string version(StringPrintf("%d.%d.%d", version_number.major,
112 version_number.minor,
113 version_number.build));
114 win::OSInfo::ServicePack service_pack = os_info->service_pack();
115 if (service_pack.major != 0) {
116 version += StringPrintf(" SP%d", service_pack.major);
117 if (service_pack.minor != 0)
118 version += StringPrintf(".%d", service_pack.minor);
123 // TODO: Implement OperatingSystemVersionComplete, which would include
124 // patchlevel/service pack number.
125 // See chrome/browser/feedback/feedback_util.h, FeedbackUtil::SetOSVersion.
128 std::string SysInfo::OperatingSystemArchitecture() {
129 win::OSInfo::WindowsArchitecture arch =
130 win::OSInfo::GetInstance()->architecture();
132 case win::OSInfo::X86_ARCHITECTURE:
134 case win::OSInfo::X64_ARCHITECTURE:
136 case win::OSInfo::IA64_ARCHITECTURE:
144 std::string SysInfo::CPUModelName() {
145 return win::OSInfo::GetInstance()->processor_model_name();
149 size_t SysInfo::VMAllocationGranularity() {
150 return win::OSInfo::GetInstance()->allocation_granularity();
154 void SysInfo::OperatingSystemVersionNumbers(int32_t* major_version,
155 int32_t* minor_version,
156 int32_t* bugfix_version) {
157 win::OSInfo* os_info = win::OSInfo::GetInstance();
158 *major_version = os_info->version_number().major;
159 *minor_version = os_info->version_number().minor;