Pyrogenesis  13997
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bcpu.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012 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 #include "lib/module_init.h"
29 
30 #include "valgrind.h"
31 #include <sys/param.h>
32 #include <sys/sysctl.h>
33 
35 {
36  static size_t numProcessors;
37 
38  if(numProcessors == 0)
39  {
40  // Valgrind reports the number of real CPUs, but only emulates a single CPU.
41  // That causes problems when we expect all those CPUs to be distinct, so
42  // just pretend there's only one CPU
43  if (RUNNING_ON_VALGRIND)
44  numProcessors = 1;
45  else
46  {
47  long res = sysconf(_SC_NPROCESSORS_CONF);
48  ENSURE(res != -1);
49  numProcessors = (size_t)res;
50  }
51  }
52 
53  return numProcessors;
54 }
55 
56 
58 {
59  static uintptr_t processorMask;
60 
61  if(!processorMask)
62  processorMask = bit_mask<uintptr_t>(os_cpu_NumProcessors());
63 
64  return processorMask;
65 }
66 
67 
69 {
70  static size_t pageSize;
71 
72  if(!pageSize)
73  pageSize = (size_t)sysconf(_SC_PAGESIZE);
74 
75  return pageSize;
76 }
77 
78 
80 {
81  // assume they're unsupported.
82  return 0;
83 }
84 
85 
87 {
88  size_t memorySize = 0;
89  size_t len = sizeof(memorySize);
90  // Argh, the API doesn't seem to be const-correct
91  /*const*/ int mib[2] = { CTL_HW, HW_PHYSMEM };
92  sysctl(mib, 2, &memorySize, &len, 0, 0);
93  memorySize /= MiB;
94  return memorySize;
95 }
96 
97 
99 {
100  size_t memoryAvailable = 0;
101  size_t len = sizeof(memoryAvailable);
102  // Argh, the API doesn't seem to be const-correct
103  /*const*/ int mib[2] = { CTL_HW, HW_USERMEM };
104  sysctl(mib, 2, &memoryAvailable, &len, 0, 0);
105  memoryAvailable /= MiB;
106  return memoryAvailable;
107 }
108 
109 uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t UNUSED(processorMask))
110 {
111  // not yet implemented
112  return os_cpu_ProcessorMask();
113 }
114 
116 {
117  for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
118  {
119  const uintptr_t processorMask = uintptr_t(1) << processor;
120  os_cpu_SetThreadAffinityMask(processorMask);
121  cb(processor, cbData);
122  }
123 
124  return INFO::OK;
125 }
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