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 <stddef.h> 8 #include <stdint.h> 9 10 #include <limits> 11 12 #include "base/files/file_util.h" 13 #include "base/lazy_instance.h" 14 #include "base/logging.h" 15 #include "base/numerics/safe_conversions.h" 16 #include "base/strings/string_number_conversions.h" 17 #include "base/sys_info_internal.h" 18 #include "build/build_config.h" 19 20 namespace { 21 22 int64_t AmountOfMemory(int pages_name) { 23 long pages = sysconf(pages_name); 24 long page_size = sysconf(_SC_PAGESIZE); 25 if (pages == -1 || page_size == -1) { 26 NOTREACHED(); 27 return 0; 28 } 29 return static_cast<int64_t>(pages) * page_size; 30 } 31 32 int64_t AmountOfPhysicalMemory() { 33 return AmountOfMemory(_SC_PHYS_PAGES); 34 } 35 36 base::LazyInstance< 37 base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky 38 g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; 39 40 } // namespace 41 42 namespace base { 43 44 // static 45 int64_t SysInfo::AmountOfAvailablePhysicalMemory() { 46 return AmountOfMemory(_SC_AVPHYS_PAGES); 47 } 48 49 // static 50 int64_t SysInfo::AmountOfPhysicalMemory() { 51 return g_lazy_physical_memory.Get().value(); 52 } 53 54 // static 55 std::string SysInfo::CPUModelName() { 56 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) 57 const char kCpuModelPrefix[] = "Hardware"; 58 #else 59 const char kCpuModelPrefix[] = "model name"; 60 #endif 61 std::string contents; 62 ReadFileToString(FilePath("/proc/cpuinfo"), &contents); 63 DCHECK(!contents.empty()); 64 if (!contents.empty()) { 65 std::istringstream iss(contents); 66 std::string line; 67 while (std::getline(iss, line)) { 68 if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { 69 size_t pos = line.find(": "); 70 return line.substr(pos + 2); 71 } 72 } 73 } 74 return std::string(); 75 } 76 77 } // namespace base 78