Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 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 | |
| 7 | #include <sys/param.h> |
| 8 | #include <sys/shm.h> |
| 9 | #include <sys/sysctl.h> |
| 10 | |
| 11 | #include "base/logging.h" |
| 12 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 13 | namespace { |
| 14 | |
| 15 | int64 AmountOfMemory(int pages_name) { |
| 16 | long pages = sysconf(pages_name); |
| 17 | long page_size = sysconf(_SC_PAGESIZE); |
| 18 | if (pages == -1 || page_size == -1) { |
| 19 | NOTREACHED(); |
| 20 | return 0; |
| 21 | } |
| 22 | return static_cast<int64>(pages) * page_size; |
| 23 | } |
| 24 | |
| 25 | } // namespace |
| 26 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | namespace base { |
| 28 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 29 | // static |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 30 | int SysInfo::NumberOfProcessors() { |
| 31 | int mib[] = { CTL_HW, HW_NCPU }; |
| 32 | int ncpu; |
| 33 | size_t size = sizeof(ncpu); |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 34 | if (sysctl(mib, arraysize(mib), &ncpu, &size, NULL, 0) < 0) { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 35 | NOTREACHED(); |
| 36 | return 1; |
| 37 | } |
| 38 | return ncpu; |
| 39 | } |
| 40 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 41 | // static |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 42 | int64 SysInfo::AmountOfPhysicalMemory() { |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 43 | return AmountOfMemory(_SC_PHYS_PAGES); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 46 | // static |
| 47 | int64 SysInfo::AmountOfAvailablePhysicalMemory() { |
| 48 | return AmountOfMemory(_SC_AVPHYS_PAGES); |
| 49 | } |
| 50 | |
| 51 | // static |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 52 | size_t SysInfo::MaxSharedMemorySize() { |
| 53 | int mib[] = { CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX }; |
| 54 | size_t limit; |
| 55 | size_t size = sizeof(limit); |
| 56 | if (sysctl(mib, arraysize(mib), &limit, &size, NULL, 0) < 0) { |
| 57 | NOTREACHED(); |
| 58 | return 0; |
| 59 | } |
| 60 | return limit; |
| 61 | } |
| 62 | |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 63 | // static |
| 64 | std::string SysInfo::CPUModelName() { |
| 65 | int mib[] = { CTL_HW, HW_MODEL }; |
| 66 | char name[256]; |
| 67 | size_t len = arraysize(name); |
| 68 | if (sysctl(mib, arraysize(mib), name, &len, NULL, 0) < 0) { |
| 69 | NOTREACHED(); |
| 70 | return std::string(); |
| 71 | } |
| 72 | return name; |
| 73 | } |
| 74 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 75 | } // namespace base |