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 return 1;
38 }
39 return ncpu;
40 }
41
42 // static
AmountOfPhysicalMemoryImpl()43 uint64_t SysInfo::AmountOfPhysicalMemoryImpl() {
44 return AmountOfMemory(_SC_PHYS_PAGES);
45 }
46
47 // static
AmountOfAvailablePhysicalMemoryImpl()48 uint64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
49 // We should add inactive file-backed memory also but there is no such
50 // information from OpenBSD unfortunately.
51 return AmountOfMemory(_SC_AVPHYS_PAGES);
52 }
53
54 // static
MaxSharedMemorySize()55 uint64_t SysInfo::MaxSharedMemorySize() {
56 int mib[] = {CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX};
57 size_t limit;
58 size_t size = sizeof(limit);
59 if (sysctl(mib, std::size(mib), &limit, &size, NULL, 0) < 0) {
60 NOTREACHED();
61 return 0;
62 }
63 return static_cast<uint64_t>(limit);
64 }
65
66 // static
CPUModelName()67 std::string SysInfo::CPUModelName() {
68 return StringSysctl({CTL_HW, HW_MODEL}).value();
69 }
70
71 } // namespace base
72