| 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/process/process_metrics.h" |
| 17 | #include "base/strings/string_number_conversions.h" |
| 18 | #include "base/strings/string_util.h" |
| 19 | #include "base/system/sys_info_internal.h" |
| 20 | #include "build/build_config.h" |
| 21 |
| 22 | namespace { |
| 23 |
| 24 | int64_t AmountOfMemory(int pages_name) { |
| 25 | long pages = sysconf(pages_name); |
| 26 | long page_size = sysconf(_SC_PAGESIZE); |
| 27 | if (pages == -1 || page_size == -1) { |
| 28 | NOTREACHED(); |
| 29 | return 0; |
| 30 | } |
| 31 | return static_cast<int64_t>(pages) * page_size; |
| 32 | } |
| 33 |
| 34 | int64_t AmountOfPhysicalMemory() { |
| 35 | return AmountOfMemory(_SC_PHYS_PAGES); |
| 36 | } |
| 37 |
| 38 | base::LazyInstance< |
| 39 | base::internal::LazySysInfoValue<int64_t, AmountOfPhysicalMemory>>::Leaky |
| 40 | g_lazy_physical_memory = LAZY_INSTANCE_INITIALIZER; |
| 41 |
| 42 | } // namespace |
| 43 |
| 44 | namespace base { |
| 45 |
| 46 | // static |
| 47 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
| 48 | return g_lazy_physical_memory.Get().value(); |
| 49 | } |
| 50 |
| 51 | // static |
| 52 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
| 53 | SystemMemoryInfoKB info; |
| 54 | if (!GetSystemMemoryInfo(&info)) |
| 55 | return 0; |
| 56 | return AmountOfAvailablePhysicalMemory(info); |
| 57 | } |
| 58 |
| 59 | // static |
| 60 | int64_t SysInfo::AmountOfAvailablePhysicalMemory( |
| 61 | const SystemMemoryInfoKB& info) { |
| 62 | // See details here: |
| 63 | // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773 |
| 64 | // The fallback logic (when there is no MemAvailable) would be more precise |
| 65 | // if we had info about zones watermarks (/proc/zoneinfo). |
| 66 | int64_t res_kb = info.available != 0 |
| 67 | ? info.available - info.active_file |
| 68 | : info.free + info.reclaimable + info.inactive_file; |
| 69 | return res_kb * 1024; |
| 70 | } |
| 71 |
| 72 | // static |
| 73 | std::string SysInfo::CPUModelName() { |
| 74 | #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARMEL) |
| 75 | const char kCpuModelPrefix[] = "Hardware"; |
| 76 | #else |
| 77 | const char kCpuModelPrefix[] = "model name"; |
| 78 | #endif |
| 79 | std::string contents; |
| 80 | ReadFileToString(FilePath("/proc/cpuinfo"), &contents); |
| 81 | DCHECK(!contents.empty()); |
| 82 | if (!contents.empty()) { |
| 83 | std::istringstream iss(contents); |
| 84 | std::string line; |
| 85 | while (std::getline(iss, line)) { |
| 86 | if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) { |
| 87 | size_t pos = line.find(": "); |
| 88 | return line.substr(pos + 2); |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | return std::string(); |
| 93 | } |
| 94 |
| 95 | #if !defined(OS_ANDROID) |
| 96 | // static |
| 97 | SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() { |
| 98 | static const size_t kMaxStringSize = 100u; |
| 99 | HardwareInfo info; |
| 100 | std::string data; |
| 101 | if (ReadFileToStringWithMaxSize( |
| 102 | FilePath("/sys/devices/virtual/dmi/id/sys_vendor"), &data, |
| 103 | kMaxStringSize)) { |
| 104 | TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.manufacturer); |
| 105 | } |
| 106 | if (ReadFileToStringWithMaxSize( |
| 107 | FilePath("/sys/devices/virtual/dmi/id/product_name"), &data, |
| 108 | kMaxStringSize)) { |
| 109 | TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.model); |
| 110 | } |
| 111 | DCHECK(IsStringUTF8(info.manufacturer)); |
| 112 | DCHECK(IsStringUTF8(info.model)); |
| 113 | return info; |
| 114 | } |
| 115 | #endif |
| 116 |
| 117 | } // namespace base |
| 118 |