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 
AmountOfMemory(int pages_name)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 
AmountOfPhysicalMemory()32 int64_t AmountOfPhysicalMemory() {
33   return AmountOfMemory(_SC_PHYS_PAGES);
34 }
35 
MaxSharedMemorySize()36 uint64_t MaxSharedMemorySize() {
37   std::string contents;
38   base::ReadFileToString(base::FilePath("/proc/sys/kernel/shmmax"), &contents);
39   DCHECK(!contents.empty());
40   if (!contents.empty() && contents[contents.length() - 1] == '\n') {
41     contents.erase(contents.length() - 1);
42   }
43 
44   uint64_t limit;
45   if (!base::StringToUint64(contents, &limit)) {
46     limit = 0;
47   }
48   DCHECK_GT(limit, 0u);
49   return limit;
50 }
51 
52 base::LazyInstance<
53     base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky
54     g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER;
55 base::LazyInstance<
56     base::internal::LazySysInfoValue<uint64_t, MaxSharedMemorySize>>::Leaky
57     g_lazy_max_shared_memory = LAZY_INSTANCE_INITIALIZER;
58 
59 }  // namespace
60 
61 namespace base {
62 
63 // static
AmountOfAvailablePhysicalMemory()64 int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
65   return AmountOfMemory(_SC_AVPHYS_PAGES);
66 }
67 
68 // static
AmountOfPhysicalMemory()69 int64_t SysInfo::AmountOfPhysicalMemory() {
70   return g_lazy_physical_memory.Get().value();
71 }
72 
73 // static
MaxSharedMemorySize()74 uint64_t SysInfo::MaxSharedMemorySize() {
75   return g_lazy_max_shared_memory.Get().value();
76 }
77 
78 // static
CPUModelName()79 std::string SysInfo::CPUModelName() {
80 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
81   const char kCpuModelPrefix[] = "Hardware";
82 #else
83   const char kCpuModelPrefix[] = "model name";
84 #endif
85   std::string contents;
86   ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
87   DCHECK(!contents.empty());
88   if (!contents.empty()) {
89     std::istringstream iss(contents);
90     std::string line;
91     while (std::getline(iss, line)) {
92       if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
93         size_t pos = line.find(": ");
94         return line.substr(pos + 2);
95       }
96     }
97   }
98   return std::string();
99 }
100 
101 }  // namespace base
102