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