[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 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/sys_info.h" |
| 6 | |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
[email protected] | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 9 | #include <sys/param.h> |
[email protected] | 167ec82 | 2011-10-24 22:05:27 | [diff] [blame] | 10 | #include <sys/shm.h> |
[email protected] | 66700d4 | 2010-03-10 07:46:43 | [diff] [blame] | 11 | #include <sys/sysctl.h> |
| 12 | |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 13 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 15 | |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 16 | namespace { |
| 17 | |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 18 | int64_t AmountOfMemory(int pages_name) { |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 19 | 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 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 25 | return static_cast<int64_t>(pages) * page_size; |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | } // namespace |
| 29 | |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 30 | namespace base { |
| 31 | |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 32 | // static |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 33 | int SysInfo::NumberOfProcessors() { |
| 34 | int mib[] = { CTL_HW, HW_NCPU }; |
| 35 | int ncpu; |
| 36 | size_t size = sizeof(ncpu); |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 37 | if (sysctl(mib, arraysize(mib), &ncpu, &size, NULL, 0) < 0) { |
[email protected] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 38 | NOTREACHED(); |
| 39 | return 1; |
| 40 | } |
| 41 | return ncpu; |
| 42 | } |
| 43 | |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 44 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 45 | int64_t SysInfo::AmountOfPhysicalMemory() { |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 46 | return AmountOfMemory(_SC_PHYS_PAGES); |
[email protected] | 817f0f14 | 2011-10-13 04:23:22 | [diff] [blame] | 47 | } |
| 48 | |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 49 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 50 | int64_t SysInfo::AmountOfAvailablePhysicalMemory() { |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 51 | return AmountOfMemory(_SC_AVPHYS_PAGES); |
| 52 | } |
| 53 | |
| 54 | // static |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 55 | uint64_t SysInfo::MaxSharedMemorySize() { |
[email protected] | 167ec82 | 2011-10-24 22:05:27 | [diff] [blame] | 56 | 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 | } |
avi | dd4e61435 | 2015-12-09 00:44:49 | [diff] [blame] | 63 | return static_cast<uint64_t>(limit); |
[email protected] | 167ec82 | 2011-10-24 22:05:27 | [diff] [blame] | 64 | } |
| 65 | |
[email protected] | 655d7c1 | 2013-04-10 02:22:15 | [diff] [blame] | 66 | // static |
| 67 | std::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] | 5f5ac6a | 2009-11-23 21:57:11 | [diff] [blame] | 78 | } // namespace base |