Blender  V3.3
system.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 
11 #include "BLI_math_base.h"
12 #include "BLI_string.h"
13 #include "BLI_system.h"
14 #include "BLI_utildefines.h"
15 
16 #include "MEM_guardedalloc.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 # include <execinfo.h>
25 # include <unistd.h>
26 #endif
27 
29 {
30 #if defined(__x86_64__) || defined(_M_X64)
31  /* x86_64 always has SSE2 instructions */
32  return 1;
33 #elif defined(__GNUC__) && defined(i386)
34  /* for GCC x86 we check cpuid */
35  unsigned int d;
36  __asm__(
37  "pushl %%ebx\n\t"
38  "cpuid\n\t"
39  "popl %%ebx\n\t"
40  : "=d"(d)
41  : "a"(1));
42  return (d & 0x04000000) != 0;
43 #elif (defined(_MSC_VER) && defined(_M_IX86))
44  /* also check cpuid for MSVC x86 */
45  unsigned int d;
46  __asm {
47  xor eax, eax
48  inc eax
49  push ebx
50  cpuid
51  pop ebx
52  mov d, edx
53  }
54  return (d & 0x04000000) != 0;
55 #else
56  return 0;
57 #endif
58 }
59 
60 /* Windows stack-walk lives in system_win32.c */
61 #if !defined(_MSC_VER)
62 void BLI_system_backtrace(FILE *fp)
63 {
64  /* ------------- */
65  /* Linux / Apple */
66 # if defined(__linux__) || defined(__APPLE__)
67 
68 # define SIZE 100
69  void *buffer[SIZE];
70  int nptrs;
71  char **strings;
72  int i;
73 
74  /* include a backtrace for good measure */
75  nptrs = backtrace(buffer, SIZE);
76  strings = backtrace_symbols(buffer, nptrs);
77  for (i = 0; i < nptrs; i++) {
78  fputs(strings[i], fp);
79  fputc('\n', fp);
80  }
81 
82  free(strings);
83 # undef SIZE
84 
85 # else
86  /* --------------------- */
87  /* Non MSVC/Apple/Linux. */
88  (void)fp;
89 # endif
90 }
91 #endif
92 /* end BLI_system_backtrace */
93 
94 /* NOTE: The code for CPU brand string is adopted from Cycles. */
95 
96 #if !defined(_WIN32) || defined(FREE_WINDOWS)
97 static void __cpuid(
98  /* Cannot be const, because it is modified below.
99  * NOLINTNEXTLINE: readability-non-const-parameter. */
100  int data[4],
101  int selector)
102 {
103 # if defined(__x86_64__)
104  asm("cpuid" : "=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : "a"(selector));
105 # elif defined(__i386__)
106  asm("pushl %%ebx \n\t"
107  "cpuid \n\t"
108  "movl %%ebx, %1 \n\t"
109  "popl %%ebx \n\t"
110  : "=a"(data[0]), "=r"(data[1]), "=c"(data[2]), "=d"(data[3])
111  : "a"(selector)
112  : "ebx");
113 # else
114  (void)selector;
115  data[0] = data[1] = data[2] = data[3] = 0;
116 # endif
117 }
118 #endif
119 
121 {
122  char buf[49] = {0};
123  int result[4] = {0};
124  __cpuid(result, 0x80000000);
125  if (result[0] >= (int)0x80000004) {
126  __cpuid((int *)(buf + 0), 0x80000002);
127  __cpuid((int *)(buf + 16), 0x80000003);
128  __cpuid((int *)(buf + 32), 0x80000004);
129  char *brand = BLI_strdup(buf);
130  /* TODO(sergey): Make it a bit more presentable by removing trademark. */
131  return brand;
132  }
133  return NULL;
134 }
135 
137 {
138  int result[4], num;
139  __cpuid(result, 0);
140  num = result[0];
141 
142  if (num >= 1) {
143  __cpuid(result, 0x00000001);
144  return (result[2] & ((int)1 << 19)) != 0;
145  }
146  return 0;
147 }
148 
149 void BLI_hostname_get(char *buffer, size_t bufsize)
150 {
151 #ifndef WIN32
152  if (gethostname(buffer, bufsize - 1) < 0) {
153  BLI_strncpy(buffer, "-unknown-", bufsize);
154  }
155  /* When gethostname() truncates, it doesn't guarantee the trailing \0. */
156  buffer[bufsize - 1] = '\0';
157 #else
158  DWORD bufsize_inout = bufsize;
159  if (!GetComputerName(buffer, &bufsize_inout)) {
160  strncpy(buffer, "-unknown-", bufsize);
161  }
162 #endif
163 }
164 
166 {
167  /* Maximum addressable bytes on this platform.
168  *
169  * NOTE: Due to the shift arithmetic this is a half of the memory. */
170  const size_t limit_bytes_half = (((size_t)1) << (sizeof(size_t[8]) - 1));
171  /* Convert it to megabytes and return. */
172  return (limit_bytes_half >> 20) * 2;
173 }
174 
176 {
177  const size_t limit_megabytes = BLI_system_memory_max_in_megabytes();
178  /* NOTE: The result will fit into integer. */
179  return (int)min_zz(limit_megabytes, (size_t)INT_MAX);
180 }
#define SIZE
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:102
MINLINE size_t min_zz(size_t a, size_t b)
char * BLI_strdup(const char *str) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL() ATTR_MALLOC
Definition: string.c:42
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t maxncpy) ATTR_NONNULL()
Definition: string.c:64
Compatibility-like things for windows.
Read Guarded memory(de)allocation.
SyclQueue void void size_t num_bytes void
ccl_global float * buffer
int BLI_cpu_support_sse41(void)
Definition: system.c:136
void BLI_hostname_get(char *buffer, size_t bufsize)
Definition: system.c:149
char * BLI_cpu_brand_string(void)
Definition: system.c:120
int BLI_cpu_support_sse2(void)
Definition: system.c:28
void BLI_system_backtrace(FILE *fp)
Definition: system.c:62
static void __cpuid(int data[4], int selector)
Definition: system.c:97
size_t BLI_system_memory_max_in_megabytes(void)
Definition: system.c:165
int BLI_system_memory_max_in_megabytes_int(void)
Definition: system.c:175