1
/**************************************************************************
3
* Copyright 2008-2010 VMware, Inc.
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:
14
* The above copyright notice and this permission notice (including the
15
* next paragraph) shall be included in all copies or substantial portions
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.
26
**************************************************************************/
38
#ifndef WIN32_LEAN_AND_MEAN
39
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
56
# define LOG_TAG "MESA"
59
# include <cutils/properties.h>
60
#elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
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>
68
# include <kernel/OS.h>
69
#elif DETECT_OS_WINDOWS
72
#error unexpected platform in os_sysinfo.c
77
os_log_message(const char *message)
79
/* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
80
* write all messages to that file.
82
static FILE *fout = NULL;
87
const char *filename = os_get_option("GALLIUM_LOG_FILE");
89
const char *mode = "w";
90
if (filename[0] == '+') {
91
/* If the filename is prefixed with '+' then open the file for
92
* appending instead of normal writing.
95
filename++; /* skip the '+' */
97
fout = fopen(filename, mode);
104
#if DETECT_OS_WINDOWS
105
OutputDebugStringA(message);
106
if(GetConsoleWindow() && !IsDebuggerPresent()) {
108
fputs(message, fout);
111
else if (fout != stderr) {
112
fputs(message, fout);
115
#else /* !DETECT_OS_WINDOWS */
117
fputs(message, fout);
119
# if DETECT_OS_ANDROID
120
LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message);
125
#if DETECT_OS_ANDROID
127
# include "hash_table.h"
129
# include "simple_mtx.h"
131
static struct hash_table *options_tbl;
134
options_tbl_fini(void)
136
_mesa_hash_table_destroy(options_tbl, NULL);
140
* Get an option value from android's property system, as a fallback to
141
* getenv() (which is generally less useful on android due to processes
142
* typically being forked from the zygote.
144
* The option name used for getenv is translated into a property name
147
* 1) convert to lowercase
148
* 2) replace '_' with '.'
149
* 3) if necessary, prepend "mesa."
152
* - MESA_EXTENSION_OVERRIDE -> mesa.extension.override
153
* - GALLIUM_HUD -> mesa.gallium.hud
155
* Note that we use a hashtable for two purposes:
156
* 1) Avoid re-translating the option name on subsequent lookups
157
* 2) Avoid leaking memory. Because property_get() returns the
158
* property value into a user allocated buffer, we cannot return
159
* that directly to the caller, so we need to strdup(). With the
160
* hashtable, subsquent lookups can return the existing string.
163
os_get_android_option(const char *name)
166
options_tbl = _mesa_hash_table_create(NULL, _mesa_hash_string,
167
_mesa_key_string_equal);
168
atexit(options_tbl_fini);
171
struct hash_entry *entry = _mesa_hash_table_search(options_tbl, name);
176
char value[PROPERTY_VALUE_MAX];
177
char key[PROPERTY_KEY_MAX];
178
char *p = key, *end = key + PROPERTY_KEY_MAX;
179
/* add "mesa." prefix if necessary: */
180
if (strstr(name, "MESA_") != name)
181
p += strlcpy(p, "mesa.", end - p);
182
p += strlcpy(p, name, end - p);
183
for (int i = 0; key[i]; i++) {
187
key[i] = tolower(key[i]);
191
const char *opt = NULL;
192
int len = property_get(key, value, NULL);
194
opt = ralloc_strdup(options_tbl, value);
197
_mesa_hash_table_insert(options_tbl, name, (void *)opt);
204
#if !defined(EMBEDDED_DEVICE)
206
os_get_option(const char *name)
208
const char *opt = getenv(name);
209
#if DETECT_OS_ANDROID
211
opt = os_get_android_option(name);
216
#endif /* !EMBEDDED_DEVICE */
219
* Return the size of the total physical memory.
220
* \param size returns the size of the total physical memory
221
* \return true for success, or false on failure
224
os_get_total_physical_memory(uint64_t *size)
226
#if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
227
const long phys_pages = sysconf(_SC_PHYS_PAGES);
228
const long page_size = sysconf(_SC_PAGE_SIZE);
230
if (phys_pages <= 0 || page_size <= 0)
233
*size = (uint64_t)phys_pages * (uint64_t)page_size;
235
#elif DETECT_OS_APPLE || DETECT_OS_BSD
236
size_t len = sizeof(*size);
242
#elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
243
mib[1] = HW_PHYSMEM64;
244
#elif DETECT_OS_FREEBSD
246
#elif DETECT_OS_DRAGONFLY
249
#error Unsupported *BSD
252
return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
253
#elif DETECT_OS_HAIKU
257
ret = get_system_info(&info);
258
if (ret != B_OK || info.max_pages <= 0)
261
*size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
263
#elif DETECT_OS_WINDOWS
264
MEMORYSTATUSEX status;
267
status.dwLength = sizeof(status);
268
ret = GlobalMemoryStatusEx(&status);
269
*size = status.ullTotalPhys;
270
return (ret == TRUE);
272
#error unexpected platform in os_sysinfo.c
278
os_get_available_system_memory(uint64_t *size)
281
char *meminfo = os_read_file("/proc/meminfo", NULL);
285
char *str = strstr(meminfo, "MemAvailable:");
291
uint64_t kb_mem_available;
292
if (sscanf(str, "MemAvailable: %" PRIu64, &kb_mem_available) == 1) {
294
*size = kb_mem_available << 10;
300
#elif DETECT_OS_OPENBSD || DETECT_OS_FREEBSD
302
#if DETECT_OS_OPENBSD
303
int mib[] = { CTL_HW, HW_USERMEM64 };
304
#elif DETECT_OS_FREEBSD
305
int mib[] = { CTL_HW, HW_USERMEM };
307
int64_t mem_available;
308
size_t len = sizeof(mem_available);
310
/* physmem - wired */
311
if (sysctl(mib, 2, &mem_available, &len, NULL, 0) == -1)
314
/* static login.conf limit */
315
if (getrlimit(RLIMIT_DATA, &rl) == -1)
318
*size = MIN2(mem_available, rl.rlim_cur);
326
* Return the size of a page
327
* \param size returns the size of a page
328
* \return true for success, or false on failure
331
os_get_page_size(uint64_t *size)
333
#if DETECT_OS_UNIX && !DETECT_OS_APPLE && !DETECT_OS_HAIKU
334
const long page_size = sysconf(_SC_PAGE_SIZE);
339
*size = (uint64_t)page_size;
341
#elif DETECT_OS_HAIKU
342
*size = (uint64_t)B_PAGE_SIZE;
344
#elif DETECT_OS_WINDOWS
347
GetSystemInfo(&SysInfo);
348
*size = SysInfo.dwPageSize;
350
#elif DETECT_OS_APPLE
351
size_t len = sizeof(*size);
355
mib[1] = HW_PAGESIZE;
356
return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
358
#error unexpected platform in os_sysinfo.c