File: sys_info_linux.cc

package info (click to toggle)
chromium 145.0.7632.159-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,976,224 kB
  • sloc: cpp: 36,198,469; ansic: 7,634,080; javascript: 3,564,060; python: 1,649,622; xml: 838,470; asm: 717,087; pascal: 185,708; sh: 88,786; perl: 88,718; objc: 79,984; sql: 59,811; cs: 42,452; fortran: 24,101; makefile: 21,144; tcl: 15,277; php: 14,022; yacc: 9,066; ruby: 7,553; awk: 3,720; lisp: 3,233; lex: 1,328; ada: 727; jsp: 228; sed: 36
file content (158 lines) | stat: -rw-r--r-- 4,919 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2011 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/system/sys_info.h"

#include <stddef.h>
#include <stdint.h>

#include <limits>
#include <sstream>
#include <type_traits>

#include "base/byte_size.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::ByteSize AmountOfMemory(int pages_name) {
  long pages = sysconf(pages_name);
  long page_size = sysconf(_SC_PAGESIZE);
  if (pages < 0 || page_size < 0) {
    return base::ByteSize(0);
  }
  return base::ByteSize(base::checked_cast<unsigned long>(page_size)) * pages;
}

base::ByteSize AmountOfPhysicalMemory() {
  return AmountOfMemory(_SC_PHYS_PAGES);
}
using LazyPhysicalMemory =
    base::internal::LazySysInfoValue<base::ByteSize, AmountOfPhysicalMemory>;

}  // namespace

namespace base {

// static
ByteSize SysInfo::AmountOfTotalPhysicalMemoryImpl() {
  static_assert(std::is_trivially_destructible<LazyPhysicalMemory>::value);
  static LazyPhysicalMemory physical_memory;
  return physical_memory.value();
}

// static
ByteSize SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
  SystemMemoryInfo info;
  if (!GetSystemMemoryInfo(&info)) {
    return ByteSize(0);
  }
  return AmountOfAvailablePhysicalMemory(info);
}

// static
ByteSize SysInfo::AmountOfAvailablePhysicalMemory(
    const SystemMemoryInfo& info) {
  // See details here:
  // https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=34e431b0ae398fc54ea69ff85ec700722c9da773
  // The fallback logic (when there is no MemAvailable) would be more precise
  // if we had info about zones watermarks (/proc/zoneinfo).
  if (info.available.is_zero()) {
    return info.free + info.reclaimable + info.inactive_file;
  } else if (info.available > info.active_file) {
    return ByteSize::FromByteSizeDelta(info.available - info.active_file);
  } else {
    return ByteSize(0);
  }
}

// static
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)
  // /proc/cpuinfo does not have a defined ABI and so devices may fall
  // through without a model name.
  // For ARM devices use /sys/devices/socX/soc_id
  //
  // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-devices-soc:
  // On many of ARM based silicon with SMCCC v1.2+ compliant firmware
  // this will contain the SOC ID appended to the family attribute
  // to ensure there is no conflict in this namespace across various
  // vendors. The format is "jep106:XXYY:ZZZZ" where XX is identity
  // code, YY is continuation code and ZZZZ is the SOC ID.

  const char kSocIdDirectory[] = "/sys/devices/soc%u";
  const char kSocIdFile[] = "/sys/devices/soc%u/soc_id";
  const char kJEP106[] = "jep106";

  // There can be multiple /sys/bus/soc/devices/socX on a system.
  // Iterate through until one with jep106:XXYY:ZZZZ is found.
  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)
// static
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

}  // namespace base