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