Pyrogenesis  trunk
os_cpu.h
Go to the documentation of this file.
1 /* Copyright (C) 2022 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 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 size_t os_cpu_NumProcessors();
66 
67 
68 //-----------------------------------------------------------------------------
69 // CPU and memory characteristics
70 
71 /**
72  * @return a rough estimate of the CPU clock frequency.
73  * this is usually accurate to a few MHz and is faster than measurement loops.
74  **/
75 double os_cpu_ClockFrequency();
76 
77 /**
78  * @return the size [bytes] of a MMU page (4096 on most IA-32 systems)
79  **/
80 size_t os_cpu_PageSize();
81 
82 /**
83  * @return the size [bytes] of a large MMU page (4 MiB on most IA-32 systems)
84  * or zero if they are not supported.
85  **/
86 size_t os_cpu_LargePageSize();
87 
88 /**
89  * @return the size [MB] of physical memory as reported by the OS;
90  * no caching/validation is performed.
91  **/
92 size_t os_cpu_QueryMemorySize();
93 
94 /**
95  * @return the size [MB] of physical memory; caches the result of
96  * os_cpu_QueryMemorySize and overrides it with a more exact value
97  * if SMBIOS information is available.
98  **/
99 size_t os_cpu_MemorySize();
100 
101 /**
102  * @return the current amount [MB] of available memory.
103  **/
104 size_t os_cpu_MemoryAvailable();
105 
106 
107 //-----------------------------------------------------------------------------
108 // scheduling
109 
110 /**
111  * restrict the current thread to a set of processors.
112  *
113  * @param processorMask a bit mask of acceptable processors
114  * (bit index i corresponds to processor i)
115  * @return the previous mask
116  **/
117 uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask);
118 
120 {
121 public:
122  os_cpu_ScopedSetThreadAffinityMask(uintptr_t processorMask)
123  : m_previousProcessorMask(os_cpu_SetThreadAffinityMask(processorMask))
124  {
125  }
126 
128  {
129  (void)os_cpu_SetThreadAffinityMask(m_previousProcessorMask);
130  }
131 
132 private:
134 };
135 
136 
137 /**
138  * called by os_cpu_CallByEachCPU.
139  * @param processor ID of processor running the current thread for the
140  * duration of this function.
141  * @param cbData user-specified data passed through os_cpu_CallByEachCPU.
142  **/
143 typedef void (*OsCpuCallback)(size_t processor, uintptr_t cbData);
144 
145 /**
146  * execute the specified function once on each processor.
147  * this proceeds serially (the callback is never reentered) in increasing
148  * order of processor ID.
149  * fails if process affinity prevents running on all processors.
150  **/
151 Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData);
152 
153 #endif // #ifndef INCLUDED_OS_CPU
void(* OsCpuCallback)(size_t processor, uintptr_t cbData)
called by os_cpu_CallByEachCPU.
Definition: os_cpu.h:143
uintptr_t m_previousProcessorMask
Definition: os_cpu.h:133
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:122
Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
execute the specified function once on each processor.
Definition: bcpu.cpp:120
size_t os_cpu_QueryMemorySize()
Definition: bcpu.cpp:91
const Status OS_CPU_RESTRICTED_AFFINITY
Definition: os_cpu.h:32
double os_cpu_ClockFrequency()
Definition: os_cpu.cpp:43
size_t os_cpu_NumProcessors()
Definition: bcpu.cpp:37
uintptr_t os_cpu_ProcessorMask()
Definition: bcpu.cpp:62
i64 Status
Error handling system.
Definition: status.h:169
size_t os_cpu_MemoryAvailable()
Definition: bcpu.cpp:103
uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask)
restrict the current thread to a set of processors.
Definition: bcpu.cpp:114
size_t os_cpu_LargePageSize()
Definition: bcpu.cpp:84
Introduction
Definition: debug.h:407
size_t os_cpu_PageSize()
Definition: bcpu.cpp:73
~os_cpu_ScopedSetThreadAffinityMask()
Definition: os_cpu.h:127
size_t os_cpu_MemorySize()
Definition: os_cpu.cpp:63
Definition: os_cpu.h:119