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

Side by Side Diff: base/sys_info_linux.cc

Issue 52403002: 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: Avoid doing IO in LazySysInfo initializer. 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 size_t MaxSharedMemorySize() {
27 std::string contents;
28 base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents);
29 DCHECK(!contents.empty());
30 if (!contents.empty() && contents[contents.length() - 1] == '\n') {
31 contents.erase(contents.length() - 1);
32 }
33
34 int64 limit;
35 if (!base::StringToInt64(contents, &limit)) {
36 limit = 0;
37 }
38 if (limit < 0 ||
39 static_cast<uint64>(limit) > std::numeric_limits<size_t>::max()) {
40 limit = 0;
41 }
42 DCHECK(limit > 0);
43 return static_cast<size_t>(limit);
44 }
45
46 class LazySysInfo {
47 public:
48 LazySysInfo()
49 : kPhysicalMemory_(AmountOfMemory(_SC_PHYS_PAGES)),
50 kMaxSharedMemorySize_(MaxSharedMemorySize()) { }
51
52 ~LazySysInfo() { }
53
54 int64 physical_memory() { return kPhysicalMemory_; }
55 size_t max_shared_memory_size() { return kMaxSharedMemorySize_; }
56
57 private:
58 const int64 kPhysicalMemory_;
59 const size_t kMaxSharedMemorySize_;
60
61 DISALLOW_COPY_AND_ASSIGN(LazySysInfo);
62 };
63
64 base::LazyInstance<LazySysInfo>::Leaky
65 g_lazy_sys_info = LAZY_INSTANCE_INITIALIZER;
66
25 } // namespace 67 } // namespace
26 68
27 namespace base { 69 namespace base {
28 70
29 // static 71 // static
30 int64 SysInfo::AmountOfPhysicalMemory() {
31 return AmountOfMemory(_SC_PHYS_PAGES);
32 }
33
34 // static
35 int64 SysInfo::AmountOfAvailablePhysicalMemory() { 72 int64 SysInfo::AmountOfAvailablePhysicalMemory() {
36 return AmountOfMemory(_SC_AVPHYS_PAGES); 73 return AmountOfMemory(_SC_AVPHYS_PAGES);
37 } 74 }
38 75
39 // static 76 // static
40 size_t SysInfo::MaxSharedMemorySize() { 77 int64 SysInfo::AmountOfPhysicalMemory() {
41 static int64 limit; 78 return g_lazy_sys_info.Get().physical_memory();
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 } 79 }
61 80
62 // static 81 // static
82 size_t SysInfo::MaxSharedMemorySize() {
83 return g_lazy_sys_info.Get().max_shared_memory_size();
84 }
85
86 // static
63 std::string SysInfo::CPUModelName() { 87 std::string SysInfo::CPUModelName() {
64 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) 88 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
65 const char kCpuModelPrefix[] = "Hardware"; 89 const char kCpuModelPrefix[] = "Hardware";
66 #else 90 #else
67 const char kCpuModelPrefix[] = "model name"; 91 const char kCpuModelPrefix[] = "model name";
68 #endif 92 #endif
69 std::string contents; 93 std::string contents;
70 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); 94 ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
71 DCHECK(!contents.empty()); 95 DCHECK(!contents.empty());
72 if (!contents.empty()) { 96 if (!contents.empty()) {
73 std::istringstream iss(contents); 97 std::istringstream iss(contents);
74 std::string line; 98 std::string line;
75 while (std::getline(iss, line)) { 99 while (std::getline(iss, line)) {
76 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { 100 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
77 size_t pos = line.find(": "); 101 size_t pos = line.find(": ");
78 return line.substr(pos + 2); 102 return line.substr(pos + 2);
79 } 103 }
80 } 104 }
81 } 105 }
82 return std::string(); 106 return std::string();
83 } 107 }
84 108
85 } // namespace base 109 } // 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