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 limit = 0; |
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 } | |
58 } | 37 } |
| 38 if (limit < 0 || |
| 39 static_cast<uint64>(limit) > std::numeric_limits<size_t>::max()) { |
| 40 limit = 0; |
| 41 } |
| 42 DCHECK(limit > 0); |
59 return static_cast<size_t>(limit); | 43 return static_cast<size_t>(limit); |
60 } | 44 } |
61 | 45 |
62 // static | 46 std::string CPUModelName() { |
63 std::string SysInfo::CPUModelName() { | |
64 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 47 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
65 const char kCpuModelPrefix[] = "Hardware"; | 48 const char kCpuModelPrefix[] = "Hardware"; |
66 #else | 49 #else |
67 const char kCpuModelPrefix[] = "model name"; | 50 const char kCpuModelPrefix[] = "model name"; |
68 #endif | 51 #endif |
69 std::string contents; | 52 std::string contents; |
70 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); | 53 base::ReadFileToString(base::FilePath("/proc/cpuinfo"), &contents); |
71 DCHECK(!contents.empty()); | 54 DCHECK(!contents.empty()); |
72 if (!contents.empty()) { | 55 if (!contents.empty()) { |
73 std::istringstream iss(contents); | 56 std::istringstream iss(contents); |
74 std::string line; | 57 std::string line; |
75 while (std::getline(iss, line)) { | 58 while (std::getline(iss, line)) { |
76 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { | 59 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
77 size_t pos = line.find(": "); | 60 size_t pos = line.find(": "); |
78 return line.substr(pos + 2); | 61 return line.substr(pos + 2); |
79 } | 62 } |
80 } | 63 } |
81 } | 64 } |
82 return std::string(); | 65 return std::string(); |
83 } | 66 } |
84 | 67 |
| 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 |
85 } // namespace base | 116 } // namespace base |
OLD | NEW |