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 : #include "base/logging.h"
22 : #include "base/string_util.h"
23 :
24 : namespace base {
25 :
26 0 : int SysInfo::NumberOfProcessors() {
27 : // It seems that sysconf returns the number of "logical" processors on both
28 : // mac and linux. So we get the number of "online logical" processors.
29 0 : static long res = sysconf(_SC_NPROCESSORS_ONLN);
30 0 : if (res == -1) {
31 0 : NOTREACHED();
32 0 : return 1;
33 : }
34 :
35 0 : return static_cast<int>(res);
36 : }
37 :
38 : // static
39 0 : int64 SysInfo::AmountOfPhysicalMemory() {
40 : // _SC_PHYS_PAGES is not part of POSIX and not available on OS X
41 : #if defined(OS_MACOSX)
42 : struct host_basic_info hostinfo;
43 : mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
44 : int result = host_info(mach_host_self(),
45 : HOST_BASIC_INFO,
46 : reinterpret_cast<host_info_t>(&hostinfo),
47 : &count);
48 : DCHECK_EQ(HOST_BASIC_INFO_COUNT, count);
49 : if (result != KERN_SUCCESS) {
50 : NOTREACHED();
51 : return 0;
52 : }
53 :
54 : return static_cast<int64>(hostinfo.max_mem);
55 : #else
56 0 : long pages = sysconf(_SC_PHYS_PAGES);
57 0 : long page_size = sysconf(_SC_PAGE_SIZE);
58 0 : if (pages == -1 || page_size == -1) {
59 0 : NOTREACHED();
60 0 : return 0;
61 : }
62 :
63 0 : return static_cast<int64>(pages) * page_size;
64 : #endif
65 : }
66 :
67 : // static
68 0 : int64 SysInfo::AmountOfFreeDiskSpace(const std::wstring& path) {
69 : #ifndef ANDROID
70 : struct statvfs stats;
71 0 : if (statvfs(WideToUTF8(path).c_str(), &stats) != 0) {
72 0 : return -1;
73 : }
74 0 : return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
75 : #else
76 : return -1;
77 : #endif
78 : }
79 :
80 : // static
81 0 : bool SysInfo::HasEnvVar(const wchar_t* var) {
82 0 : std::string var_utf8 = WideToUTF8(std::wstring(var));
83 0 : return getenv(var_utf8.c_str()) != NULL;
84 : }
85 :
86 : // static
87 0 : std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
88 0 : std::string var_utf8 = WideToUTF8(std::wstring(var));
89 0 : char* value = getenv(var_utf8.c_str());
90 0 : if (!value) {
91 0 : return L"";
92 : } else {
93 0 : return UTF8ToWide(value);
94 : }
95 : }
96 :
97 : // static
98 0 : std::string SysInfo::OperatingSystemName() {
99 : utsname info;
100 0 : if (uname(&info) < 0) {
101 0 : NOTREACHED();
102 0 : return "";
103 : }
104 0 : return std::string(info.sysname);
105 : }
106 :
107 : // static
108 0 : std::string SysInfo::OperatingSystemVersion() {
109 : utsname info;
110 0 : if (uname(&info) < 0) {
111 0 : NOTREACHED();
112 0 : return "";
113 : }
114 0 : return std::string(info.release);
115 : }
116 :
117 : // static
118 0 : std::string SysInfo::CPUArchitecture() {
119 : utsname info;
120 0 : if (uname(&info) < 0) {
121 0 : NOTREACHED();
122 0 : return "";
123 : }
124 0 : return std::string(info.machine);
125 : }
126 :
127 : // static
128 0 : void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
129 0 : NOTIMPLEMENTED();
130 0 : }
131 :
132 : // static
133 0 : int SysInfo::DisplayCount() {
134 0 : NOTIMPLEMENTED();
135 0 : return 1;
136 : }
137 :
138 : // static
139 0 : size_t SysInfo::VMAllocationGranularity() {
140 0 : return getpagesize();
141 : }
142 :
143 : } // namespace base
|