Pyrogenesis  trunk
os_cpu.h
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 /*
24  * OS-specific support functions relating to CPU and memory
25  */
26 
27 #ifndef INCLUDED_OS_CPU
28 #define INCLUDED_OS_CPU
29 
30 namespace ERR
31 {
33 }
34 
35 
36 //-----------------------------------------------------------------------------
37 // processor topology
38 
39 // processor ID = [0, os_cpu_NumProcessors())
40 // they are a numbering of the bits of the process affinity mask where the
41 // least significant nonzero bit corresponds to ID 0.
42 // rationale: this spares users from having to deal with noncontiguous IDs,
43 // e.g. when administrative tools are used to restrict process affinity.
44 
45 
46 /**
47  * maximum number of processors supported by the OS (determined by the
48  * number of bits in an affinity mask)
49  **/
50 static const size_t os_cpu_MaxProcessors = sizeof(uintptr_t)*CHAR_BIT;
51 
52 /**
53  * @return bit mask of processors that exist and are available to
54  * this process.
55  * its population count is by definition equal to os_cpu_NumProcessors().
56  **/
57 LIB_API uintptr_t os_cpu_ProcessorMask();
58 
59 /**
60  * @return the number of processors available to this process.
61  *
62  * note: this function is necessary because POSIX sysconf _SC_NPROCESSORS_CONF
63  * is not suppored on MacOSX, else we would use that.
64  **/
65 LIB_API size_t os_cpu_NumProcessors();
66 
67 // note: we do not provide an os_cpu_CurrentProcessor routine. that would
68 // require Windows 2003 or a lot of work. worse, its results would be
69 // worthless because they may change immediately afterwards. instead,
70 // the recommended approach is to pin OpenMP threads (whose ID can be
71 // queried) to the processor with the same number.
72 
73 
74 //-----------------------------------------------------------------------------
75 // CPU and memory characteristics
76 
77 /**
78  * @return a rough estimate of the CPU clock frequency.
79  * this is usually accurate to a few MHz and is faster than measurement loops.
80  **/
81 LIB_API double os_cpu_ClockFrequency();
82 
83 /**
84  * @return the size [bytes] of a MMU page (4096 on most IA-32 systems)
85  **/
86 LIB_API size_t os_cpu_PageSize();
87 
88 /**
89  * @return the size [bytes] of a large MMU page (4 MiB on most IA-32 systems)
90  * or zero if they are not supported.
91  **/
92 LIB_API size_t os_cpu_LargePageSize();
93 
94 /**
95  * @return the size [MB] of physical memory as reported by the OS;
96  * no caching/validation is performed.
97  **/
98 LIB_API size_t os_cpu_QueryMemorySize();
99 
100 /**
101  * @return the size [MB] of physical memory; caches the result of
102  * os_cpu_QueryMemorySize and overrides it with a more exact value
103  * if SMBIOS information is available.
104  **/
105 LIB_API size_t os_cpu_MemorySize();
106 
107 /**
108  * @return the current amount [MB] of available memory.
109  **/
110 LIB_API size_t os_cpu_MemoryAvailable();
111 
112 
113 //-----------------------------------------------------------------------------
114 // scheduling
115 
116 /**
117  * restrict the current thread to a set of processors.
118  *
119  * @param processorMask a bit mask of acceptable processors
120  * (bit index i corresponds to processor i)
121  * @return the previous mask
122  **/
123 LIB_API uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask);
124 
126 {
127 public:
128  os_cpu_ScopedSetThreadAffinityMask(uintptr_t processorMask)
129  : m_previousProcessorMask(os_cpu_SetThreadAffinityMask(processorMask))
130  {
131  }
132 
134  {
135  (void)os_cpu_SetThreadAffinityMask(m_previousProcessorMask);
136  }
137 
138 private:
140 };
141 
142 
143 /**
144  * called by os_cpu_CallByEachCPU.
145  * @param processor ID of processor running the current thread for the
146  * duration of this function.
147  * @param cbData user-specified data passed through os_cpu_CallByEachCPU.
148  **/
149 typedef void (*OsCpuCallback)(size_t processor, uintptr_t cbData);
150 
151 /**
152  * execute the specified function once on each processor.
153  * this proceeds serially (the callback is never reentered) in increasing
154  * order of processor ID.
155  * fails if process affinity prevents running on all processors.
156  **/
157 LIB_API Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData);
158 
159 #endif // #ifndef INCLUDED_OS_CPU
void(* OsCpuCallback)(size_t processor, uintptr_t cbData)
called by os_cpu_CallByEachCPU.
Definition: os_cpu.h:149
uintptr_t m_previousProcessorMask
Definition: os_cpu.h:139
LIB_API size_t os_cpu_NumProcessors()
Definition: bcpu.cpp:34
static const size_t os_cpu_MaxProcessors
maximum number of processors supported by the OS (determined by the number of bits in an affinity mas...
Definition: os_cpu.h:50
os_cpu_ScopedSetThreadAffinityMask(uintptr_t processorMask)
Definition: os_cpu.h:128
LIB_API uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask)
restrict the current thread to a set of processors.
Definition: bcpu.cpp:109
const Status OS_CPU_RESTRICTED_AFFINITY
Definition: os_cpu.h:32
LIB_API uintptr_t os_cpu_ProcessorMask()
Definition: bcpu.cpp:57
LIB_API size_t os_cpu_MemoryAvailable()
Definition: bcpu.cpp:98
LIB_API size_t os_cpu_MemorySize()
Definition: os_cpu.cpp:63
i64 Status
Error handling system.
Definition: status.h:171
LIB_API double os_cpu_ClockFrequency()
Definition: os_cpu.cpp:43
Introduction
Definition: debug.h:404
LIB_API size_t os_cpu_PageSize()
Definition: bcpu.cpp:68
LIB_API size_t os_cpu_LargePageSize()
Definition: bcpu.cpp:79
~os_cpu_ScopedSetThreadAffinityMask()
Definition: os_cpu.h:133
LIB_API size_t os_cpu_QueryMemorySize()
Definition: bcpu.cpp:86
LIB_API Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
execute the specified function once on each processor.
Definition: bcpu.cpp:115
Definition: os_cpu.h:125