blob: 5a1ad561dafea01a88d0fd6bc18258ef47854d7c [file] [log] [blame]
[email protected]817f0f142011-10-13 04:23:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]5f5ac6a2009-11-23 21:57:112// 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/sys_info.h"
6
avi9b6f42932015-12-26 22:15:147#include <stddef.h>
8#include <stdint.h>
[email protected]66700d42010-03-10 07:46:439#include <sys/param.h>
[email protected]167ec822011-10-24 22:05:2710#include <sys/shm.h>
[email protected]66700d42010-03-10 07:46:4311#include <sys/sysctl.h>
12
[email protected]5f5ac6a2009-11-23 21:57:1113#include "base/logging.h"
avi9b6f42932015-12-26 22:15:1414#include "base/macros.h"
[email protected]5f5ac6a2009-11-23 21:57:1115
[email protected]655d7c12013-04-10 02:22:1516namespace {
17
avidd4e614352015-12-09 00:44:4918int64_t AmountOfMemory(int pages_name) {
[email protected]655d7c12013-04-10 02:22:1519 long pages = sysconf(pages_name);
20 long page_size = sysconf(_SC_PAGESIZE);
21 if (pages == -1 || page_size == -1) {
22 NOTREACHED();
23 return 0;
24 }
avidd4e614352015-12-09 00:44:4925 return static_cast<int64_t>(pages) * page_size;
[email protected]655d7c12013-04-10 02:22:1526}
27
28} // namespace
29
[email protected]5f5ac6a2009-11-23 21:57:1130namespace base {
31
[email protected]655d7c12013-04-10 02:22:1532// static
[email protected]5f5ac6a2009-11-23 21:57:1133int SysInfo::NumberOfProcessors() {
34 int mib[] = { CTL_HW, HW_NCPU };
35 int ncpu;
36 size_t size = sizeof(ncpu);
[email protected]655d7c12013-04-10 02:22:1537 if (sysctl(mib, arraysize(mib), &ncpu, &size, NULL, 0) < 0) {
[email protected]5f5ac6a2009-11-23 21:57:1138 NOTREACHED();
39 return 1;
40 }
41 return ncpu;
42}
43
[email protected]655d7c12013-04-10 02:22:1544// static
Eric Karla628c092017-07-26 01:18:3745int64_t SysInfo::AmountOfPhysicalMemoryImpl() {
[email protected]655d7c12013-04-10 02:22:1546 return AmountOfMemory(_SC_PHYS_PAGES);
[email protected]817f0f142011-10-13 04:23:2247}
48
[email protected]655d7c12013-04-10 02:22:1549// static
Eric Karla628c092017-07-26 01:18:3750int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() {
mkolom01ac10b2017-03-22 01:47:2951 // We should add inactive file-backed memory also but there is no such
52 // information from OpenBSD unfortunately.
[email protected]655d7c12013-04-10 02:22:1553 return AmountOfMemory(_SC_AVPHYS_PAGES);
54}
55
56// static
avidd4e614352015-12-09 00:44:4957uint64_t SysInfo::MaxSharedMemorySize() {
[email protected]167ec822011-10-24 22:05:2758 int mib[] = { CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX };
59 size_t limit;
60 size_t size = sizeof(limit);
61 if (sysctl(mib, arraysize(mib), &limit, &size, NULL, 0) < 0) {
62 NOTREACHED();
63 return 0;
64 }
avidd4e614352015-12-09 00:44:4965 return static_cast<uint64_t>(limit);
[email protected]167ec822011-10-24 22:05:2766}
67
[email protected]655d7c12013-04-10 02:22:1568// static
69std::string SysInfo::CPUModelName() {
70 int mib[] = { CTL_HW, HW_MODEL };
71 char name[256];
72 size_t len = arraysize(name);
73 if (sysctl(mib, arraysize(mib), name, &len, NULL, 0) < 0) {
74 NOTREACHED();
75 return std::string();
76 }
77 return name;
78}
79
[email protected]5f5ac6a2009-11-23 21:57:1180} // namespace base