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