blob: ffbc204b740652434a6e8ebe36902e2e3c83c6eb [file] [log] [blame]
[email protected]2a758d612008-09-17 10:09:391// 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
7#include <errno.h>
8#include <string.h>
[email protected]0e91dd22008-09-18 12:34:249#include <sys/statvfs.h>
[email protected]05f9b682008-09-29 22:18:0110#include <sys/utsname.h>
[email protected]2a758d612008-09-17 10:09:3911#include <unistd.h>
12
[email protected]4075c932009-06-05 01:21:0913#if defined(OS_OPENBSD)
14#include <sys/param.h>
15#include <sys/sysctl.h>
16#endif
17
[email protected]047a03f2009-10-07 02:10:2018#include "base/basictypes.h"
19#include "base/file_util.h"
[email protected]2a758d612008-09-17 10:09:3920#include "base/logging.h"
[email protected]047a03f2009-10-07 02:10:2021#include "base/utf_string_conversions.h"
[email protected]2a758d612008-09-17 10:09:3922
23namespace base {
24
25int SysInfo::NumberOfProcessors() {
[email protected]4075c932009-06-05 01:21:0926#if defined(OS_OPENBSD)
27 int mib[] = { CTL_HW, HW_NCPU };
28 int ncpu;
29 size_t size = sizeof(ncpu);
30 if (sysctl(mib, 2, &ncpu, &size, NULL, 0) == -1) {
31 NOTREACHED();
32 return 1;
33 }
34 return ncpu;
35#else
[email protected]2a758d612008-09-17 10:09:3936 // It seems that sysconf returns the number of "logical" processors on both
37 // mac and linux. So we get the number of "online logical" processors.
[email protected]0203ed72009-07-02 13:59:0838 long res = sysconf(_SC_NPROCESSORS_ONLN);
[email protected]2a758d612008-09-17 10:09:3939 if (res == -1) {
40 NOTREACHED();
41 return 1;
42 }
43
44 return static_cast<int>(res);
[email protected]4075c932009-06-05 01:21:0945#endif
[email protected]2a758d612008-09-17 10:09:3946}
47
[email protected]c8587632009-11-12 02:17:1948#if !defined(OS_MACOSX)
[email protected]d632798e2008-09-17 13:10:4549// static
50int64 SysInfo::AmountOfPhysicalMemory() {
[email protected]c8587632009-11-12 02:17:1951#if defined(OS_FREEBSD)
[email protected]4a34ce02009-08-31 22:25:0052 // _SC_PHYS_PAGES is not part of POSIX and not available on OS X or
53 // FreeBSD
[email protected]4a34ce02009-08-31 22:25:0054 // TODO(benl): I have no idea how to get this
55 NOTIMPLEMENTED();
56 return 0;
[email protected]d632798e2008-09-17 13:10:4557#else
58 long pages = sysconf(_SC_PHYS_PAGES);
59 long page_size = sysconf(_SC_PAGE_SIZE);
60 if (pages == -1 || page_size == -1) {
61 NOTREACHED();
62 return 0;
63 }
64
65 return static_cast<int64>(pages) * page_size;
66#endif
67}
[email protected]c8587632009-11-12 02:17:1968#endif
[email protected]d632798e2008-09-17 13:10:4569
[email protected]0e91dd22008-09-18 12:34:2470// static
[email protected]13326bb2009-10-14 00:41:5671int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) {
[email protected]0e91dd22008-09-18 12:34:2472 struct statvfs stats;
[email protected]13326bb2009-10-14 00:41:5673 if (statvfs(path.value().c_str(), &stats) != 0) {
[email protected]02ee34a2008-09-20 01:16:2374 return -1;
[email protected]0e91dd22008-09-18 12:34:2475 }
76 return static_cast<int64>(stats.f_bavail) * stats.f_frsize;
77}
78
[email protected]05f9b682008-09-29 22:18:0179// static
80bool SysInfo::HasEnvVar(const wchar_t* var) {
81 std::string var_utf8 = WideToUTF8(std::wstring(var));
82 return getenv(var_utf8.c_str()) != NULL;
83}
84
85// static
86std::wstring SysInfo::GetEnvVar(const wchar_t* var) {
87 std::string var_utf8 = WideToUTF8(std::wstring(var));
88 char* value = getenv(var_utf8.c_str());
89 if (!value) {
[email protected]047a03f2009-10-07 02:10:2090 return std::wstring();
[email protected]05f9b682008-09-29 22:18:0191 } else {
92 return UTF8ToWide(value);
93 }
94}
95
96// static
97std::string SysInfo::OperatingSystemName() {
98 utsname info;
99 if (uname(&info) < 0) {
100 NOTREACHED();
101 return "";
102 }
103 return std::string(info.sysname);
104}
105
106// static
107std::string SysInfo::OperatingSystemVersion() {
108 utsname info;
109 if (uname(&info) < 0) {
110 NOTREACHED();
111 return "";
112 }
113 return std::string(info.release);
114}
115
116// static
117std::string SysInfo::CPUArchitecture() {
118 utsname info;
119 if (uname(&info) < 0) {
120 NOTREACHED();
121 return "";
122 }
123 return std::string(info.machine);
124}
125
[email protected]c8587632009-11-12 02:17:19126#if !defined(OS_MACOSX)
[email protected]05f9b682008-09-29 22:18:01127// static
128void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) {
[email protected]60f1e8f2009-09-14 03:24:49129 // TODO(port): http://crbug.com/21732
[email protected]05f9b682008-09-29 22:18:01130 NOTIMPLEMENTED();
[email protected]60f1e8f2009-09-14 03:24:49131 if (width)
132 *width = 0;
133 if (height)
134 *height = 0;
[email protected]05f9b682008-09-29 22:18:01135}
136
137// static
138int SysInfo::DisplayCount() {
[email protected]60f1e8f2009-09-14 03:24:49139 // TODO(port): http://crbug.com/21732
[email protected]05f9b682008-09-29 22:18:01140 NOTIMPLEMENTED();
141 return 1;
142}
[email protected]c8587632009-11-12 02:17:19143#endif
[email protected]05f9b682008-09-29 22:18:01144
[email protected]037fce02009-01-22 01:42:15145// static
146size_t SysInfo::VMAllocationGranularity() {
147 return getpagesize();
148}
149
[email protected]80a086c52009-08-04 17:52:04150#if defined(OS_LINUX)
151// static
152size_t SysInfo::MaxSharedMemorySize() {
153 static size_t limit;
154 static bool limit_valid = false;
155
156 if (!limit_valid) {
157 std::string contents;
158 file_util::ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
159 limit = strtoul(contents.c_str(), NULL, 0);
160 limit_valid = true;
161 }
162
163 return limit;
164}
165#endif
166
[email protected]2a758d612008-09-17 10:09:39167} // namespace base