| 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 | #include "precompiled.h"
|
|---|
| 24 |
|
|---|
| 25 | #include "lib/sysdep/os_cpu.h"
|
|---|
| 26 | #include "lib/alignment.h"
|
|---|
| 27 | #include "lib/bits.h"
|
|---|
| 28 | #include "lib/config2.h"
|
|---|
| 29 | #include "lib/module_init.h"
|
|---|
| 30 |
|
|---|
| 31 | #if CONFIG2_VALGRIND
|
|---|
| 32 | # include "valgrind.h"
|
|---|
| 33 | #endif
|
|---|
| 34 | #include <sys/param.h>
|
|---|
| 35 | #include <sys/sysctl.h>
|
|---|
| 36 |
|
|---|
| 37 | size_t os_cpu_NumProcessors()
|
|---|
| 38 | {
|
|---|
| 39 | static size_t numProcessors;
|
|---|
| 40 |
|
|---|
| 41 | if(numProcessors == 0)
|
|---|
| 42 | {
|
|---|
| 43 | #if CONFIG2_VALGRIND
|
|---|
| 44 | // Valgrind reports the number of real CPUs, but only emulates a single CPU.
|
|---|
| 45 | // That causes problems when we expect all those CPUs to be distinct, so
|
|---|
| 46 | // just pretend there's only one CPU
|
|---|
| 47 | if (RUNNING_ON_VALGRIND)
|
|---|
| 48 | numProcessors = 1;
|
|---|
| 49 | else
|
|---|
| 50 | #endif
|
|---|
| 51 | {
|
|---|
| 52 | long res = sysconf(_SC_NPROCESSORS_CONF);
|
|---|
| 53 | ENSURE(res != -1);
|
|---|
| 54 | numProcessors = (size_t)res;
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | return numProcessors;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | uintptr_t os_cpu_ProcessorMask()
|
|---|
| 63 | {
|
|---|
| 64 | static uintptr_t processorMask;
|
|---|
| 65 |
|
|---|
| 66 | if(!processorMask)
|
|---|
| 67 | processorMask = bit_mask<uintptr_t>(os_cpu_NumProcessors());
|
|---|
| 68 |
|
|---|
| 69 | return processorMask;
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 |
|
|---|
| 73 | size_t os_cpu_PageSize()
|
|---|
| 74 | {
|
|---|
| 75 | static size_t pageSize;
|
|---|
| 76 |
|
|---|
| 77 | if(!pageSize)
|
|---|
| 78 | pageSize = (size_t)sysconf(_SC_PAGESIZE);
|
|---|
| 79 |
|
|---|
| 80 | return pageSize;
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 | size_t os_cpu_LargePageSize()
|
|---|
| 85 | {
|
|---|
| 86 | // assume they're unsupported.
|
|---|
| 87 | return 0;
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | size_t os_cpu_QueryMemorySize()
|
|---|
| 92 | {
|
|---|
| 93 | size_t memorySize = 0;
|
|---|
| 94 | size_t len = sizeof(memorySize);
|
|---|
| 95 | // Argh, the API doesn't seem to be const-correct
|
|---|
| 96 | /*const*/ int mib[2] = { CTL_HW, HW_PHYSMEM };
|
|---|
| 97 | sysctl(mib, 2, &memorySize, &len, nullptr, 0);
|
|---|
| 98 | memorySize /= MiB;
|
|---|
| 99 | return memorySize;
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 | size_t os_cpu_MemoryAvailable()
|
|---|
| 104 | {
|
|---|
| 105 | size_t memoryAvailable = 0;
|
|---|
| 106 | size_t len = sizeof(memoryAvailable);
|
|---|
| 107 | // Argh, the API doesn't seem to be const-correct
|
|---|
| 108 | /*const*/ int mib[2] = { CTL_HW, HW_USERMEM };
|
|---|
| 109 | sysctl(mib, 2, &memoryAvailable, &len, nullptr, 0);
|
|---|
| 110 | memoryAvailable /= MiB;
|
|---|
| 111 | return memoryAvailable;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t UNUSED(processorMask))
|
|---|
| 115 | {
|
|---|
| 116 | // not yet implemented
|
|---|
| 117 | return os_cpu_ProcessorMask();
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
|
|---|
| 121 | {
|
|---|
| 122 | for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
|
|---|
| 123 | {
|
|---|
| 124 | const uintptr_t processorMask = uintptr_t(1) << processor;
|
|---|
| 125 | os_cpu_SetThreadAffinityMask(processorMask);
|
|---|
| 126 | cb(processor, cbData);
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | return INFO::OK;
|
|---|
| 130 | }
|
|---|