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