OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <limits> | 10 #include <limits> |
11 | 11 |
12 #include "base/files/file_util.h" | 12 #include "base/files/file_util.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
16 #include "base/process/process_metrics.h" | |
17 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
18 #include "base/sys_info_internal.h" | 17 #include "base/sys_info_internal.h" |
19 #include "build/build_config.h" | 18 #include "build/build_config.h" |
20 | 19 |
21 namespace { | 20 namespace { |
22 | 21 |
23 int64_t AmountOfMemory(int pages_name) { | 22 int64_t AmountOfMemory(int pages_name) { |
24 long pages = sysconf(pages_name); | 23 long pages = sysconf(pages_name); |
25 long page_size = sysconf(_SC_PAGESIZE); | 24 long page_size = sysconf(_SC_PAGESIZE); |
26 if (pages == -1 || page_size == -1) { | 25 if (pages == -1 || page_size == -1) { |
27 NOTREACHED(); | 26 NOTREACHED(); |
28 return 0; | 27 return 0; |
29 } | 28 } |
30 return static_cast<int64_t>(pages) * page_size; | 29 return static_cast<int64_t>(pages) * page_size; |
31 } | 30 } |
32 | 31 |
33 int64_t AmountOfPhysicalMemory() { | 32 int64_t AmountOfPhysicalMemory() { |
34 return AmountOfMemory(_SC_PHYS_PAGES); | 33 return AmountOfMemory(_SC_PHYS_PAGES); |
35 } | 34 } |
36 | 35 |
37 base::LazyInstance< | 36 base::LazyInstance< |
38 base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky | 37 base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky |
39 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; | 38 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; |
40 | 39 |
41 } // namespace | 40 } // namespace |
42 | 41 |
43 namespace base { | 42 namespace base { |
44 | 43 |
45 // static | 44 // static |
| 45 int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
| 46 return AmountOfMemory(_SC_AVPHYS_PAGES); |
| 47 } |
| 48 |
| 49 // static |
46 int64_t SysInfo::AmountOfPhysicalMemory() { | 50 int64_t SysInfo::AmountOfPhysicalMemory() { |
47 return g_lazy_physical_memory.Get().value(); | 51 return g_lazy_physical_memory.Get().value(); |
48 } | 52 } |
49 | 53 |
50 // static | 54 // static |
51 int64_t SysInfo::AmountOfAvailablePhysicalMemory() { | |
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; | |
69 } | |
70 | |
71 // static | |
72 std::string SysInfo::CPUModelName() { | 55 std::string SysInfo::CPUModelName() { |
73 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 56 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
74 const char kCpuModelPrefix[] = "Hardware"; | 57 const char kCpuModelPrefix[] = "Hardware"; |
75 #else | 58 #else |
76 const char kCpuModelPrefix[] = "model name"; | 59 const char kCpuModelPrefix[] = "model name"; |
77 #endif | 60 #endif |
78 std::string contents; | 61 std::string contents; |
79 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); | 62 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
80 DCHECK(!contents.empty()); | 63 DCHECK(!contents.empty()); |
81 if (!contents.empty()) { | 64 if (!contents.empty()) { |
82 std::istringstream iss(contents); | 65 std::istringstream iss(contents); |
83 std::string line; | 66 std::string line; |
84 while (std::getline(iss, line)) { | 67 while (std::getline(iss, line)) { |
85 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { | 68 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
86 size_t pos = line.find(": "); | 69 size_t pos = line.find(": "); |
87 return line.substr(pos + 2); | 70 return line.substr(pos + 2); |
88 } | 71 } |
89 } | 72 } |
90 } | 73 } |
91 return std::string(); | 74 return std::string(); |
92 } | 75 } |
93 | 76 |
94 } // namespace base | 77 } // namespace base |
OLD | NEW |