1 /**************************************************************************
2 *
3 * Copyright 2008-2010 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28
29 #include "hash_table.h"
30 #include "macros.h"
31 #include "os_misc.h"
32 #include "os_file.h"
33 #include "ralloc.h"
34 #include "simple_mtx.h"
35
36 #include <stdarg.h>
37
38
39 #if DETECT_OS_WINDOWS
40
41 #include <windows.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44
45 #else
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <inttypes.h>
51
52 #endif
53
54
55 #if DETECT_OS_ANDROID
56 # define LOG_TAG "MESA"
57 # include <unistd.h>
58 # include <log/log.h>
59 # include <cutils/properties.h>
60 #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
61 # include <unistd.h>
62 #elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD
63 # include <sys/resource.h>
64 # include <sys/sysctl.h>
65 #elif DETECT_OS_APPLE || DETECT_OS_BSD
66 # include <sys/sysctl.h>
67 #elif DETECT_OS_HAIKU
68 # include <kernel/OS.h>
69 #elif DETECT_OS_WINDOWS
70 # include <windows.h>
71 #else
72 #error unexpected platform in os_sysinfo.c
73 #endif
74
75
76 void
os_log_message(const char * message)77 os_log_message(const char *message)
78 {
79 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
80 * write all messages to that file.
81 */
82 static FILE *fout = NULL;
83
84 if (!fout) {
85 #ifdef DEBUG
86 /* one-time init */
87 const char *filename = os_get_option("GALLIUM_LOG_FILE");
88 if (filename) {
89 if (strcmp(filename, "stdout") == 0) {
90 fout = stdout;
91 } else {
92 const char *mode = "w";
93 if (filename[0] == '+') {
94 /* If the filename is prefixed with '+' then open the file for
95 * appending instead of normal writing.
96 */
97 mode = "a";
98 filename++; /* skip the '+' */
99 }
100 fout = fopen(filename, mode);
101 }
102 }
103 #endif
104 if (!fout)
105 fout = stderr;
106 }
107
108 #if DETECT_OS_WINDOWS
109 OutputDebugStringA(message);
110 #if !defined(_GAMING_XBOX)
111 if(GetConsoleWindow() && !IsDebuggerPresent()) {
112 fflush(stdout);
113 fputs(message, fout);
114 fflush(fout);
115 }
116 else if (fout != stderr) {
117 fputs(message, fout);
118 fflush(fout);
119 }
120 #endif
121 #else /* !DETECT_OS_WINDOWS */
122 fflush(stdout);
123 fputs(message, fout);
124 fflush(fout);
125 # if DETECT_OS_ANDROID
126 LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message);
127 # endif
128 #endif
129 }
130
131 #if DETECT_OS_ANDROID
132 # include <ctype.h>
133 # include "c11/threads.h"
134
135 /**
136 * Get an option value from android's property system, as a fallback to
137 * getenv() (which is generally less useful on android due to processes
138 * typically being forked from the zygote.
139 *
140 * The option name used for getenv is translated into a property name
141 * by:
142 *
143 * 1) convert to lowercase
144 * 2) replace '_' with '.'
145 * 3) if necessary, prepend "mesa."
146 *
147 * For example:
148 * - MESA_EXTENSION_OVERRIDE -> mesa.extension.override
149 * - GALLIUM_HUD -> mesa.gallium.hud
150 *
151 */
152 static char *
os_get_android_option(const char * name)153 os_get_android_option(const char *name)
154 {
155 static thread_local char os_android_option_value[PROPERTY_VALUE_MAX];
156 char key[PROPERTY_KEY_MAX];
157 char *p = key, *end = key + PROPERTY_KEY_MAX;
158 /* add "mesa." prefix if necessary: */
159 if (strstr(name, "MESA_") != name)
160 p += strlcpy(p, "mesa.", end - p);
161 p += strlcpy(p, name, end - p);
162 for (int i = 0; key[i]; i++) {
163 if (key[i] == '_') {
164 key[i] = '.';
165 } else {
166 key[i] = tolower(key[i]);
167 }
168 }
169
170 int len = property_get(key, os_android_option_value, NULL);
171 if (len > 1) {
172 return os_android_option_value;
173 }
174 return NULL;
175 }
176 #endif
177
178 const char *
os_get_option(const char * name)179 os_get_option(const char *name)
180 {
181 const char *opt = getenv(name);
182 #if DETECT_OS_ANDROID
183 if (!opt) {
184 opt = os_get_android_option(name);
185 }
186 #endif
187 return opt;
188 }
189
190 static struct hash_table *options_tbl;
191 static bool options_tbl_exited = false;
192 static simple_mtx_t options_tbl_mtx = SIMPLE_MTX_INITIALIZER;
193
194 /**
195 * NOTE: The strings that allocated with ralloc_strdup(options_tbl, ...)
196 * are freed by _mesa_hash_table_destroy automatically
197 */
198 static void
options_tbl_fini(void)199 options_tbl_fini(void)
200 {
201 simple_mtx_lock(&options_tbl_mtx);
202 _mesa_hash_table_destroy(options_tbl, NULL);
203 options_tbl = NULL;
204 options_tbl_exited = true;
205 simple_mtx_unlock(&options_tbl_mtx);
206 }
207
208 const char *
os_get_option_cached(const char * name)209 os_get_option_cached(const char *name)
210 {
211 const char *opt = NULL;
212 simple_mtx_lock(&options_tbl_mtx);
213 if (options_tbl_exited) {
214 opt = os_get_option(name);
215 goto exit_mutex;
216 }
217
218 if (!options_tbl) {
219 options_tbl = _mesa_hash_table_create(NULL, _mesa_hash_string,
220 _mesa_key_string_equal);
221 if (options_tbl == NULL) {
222 goto exit_mutex;
223 }
224 atexit(options_tbl_fini);
225 }
226 struct hash_entry *entry = _mesa_hash_table_search(options_tbl, name);
227 if (entry) {
228 opt = entry->data;
229 goto exit_mutex;
230 }
231
232 char *name_dup = ralloc_strdup(options_tbl, name);
233 if (name_dup == NULL) {
234 goto exit_mutex;
235 }
236 opt = ralloc_strdup(options_tbl, os_get_option(name));
237 _mesa_hash_table_insert(options_tbl, name_dup, (void *)opt);
238 exit_mutex:
239 simple_mtx_unlock(&options_tbl_mtx);
240 return opt;
241 }
242
243 /**
244 * Return the size of the total physical memory.
245 * \param size returns the size of the total physical memory
246 * \return true for success, or false on failure
247 */
248 bool
os_get_total_physical_memory(uint64_t * size)249 os_get_total_physical_memory(uint64_t *size)
250 {
251 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
252 const long phys_pages = sysconf(_SC_PHYS_PAGES);
253 const long page_size = sysconf(_SC_PAGE_SIZE);
254
255 if (phys_pages <= 0 || page_size <= 0)
256 return false;
257
258 *size = (uint64_t)phys_pages * (uint64_t)page_size;
259 return true;
260 #elif DETECT_OS_APPLE || DETECT_OS_BSD
261 size_t len = sizeof(*size);
262 int mib[2];
263
264 mib[0] = CTL_HW;
265 #if DETECT_OS_APPLE
266 mib[1] = HW_MEMSIZE;
267 #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
268 mib[1] = HW_PHYSMEM64;
269 #elif DETECT_OS_FREEBSD
270 mib[1] = HW_REALMEM;
271 #elif DETECT_OS_DRAGONFLY
272 mib[1] = HW_PHYSMEM;
273 #else
274 #error Unsupported *BSD
275 #endif
276
277 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
278 #elif DETECT_OS_HAIKU
279 system_info info;
280 status_t ret;
281
282 ret = get_system_info(&info);
283 if (ret != B_OK || info.max_pages <= 0)
284 return false;
285
286 *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
287 return true;
288 #elif DETECT_OS_WINDOWS
289 MEMORYSTATUSEX status;
290 BOOL ret;
291
292 status.dwLength = sizeof(status);
293 ret = GlobalMemoryStatusEx(&status);
294 *size = status.ullTotalPhys;
295 return (ret == true);
296 #else
297 #error unexpected platform in os_misc.c
298 return false;
299 #endif
300 }
301
302 bool
os_get_available_system_memory(uint64_t * size)303 os_get_available_system_memory(uint64_t *size)
304 {
305 #if DETECT_OS_LINUX
306 char *meminfo = os_read_file("/proc/meminfo", NULL);
307 if (!meminfo)
308 return false;
309
310 char *str = strstr(meminfo, "MemAvailable:");
311 if (!str) {
312 free(meminfo);
313 return false;
314 }
315
316 uint64_t kb_mem_available;
317 if (sscanf(str, "MemAvailable: %" PRIu64, &kb_mem_available) == 1) {
318 free(meminfo);
319 *size = kb_mem_available << 10;
320 return true;
321 }
322
323 free(meminfo);
324 return false;
325 #elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD
326 struct rlimit rl;
327 #if DETECT_OS_OPENBSD
328 int mib[] = { CTL_HW, HW_USERMEM64 };
329 #elif DETECT_OS_FREEBSD
330 int mib[] = { CTL_HW, HW_USERMEM };
331 #endif
332 int64_t mem_available;
333 size_t len = sizeof(mem_available);
334
335 /* physmem - wired */
336 if (sysctl(mib, 2, &mem_available, &len, NULL, 0) == -1)
337 return false;
338
339 /* static login.conf limit */
340 if (getrlimit(RLIMIT_DATA, &rl) == -1)
341 return false;
342
343 *size = MIN2(mem_available, rl.rlim_cur);
344 return true;
345 #elif DETECT_OS_WINDOWS
346 MEMORYSTATUSEX status;
347 BOOL ret;
348
349 status.dwLength = sizeof(status);
350 ret = GlobalMemoryStatusEx(&status);
351 *size = status.ullAvailPhys;
352 return (ret == true);
353 #else
354 return false;
355 #endif
356 }
357
358 /**
359 * Return the size of a page
360 * \param size returns the size of a page
361 * \return true for success, or false on failure
362 */
363 bool
os_get_page_size(uint64_t * size)364 os_get_page_size(uint64_t *size)
365 {
366 #if DETECT_OS_UNIX && !DETECT_OS_APPLE && !DETECT_OS_HAIKU
367 const long page_size = sysconf(_SC_PAGE_SIZE);
368
369 if (page_size <= 0)
370 return false;
371
372 *size = (uint64_t)page_size;
373 return true;
374 #elif DETECT_OS_HAIKU
375 *size = (uint64_t)B_PAGE_SIZE;
376 return true;
377 #elif DETECT_OS_WINDOWS
378 SYSTEM_INFO SysInfo;
379
380 GetSystemInfo(&SysInfo);
381 *size = SysInfo.dwPageSize;
382 return true;
383 #elif DETECT_OS_APPLE
384 size_t len = sizeof(*size);
385 int mib[2];
386
387 mib[0] = CTL_HW;
388 mib[1] = HW_PAGESIZE;
389 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
390 #else
391 #error unexpected platform in os_sysinfo.c
392 return false;
393 #endif
394 }
395