summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/sys_info.h3
-rw-r--r--base/sys_info_linux.cc74
-rw-r--r--content/zygote/zygote_main_linux.cc1
3 files changed, 54 insertions, 24 deletions
diff --git a/base/sys_info.h b/base/sys_info.h
index ba45bfc..aa40cad 100644
--- a/base/sys_info.h
+++ b/base/sys_info.h
@@ -75,7 +75,8 @@ class BASE_EXPORT SysInfo {
static size_t VMAllocationGranularity();
#if defined(OS_POSIX) && !defined(OS_MACOSX)
- // Returns the maximum SysV shared memory segment size.
+ // Returns the maximum SysV shared memory segment size, or zero if there is no
+ // limit.
static size_t MaxSharedMemorySize();
#endif // defined(OS_POSIX) && !defined(OS_MACOSX)
diff --git a/base/sys_info_linux.cc b/base/sys_info_linux.cc
index acc4771..d7c9f10 100644
--- a/base/sys_info_linux.cc
+++ b/base/sys_info_linux.cc
@@ -7,6 +7,7 @@
#include <limits>
#include "base/file_util.h"
+#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
@@ -22,41 +23,68 @@ int64 AmountOfMemory(int pages_name) {
return static_cast<int64>(pages) * page_size;
}
+int64 AmountOfPhysicalMemory() {
+ return AmountOfMemory(_SC_PHYS_PAGES);
+}
+
+size_t MaxSharedMemorySize() {
+ std::string contents;
+ base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents);
+ DCHECK(!contents.empty());
+ if (!contents.empty() && contents[contents.length() - 1] == '\n') {
+ contents.erase(contents.length() - 1);
+ }
+
+ int64 limit;
+ if (!base::StringToInt64(contents, &limit)) {
+ limit = 0;
+ }
+ if (limit < 0 ||
+ static_cast<uint64>(limit) > std::numeric_limits<size_t>::max()) {
+ limit = 0;
+ }
+ DCHECK(limit > 0);
+ return static_cast<size_t>(limit);
+}
+
+template<typename T, T (*F)(void)>
+class LazySysInfoValue {
+ public:
+ LazySysInfoValue()
+ : value_(F()) { }
+
+ ~LazySysInfoValue() { }
+
+ T value() { return value_; }
+
+ private:
+ const T value_;
+
+ DISALLOW_COPY_AND_ASSIGN(LazySysInfoValue);
+};
+
+base::LazyInstance<LazySysInfoValue<int64, AmountOfPhysicalMemory> >::Leaky
+ g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER;
+base::LazyInstance<LazySysInfoValue<size_t, MaxSharedMemorySize> >::Leaky
+ g_lazy_max_shared_memory = LAZY_INSTANCE_INITIALIZER;
+
} // namespace
namespace base {
// static
-int64 SysInfo::AmountOfPhysicalMemory() {
- return AmountOfMemory(_SC_PHYS_PAGES);
+int64 SysInfo::AmountOfAvailablePhysicalMemory() {
+ return AmountOfMemory(_SC_AVPHYS_PAGES);
}
// static
-int64 SysInfo::AmountOfAvailablePhysicalMemory() {
- return AmountOfMemory(_SC_AVPHYS_PAGES);
+int64 SysInfo::AmountOfPhysicalMemory() {
+ return g_lazy_physical_memory.Get().value();
}
// static
size_t SysInfo::MaxSharedMemorySize() {
- static int64 limit;
- static bool limit_valid = false;
- if (!limit_valid) {
- std::string contents;
- ReadFileToString(FilePath("/proc/sys/kernel/shmmax"), &contents);
- DCHECK(!contents.empty());
- if (!contents.empty() && contents[contents.length() - 1] == '\n') {
- contents.erase(contents.length() - 1);
- }
- if (base::StringToInt64(contents, &limit)) {
- DCHECK(limit >= 0);
- DCHECK(static_cast<uint64>(limit) <= std::numeric_limits<size_t>::max());
- limit_valid = true;
- } else {
- NOTREACHED();
- return 0;
- }
- }
- return static_cast<size_t>(limit);
+ return g_lazy_max_shared_memory.Get().value();
}
// static
diff --git a/content/zygote/zygote_main_linux.cc b/content/zygote/zygote_main_linux.cc
index 567b305..503b348 100644
--- a/content/zygote/zygote_main_linux.cc
+++ b/content/zygote/zygote_main_linux.cc
@@ -274,6 +274,7 @@ void PreloadPepperPlugins() {
static void PreSandboxInit() {
base::RandUint64();
+ base::SysInfo::AmountOfPhysicalMemory();
base::SysInfo::MaxSharedMemorySize();
// ICU DateFormat class (used in base/time_format.cc) needs to get the