This Trac instance is not used for development anymore!

We migrated our development workflow to git and Gitea.
To test the future redirection, replace trac by ariadne in the page URL.

source: ps/trunk/source/lib/sysdep/os/osx/ocpu.cpp

Last change on this file was 26888, checked in by Stan, 3 years ago

Fix RAM amount detection on BSD and macOS.
Tested by: @Mastoras
Comments by: @vladislavbelov

Differential Revision: https://code.wildfiregames.com/D4651

  • Property svn:eol-style set to native
File size: 3.0 KB
RevLine 
[26888]1/* Copyright (C) 2022 Wildfire Games.
[6830]2 *
[7316]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:
[18989]10 *
[7316]11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
[18989]13 *
[7316]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.
[6830]21 */
22
[6828]23#include "precompiled.h"
24
25#include "lib/sysdep/os_cpu.h"
[9365]26#include "lib/alignment.h"
[6828]27#include "lib/bits.h"
28
29#include <sys/sysctl.h>
30
31size_t os_cpu_NumProcessors()
32{
33 static size_t numProcessors;
34
35 if(numProcessors == 0)
36 {
37 // Mac OS X doesn't have sysconf(_SC_NPROCESSORS_CONF)
38 int mib[]={CTL_HW, HW_NCPU};
39 int ncpus;
40 size_t len = sizeof(ncpus);
41 int ret = sysctl(mib, 2, &ncpus, &len, NULL, 0);
[9365]42 ENSURE(ret != -1);
[6828]43 numProcessors = (size_t)ncpus;
44 }
45
46 return numProcessors;
47}
48
49
50uintptr_t os_cpu_ProcessorMask()
51{
52 static uintptr_t processorMask;
53
54 if(!processorMask)
55 processorMask = bit_mask<uintptr_t>(os_cpu_NumProcessors());
56
57 return processorMask;
58}
59
60
61size_t os_cpu_PageSize()
62{
63 static size_t pageSize;
64
65 if(!pageSize)
66 pageSize = (size_t)sysconf(_SC_PAGESIZE);
67
68 return pageSize;
69}
70
71
72size_t os_cpu_LargePageSize()
73{
74 // assume they're unsupported.
75 return 0;
76}
77
78
[9195]79size_t os_cpu_QueryMemorySize()
[6828]80{
[9196]81 size_t memorySize = 0;
[9195]82 size_t len = sizeof(memorySize);
83 // Argh, the API doesn't seem to be const-correct
[26888]84 /*const*/ int mib[2] = { CTL_HW, HW_MEMSIZE };
85 sysctl(mib, 2, &memorySize, &len, nullptr, 0);
[9195]86 memorySize /= MiB;
[6828]87 return memorySize;
88}
89
90
91size_t os_cpu_MemoryAvailable()
92{
93 size_t memoryAvailable = 0;
94 size_t len = sizeof(memoryAvailable);
95 // Argh, the API doesn't seem to be const-correct
96 /*const*/ int mib[2] = { CTL_HW, HW_USERMEM };
[26888]97 sysctl(mib, 2, &memoryAvailable, &len, nullptr, 0);
[6828]98 memoryAvailable /= MiB;
99 return memoryAvailable;
100}
101
102
[9453]103uintptr_t os_cpu_SetThreadAffinityMask(uintptr_t UNUSED(processorMask))
[6828]104{
105 // not yet implemented. when doing so, see http://developer.apple.com/releasenotes/Performance/RN-AffinityAPI/
106
107 return os_cpu_ProcessorMask();
108}
109
110
[9412]111Status os_cpu_CallByEachCPU(OsCpuCallback cb, uintptr_t cbData)
[6828]112{
113 for(size_t processor = 0; processor < os_cpu_NumProcessors(); processor++)
114 {
115 const uintptr_t processorMask = uintptr_t(1) << processor;
116 os_cpu_SetThreadAffinityMask(processorMask);
117 cb(processor, cbData);
118 }
119
120 return INFO::OK;
121}
Note: See TracBrowser for help on using the repository browser.