|
1 // Copyright (c) 2008 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. |
|
4 |
|
5 #include "base/sys_info.h" |
|
6 #include "base/basictypes.h" |
|
7 |
|
8 #include <errno.h> |
|
9 #include <string.h> |
|
10 #ifndef ANDROID |
|
11 #include <sys/statvfs.h> |
|
12 #endif |
|
13 #include <sys/utsname.h> |
|
14 #include <unistd.h> |
|
15 |
|
16 #if defined(OS_MACOSX) |
|
17 #include <mach/mach_host.h> |
|
18 #include <mach/mach_init.h> |
|
19 #endif |
|
20 |
|
21 #if defined(OS_NETBSD) |
|
22 #include <sys/param.h> |
|
23 #include <sys/sysctl.h> |
|
24 #endif |
|
25 |
|
26 #include "base/logging.h" |
|
27 #include "base/string_util.h" |
|
28 |
|
29 namespace base { |
|
30 |
|
31 int SysInfo::NumberOfProcessors() { |
|
32 // It seems that sysconf returns the number of "logical" processors on both |
|
33 // mac and linux. So we get the number of "online logical" processors. |
|
34 #ifdef _SC_NPROCESSORS_ONLN |
|
35 static long res = sysconf(_SC_NPROCESSORS_ONLN); |
|
36 #else |
|
37 static long res = 1; |
|
38 #endif |
|
39 if (res == -1) { |
|
40 NOTREACHED(); |
|
41 return 1; |
|
42 } |
|
43 |
|
44 return static_cast<int>(res); |
|
45 } |
|
46 |
|
47 // static |
|
48 int64_t SysInfo::AmountOfPhysicalMemory() { |
|
49 // _SC_PHYS_PAGES is not part of POSIX and not available on OS X |
|
50 #if defined(OS_MACOSX) |
|
51 struct host_basic_info hostinfo; |
|
52 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
|
53 int result = host_info(mach_host_self(), |
|
54 HOST_BASIC_INFO, |
|
55 reinterpret_cast<host_info_t>(&hostinfo), |
|
56 &count); |
|
57 DCHECK_EQ(HOST_BASIC_INFO_COUNT, count); |
|
58 if (result != KERN_SUCCESS) { |
|
59 NOTREACHED(); |
|
60 return 0; |
|
61 } |
|
62 |
|
63 return static_cast<int64_t>(hostinfo.max_mem); |
|
64 #elif defined(OS_NETBSD) |
|
65 int mib[2]; |
|
66 int rc; |
|
67 int64_t memSize; |
|
68 size_t len = sizeof(memSize); |
|
69 |
|
70 mib[0] = CTL_HW; |
|
71 mib[1] = HW_PHYSMEM64; |
|
72 rc = sysctl( mib, 2, &memSize, &len, NULL, 0 ); |
|
73 if (-1 != rc) { |
|
74 return memSize; |
|
75 } |
|
76 return 0; |
|
77 |
|
78 #else |
|
79 long pages = sysconf(_SC_PHYS_PAGES); |
|
80 long page_size = sysconf(_SC_PAGE_SIZE); |
|
81 if (pages == -1 || page_size == -1) { |
|
82 NOTREACHED(); |
|
83 return 0; |
|
84 } |
|
85 |
|
86 return static_cast<int64_t>(pages) * page_size; |
|
87 #endif |
|
88 } |
|
89 |
|
90 // static |
|
91 int64_t SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) { |
|
92 #ifndef ANDROID |
|
93 struct statvfs stats; |
|
94 if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) { |
|
95 return -1; |
|
96 } |
|
97 return static_cast<int64_t>(stats.f_bavail) * stats.f_frsize; |
|
98 #else |
|
99 return -1; |
|
100 #endif |
|
101 } |
|
102 |
|
103 // static |
|
104 bool SysInfo::HasEnvVar(const wchar_t* var) { |
|
105 std::string var_utf8 = WideToUTF8(std::wstring(var)); |
|
106 return getenv(var_utf8.c_str()) != NULL; |
|
107 } |
|
108 |
|
109 // static |
|
110 std::wstring SysInfo::GetEnvVar(const wchar_t* var) { |
|
111 std::string var_utf8 = WideToUTF8(std::wstring(var)); |
|
112 char* value = getenv(var_utf8.c_str()); |
|
113 if (!value) { |
|
114 return L""; |
|
115 } else { |
|
116 return UTF8ToWide(value); |
|
117 } |
|
118 } |
|
119 |
|
120 // static |
|
121 std::string SysInfo::OperatingSystemName() { |
|
122 utsname info; |
|
123 if (uname(&info) < 0) { |
|
124 NOTREACHED(); |
|
125 return ""; |
|
126 } |
|
127 return std::string(info.sysname); |
|
128 } |
|
129 |
|
130 // static |
|
131 std::string SysInfo::OperatingSystemVersion() { |
|
132 utsname info; |
|
133 if (uname(&info) < 0) { |
|
134 NOTREACHED(); |
|
135 return ""; |
|
136 } |
|
137 return std::string(info.release); |
|
138 } |
|
139 |
|
140 // static |
|
141 std::string SysInfo::CPUArchitecture() { |
|
142 utsname info; |
|
143 if (uname(&info) < 0) { |
|
144 NOTREACHED(); |
|
145 return ""; |
|
146 } |
|
147 return std::string(info.machine); |
|
148 } |
|
149 |
|
150 // static |
|
151 void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) { |
|
152 NOTIMPLEMENTED(); |
|
153 } |
|
154 |
|
155 // static |
|
156 int SysInfo::DisplayCount() { |
|
157 NOTIMPLEMENTED(); |
|
158 return 1; |
|
159 } |
|
160 |
|
161 // static |
|
162 size_t SysInfo::VMAllocationGranularity() { |
|
163 return getpagesize(); |
|
164 } |
|
165 |
|
166 } // namespace base |