Blender  V2.93
system.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 #include "BLI_math_base.h"
26 #include "BLI_string.h"
27 #include "BLI_system.h"
28 #include "BLI_utildefines.h"
29 
30 #include "MEM_guardedalloc.h"
31 
32 /* for backtrace and gethostname/GetComputerName */
33 #if defined(WIN32)
34 # include <intrin.h>
35 
36 # include "BLI_winstuff.h"
37 #else
38 # include <execinfo.h>
39 # include <unistd.h>
40 #endif
41 
43 {
44 #if defined(__x86_64__) || defined(_M_X64)
45  /* x86_64 always has SSE2 instructions */
46  return 1;
47 #elif defined(__GNUC__) && defined(i386)
48  /* for GCC x86 we check cpuid */
49  unsigned int d;
50  __asm__(
51  "pushl %%ebx\n\t"
52  "cpuid\n\t"
53  "popl %%ebx\n\t"
54  : "=d"(d)
55  : "a"(1));
56  return (d & 0x04000000) != 0;
57 #elif (defined(_MSC_VER) && defined(_M_IX86))
58  /* also check cpuid for MSVC x86 */
59  unsigned int d;
60  __asm {
61  xor eax, eax
62  inc eax
63  push ebx
64  cpuid
65  pop ebx
66  mov d, edx
67  }
68  return (d & 0x04000000) != 0;
69 #else
70  return 0;
71 #endif
72 }
73 
74 /* Windows stack-walk lives in system_win32.c */
75 #if !defined(_MSC_VER)
79 void BLI_system_backtrace(FILE *fp)
80 {
81  /* ------------- */
82  /* Linux / Apple */
83 # if defined(__linux__) || defined(__APPLE__)
84 
85 # define SIZE 100
86  void *buffer[SIZE];
87  int nptrs;
88  char **strings;
89  int i;
90 
91  /* include a backtrace for good measure */
92  nptrs = backtrace(buffer, SIZE);
93  strings = backtrace_symbols(buffer, nptrs);
94  for (i = 0; i < nptrs; i++) {
95  fputs(strings[i], fp);
96  fputc('\n', fp);
97  }
98 
99  free(strings);
100 # undef SIZE
101 
102 # else
103  /* ------------------ */
104  /* non msvc/osx/linux */
105  (void)fp;
106 # endif
107 }
108 #endif
109 /* end BLI_system_backtrace */
110 
111 /* NOTE: The code for CPU brand string is adopted from Cycles. */
112 
113 #if !defined(_WIN32) || defined(FREE_WINDOWS)
114 static void __cpuid(
115  /* Cannot be const, because it is modified below.
116  * NOLINTNEXTLINE: readability-non-const-parameter. */
117  int data[4],
118  int selector)
119 {
120 # if defined(__x86_64__)
121  asm("cpuid" : "=a"(data[0]), "=b"(data[1]), "=c"(data[2]), "=d"(data[3]) : "a"(selector));
122 # elif defined(__i386__)
123  asm("pushl %%ebx \n\t"
124  "cpuid \n\t"
125  "movl %%ebx, %1 \n\t"
126  "popl %%ebx \n\t"
127  : "=a"(data[0]), "=r"(data[1]), "=c"(data[2]), "=d"(data[3])
128  : "a"(selector)
129  : "ebx");
130 # else
131  (void)selector;
132  data[0] = data[1] = data[2] = data[3] = 0;
133 # endif
134 }
135 #endif
136 
138 {
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  return NULL;
151 }
152 
154 {
155  int result[4], num;
156  __cpuid(result, 0);
157  num = result[0];
158 
159  if (num >= 1) {
160  __cpuid(result, 0x00000001);
161  return (result[2] & ((int)1 << 19)) != 0;
162  }
163  return 0;
164 }
165 
166 void BLI_hostname_get(char *buffer, size_t bufsize)
167 {
168 #ifndef WIN32
169  if (gethostname(buffer, bufsize - 1) < 0) {
170  BLI_strncpy(buffer, "-unknown-", bufsize);
171  }
172  /* When gethostname() truncates, it doesn't guarantee the trailing \0. */
173  buffer[bufsize - 1] = '\0';
174 #else
175  DWORD bufsize_inout = bufsize;
176  if (!GetComputerName(buffer, &bufsize_inout)) {
177  strncpy(buffer, "-unknown-", bufsize);
178  }
179 #endif
180 }
181 
183 {
184  /* Maximum addressable bytes on this platform.
185  *
186  * NOTE: Due to the shift arithmetic this is a half of the memory. */
187  const size_t limit_bytes_half = (((size_t)1) << ((sizeof(size_t[8])) - 1));
188  /* Convert it to megabytes and return. */
189  return (limit_bytes_half >> 20) * 2;
190 }
191 
193 {
194  const size_t limit_megabytes = BLI_system_memory_max_in_megabytes();
195  /* NOTE: The result will fit into integer. */
196  return (int)min_zz(limit_megabytes, (size_t)INT_MAX);
197 }
#define SIZE
void BLI_kdtree_nd_() free(KDTree *tree)
Definition: kdtree_impl.h:116
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:70
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, const size_t maxncpy) ATTR_NONNULL()
Definition: string.c:108
Compatibility-like things for windows.
Read Guarded memory(de)allocation.
__kernel void ccl_constant KernelData ccl_global void ccl_global char ccl_global int ccl_global char ccl_global unsigned int ccl_global float * buffer
int BLI_cpu_support_sse41(void)
Definition: system.c:153
void BLI_hostname_get(char *buffer, size_t bufsize)
Definition: system.c:166
char * BLI_cpu_brand_string(void)
Definition: system.c:137
int BLI_cpu_support_sse2(void)
Definition: system.c:42
void BLI_system_backtrace(FILE *fp)
Definition: system.c:79
static void __cpuid(int data[4], int selector)
Definition: system.c:114
size_t BLI_system_memory_max_in_megabytes(void)
Definition: system.c:182
int BLI_system_memory_max_in_megabytes_int(void)
Definition: system.c:192