Blender V5.3
system.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm>
10#include <climits>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14
15#include "CLG_log.h"
16
17#include "BLI_math_base_c.hh"
18#include "BLI_mutex.hh"
19#include "BLI_string.hh"
20#include "BLI_system.hh"
21
22/* for backtrace and gethostname/GetComputerName */
23#if defined(WIN32)
24# include <intrin.h>
25
26# include "BLI_winstuff.hh"
27#else
28# if defined(HAVE_EXECINFO_H)
29# include <execinfo.h>
30# endif
31# include <sys/resource.h>
32# include <unistd.h>
33#endif
34
35namespace blender {
36
37static CLG_LogRef LOG = {"system"};
38
40{
41#if defined(__x86_64__) || defined(_M_X64)
42 /* x86_64 always has SSE2 instructions */
43 return 1;
44#elif defined(__GNUC__) && defined(i386)
45 /* for GCC x86 we check cpuid */
46 uint d;
47 __asm__(
48 "pushl %%ebx\n\t"
49 "cpuid\n\t"
50 "popl %%ebx\n\t"
51 : "=d"(d)
52 : "a"(1));
53 return (d & 0x04000000) != 0;
54#elif (defined(_MSC_VER) && defined(_M_IX86))
55 /* also check cpuid for MSVC x86 */
56 uint d;
57 __asm {
58 xor eax, eax
59 inc eax
60 push ebx
61 cpuid
62 pop ebx
63 mov d, edx
64 }
65 return (d & 0x04000000) != 0;
66#else
67 return 0;
68#endif
69}
70
71/* Windows stack-walk lives in system_win32.cc */
72#if !defined(_MSC_VER)
73void BLI_system_backtrace_with_os_info(FILE *fp, const void * /*os_info*/)
74{
75 /* ----------------------- */
76 /* If system as execinfo.h */
77# if defined(HAVE_EXECINFO_H)
78
79# define SIZE 100
80 void *buffer[SIZE];
81 int nptrs;
82 char **strings;
83 int i;
84
85 /* Include a back-trace for good measure.
86 *
87 * NOTE: often values printed are addresses (no line numbers of function names),
88 * this information can be expanded using `addr2line`, a utility is included to
89 * conveniently run addr2line on the output generated here:
90 *
91 * `./tools/utils/addr2line_backtrace.py --exe=/path/to/blender trace.txt`
92 */
93 nptrs = backtrace(buffer, SIZE);
94 strings = backtrace_symbols(buffer, nptrs);
95 for (i = 0; i < nptrs; i++) {
96 fputs(strings[i], fp);
97 fputc('\n', fp);
98 }
99
100 free(strings);
101# undef SIZE
102
103# else
104 /* --------------------- */
105 /* Non MSVC/Apple/Linux. */
106 (void)fp;
107# endif
108}
109#endif
110/* end BLI_system_backtrace_with_os_info */
111
113{
114 static Mutex mutex;
115 std::scoped_lock lock(mutex);
117}
118
119/* NOTE: The code for CPU brand string is adopted from Cycles. */
120
121#if !defined(_WIN32) || defined(FREE_WINDOWS)
122static void __cpuid(
123 /* Cannot be const, because it is modified below.
124 * NOLINTNEXTLINE: readability-non-const-parameter. */
125 int data[4],
126 int selector)
127{
128# if defined(__x86_64__)
129 asm("cpuid" : "=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : "a"(selector));
130# elif defined(__i386__)
131 asm("pushl %%ebx \n\t"
132 "cpuid \n\t"
133 "movl %%ebx, %1 \n\t"
134 "popl %%ebx \n\t"
135 : "=a"(data[0]), "=r"(data[1]), "=c"(data[2]), "=d"(data[3])
136 : "a"(selector)
137 : "ebx");
138# else
139 (void)selector;
140 data[0] = data[1] = data[2] = data[3] = 0;
141# endif
142}
143#endif
144
146{
147#if !defined(_M_ARM64)
148 char buf[49] = {0};
149 int result[4] = {0};
150 __cpuid(result, 0x80000000);
151 if (result[0] >= int(0x80000004)) {
152 __cpuid(reinterpret_cast<int *>(buf + 0), 0x80000002);
153 __cpuid(reinterpret_cast<int *>(buf + 16), 0x80000003);
154 __cpuid(reinterpret_cast<int *>(buf + 32), 0x80000004);
155 char *brand = BLI_strdup(buf);
156 /* TODO(sergey): Make it a bit more presentable by removing trademark. */
157 return brand;
158 }
159#else
160 /* No CPUID on ARM64, so we pull from the registry (on Windows) instead. */
161 DWORD processorNameStringLength = 255;
162 char processorNameString[255];
163 if (RegGetValueA(HKEY_LOCAL_MACHINE,
164 "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0",
165 "ProcessorNameString",
166 RRF_RT_REG_SZ,
167 nullptr,
168 &processorNameString,
169 &processorNameStringLength) == ERROR_SUCCESS)
170 {
171 return BLI_strdup(processorNameString);
172 }
173#endif
174 return nullptr;
175}
176
178{
179#if !defined(_M_ARM64)
180 int result[4], num;
181 __cpuid(result, 0);
182 num = result[0];
183
184 if (num >= 1) {
185 __cpuid(result, 0x00000001);
186 return (result[2] & (int(1) << 20)) != 0;
187 }
188#endif
189 return 0;
190}
191
192void BLI_hostname_get(char *buffer, size_t buffer_maxncpy)
193{
194#ifndef WIN32
195 if (gethostname(buffer, buffer_maxncpy - 1) < 0) {
196 BLI_strncpy(buffer, "-unknown-", buffer_maxncpy);
197 }
198 /* When `gethostname()` truncates, it doesn't guarantee the trailing `\0`. */
199 buffer[buffer_maxncpy - 1] = '\0';
200#else
201 DWORD buffer_size_in_out = buffer_maxncpy;
202 if (!GetComputerName(buffer, &buffer_size_in_out)) {
203 BLI_strncpy(buffer, "-unknown-", buffer_maxncpy);
204 }
205#endif
206}
207
209{
210 /* Maximum addressable bytes on this platform.
211 *
212 * NOTE: Due to the shift arithmetic this is a half of the memory. */
213 const size_t limit_bytes_half = size_t(1) << (sizeof(size_t[8]) - 1);
214 /* Convert it to megabytes and return. */
215 return (limit_bytes_half >> 20) * 2;
216}
217
219{
220 const size_t limit_megabytes = BLI_system_memory_max_in_megabytes();
221 /* NOTE: The result will fit into integer. */
222 return int(min_zz(limit_megabytes, size_t(INT_MAX)));
223}
224
226{
227 /* The Windows maximum is documented as 8192. */
228 constexpr int max_open_files = 8192;
229 bool ok = true;
230
231#if defined(WIN32)
232 if (_getmaxstdio() < max_open_files) {
233 ok = _setmaxstdio(max_open_files) == max_open_files;
234 }
235#else
236 rlimit limit;
237 ok = getrlimit(RLIMIT_NOFILE, &limit) == 0;
238 if (ok && limit.rlim_cur < rlim_t(max_open_files)) {
239 limit.rlim_cur = std::min(rlim_t(max_open_files), limit.rlim_max);
240 ok = setrlimit(RLIMIT_NOFILE, &limit) == 0;
241 }
242#endif
243
244 if (!ok) {
246 "Failed to ensure max open files is at least %d: %s",
247 max_open_files,
248 strerror(errno));
249 }
250}
251
252} // namespace blender
#define SIZE
Compatibility-like things for windows.
#define CLOG_DEBUG(clg_ref,...)
Definition CLG_log.h:191
volatile int lock
#define INT_MAX
void free(Subdiv *subdiv)
Definition subdiv.cc:192
BMesh Iterators.
void BLI_hostname_get(char *buffer, size_t buffer_maxncpy)
Definition system.cc:192
unsigned int uint
Definition BKE_text.h:55
void BLI_system_backtrace_with_os_info(FILE *fp, const void *os_info)
Definition system.cc:73
void BLI_system_max_open_files_ensure()
Definition system.cc:225
int BLI_cpu_support_sse2()
Definition system.cc:39
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
BMesh const char void * data
MINLINE size_t min_zz(size_t a, size_t b)
int BLI_cpu_support_sse42()
Definition system.cc:177
ATTR_WARN_UNUSED_RESULT const size_t num
int BLI_system_memory_max_in_megabytes_int()
Definition system.cc:218
void BLI_system_backtrace(FILE *fp)
Definition system.cc:112
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1) ATTR_MALLOC
std::mutex Mutex
Definition BLI_mutex.hh:51
static void __cpuid(int data[4], int selector)
Definition system.cc:122
char * BLI_cpu_brand_string()
Definition system.cc:145
static CLG_LogRef LOG
Definition logging.cpp:12
size_t BLI_system_memory_max_in_megabytes()
Definition system.cc:208