xref: /aosp_15_r20/external/cronet/base/system/sys_info_linux.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
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/system/sys_info.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 
10 #include <algorithm>
11 #include <limits>
12 #include <sstream>
13 
14 #include "base/check.h"
15 #include "base/files/file_util.h"
16 #include "base/lazy_instance.h"
17 #include "base/notreached.h"
18 #include "base/numerics/safe_conversions.h"
19 #include "base/process/process_metrics.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/system/sys_info_internal.h"
24 #include "build/build_config.h"
25 
26 namespace {
27 
AmountOfMemory(int pages_name)28 uint64_t AmountOfMemory(int pages_name) {
29   long pages = sysconf(pages_name);
30   long page_size = sysconf(_SC_PAGESIZE);
31   if (pages < 0 || page_size < 0)
32     return 0;
33   return static_cast<uint64_t>(pages) * static_cast<uint64_t>(page_size);
34 }
35 
AmountOfPhysicalMemory()36 uint64_t AmountOfPhysicalMemory() {
37   return AmountOfMemory(_SC_PHYS_PAGES);
38 }
39 
40 base::LazyInstance<
41     base::internal::LazySysInfoValue<uint64_t, AmountOfPhysicalMemory>>::Leaky
42     g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER;
43 
44 }  // namespace
45 
46 namespace base {
47 
48 // static
AmountOfPhysicalMemoryImpl()49 uint64_t SysInfo::AmountOfPhysicalMemoryImpl() {
50   return g_lazy_physical_memory.Get().value();
51 }
52 
53 // static
AmountOfAvailablePhysicalMemoryImpl()54 uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
55   SystemMemoryInfoKB info;
56   if (!GetSystemMemoryInfo(&info))
57     return 0;
58   return AmountOfAvailablePhysicalMemory(info);
59 }
60 
61 // static
AmountOfAvailablePhysicalMemory(const SystemMemoryInfoKB & info)62 uint64_t SysInfo::AmountOfAvailablePhysicalMemory(
63     const SystemMemoryInfoKB& info) {
64   // See details here:
65   // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
66   // The fallback logic (when there is no MemAvailable) would be more precise
67   // if we had info about zones watermarks (/proc/zoneinfo).
68   int res_kb = info.available != 0
69                    ? std::max(info.available - info.active_file, 0)
70                    : info.free + info.reclaimable + info.inactive_file;
71   return checked_cast<uint64_t>(res_kb) * 1024;
72 }
73 
74 // static
CPUModelName()75 std::string SysInfo::CPUModelName() {
76 #if BUILDFLAG(IS_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 
95 #if defined(ARCH_CPU_ARMEL)
96   // /proc/cpuinfo does not have a defined ABI and so devices may fall
97   // through without a model name.
98   // For ARM devices use /sys/devices/socX/soc_id
99   //
100   // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-soc:
101   // On many of ARM based silicon with SMCCC v1.2+ compliant firmware
102   // this will contain the SOC ID appended to the family attribute
103   // to ensure there is no conflict in this namespace across various
104   // vendors. The format is "jep106:XXYY:ZZZZ" where XX is identity
105   // code, YY is continuation code and ZZZZ is the SOC ID.
106 
107   const char kSocIdDirectory[] = "/sys/devices/soc%u";
108   const char kSocIdFile[] = "/sys/devices/soc%u/soc_id";
109   const char kJEP106[] = "jep106";
110 
111   // There can be multiple /sys/bus/soc/devices/socX on a system.
112   // Iterate through until one with jep106:XXYY:ZZZZ is found.
113   for (int soc_instance = 0;; ++soc_instance) {
114     if (!PathExists(
115             FilePath(base::StringPrintf(kSocIdDirectory, soc_instance))))
116       break;
117 
118     std::string soc_id;
119     ReadFileToString(FilePath(base::StringPrintf(kSocIdFile, soc_instance)),
120                      &soc_id);
121     if (soc_id.find(kJEP106) == 0)
122       return soc_id;
123   }
124 #endif
125 
126   return std::string();
127 }
128 
129 #if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
130 // static
GetHardwareInfoSync()131 SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
132   static const size_t kMaxStringSize = 100u;
133   HardwareInfo info;
134   std::string data;
135   if (ReadFileToStringWithMaxSize(
136           FilePath("/sys/devices/virtual/dmi/id/sys_vendor"), &data,
137           kMaxStringSize)) {
138     TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.manufacturer);
139   }
140   if (ReadFileToStringWithMaxSize(
141           FilePath("/sys/devices/virtual/dmi/id/product_name"), &data,
142           kMaxStringSize)) {
143     TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.model);
144   }
145   DCHECK(IsStringUTF8(info.manufacturer));
146   DCHECK(IsStringUTF8(info.model));
147   return info;
148 }
149 #endif
150 
151 }  // namespace base
152