Index: base/sys_info_linux.cc |
diff --git a/base/sys_info_linux.cc b/base/sys_info_linux.cc |
index acc477134cd38bed2ba37e6add9f84370bfc87ca..cc23547746bcac4b912aadaae3fc6fd0cca3bf2d 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,52 +23,32 @@ int64 AmountOfMemory(int pages_name) { |
return static_cast<int64>(pages) * page_size; |
} |
-} // namespace |
- |
-namespace base { |
- |
-// static |
-int64 SysInfo::AmountOfPhysicalMemory() { |
- return AmountOfMemory(_SC_PHYS_PAGES); |
-} |
- |
-// static |
-int64 SysInfo::AmountOfAvailablePhysicalMemory() { |
- return AmountOfMemory(_SC_AVPHYS_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); |
+ } |
-// 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; |
- } |
+ int64 limit; |
+ if (!base::StringToInt64(contents, &limit)) { |
+ NOTREACHED(); |
jln (very slow on Chromium)
2013/10/21 23:27:28
How about just "limit = 0" ?
rmcilroy
2013/10/23 14:51:34
Done.
|
+ return 0; |
} |
+ DCHECK(limit >= 0); |
+ DCHECK(static_cast<uint64>(limit) <= std::numeric_limits<size_t>::max()); |
jln (very slow on Chromium)
2013/10/21 23:27:28
The cast to unsigned is a bit strange and I don't
rmcilroy
2013/10/23 14:51:34
Done. Are we sure we want to return 0 rather than
|
return static_cast<size_t>(limit); |
} |
-// static |
-std::string SysInfo::CPUModelName() { |
+std::string CPUModelName() { |
#if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
const char kCpuModelPrefix[] = "Hardware"; |
#else |
const char kCpuModelPrefix[] = "model name"; |
#endif |
std::string contents; |
- ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
+ base::ReadFileToString(base::FilePath("/proc/cpuinfo"), &contents); |
DCHECK(!contents.empty()); |
if (!contents.empty()) { |
std::istringstream iss(contents); |
@@ -82,4 +63,59 @@ std::string SysInfo::CPUModelName() { |
return std::string(); |
} |
+class LazySysInfo { |
+ public: |
+ LazySysInfo() { |
+ physical_memory_ = AmountOfMemory(_SC_PHYS_PAGES); |
+ max_shared_memory_size_ = MaxSharedMemorySize(); |
+ cpu_model_name_ = CPUModelName(); |
jar (doing other things)
2013/10/21 23:54:14
nit: Given these three items are all constant... h
rmcilroy
2013/10/23 14:51:34
Done.
|
+ } |
+ |
+ ~LazySysInfo() { } |
+ |
+ int64 GetPhysicalMemory() { |
jln (very slow on Chromium)
2013/10/21 23:27:28
Style: physical_memory() as it's a trivial accesso
rmcilroy
2013/10/23 14:51:34
Done.
|
+ return physical_memory_; |
+ } |
+ |
+ size_t GetMaxSharedMemorySize() { |
+ return max_shared_memory_size_; |
+ } |
+ |
+ std::string GetCPUModelName() { |
+ return cpu_model_name_; |
+ } |
+ |
+ private: |
+ int64 physical_memory_; |
+ size_t max_shared_memory_size_; |
+ std::string cpu_model_name_; |
jln (very slow on Chromium)
2013/10/21 23:27:28
Style: add DISALLOW_COPY_AND_ASSIGN?
rmcilroy
2013/10/23 14:51:34
Done.
|
+}; |
+ |
+base::LazyInstance<LazySysInfo>::Leaky |
+ g_lazy_sys_info = LAZY_INSTANCE_INITIALIZER; |
+ |
+} // namespace |
+ |
+namespace base { |
+ |
+// static |
+int64 SysInfo::AmountOfAvailablePhysicalMemory() { |
+ return AmountOfMemory(_SC_AVPHYS_PAGES); |
+} |
+ |
+// static |
+int64 SysInfo::AmountOfPhysicalMemory() { |
+ return g_lazy_sys_info.Get().GetPhysicalMemory(); |
+} |
+ |
+// static |
+size_t SysInfo::MaxSharedMemorySize() { |
+ return g_lazy_sys_info.Get().GetMaxSharedMemorySize(); |
+} |
+ |
+// static |
+std::string SysInfo::CPUModelName() { |
+ return g_lazy_sys_info.Get().GetCPUModelName(); |
+} |
+ |
} // namespace base |