1 // Copyright (c) 2012 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"
10 #include "base/files/file.h"
11 #include "base/files/file_path.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/threading/thread_restrictions.h"
16 #include "base/win/windows_version.h"
20 int64
AmountOfMemory(DWORDLONG
MEMORYSTATUSEX::* memory_field
) {
21 MEMORYSTATUSEX memory_info
;
22 memory_info
.dwLength
= sizeof(memory_info
);
23 if (!GlobalMemoryStatusEx(&memory_info
)) {
28 int64 rv
= static_cast<int64
>(memory_info
.*memory_field
);
29 return rv
< 0 ? kint64max
: rv
;
37 int SysInfo::NumberOfProcessors() {
38 return win::OSInfo::GetInstance()->processors();
42 int64
SysInfo::AmountOfPhysicalMemory() {
43 return AmountOfMemory(&MEMORYSTATUSEX::ullTotalPhys
);
47 int64
SysInfo::AmountOfAvailablePhysicalMemory() {
48 return AmountOfMemory(&MEMORYSTATUSEX::ullAvailPhys
);
52 int64
SysInfo::AmountOfVirtualMemory() {
57 int64
SysInfo::AmountOfFreeDiskSpace(const FilePath
& path
) {
58 ThreadRestrictions::AssertIOAllowed();
60 ULARGE_INTEGER available
, total
, free
;
61 if (!GetDiskFreeSpaceExW(path
.value().c_str(), &available
, &total
, &free
))
64 int64 rv
= static_cast<int64
>(available
.QuadPart
);
65 return rv
< 0 ? kint64max
: rv
;
68 bool SysInfo::HasSeekPenalty(const FilePath
& path
, bool* has_seek_penalty
) {
69 ThreadRestrictions::AssertIOAllowed();
71 DCHECK(path
.IsAbsolute());
72 DCHECK(has_seek_penalty
);
74 // TODO(dbeam): Vista, XP support.
75 if (win::GetVersion() < win::VERSION_WIN7
)
78 std::vector
<FilePath::StringType
> components
;
79 path
.GetComponents(&components
);
81 File
drive(FilePath(L
"\\\\.\\" + components
[0]), File::FLAG_OPEN
);
85 STORAGE_PROPERTY_QUERY query
= {};
86 query
.QueryType
= PropertyStandardQuery
;
87 query
.PropertyId
= StorageDeviceSeekPenaltyProperty
;
89 DEVICE_SEEK_PENALTY_DESCRIPTOR result
;
92 BOOL success
= DeviceIoControl(drive
.GetPlatformFile(),
93 IOCTL_STORAGE_QUERY_PROPERTY
,
94 &query
, sizeof(query
),
95 &result
, sizeof(result
),
98 if (success
== FALSE
|| bytes_returned
< sizeof(result
))
101 *has_seek_penalty
= result
.IncursSeekPenalty
!= FALSE
;
106 std::string
SysInfo::OperatingSystemName() {
111 std::string
SysInfo::OperatingSystemVersion() {
112 win::OSInfo
* os_info
= win::OSInfo::GetInstance();
113 win::OSInfo::VersionNumber version_number
= os_info
->version_number();
114 std::string
version(StringPrintf("%d.%d", version_number
.major
,
115 version_number
.minor
));
116 win::OSInfo::ServicePack service_pack
= os_info
->service_pack();
117 if (service_pack
.major
!= 0) {
118 version
+= StringPrintf(" SP%d", service_pack
.major
);
119 if (service_pack
.minor
!= 0)
120 version
+= StringPrintf(".%d", service_pack
.minor
);
125 // TODO: Implement OperatingSystemVersionComplete, which would include
126 // patchlevel/service pack number.
127 // See chrome/browser/feedback/feedback_util.h, FeedbackUtil::SetOSVersion.
130 std::string
SysInfo::OperatingSystemArchitecture() {
131 win::OSInfo::WindowsArchitecture arch
=
132 win::OSInfo::GetInstance()->architecture();
134 case win::OSInfo::X86_ARCHITECTURE
:
136 case win::OSInfo::X64_ARCHITECTURE
:
138 case win::OSInfo::IA64_ARCHITECTURE
:
146 std::string
SysInfo::CPUModelName() {
147 return win::OSInfo::GetInstance()->processor_model_name();
151 size_t SysInfo::VMAllocationGranularity() {
152 return win::OSInfo::GetInstance()->allocation_granularity();
156 void SysInfo::OperatingSystemVersionNumbers(int32
* major_version
,
157 int32
* minor_version
,
158 int32
* bugfix_version
) {
159 win::OSInfo
* os_info
= win::OSInfo::GetInstance();
160 *major_version
= os_info
->version_number().major
;
161 *minor_version
= os_info
->version_number().minor
;