sys_info_linux.cc 4.7 KB

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