Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: base/sys_info_linux.cc

Issue 2558043007: Fix free memory calculation. (Closed)
Patch Set: Rebase. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
16 #include "base/strings/string_number_conversions.h" 17 #include "base/strings/string_number_conversions.h"
17 #include "base/sys_info_internal.h" 18 #include "base/sys_info_internal.h"
18 #include "build/build_config.h" 19 #include "build/build_config.h"
19 20
20 namespace { 21 namespace {
21 22
22 int64_t AmountOfMemory(int pages_name) { 23 int64_t AmountOfMemory(int pages_name) {
23 long pages = sysconf(pages_name); 24 long pages = sysconf(pages_name);
24 long page_size = sysconf(_SC_PAGESIZE); 25 long page_size = sysconf(_SC_PAGESIZE);
25 if (pages == -1 || page_size == -1) { 26 if (pages == -1 || page_size == -1) {
26 NOTREACHED(); 27 NOTREACHED();
27 return 0; 28 return 0;
28 } 29 }
29 return static_cast<int64_t>(pages) * page_size; 30 return static_cast<int64_t>(pages) * page_size;
30 } 31 }
31 32
32 int64_t AmountOfPhysicalMemory() { 33 int64_t AmountOfPhysicalMemory() {
33 return AmountOfMemory(_SC_PHYS_PAGES); 34 return AmountOfMemory(_SC_PHYS_PAGES);
34 } 35 }
35 36
36 base::LazyInstance< 37 base::LazyInstance<
37 base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky 38 base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky
38 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; 39 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER;
39 40
40 } // namespace 41 } // namespace
41 42
42 namespace base { 43 namespace base {
43 44
44 // static 45 // static
45 int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
46 return AmountOfMemory(_SC_AVPHYS_PAGES);
47 }
48
49 // static
50 int64_t SysInfo::AmountOfPhysicalMemory() { 46 int64_t SysInfo::AmountOfPhysicalMemory() {
51 return g_lazy_physical_memory.Get().value(); 47 return g_lazy_physical_memory.Get().value();
52 } 48 }
53 49
54 // static 50 // static
51 int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
52 SystemMemoryInfoKB info;
53 if (!GetSystemMemoryInfo(&info)) {
54 NOTREACHED();
danakj 2017/03/10 17:13:49 Do not write "NOTREACHED(); return;" Choose one: D
Michael K. (Yandex Team) 2017/03/10 18:22:37 Done. (But there are a lot of already written cod
55 return 0;
56 }
57 return AmountOfAvailablePhysicalMemory(info);
58 }
59
60 // static
61 int64_t SysInfo::AmountOfAvailablePhysicalMemory(
62 const SystemMemoryInfoKB& info) {
63 // See details here:
64 // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id= 34e431b0ae398fc54ea69ff85ec700722c9da773
65 // The fallback logic (when there is no MemAvailable) would be more precise
66 // if we had info about zones watermarks (/proc/zoneinfo).
67 return info.available != 0
danakj 2017/03/10 17:13:49 i suggest storing a temporary var with as MB then
Michael K. (Yandex Team) 2017/03/10 18:22:37 Done.
68 ? static_cast<int64_t>(info.available - info.active_file) * 1024
69 : static_cast<int64_t>(info.free + info.reclaimable +
70 info.inactive_file) *
71 1024;
72 }
73
74 // static
55 std::string SysInfo::CPUModelName() { 75 std::string SysInfo::CPUModelName() {
56 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) 76 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
57 const char kCpuModelPrefix[] = "Hardware"; 77 const char kCpuModelPrefix[] = "Hardware";
58 #else 78 #else
59 const char kCpuModelPrefix[] = "model name"; 79 const char kCpuModelPrefix[] = "model name";
60 #endif 80 #endif
61 std::string contents; 81 std::string contents;
62 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); 82 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
63 DCHECK(!contents.empty()); 83 DCHECK(!contents.empty());
64 if (!contents.empty()) { 84 if (!contents.empty()) {
65 std::istringstream iss(contents); 85 std::istringstream iss(contents);
66 std::string line; 86 std::string line;
67 while (std::getline(iss, line)) { 87 while (std::getline(iss, line)) {
68 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { 88 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
69 size_t pos = line.find(": "); 89 size_t pos = line.find(": ");
70 return line.substr(pos + 2); 90 return line.substr(pos + 2);
71 } 91 }
72 } 92 }
73 } 93 }
74 return std::string(); 94 return std::string();
75 } 95 }
76 96
77 } // namespace base 97 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698