OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/sys_info.h" | 5 #include "base/sys_info.h" |
6 | 6 |
7 #include <sys/param.h> | 7 #include <sys/param.h> |
8 #include <sys/shm.h> | 8 #include <sys/shm.h> |
9 #include <sys/sysctl.h> | 9 #include <sys/sysctl.h> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 | 12 |
13 namespace { | |
14 | |
15 int64 AmountOfMemory(int pages_name) { | |
Mark Mentovai
2013/04/09 21:25:28
This is good. You can do Linux this way too, and W
mdempsky_google
2013/04/09 21:45:09
Done. (Not sure if the way I did the Windows code
| |
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 | |
13 namespace base { | 27 namespace base { |
14 | 28 |
29 // static | |
15 int SysInfo::NumberOfProcessors() { | 30 int SysInfo::NumberOfProcessors() { |
16 int mib[] = { CTL_HW, HW_NCPU }; | 31 static const int mib[] = { CTL_HW, HW_NCPU }; |
17 int ncpu; | 32 int ncpu; |
18 size_t size = sizeof(ncpu); | 33 size_t size = sizeof(ncpu); |
19 if (sysctl(mib, arraysize(mib), &ncpu, &size, NULL, 0) == -1) { | 34 if (sysctl(const_cast<int*>(mib), arraysize(mib), &ncpu, &size, NULL, 0) |
Mark Mentovai
2013/04/09 21:25:28
No no, don’t const_cast it, that’s even worse. Jus
mdempsky_google
2013/04/09 21:45:09
Okay, reverted.
| |
35 == -1) { | |
20 NOTREACHED(); | 36 NOTREACHED(); |
21 return 1; | 37 return 1; |
22 } | 38 } |
23 return ncpu; | 39 return ncpu; |
24 } | 40 } |
25 | 41 |
42 // static | |
26 int64 SysInfo::AmountOfPhysicalMemory() { | 43 int64 SysInfo::AmountOfPhysicalMemory() { |
27 long pages = sysconf(_SC_PHYS_PAGES); | 44 return AmountOfMemory(_SC_PHYS_PAGES); |
28 long page_size = sysconf(_SC_PAGESIZE); | |
29 if (pages == -1 || page_size == -1) { | |
30 NOTREACHED(); | |
31 return 0; | |
32 } | |
33 | |
34 return static_cast<int64>(pages) * page_size; | |
35 } | 45 } |
36 | 46 |
47 // static | |
48 int64 SysInfo::AmountOfAvailablePhysicalMemory() { | |
49 return AmountOfMemory(_SC_AVPHYS_PAGES); | |
50 } | |
51 | |
52 // static | |
37 size_t SysInfo::MaxSharedMemorySize() { | 53 size_t SysInfo::MaxSharedMemorySize() { |
38 int mib[] = { CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX }; | 54 static const int mib[] = { CTL_KERN, KERN_SHMINFO, KERN_SHMINFO_SHMMAX }; |
39 size_t limit; | 55 size_t limit; |
40 size_t size = sizeof(limit); | 56 size_t size = sizeof(limit); |
41 if (sysctl(mib, arraysize(mib), &limit, &size, NULL, 0) < 0) { | 57 if (sysctl(const_cast<int*>(mib), arraysize(mib), &limit, &size, NULL, 0) |
58 == -1) { | |
42 NOTREACHED(); | 59 NOTREACHED(); |
43 return 0; | 60 return 0; |
44 } | 61 } |
45 return limit; | 62 return limit; |
46 } | 63 } |
47 | 64 |
65 // static | |
66 std::string SysInfo::CPUModelName() { | |
67 static const int mib[] = { CTL_HW, HW_MODEL }; | |
68 char name[256]; | |
69 size_t len = arraysize(name); | |
70 if (sysctl(const_cast<int*>(mib), arraysize(mib), name, &len, NULL, 0) | |
71 == -1) { | |
72 NOTREACHED(); | |
73 return std::string(); | |
74 } | |
75 return name; | |
76 } | |
77 | |
48 } // namespace base | 78 } // namespace base |
OLD | NEW |