Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ocpu.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2010 Wildfire Games
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining
4  * a copy of this software and associated documentation files (the
5  * "Software"), to deal in the Software without restriction, including
6  * without limitation the rights to use, copy, modify, merge, publish,
7  * distribute, sublicense, and/or sell copies of the Software, and to
8  * permit persons to whom the Software is furnished to do so, subject to
9  * the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included
12  * in all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21  */
22 
23 #include "precompiled.h"
24 
25 #include "lib/sysdep/os_cpu.h"
26 #include "lib/alignment.h"
27 #include "lib/bits.h"
28 
29 #include <sys/sysctl.h>
30 
32 {
33  static size_t numProcessors;
34 
35  if(numProcessors == 0)
36  {
37  // Mac OS X doesn't have sysconf(_SC_NPROCESSORS_CONF)
38  int mib[]={CTL_HW, HW_NCPU};
39  int ncpus;
40  size_t len = sizeof(ncpus);
41  int ret = sysctl(mib, 2, &ncpus, &len, NULL, 0);
42  ENSURE(ret != -1);
43  numProcessors = (size_t)ncpus;
44  }
45 
46  return numProcessors;
47 }
48 
49 
51 {
52  static uintptr_t processorMask;
53 
54  if(!processorMask)
55  processorMask = bit_mask<uintptr_t>(os_cpu_NumProcessors());
56 
57  return processorMask;
58 }
59 
60 
62 {
63  static size_t pageSize;
64 
65  if(!pageSize)
66  pageSize = (size_t)sysconf(_SC_PAGESIZE);
67 
68  return pageSize;
69 }
70 
71 
73 {
74  // assume they're unsupported.
75  return 0;
76 }
77 
78 
80 {
81  size_t memorySize = 0;
82  size_t len = sizeof(memorySize);
83  // Argh, the API doesn't seem to be const-correct
84  /*const*/ int mib[2] = { CTL_HW, HW_PHYSMEM };
85  sysctl(mib, 2, &memorySize, &len, 0, 0);
86  memorySize /= MiB;
87  return memorySize;
88 }
89 
90 
92 {
93  size_t memoryAvailable = 0;
94  size_t len = sizeof(memoryAvailable);
95  // Argh, the API doesn't seem to be const-correct
96  /*const*/ int mib[2] = { CTL_HW, HW_USERMEM };
97  sysctl(mib, 2, &memoryAvailable, &len, 0, 0);
98  memoryAvailable /= MiB;
99  return memoryAvailable;
100 }
101 
102 
103 uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t UNUSED(processorMask))
104 {
105  // not yet implemented. when doing so, see http://developer.apple.com/releasenotes/Performance/RN-AffinityAPI/
106 
107  return os_cpu_ProcessorMask();
108 }
109 
110 
112 {
113  for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
114  {
115  const uintptr_t processorMask = uintptr_t(1) << processor;
116  os_cpu_SetThreadAffinityMask(processorMask);
117  cb(processor, cbData);
118  }
119 
120  return INFO::OK;
121 }
size_t os_cpu_QueryMemorySize()
Definition: bcpu.cpp:86
static const size_t pageSize
Definition: alignment.h:61
#define UNUSED(param)
mark a function parameter as unused and avoid the corresponding compiler warning. ...
const Status OK
Definition: status.h:386
size_t os_cpu_PageSize()
Definition: bcpu.cpp:68
Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
execute the specified function once on each processor.
Definition: bcpu.cpp:115
size_t os_cpu_NumProcessors()
Definition: bcpu.cpp:34
size_t os_cpu_LargePageSize()
Definition: bcpu.cpp:79
uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask)
restrict the current thread to a set of processors.
Definition: bcpu.cpp:109
#define ENSURE(expr)
ensure the expression &lt;expr&gt; evaluates to non-zero.
Definition: debug.h:282
size_t os_cpu_MemoryAvailable()
Definition: bcpu.cpp:98
static const size_t MiB
Definition: alignment.h:72
i64 Status
Error handling system.
Definition: status.h:171
uintptr_t os_cpu_ProcessorMask()
Definition: bcpu.cpp:57
void(* OsCpuCallback)(size_t processor, uintptr_t cbData)
called by os_cpu_CallByEachCPU.
Definition: os_cpu.h:149