blob: 531c11733b62b241c919c19d99cf290ed24927e7 [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
avidd4e614352015-12-09 00:44:4945int64_t SysInfo::AmountOfPhysicalMemory() {
[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
avidd4e614352015-12-09 00:44:4950int64_t SysInfo::AmountOfAvailablePhysicalMemory() {
[email protected]655d7c12013-04-10 02:22:1551 return AmountOfMemory(_SC_AVPHYS_PAGES);
52}
53
54// static
avidd4e614352015-12-09 00:44:4955uint64_t SysInfo::MaxSharedMemorySize() {
[email protected]167ec822011-10-24 22:05:2756 int mib[] = { CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX };
57 size_t limit;
58 size_t size = sizeof(limit);
59 if (sysctl(mib, arraysize(mib), &limit, &size, NULL, 0) < 0) {
60 NOTREACHED();
61 return 0;
62 }
avidd4e614352015-12-09 00:44:4963 return static_cast<uint64_t>(limit);
[email protected]167ec822011-10-24 22:05:2764}
65
[email protected]655d7c12013-04-10 02:22:1566// static
67std::string SysInfo::CPUModelName() {
68 int mib[] = { CTL_HW, HW_MODEL };
69 char name[256];
70 size_t len = arraysize(name);
71 if (sysctl(mib, arraysize(mib), name, &len, NULL, 0) < 0) {
72 NOTREACHED();
73 return std::string();
74 }
75 return name;
76}
77
[email protected]5f5ac6a2009-11-23 21:57:1178} // namespace base