blob: b1fecff4bd25a4c6756a9ea382864771d5d79908 [file] [log] [blame]
robert.nagy@gmail.comea54e462011-10-25 07:05:27 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
evan@chromium.orgfae72682009-11-24 06:57:11 +09002// 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
avia6a6a682015-12-27 07:15:14 +09007#include <stddef.h>
8#include <stdint.h>
9
robert.nagy@gmail.comea54e462011-10-25 07:05:27 +090010#include <limits>
11
brettw@chromium.org01f3da42014-08-14 05:22:14 +090012#include "base/files/file_util.h"
rmcilroy@chromium.orge6d35102013-11-05 05:50:14 +090013#include "base/lazy_instance.h"
evan@chromium.orgfae72682009-11-24 06:57:11 +090014#include "base/logging.h"
ljagielski8af7af72015-04-14 22:45:42 +090015#include "base/numerics/safe_conversions.h"
mkolomaf6f3f82017-03-22 10:47:29 +090016#include "base/process/process_metrics.h"
brettw@chromium.orgabcde5c2013-02-07 11:57:22 +090017#include "base/strings/string_number_conversions.h"
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090018#include "base/sys_info_internal.h"
avia6a6a682015-12-27 07:15:14 +090019#include "build/build_config.h"
evan@chromium.orgfae72682009-11-24 06:57:11 +090020
mdempsky@google.com92721502013-04-10 11:22:15 +090021namespace {
evan@chromium.orgfae72682009-11-24 06:57:11 +090022
avi95a2f372015-12-09 09:44:49 +090023int64_t AmountOfMemory(int pages_name) {
mdempsky@google.com92721502013-04-10 11:22:15 +090024 long pages = sysconf(pages_name);
25 long page_size = sysconf(_SC_PAGESIZE);
evan@chromium.orgfae72682009-11-24 06:57:11 +090026 if (pages == -1 || page_size == -1) {
27 NOTREACHED();
28 return 0;
29 }
avi95a2f372015-12-09 09:44:49 +090030 return static_cast<int64_t>(pages) * page_size;
evan@chromium.orgfae72682009-11-24 06:57:11 +090031}
32
avi95a2f372015-12-09 09:44:49 +090033int64_t AmountOfPhysicalMemory() {
rmcilroy@chromium.orge6d35102013-11-05 05:50:14 +090034 return AmountOfMemory(_SC_PHYS_PAGES);
35}
36
jochen@chromium.org4abdb3e2013-11-20 20:14:51 +090037base::LazyInstance<
avi95a2f372015-12-09 09:44:49 +090038 base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky
rmcilroy@chromium.orge6d35102013-11-05 05:50:14 +090039 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER;
rmcilroy@chromium.orge6d35102013-11-05 05:50:14 +090040
dglazkov@chromium.org69cd2b52013-10-30 05:14:54 +090041} // namespace
rmcilroy@chromium.org75d0ba22013-10-30 04:32:45 +090042
dglazkov@chromium.org69cd2b52013-10-30 05:14:54 +090043namespace base {
44
45// static
Eric Karl30097182017-07-26 10:18:37 +090046int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
mkolomaf6f3f82017-03-22 10:47:29 +090047 return g_lazy_physical_memory.Get().value();
horofbe605b2017-03-21 16:02:56 +090048}
49
50// static
Eric Karl30097182017-07-26 10:18:37 +090051int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
mkolomaf6f3f82017-03-22 10:47:29 +090052 SystemMemoryInfoKB info;
53 if (!GetSystemMemoryInfo(&info))
54 return 0;
55 return AmountOfAvailablePhysicalMemory(info);
56}
57
58// static
59int64_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.org69cd2b52013-10-30 05:14:54 +090069}
70
71// static
dglazkov@chromium.org69cd2b52013-10-30 05:14:54 +090072std::string SysInfo::CPUModelName() {
zelidrag@chromium.orgcedb2672013-05-06 14:35:27 +090073#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.com893f7d62012-09-08 18:53:01 +090078 std::string contents;
dglazkov@chromium.org69cd2b52013-10-30 05:14:54 +090079 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
hongbo.min@intel.com893f7d62012-09-08 18:53:01 +090080 DCHECK(!contents.empty());
81 if (!contents.empty()) {
82 std::istringstream iss(contents);
83 std::string line;
zelidrag@chromium.orgcedb2672013-05-06 14:35:27 +090084 while (std::getline(iss, line)) {
85 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
hongbo.min@intel.com893f7d62012-09-08 18:53:01 +090086 size_t pos = line.find(": ");
87 return line.substr(pos + 2);
88 }
89 }
90 }
91 return std::string();
92}
93
evan@chromium.orgfae72682009-11-24 06:57:11 +090094} // namespace base