Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
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
30namespace 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 **/
50static 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 **/
57uintptr_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 **/
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 **/
76
77/**
78 * @return the size [bytes] of a MMU page (4096 on most IA-32 systems)
79 **/
80size_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 **/
87
88/**
89 * @return the size [MB] of physical memory as reported by the OS;
90 * no caching/validation is performed.
91 **/
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 **/
99size_t os_cpu_MemorySize();
100
101/**
102 * @return the current amount [MB] of available memory.
103 **/
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 **/
117uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask);
118
120{
121public:
122 os_cpu_ScopedSetThreadAffinityMask(uintptr_t processorMask)
124 {
125 }
126
128 {
130 }
131
132private:
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 **/
143typedef 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 **/
151Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData);
152
153#endif // #ifndef INCLUDED_OS_CPU
Definition: os_cpu.h:120
uintptr_t m_previousProcessorMask
Definition: os_cpu.h:133
~os_cpu_ScopedSetThreadAffinityMask()
Definition: os_cpu.h:127
os_cpu_ScopedSetThreadAffinityMask(uintptr_t processorMask)
Definition: os_cpu.h:122
Definition: debug.h:395
const Status OS_CPU_RESTRICTED_AFFINITY
Definition: os_cpu.h:32
size_t os_cpu_NumProcessors()
Definition: bcpu.cpp:37
size_t os_cpu_QueryMemorySize()
Definition: bcpu.cpp:91
void(* OsCpuCallback)(size_t processor, uintptr_t cbData)
called by os_cpu_CallByEachCPU.
Definition: os_cpu.h:143
size_t os_cpu_MemoryAvailable()
Definition: bcpu.cpp:103
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
double os_cpu_ClockFrequency()
Definition: os_cpu.cpp:43
size_t os_cpu_PageSize()
Definition: bcpu.cpp:73
size_t os_cpu_MemorySize()
Definition: os_cpu.cpp:63
size_t os_cpu_LargePageSize()
Definition: bcpu.cpp:84
Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
execute the specified function once on each processor.
Definition: bcpu.cpp:120
uintptr_t os_cpu_ProcessorMask()
Definition: bcpu.cpp:62
uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t processorMask)
restrict the current thread to a set of processors.
Definition: bcpu.cpp:114
i64 Status
Error handling system.
Definition: status.h:173