• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <sys/param.h>
10 #include <sys/shm.h>
11 #include <sys/sysctl.h>
12 
13 #include "base/notreached.h"
14 #include "base/posix/sysctl.h"
15 
16 namespace {
17 
AmountOfMemory(int pages_name)18 uint64_t AmountOfMemory(int pages_name) {
19   long pages = sysconf(pages_name);
20   long page_size = sysconf(_SC_PAGESIZE);
21   if (pages < 0 || page_size < 0)
22     return 0;
23   return static_cast<uint64_t>(pages) * static_cast<uint64_t>(page_size);
24 }
25 
26 }  // namespace
27 
28 namespace base {
29 
30 // static
NumberOfProcessors()31 int SysInfo::NumberOfProcessors() {
32   int mib[] = {CTL_HW, HW_NCPU};
33   int ncpu;
34   size_t size = sizeof(ncpu);
35   if (sysctl(mib, std::size(mib), &ncpu, &size, NULL, 0) < 0) {
36     NOTREACHED();
37   }
38   return ncpu;
39 }
40 
41 // static
AmountOfPhysicalMemoryImpl()42 uint64_t SysInfo::AmountOfPhysicalMemoryImpl() {
43   return AmountOfMemory(_SC_PHYS_PAGES);
44 }
45 
46 // static
AmountOfAvailablePhysicalMemoryImpl()47 uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
48   // We should add inactive file-backed memory also but there is no such
49   // information from OpenBSD unfortunately.
50   return AmountOfMemory(_SC_AVPHYS_PAGES);
51 }
52 
53 // static
MaxSharedMemorySize()54 uint64_t SysInfo::MaxSharedMemorySize() {
55   int mib[] = {CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX};
56   size_t limit;
57   size_t size = sizeof(limit);
58   if (sysctl(mib, std::size(mib), &limit, &size, NULL, 0) < 0) {
59     NOTREACHED();
60   }
61   return static_cast<uint64_t>(limit);
62 }
63 
64 // static
CPUModelName()65 std::string SysInfo::CPUModelName() {
66   return StringSysctl({CTL_HW, HW_MODEL}).value();
67 }
68 
69 }  // namespace base
70