#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_libc_calls
#endif
#include "base/system/sys_info.h"
#include <stddef.h>
#include <stdint.h>
#include <algorithm>
#include <limits>
#include <sstream>
#include <type_traits>
#include "base/byte_count.h"
#include "base/check.h"
#include "base/files/file_util.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/process_metrics.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info_internal.h"
#include "build/build_config.h"
namespace {
base::ByteCount AmountOfMemory(int pages_name) {
long pages = sysconf(pages_name);
long page_size = sysconf(_SC_PAGESIZE);
if (pages < 0 || page_size < 0) {
return base::ByteCount(0);
}
return base::ByteCount(page_size) * pages;
}
base::ByteCount AmountOfPhysicalMemory() {
return AmountOfMemory(_SC_PHYS_PAGES);
}
using LazyPhysicalMemory =
base::internal::LazySysInfoValue<base::ByteCount, AmountOfPhysicalMemory>;
}
namespace base {
ByteCount SysInfo::AmountOfPhysicalMemoryImpl() {
static_assert(std::is_trivially_destructible<LazyPhysicalMemory>::value);
static LazyPhysicalMemory physical_memory;
return physical_memory.value();
}
ByteCount SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
SystemMemoryInfo info;
if (!GetSystemMemoryInfo(&info)) {
return ByteCount(0);
}
return AmountOfAvailablePhysicalMemory(info);
}
ByteCount SysInfo::AmountOfAvailablePhysicalMemory(
const SystemMemoryInfo& info) {
ByteCount res =
!info.available.is_zero()
? std::max(info.available - info.active_file, ByteCount(0))
: info.free + info.reclaimable + info.inactive_file;
return res;
}
std::string SysInfo::CPUModelName() {
#if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_ARMEL)
const char kCpuModelPrefix[] = "Hardware";
#else
const char kCpuModelPrefix[] = "model name";
#endif
std::string contents;
ReadFileToString(FilePath("/proc/cpuinfo"), &contents);
DCHECK(!contents.empty());
if (!contents.empty()) {
std::istringstream iss(contents);
std::string line;
while (std::getline(iss, line)) {
if (line.compare(0, strlen(kCpuModelPrefix), kCpuModelPrefix) == 0) {
size_t pos = line.find(": ");
return line.substr(pos + 2);
}
}
}
#if defined(ARCH_CPU_ARMEL)
const char kSocIdDirectory[] = "/sys/devices/soc%u";
const char kSocIdFile[] = "/sys/devices/soc%u/soc_id";
const char kJEP106[] = "jep106";
for (int soc_instance = 0;; ++soc_instance) {
if (!PathExists(
FilePath(base::StringPrintf(kSocIdDirectory, soc_instance)))) {
break;
}
std::string soc_id;
ReadFileToString(FilePath(base::StringPrintf(kSocIdFile, soc_instance)),
&soc_id);
if (soc_id.find(kJEP106) == 0) {
return soc_id;
}
}
#endif
return std::string();
}
#if !BUILDFLAG(IS_CHROMEOS) && !BUILDFLAG(IS_ANDROID)
SysInfo::HardwareInfo SysInfo::GetHardwareInfoSync() {
static const size_t kMaxStringSize = 100u;
HardwareInfo info;
std::string data;
if (ReadFileToStringWithMaxSize(
FilePath("/sys/devices/virtual/dmi/id/sys_vendor"), &data,
kMaxStringSize)) {
TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.manufacturer);
}
if (ReadFileToStringWithMaxSize(
FilePath("/sys/devices/virtual/dmi/id/product_name"), &data,
kMaxStringSize)) {
TrimWhitespaceASCII(data, TrimPositions::TRIM_ALL, &info.model);
}
DCHECK(IsStringUTF8(info.manufacturer));
DCHECK(IsStringUTF8(info.model));
return info;
}
#endif
}