| 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" | |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 int64 AmountOfMemory(int pages_name) { | 15 int64 AmountOfMemory(int pages_name) { |
| 17 long pages = sysconf(pages_name); | 16 long pages = sysconf(pages_name); |
| 18 long page_size = sysconf(_SC_PAGESIZE); | 17 long page_size = sysconf(_SC_PAGESIZE); |
| 19 if (pages == -1 || page_size == -1) { | 18 if (pages == -1 || page_size == -1) { |
| 20 NOTREACHED(); | 19 NOTREACHED(); |
| 21 return 0; | 20 return 0; |
| 22 } | 21 } |
| 23 return static_cast<int64>(pages) * page_size; | 22 return static_cast<int64>(pages) * page_size; |
| 24 } | 23 } |
| 25 | 24 |
| 26 size_t MaxSharedMemorySize() { | 25 } // namespace |
| 27 std::string contents; | 26 |
| 28 base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents); | 27 namespace base { |
| 29 DCHECK(!contents.empty()); | 28 |
| 30 if (!contents.empty() && contents[contents.length() - 1] == '\n') { | 29 // static |
| 31 contents.erase(contents.length() - 1); | 30 int64 SysInfo::AmountOfPhysicalMemory() { |
| 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 } |
| 32 } | 58 } |
| 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); | 59 return static_cast<size_t>(limit); |
| 44 } | 60 } |
| 45 | 61 |
| 46 std::string CPUModelName() { | 62 // static |
| 63 std::string SysInfo::CPUModelName() { |
| 47 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 64 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
| 48 const char kCpuModelPrefix[] = "Hardware"; | 65 const char kCpuModelPrefix[] = "Hardware"; |
| 49 #else | 66 #else |
| 50 const char kCpuModelPrefix[] = "model name"; | 67 const char kCpuModelPrefix[] = "model name"; |
| 51 #endif | 68 #endif |
| 52 std::string contents; | 69 std::string contents; |
| 53 base::ReadFileToString(base::FilePath("/proc/cpuinfo"), &contents); | 70 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
| 54 DCHECK(!contents.empty()); | 71 DCHECK(!contents.empty()); |
| 55 if (!contents.empty()) { | 72 if (!contents.empty()) { |
| 56 std::istringstream iss(contents); | 73 std::istringstream iss(contents); |
| 57 std::string line; | 74 std::string line; |
| 58 while (std::getline(iss, line)) { | 75 while (std::getline(iss, line)) { |
| 59 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { | 76 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
| 60 size_t pos = line.find(": "); | 77 size_t pos = line.find(": "); |
| 61 return line.substr(pos + 2); | 78 return line.substr(pos + 2); |
| 62 } | 79 } |
| 63 } | 80 } |
| 64 } | 81 } |
| 65 return std::string(); | 82 return std::string(); |
| 66 } | 83 } |
| 67 | 84 |
| 68 class LazySysInfo { | |
| 69 public: | |
| 70 LazySysInfo() | |
| 71 : kPhysicalMemory_(AmountOfMemory(_SC_PHYS_PAGES)), | |
| 72 kMaxSharedMemorySize_(MaxSharedMemorySize()), | |
| 73 kCpuModelName_(CPUModelName()) { } | |
| 74 | |
| 75 ~LazySysInfo() { } | |
| 76 | |
| 77 int64 physical_memory() { return kPhysicalMemory_; } | |
| 78 size_t max_shared_memory_size() { return kMaxSharedMemorySize_; } | |
| 79 std::string cpu_model_name() { return kCpuModelName_; } | |
| 80 | |
| 81 private: | |
| 82 const int64 kPhysicalMemory_; | |
| 83 const size_t kMaxSharedMemorySize_; | |
| 84 const std::string kCpuModelName_; | |
| 85 | |
| 86 DISALLOW_COPY_AND_ASSIGN(LazySysInfo); | |
| 87 }; | |
| 88 | |
| 89 base::LazyInstance<LazySysInfo>::Leaky | |
| 90 g_lazy_sys_info = LAZY_INSTANCE_INITIALIZER; | |
| 91 | |
| 92 } // namespace | |
| 93 | |
| 94 namespace base { | |
| 95 | |
| 96 // static | |
| 97 int64 SysInfo::AmountOfAvailablePhysicalMemory() { | |
| 98 return AmountOfMemory(_SC_AVPHYS_PAGES); | |
| 99 } | |
| 100 | |
| 101 // static | |
| 102 int64 SysInfo::AmountOfPhysicalMemory() { | |
| 103 return g_lazy_sys_info.Get().physical_memory(); | |
| 104 } | |
| 105 | |
| 106 // static | |
| 107 size_t SysInfo::MaxSharedMemorySize() { | |
| 108 return g_lazy_sys_info.Get().max_shared_memory_size(); | |
| 109 } | |
| 110 | |
| 111 // static | |
| 112 std::string SysInfo::CPUModelName() { | |
| 113 return g_lazy_sys_info.Get().cpu_model_name(); | |
| 114 } | |
| 115 | |
| 116 } // namespace base | 85 } // namespace base |
| OLD | NEW |