[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 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 | |
| 7 | #include <errno.h> |
| 8 | #include <string.h> |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 9 | #include <sys/statvfs.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 10 | #include <sys/utsname.h> |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 11 | #include <unistd.h> |
| 12 | |
[email protected] | 4075c93 | 2009-06-05 01:21:09 | [diff] [blame] | 13 | #if defined(OS_OPENBSD) |
| 14 | #include <sys/param.h> |
| 15 | #include <sys/sysctl.h> |
| 16 | #endif |
| 17 | |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 18 | #include "base/basictypes.h" |
| 19 | #include "base/file_util.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 20 | #include "base/logging.h" |
[email protected] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 21 | #include "base/utf_string_conversions.h" |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 22 | |
| 23 | namespace base { |
| 24 | |
| 25 | int SysInfo::NumberOfProcessors() { |
[email protected] | 4075c93 | 2009-06-05 01:21:09 | [diff] [blame] | 26 | #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] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 36 | // 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] | 0203ed7 | 2009-07-02 13:59:08 | [diff] [blame] | 38 | long res = sysconf(_SC_NPROCESSORS_ONLN); |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 39 | if (res == -1) { |
| 40 | NOTREACHED(); |
| 41 | return 1; |
| 42 | } |
| 43 | |
| 44 | return static_cast<int>(res); |
[email protected] | 4075c93 | 2009-06-05 01:21:09 | [diff] [blame] | 45 | #endif |
[email protected] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame^] | 48 | #if !defined(OS_MACOSX) |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 49 | // static |
| 50 | int64 SysInfo::AmountOfPhysicalMemory() { |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame^] | 51 | #if defined(OS_FREEBSD) |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 52 | // _SC_PHYS_PAGES is not part of POSIX and not available on OS X or |
| 53 | // FreeBSD |
[email protected] | 4a34ce0 | 2009-08-31 22:25:00 | [diff] [blame] | 54 | // TODO(benl): I have no idea how to get this |
| 55 | NOTIMPLEMENTED(); |
| 56 | return 0; |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 57 | #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] | c858763 | 2009-11-12 02:17:19 | [diff] [blame^] | 68 | #endif |
[email protected] | d632798e | 2008-09-17 13:10:45 | [diff] [blame] | 69 | |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 70 | // static |
[email protected] | 13326bb | 2009-10-14 00:41:56 | [diff] [blame] | 71 | int64 SysInfo::AmountOfFreeDiskSpace(const FilePath& path) { |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 72 | struct statvfs stats; |
[email protected] | 13326bb | 2009-10-14 00:41:56 | [diff] [blame] | 73 | if (statvfs(path.value().c_str(), &stats) != 0) { |
[email protected] | 02ee34a | 2008-09-20 01:16:23 | [diff] [blame] | 74 | return -1; |
[email protected] | 0e91dd2 | 2008-09-18 12:34:24 | [diff] [blame] | 75 | } |
| 76 | return static_cast<int64>(stats.f_bavail) * stats.f_frsize; |
| 77 | } |
| 78 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 79 | // static |
| 80 | bool 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 |
| 86 | std::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] | 047a03f | 2009-10-07 02:10:20 | [diff] [blame] | 90 | return std::wstring(); |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 91 | } else { |
| 92 | return UTF8ToWide(value); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | // static |
| 97 | std::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 |
| 107 | std::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 |
| 117 | std::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] | c858763 | 2009-11-12 02:17:19 | [diff] [blame^] | 126 | #if !defined(OS_MACOSX) |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 127 | // static |
| 128 | void SysInfo::GetPrimaryDisplayDimensions(int* width, int* height) { |
[email protected] | 60f1e8f | 2009-09-14 03:24:49 | [diff] [blame] | 129 | // TODO(port): http://crbug.com/21732 |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 130 | NOTIMPLEMENTED(); |
[email protected] | 60f1e8f | 2009-09-14 03:24:49 | [diff] [blame] | 131 | if (width) |
| 132 | *width = 0; |
| 133 | if (height) |
| 134 | *height = 0; |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // static |
| 138 | int SysInfo::DisplayCount() { |
[email protected] | 60f1e8f | 2009-09-14 03:24:49 | [diff] [blame] | 139 | // TODO(port): http://crbug.com/21732 |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 140 | NOTIMPLEMENTED(); |
| 141 | return 1; |
| 142 | } |
[email protected] | c858763 | 2009-11-12 02:17:19 | [diff] [blame^] | 143 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 144 | |
[email protected] | 037fce0 | 2009-01-22 01:42:15 | [diff] [blame] | 145 | // static |
| 146 | size_t SysInfo::VMAllocationGranularity() { |
| 147 | return getpagesize(); |
| 148 | } |
| 149 | |
[email protected] | 80a086c5 | 2009-08-04 17:52:04 | [diff] [blame] | 150 | #if defined(OS_LINUX) |
| 151 | // static |
| 152 | size_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] | 2a758d61 | 2008-09-17 10:09:39 | [diff] [blame] | 167 | } // namespace base |