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