robert.nagy@gmail.com | ea54e46 | 2011-10-25 07:05:27 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 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 | |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | |
robert.nagy@gmail.com | ea54e46 | 2011-10-25 07:05:27 +0900 | [diff] [blame] | 10 | #include <limits> |
| 11 | |
brettw@chromium.org | 01f3da4 | 2014-08-14 05:22:14 +0900 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
rmcilroy@chromium.org | e6d3510 | 2013-11-05 05:50:14 +0900 | [diff] [blame] | 13 | #include "base/lazy_instance.h" |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
ljagielski | 8af7af7 | 2015-04-14 22:45:42 +0900 | [diff] [blame] | 15 | #include "base/numerics/safe_conversions.h" |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 16 | #include "base/process/process_metrics.h" |
brettw@chromium.org | abcde5c | 2013-02-07 11:57:22 +0900 | [diff] [blame] | 17 | #include "base/strings/string_number_conversions.h" |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 18 | #include "base/sys_info_internal.h" |
avi | a6a6a68 | 2015-12-27 07:15:14 +0900 | [diff] [blame] | 19 | #include "build/build_config.h" |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 20 | |
mdempsky@google.com | 9272150 | 2013-04-10 11:22:15 +0900 | [diff] [blame] | 21 | namespace { |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 22 | |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 23 | int64_t AmountOfMemory(int pages_name) { |
mdempsky@google.com | 9272150 | 2013-04-10 11:22:15 +0900 | [diff] [blame] | 24 | long pages = sysconf(pages_name); |
| 25 | long page_size = sysconf(_SC_PAGESIZE); |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 26 | if (pages == -1 || page_size == -1) { |
| 27 | NOTREACHED(); |
| 28 | return 0; |
| 29 | } |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 30 | return static_cast<int64_t>(pages) * page_size; |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 31 | } |
| 32 | |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 33 | int64_t AmountOfPhysicalMemory() { |
rmcilroy@chromium.org | e6d3510 | 2013-11-05 05:50:14 +0900 | [diff] [blame] | 34 | return AmountOfMemory(_SC_PHYS_PAGES); |
| 35 | } |
| 36 | |
jochen@chromium.org | 4abdb3e | 2013-11-20 20:14:51 +0900 | [diff] [blame] | 37 | base::LazyInstance< |
avi | 95a2f37 | 2015-12-09 09:44:49 +0900 | [diff] [blame] | 38 | base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky |
rmcilroy@chromium.org | e6d3510 | 2013-11-05 05:50:14 +0900 | [diff] [blame] | 39 | g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; |
rmcilroy@chromium.org | e6d3510 | 2013-11-05 05:50:14 +0900 | [diff] [blame] | 40 | |
dglazkov@chromium.org | 69cd2b5 | 2013-10-30 05:14:54 +0900 | [diff] [blame] | 41 | } // namespace |
rmcilroy@chromium.org | 75d0ba2 | 2013-10-30 04:32:45 +0900 | [diff] [blame] | 42 | |
dglazkov@chromium.org | 69cd2b5 | 2013-10-30 05:14:54 +0900 | [diff] [blame] | 43 | namespace base { |
| 44 | |
| 45 | // static |
Eric Karl | 3009718 | 2017-07-26 10:18:37 +0900 | [diff] [blame] | 46 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 47 | return g_lazy_physical_memory.Get().value(); |
horo | fbe605b | 2017-03-21 16:02:56 +0900 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | // static |
Eric Karl | 3009718 | 2017-07-26 10:18:37 +0900 | [diff] [blame] | 51 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
mkolom | af6f3f8 | 2017-03-22 10:47:29 +0900 | [diff] [blame] | 52 | SystemMemoryInfoKB info; |
| 53 | if (!GetSystemMemoryInfo(&info)) |
| 54 | return 0; |
| 55 | return AmountOfAvailablePhysicalMemory(info); |
| 56 | } |
| 57 | |
| 58 | // static |
| 59 | int64_t SysInfo::AmountOfAvailablePhysicalMemory( |
| 60 | const SystemMemoryInfoKB& info) { |
| 61 | // See details here: |
| 62 | // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 |
| 63 | // The fallback logic (when there is no MemAvailable) would be more precise |
| 64 | // if we had info about zones watermarks (/proc/zoneinfo). |
| 65 | int64_t res_kb = info.available != 0 |
| 66 | ? info.available - info.active_file |
| 67 | : info.free + info.reclaimable + info.inactive_file; |
| 68 | return res_kb * 1024; |
dglazkov@chromium.org | 69cd2b5 | 2013-10-30 05:14:54 +0900 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // static |
dglazkov@chromium.org | 69cd2b5 | 2013-10-30 05:14:54 +0900 | [diff] [blame] | 72 | std::string SysInfo::CPUModelName() { |
zelidrag@chromium.org | cedb267 | 2013-05-06 14:35:27 +0900 | [diff] [blame] | 73 | #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
| 74 | const char kCpuModelPrefix[] = "Hardware"; |
| 75 | #else |
| 76 | const char kCpuModelPrefix[] = "model name"; |
| 77 | #endif |
hongbo.min@intel.com | 893f7d6 | 2012-09-08 18:53:01 +0900 | [diff] [blame] | 78 | std::string contents; |
dglazkov@chromium.org | 69cd2b5 | 2013-10-30 05:14:54 +0900 | [diff] [blame] | 79 | ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
hongbo.min@intel.com | 893f7d6 | 2012-09-08 18:53:01 +0900 | [diff] [blame] | 80 | DCHECK(!contents.empty()); |
| 81 | if (!contents.empty()) { |
| 82 | std::istringstream iss(contents); |
| 83 | std::string line; |
zelidrag@chromium.org | cedb267 | 2013-05-06 14:35:27 +0900 | [diff] [blame] | 84 | while (std::getline(iss, line)) { |
| 85 | if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
hongbo.min@intel.com | 893f7d6 | 2012-09-08 18:53:01 +0900 | [diff] [blame] | 86 | size_t pos = line.find(": "); |
| 87 | return line.substr(pos + 2); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | return std::string(); |
| 92 | } |
| 93 | |
evan@chromium.org | fae7268 | 2009-11-24 06:57:11 +0900 | [diff] [blame] | 94 | } // namespace base |