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/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/numerics/safe_conversions.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/sys_info_internal.h" | 14 #include "base/sys_info_internal.h" |
14 | 15 |
15 namespace { | 16 namespace { |
16 | 17 |
17 int64 AmountOfMemory(int pages_name) { | 18 int64 AmountOfMemory(int pages_name) { |
18 long pages = sysconf(pages_name); | 19 long pages = sysconf(pages_name); |
19 long page_size = sysconf(_SC_PAGESIZE); | 20 long page_size = sysconf(_SC_PAGESIZE); |
20 if (pages == -1 || page_size == -1) { | 21 if (pages == -1 || page_size == -1) { |
21 NOTREACHED(); | 22 NOTREACHED(); |
22 return 0; | 23 return 0; |
23 } | 24 } |
24 return static_cast<int64>(pages) * page_size; | 25 return static_cast<int64>(pages) * page_size; |
25 } | 26 } |
26 | 27 |
27 int64 AmountOfPhysicalMemory() { | 28 int64 AmountOfPhysicalMemory() { |
28 return AmountOfMemory(_SC_PHYS_PAGES); | 29 return AmountOfMemory(_SC_PHYS_PAGES); |
29 } | 30 } |
30 | 31 |
31 size_t MaxSharedMemorySize() { | 32 uint64 MaxSharedMemorySize() { |
32 std::string contents; | 33 std::string contents; |
33 base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents); | 34 base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents); |
34 DCHECK(!contents.empty()); | 35 DCHECK(!contents.empty()); |
35 if (!contents.empty() && contents[contents.length() - 1] == '\n') { | 36 if (!contents.empty() && contents[contents.length() - 1] == '\n') { |
36 contents.erase(contents.length() - 1); | 37 contents.erase(contents.length() - 1); |
37 } | 38 } |
38 | 39 |
39 uint64 limit; | 40 uint64 limit; |
40 if (!base::StringToUint64(contents, &limit)) { | 41 if (!base::StringToUint64(contents, &limit)) { |
41 limit = 0; | 42 limit = 0; |
42 } | 43 } |
43 if (limit > std::numeric_limits<size_t>::max()) { | |
44 limit = 0; | |
45 } | |
46 DCHECK_GT(limit, 0u); | 44 DCHECK_GT(limit, 0u); |
47 return static_cast<size_t>(limit); | 45 return limit; |
48 } | 46 } |
49 | 47 |
50 base::LazyInstance< | 48 base::LazyInstance< |
51 base::internal::LazySysInfoValue<int64, AmountOfPhysicalMemory> >::Leaky | 49 base::internal::LazySysInfoValue<int64, AmountOfPhysicalMemory> >::Leaky |
52 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; | 50 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; |
53 base::LazyInstance< | 51 base::LazyInstance< |
54 base::internal::LazySysInfoValue<size_t, MaxSharedMemorySize> >::Leaky | 52 base::internal::LazySysInfoValue<uint64, MaxSharedMemorySize> >::Leaky |
55 g_lazy_max_shared_memory = LAZY_INSTANCE_INITIALIZER; | 53 g_lazy_max_shared_memory = LAZY_INSTANCE_INITIALIZER; |
56 | 54 |
57 } // namespace | 55 } // namespace |
58 | 56 |
59 namespace base { | 57 namespace base { |
60 | 58 |
61 // static | 59 // static |
62 int64 SysInfo::AmountOfAvailablePhysicalMemory() { | 60 int64 SysInfo::AmountOfAvailablePhysicalMemory() { |
63 return AmountOfMemory(_SC_AVPHYS_PAGES); | 61 return AmountOfMemory(_SC_AVPHYS_PAGES); |
64 } | 62 } |
65 | 63 |
66 // static | 64 // static |
67 int64 SysInfo::AmountOfPhysicalMemory() { | 65 int64 SysInfo::AmountOfPhysicalMemory() { |
68 return g_lazy_physical_memory.Get().value(); | 66 return g_lazy_physical_memory.Get().value(); |
69 } | 67 } |
70 | 68 |
71 // static | 69 // static |
72 size_t SysInfo::MaxSharedMemorySize() { | 70 uint64 SysInfo::MaxSharedMemorySize() { |
73 return g_lazy_max_shared_memory.Get().value(); | 71 return g_lazy_max_shared_memory.Get().value(); |
74 } | 72 } |
75 | 73 |
76 // static | 74 // static |
77 std::string SysInfo::CPUModelName() { | 75 std::string SysInfo::CPUModelName() { |
78 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) | 76 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
79 const char kCpuModelPrefix[] = "Hardware"; | 77 const char kCpuModelPrefix[] = "Hardware"; |
80 #else | 78 #else |
81 const char kCpuModelPrefix[] = "model name"; | 79 const char kCpuModelPrefix[] = "model name"; |
82 #endif | 80 #endif |
83 std::string contents; | 81 std::string contents; |
84 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); | 82 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
85 DCHECK(!contents.empty()); | 83 DCHECK(!contents.empty()); |
86 if (!contents.empty()) { | 84 if (!contents.empty()) { |
87 std::istringstream iss(contents); | 85 std::istringstream iss(contents); |
88 std::string line; | 86 std::string line; |
89 while (std::getline(iss, line)) { | 87 while (std::getline(iss, line)) { |
90 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { | 88 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
91 size_t pos = line.find(": "); | 89 size_t pos = line.find(": "); |
92 return line.substr(pos + 2); | 90 return line.substr(pos + 2); |
93 } | 91 } |
94 } | 92 } |
95 } | 93 } |
96 return std::string(); | 94 return std::string(); |
97 } | 95 } |
98 | 96 |
99 } // namespace base | 97 } // namespace base |
OLD | NEW |