| 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/logging.h" |
| 14 | #include "base/stl_util.h" |
| 15 |
| 16 | namespace { |
| 17 |
| 18 | int64_t AmountOfMemory(int pages_name) { |
| 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 | } |
| 25 | return static_cast<int64_t>(pages) * page_size; |
| 26 | } |
| 27 |
| 28 | } // namespace |
| 29 |
| 30 | namespace base { |
| 31 |
| 32 | // static |
| 33 | int SysInfo::NumberOfProcessors() { |
| 34 | int mib[] = {CTL_HW, HW_NCPU}; |
| 35 | int ncpu; |
| 36 | size_t size = sizeof(ncpu); |
| 37 | if (sysctl(mib, base::size(mib), &ncpu, &size, NULL, 0) < 0) { |
| 38 | NOTREACHED(); |
| 39 | return 1; |
| 40 | } |
| 41 | return ncpu; |
| 42 | } |
| 43 |
| 44 | // static |
| 45 | int64_t SysInfo::AmountOfPhysicalMemoryImpl() { |
| 46 | return AmountOfMemory(_SC_PHYS_PAGES); |
| 47 | } |
| 48 |
| 49 | // static |
| 50 | int64_t SysInfo::AmountOfAvailablePhysicalMemoryImpl() { |
| 51 | // We should add inactive file-backed memory also but there is no such |
| 52 | // information from OpenBSD unfortunately. |
| 53 | return AmountOfMemory(_SC_AVPHYS_PAGES); |
| 54 | } |
| 55 |
| 56 | // static |
| 57 | uint64_t SysInfo::MaxSharedMemorySize() { |
| 58 | int mib[] = {CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX}; |
| 59 | size_t limit; |
| 60 | size_t size = sizeof(limit); |
| 61 | if (sysctl(mib, base::size(mib), &limit, &size, NULL, 0) < 0) { |
| 62 | NOTREACHED(); |
| 63 | return 0; |
| 64 | } |
| 65 | return static_cast<uint64_t>(limit); |
| 66 | } |
| 67 |
| 68 | // static |
| 69 | std::string SysInfo::CPUModelName() { |
| 70 | int mib[] = {CTL_HW, HW_MODEL}; |
| 71 | char name[256]; |
| 72 | size_t len = base::size(name); |
| 73 | if (sysctl(mib, base::size(mib), name, &len, NULL, 0) < 0) { |
| 74 | NOTREACHED(); |
| 75 | return std::string(); |
| 76 | } |
| 77 | return name; |
| 78 | } |
| 79 |
| 80 | } // namespace base |
| 81 |