Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| 25 } // namespace | 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 } | |
| 26 | 33 |
| 27 namespace base { | 34 int64 limit; |
| 28 | 35 if (!base::StringToInt64(contents, &limit)) { |
| 29 // static | 36 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.
| |
| 30 int64 SysInfo::AmountOfPhysicalMemory() { | 37 return 0; |
| 31 return AmountOfMemory(_SC_PHYS_PAGES); | |
| 32 } | |
| 33 | |
| 34 // static | |
| 35 int64 SysInfo::AmountOfAvailablePhysicalMemory() { | |
| 36 return AmountOfMemory(_SC_AVPHYS_PAGES); | |
| 37 } | |
| 38 | |
| 39 // static | |
| 40 size_t SysInfo::MaxSharedMemorySize() { | |
| 41 static int64 limit; | |
| 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 } | 38 } |
| 39 DCHECK(limit >= 0); | |
| 40 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
| |
| 59 return static_cast<size_t>(limit); | 41 return static_cast<size_t>(limit); |
| 60 } | 42 } |
| 61 | 43 |
| 62 // static | 44 std::string CPUModelName() { |
| 63 std::string SysInfo::CPUModelName() { | |
| 64 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 45 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
| 65 const char kCpuModelPrefix[] = "Hardware"; | 46 const char kCpuModelPrefix[] = "Hardware"; |
| 66 #else | 47 #else |
| 67 const char kCpuModelPrefix[] = "model name"; | 48 const char kCpuModelPrefix[] = "model name"; |
| 68 #endif | 49 #endif |
| 69 std::string contents; | 50 std::string contents; |
| 70 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); | 51 base::ReadFileToString(base::FilePath("/proc/cpuinfo"), &contents); |
| 71 DCHECK(!contents.empty()); | 52 DCHECK(!contents.empty()); |
| 72 if (!contents.empty()) { | 53 if (!contents.empty()) { |
| 73 std::istringstream iss(contents); | 54 std::istringstream iss(contents); |
| 74 std::string line; | 55 std::string line; |
| 75 while (std::getline(iss, line)) { | 56 while (std::getline(iss, line)) { |
| 76 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { | 57 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
| 77 size_t pos = line.find(": "); | 58 size_t pos = line.find(": "); |
| 78 return line.substr(pos + 2); | 59 return line.substr(pos + 2); |
| 79 } | 60 } |
| 80 } | 61 } |
| 81 } | 62 } |
| 82 return std::string(); | 63 return std::string(); |
| 83 } | 64 } |
| 84 | 65 |
| 66 class LazySysInfo { | |
| 67 public: | |
| 68 LazySysInfo() { | |
| 69 physical_memory_ = AmountOfMemory(_SC_PHYS_PAGES); | |
| 70 max_shared_memory_size_ = MaxSharedMemorySize(); | |
| 71 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.
| |
| 72 } | |
| 73 | |
| 74 ~LazySysInfo() { } | |
| 75 | |
| 76 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.
| |
| 77 return physical_memory_; | |
| 78 } | |
| 79 | |
| 80 size_t GetMaxSharedMemorySize() { | |
| 81 return max_shared_memory_size_; | |
| 82 } | |
| 83 | |
| 84 std::string GetCPUModelName() { | |
| 85 return cpu_model_name_; | |
| 86 } | |
| 87 | |
| 88 private: | |
| 89 int64 physical_memory_; | |
| 90 size_t max_shared_memory_size_; | |
| 91 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.
| |
| 92 }; | |
| 93 | |
| 94 base::LazyInstance<LazySysInfo>::Leaky | |
| 95 g_lazy_sys_info = LAZY_INSTANCE_INITIALIZER; | |
| 96 | |
| 97 } // namespace | |
| 98 | |
| 99 namespace base { | |
| 100 | |
| 101 // static | |
| 102 int64 SysInfo::AmountOfAvailablePhysicalMemory() { | |
| 103 return AmountOfMemory(_SC_AVPHYS_PAGES); | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 int64 SysInfo::AmountOfPhysicalMemory() { | |
| 108 return g_lazy_sys_info.Get().GetPhysicalMemory(); | |
| 109 } | |
| 110 | |
| 111 // static | |
| 112 size_t SysInfo::MaxSharedMemorySize() { | |
| 113 return g_lazy_sys_info.Get().GetMaxSharedMemorySize(); | |
| 114 } | |
| 115 | |
| 116 // static | |
| 117 std::string SysInfo::CPUModelName() { | |
| 118 return g_lazy_sys_info.Get().GetCPUModelName(); | |
| 119 } | |
| 120 | |
| 85 } // namespace base | 121 } // namespace base |
| OLD | NEW |