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

Side by Side Diff: base/sys_info_linux.cc

Issue 55913004: Reland "Enable SysInfo::AmountOfPhysicalMemory to be called from within the Linux Sandbox" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Seperate MaxSharedMemorySize and AmountOfPhysicalMemory initialization. Created 7 years, 1 month 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
« no previous file with comments | « base/sys_info.h ('k') | content/zygote/zygote_main_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <limits> 7 #include <limits>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/lazy_instance.h"
10 #include "base/logging.h" 11 #include "base/logging.h"
11 #include "base/strings/string_number_conversions.h" 12 #include "base/strings/string_number_conversions.h"
12 13
13 namespace { 14 namespace {
14 15
15 int64 AmountOfMemory(int pages_name) { 16 int64 AmountOfMemory(int pages_name) {
16 long pages = sysconf(pages_name); 17 long pages = sysconf(pages_name);
17 long page_size = sysconf(_SC_PAGESIZE); 18 long page_size = sysconf(_SC_PAGESIZE);
18 if (pages == -1 || page_size == -1) { 19 if (pages == -1 || page_size == -1) {
19 NOTREACHED(); 20 NOTREACHED();
20 return 0; 21 return 0;
21 } 22 }
22 return static_cast<int64>(pages) * page_size; 23 return static_cast<int64>(pages) * page_size;
23 } 24 }
24 25
26 int64 AmountOfPhysicalMemory() {
27 return AmountOfMemory(_SC_PHYS_PAGES);
28 }
29
30 size_t MaxSharedMemorySize() {
31 std::string contents;
32 base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents);
33 DCHECK(!contents.empty());
34 if (!contents.empty() && contents[contents.length() - 1] == '\n') {
35 contents.erase(contents.length() - 1);
36 }
37
38 int64 limit;
39 if (!base::StringToInt64(contents, &limit)) {
40 limit = 0;
41 }
42 if (limit < 0 ||
43 static_cast<uint64>(limit) > std::numeric_limits<size_t>::max()) {
44 limit = 0;
45 }
46 DCHECK(limit > 0);
47 return static_cast<size_t>(limit);
48 }
49
50 template<typename T, T (*F)(void)>
51 class LazySysInfoValue {
52 public:
53 LazySysInfoValue()
54 : value_(F()) { }
55
56 ~LazySysInfoValue() { }
57
58 T value() { return value_; }
59
60 private:
61 const T value_;
62
63 DISALLOW_COPY_AND_ASSIGN(LazySysInfoValue);
64 };
65
66 base::LazyInstance<LazySysInfoValue<int64, AmountOfPhysicalMemory> >::Leaky
67 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER;
68 base::LazyInstance<LazySysInfoValue<size_t, MaxSharedMemorySize> >::Leaky
69 g_lazy_max_shared_memory = LAZY_INSTANCE_INITIALIZER;
70
25 } // namespace 71 } // namespace
26 72
27 namespace base { 73 namespace base {
28 74
29 // static 75 // static
30 int64 SysInfo::AmountOfPhysicalMemory() {
31 return AmountOfMemory(_SC_PHYS_PAGES);
32 }
33
34 // static
35 int64 SysInfo::AmountOfAvailablePhysicalMemory() { 76 int64 SysInfo::AmountOfAvailablePhysicalMemory() {
36 return AmountOfMemory(_SC_AVPHYS_PAGES); 77 return AmountOfMemory(_SC_AVPHYS_PAGES);
37 } 78 }
38 79
39 // static 80 // static
40 size_t SysInfo::MaxSharedMemorySize() { 81 int64 SysInfo::AmountOfPhysicalMemory() {
41 static int64 limit; 82 return g_lazy_physical_memory.Get().value();
42 static bool limit_valid = false;
43 if (!limit_valid) {
44 std::string contents;
45 ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
46 DCHECK(!contents.empty());
47 if (!contents.empty() && contents[contents.length() - 1] == '\n') {
48 contents.erase(contents.length() - 1);
49 }
50 if (base::StringToInt64(contents, &limit)) {
51 DCHECK(limit >= 0);
52 DCHECK(static_cast<uint64>(limit) <= std::numeric_limits<size_t>::max());
53 limit_valid = true;
54 } else {
55 NOTREACHED();
56 return 0;
57 }
58 }
59 return static_cast<size_t>(limit);
60 } 83 }
61 84
62 // static 85 // static
86 size_t SysInfo::MaxSharedMemorySize() {
87 return g_lazy_max_shared_memory.Get().value();
88 }
89
90 // static
63 std::string SysInfo::CPUModelName() { 91 std::string SysInfo::CPUModelName() {
64 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) 92 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
65 const char kCpuModelPrefix[] = "Hardware"; 93 const char kCpuModelPrefix[] = "Hardware";
66 #else 94 #else
67 const char kCpuModelPrefix[] = "model name"; 95 const char kCpuModelPrefix[] = "model name";
68 #endif 96 #endif
69 std::string contents; 97 std::string contents;
70 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); 98 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
71 DCHECK(!contents.empty()); 99 DCHECK(!contents.empty());
72 if (!contents.empty()) { 100 if (!contents.empty()) {
73 std::istringstream iss(contents); 101 std::istringstream iss(contents);
74 std::string line; 102 std::string line;
75 while (std::getline(iss, line)) { 103 while (std::getline(iss, line)) {
76 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { 104 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
77 size_t pos = line.find(": "); 105 size_t pos = line.find(": ");
78 return line.substr(pos + 2); 106 return line.substr(pos + 2);
79 } 107 }
80 } 108 }
81 } 109 }
82 return std::string(); 110 return std::string();
83 } 111 }
84 112
85 } // namespace base 113 } // namespace base
OLDNEW
« no previous file with comments | « base/sys_info.h ('k') | content/zygote/zygote_main_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698