Blender V5.3
system.cpp
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "util/system.h"
6#include "util/log.h"
7#include "util/string.h"
8
9#include <algorithm>
10#include <cstring>
11
12#ifdef _WIN32
13# include <cstdio>
14# if (!defined(FREE_WINDOWS))
15# include <intrin.h>
16# endif
17# include "util/windows.h"
18#elif defined(__APPLE__)
19# include <cstdint>
20# include <sys/ioctl.h>
21# include <sys/resource.h>
22# include <sys/sysctl.h>
23# include <sys/types.h>
24# include <unistd.h>
25#else
26# include <cstdint>
27# include <sys/ioctl.h>
28# include <sys/resource.h>
29# include <unistd.h>
30#endif
31
33
35{
36 int columns = 0;
37
38#ifdef _WIN32
39 CONSOLE_SCREEN_BUFFER_INFO csbi;
40 if (GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi)) {
41 columns = csbi.dwSize.X;
42 }
43#else
44 struct winsize w;
45 if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == 0) {
46 columns = w.ws_col;
47 }
48#endif
49
50 return (columns > 0) ? columns : 80;
51}
52
53/* Equivalent of Windows __cpuid for x86 processors on other platforms. */
54#if (!defined(_WIN32) || defined(FREE_WINDOWS)) && (defined(__x86_64__) || defined(__i386__))
55static void __cpuid(int data[4], int selector)
56{
57# if defined(__x86_64__)
58 asm("cpuid" : "=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : "a"(selector));
59# elif defined(__i386__)
60 asm("pushl %%ebx \n\t"
61 "cpuid \n\t"
62 "movl %%ebx, %1 \n\t"
63 "popl %%ebx \n\t"
64 : "=a"(data[0]), "=r"(data[1]), "=c"(data[2]), "=d"(data[3])
65 : "a"(selector)
66 : "ebx");
67# else
68 data[0] = data[1] = data[2] = data[3] = 0;
69# endif
70}
71#endif
72
74{
75#if defined(__APPLE__)
76 /* Get from system on macOS. */
77 char modelname[512] = "";
78 size_t bufferlen = 512;
79 if (sysctlbyname("machdep.cpu.brand_string", &modelname, &bufferlen, nullptr, 0) == 0) {
80 return modelname;
81 }
82#elif (defined(WIN32) || defined(__x86_64__) || defined(__i386__)) && !defined(_M_ARM64)
83 /* Get from intrinsics on Windows and x86. */
84 char buf[49] = {0};
85 int result[4] = {0};
86
87 __cpuid(result, 0x80000000);
88
89 if (result[0] != 0 && result[0] >= (int)0x80000004) {
90 __cpuid((int *)(buf + 0), 0x80000002);
91 __cpuid((int *)(buf + 16), 0x80000003);
92 __cpuid((int *)(buf + 32), 0x80000004);
93
94 string brand = buf;
95
96 /* Make it a bit more presentable. */
97 brand = string_remove_trademark(brand);
99
100 return brand;
101 }
102#elif defined(_M_ARM64)
103 DWORD processorNameStringLength = 255;
104 char processorNameString[255];
105 if (RegGetValueA(HKEY_LOCAL_MACHINE,
106 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
107 "ProcessorNameString",
108 RRF_RT_REG_SZ,
109 nullptr,
110 &processorNameString,
111 &processorNameStringLength) == ERROR_SUCCESS)
112 {
113 return processorNameString;
114 }
115#else
116 /* Get from /proc/cpuinfo on Unix systems. */
117 FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
118 if (cpuinfo != nullptr) {
119 char cpuinfo_buf[513] = "";
120 fread(cpuinfo_buf, sizeof(cpuinfo_buf) - 1, 1, cpuinfo);
121 fclose(cpuinfo);
122
123 char *modelname = strstr(cpuinfo_buf, "model name");
124 if (modelname != nullptr) {
125 modelname = strchr(modelname, ':');
126 if (modelname != nullptr) {
127 modelname += 2;
128 char *modelname_end = strchr(modelname, '\n');
129 if (modelname_end != nullptr) {
130 *modelname_end = '\0';
131 return modelname;
132 }
133 }
134 }
135 }
136#endif
137 return "Unknown CPU";
138}
139
141{
142 return (sizeof(void *) * 8);
143}
144
145#if defined(__x86_64__) || defined(_M_X64) || defined(__i386__) || defined(_M_IX86)
146
147struct CPUCapabilities {
148 bool sse42;
149 bool avx2;
150 bool f16c;
151};
152
153static CPUCapabilities system_cpu_capabilities_init()
154{
155 CPUCapabilities caps = {};
156
157 int result[4], num;
158
159 __cpuid(result, 0);
160 num = result[0];
161
162 if (num >= 1) {
163 __cpuid(result, 0x00000001);
164 const bool sse = (result[3] & ((int)1 << 25)) != 0;
165 const bool sse2 = (result[3] & ((int)1 << 26)) != 0;
166 const bool sse3 = (result[2] & ((int)1 << 0)) != 0;
167
168 const bool ssse3 = (result[2] & ((int)1 << 9)) != 0;
169 const bool sse41 = (result[2] & ((int)1 << 19)) != 0;
170 const bool sse42 = (result[2] & ((int)1 << 20)) != 0;
171
172 const bool fma3 = (result[2] & ((int)1 << 12)) != 0;
173 const bool os_uses_xsave_xrestore = (result[2] & ((int)1 << 27)) != 0;
174 const bool cpu_avx_support = (result[2] & ((int)1 << 28)) != 0;
175
176 /* Simplify to combined capabilities for which we specialize kernels. */
177 caps.sse42 = sse && sse2 && sse3 && ssse3 && sse41 && sse42;
178
179 if (os_uses_xsave_xrestore && cpu_avx_support) {
180 // Check if the OS will save the YMM registers
181 uint32_t xcr_feature_mask;
182# if defined(__GNUC__)
183 int edx; /* not used */
184 /* actual opcode for xgetbv */
185 __asm__(".byte 0x0f, 0x01, 0xd0" : "=a"(xcr_feature_mask), "=d"(edx) : "c"(0));
186# elif defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK)
187 /* Minimum VS2010 SP1 compiler is required. */
188 xcr_feature_mask = (uint32_t)_xgetbv(_XCR_XFEATURE_ENABLED_MASK);
189# else
190 xcr_feature_mask = 0;
191# endif
192 const bool avx = (xcr_feature_mask & 0x6) == 0x6;
193 const bool f16c = (result[2] & ((int)1 << 29)) != 0;
194
195 caps.f16c = avx && f16c;
196
197 __cpuid(result, 0x00000007);
198 bool bmi1 = (result[1] & ((int)1 << 3)) != 0;
199 bool bmi2 = (result[1] & ((int)1 << 8)) != 0;
200 bool avx2 = (result[1] & ((int)1 << 5)) != 0;
201
202 caps.avx2 = sse && sse2 && sse3 && ssse3 && sse41 && sse42 && avx && f16c && avx2 && fma3 &&
203 bmi1 && bmi2;
204 }
205 }
206
207 return caps;
208}
209
210static const CPUCapabilities &system_cpu_capabilities()
211{
212 static const CPUCapabilities caps = system_cpu_capabilities_init();
213 return caps;
214}
215
217{
218 const CPUCapabilities &caps = system_cpu_capabilities();
219 return caps.sse42;
220}
221
223{
224 /* F16C is considered part of AVX2 for our purpose, as all physical CPUs with
225 * AVX2 support also support it so there is no point having a separate kernel.
226 *
227 * Some cases where it might be missing is Rosetta or virtual machines, so we
228 * check for it just to be safe. */
229 const CPUCapabilities &caps = system_cpu_capabilities();
230 return caps.avx2 && caps.f16c;
231}
232
233#else
234
236{
237 return false;
238}
239
241{
242 return false;
243}
244
245#endif
246
248{
249#ifdef _WIN32
250 MEMORYSTATUSEX ram;
251 ram.dwLength = sizeof(ram);
252 GlobalMemoryStatusEx(&ram);
253 return ram.ullTotalPhys;
254#elif defined(__APPLE__)
255 uint64_t ram = 0;
256 size_t len = sizeof(ram);
257 if (sysctlbyname("hw.memsize", &ram, &len, nullptr, 0) == 0) {
258 return ram;
259 }
260 return 0;
261#else
262 size_t ps = sysconf(_SC_PAGESIZE);
263 size_t pn = sysconf(_SC_PHYS_PAGES);
264 return ps * pn;
265#endif
266}
267
269{
270#ifdef _WIN32
271 return GetCurrentProcessId();
272#else
273 return getpid();
274#endif
275}
276
278{
279#if defined(_WIN32)
280 return _getmaxstdio();
281#else
282 struct rlimit limit = {};
283 return (getrlimit(RLIMIT_NOFILE, &limit) == 0) ? limit.rlim_cur : SIZE_MAX;
284#endif
285}
286
288{
289 /* The Windows maximum is 8192 open files. */
290 constexpr int max_open_files = 8192;
291 bool ok = true;
292
293#if defined(_WIN32)
294 if (_getmaxstdio() < max_open_files) {
295 ok = _setmaxstdio(max_open_files) == max_open_files;
296 }
297#else
298 struct rlimit limit = {};
299 ok = getrlimit(RLIMIT_NOFILE, &limit) == 0;
300 if (ok && limit.rlim_cur < rlim_t(max_open_files)) {
301 limit.rlim_cur = std::min(rlim_t(max_open_files), limit.rlim_max);
302 ok = setrlimit(RLIMIT_NOFILE, &limit) == 0;
303 }
304#endif
305
306 if (!ok) {
307 LOG_DEBUG << "Failed to ensure max open files is at least " << max_open_files << ": "
308 << strerror(errno);
309 }
310}
311
#define SIZE_MAX
unsigned long long int uint64_t
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define CCL_NAMESPACE_END
#define LOG_DEBUG
Definition log.h:107
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
unsigned int uint32_t
Definition BKE_text.h:81
BMesh const char void * data
ATTR_WARN_UNUSED_RESULT const size_t num
string string_remove_trademark(const string &s)
Definition string.cpp:170
string string_remove_gpu_from_cpu_name(const string &s)
Definition string.cpp:224
size_t system_physical_ram()
Definition system.cpp:247
bool system_cpu_support_avx2()
Definition system.cpp:240
void system_max_open_files_ensure()
Definition system.cpp:287
int system_cpu_bits()
Definition system.cpp:140
size_t system_max_open_files()
Definition system.cpp:277
CCL_NAMESPACE_BEGIN int system_console_width()
Definition system.cpp:34
bool system_cpu_support_sse42()
Definition system.cpp:235
string system_cpu_brand_string()
Definition system.cpp:73
uint64_t system_self_process_id()
Definition system.cpp:268