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 **************************************************************************/
37 #ifndef WIN32_LEAN_AND_MEAN
38 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
55 # define LOG_TAG "MESA"
58 #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
60 #elif DETECT_OS_APPLE || DETECT_OS_BSD
61 # include <sys/sysctl.h>
63 # include <kernel/OS.h>
64 #elif DETECT_OS_WINDOWS
67 #error unexpected platform in os_sysinfo.c
72 os_log_message(const char *message
)
74 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
75 * write all messages to that file.
77 static FILE *fout
= NULL
;
82 const char *filename
= os_get_option("GALLIUM_LOG_FILE");
84 const char *mode
= "w";
85 if (filename
[0] == '+') {
86 /* If the filename is prefixed with '+' then open the file for
87 * appending instead of normal writing.
90 filename
++; /* skip the '+' */
92 fout
= fopen(filename
, mode
);
100 OutputDebugStringA(message
);
101 if(GetConsoleWindow() && !IsDebuggerPresent()) {
103 fputs(message
, fout
);
106 else if (fout
!= stderr
) {
107 fputs(message
, fout
);
110 #else /* !DETECT_OS_WINDOWS */
112 fputs(message
, fout
);
114 # if DETECT_OS_ANDROID
115 LOG_PRI(ANDROID_LOG_ERROR
, LOG_TAG
, "%s", message
);
121 #if !defined(EMBEDDED_DEVICE)
123 os_get_option(const char *name
)
127 #endif /* !EMBEDDED_DEVICE */
131 * Return the size of the total physical memory.
132 * \param size returns the size of the total physical memory
133 * \return true for success, or false on failure
136 os_get_total_physical_memory(uint64_t *size
)
138 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
139 const long phys_pages
= sysconf(_SC_PHYS_PAGES
);
140 const long page_size
= sysconf(_SC_PAGE_SIZE
);
142 if (phys_pages
<= 0 || page_size
<= 0)
145 *size
= (uint64_t)phys_pages
* (uint64_t)page_size
;
147 #elif DETECT_OS_APPLE || DETECT_OS_BSD
148 size_t len
= sizeof(*size
);
154 #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
155 mib
[1] = HW_PHYSMEM64
;
156 #elif DETECT_OS_FREEBSD
158 #elif DETECT_OS_DRAGONFLY
161 #error Unsupported *BSD
164 return (sysctl(mib
, 2, size
, &len
, NULL
, 0) == 0);
165 #elif DETECT_OS_HAIKU
169 ret
= get_system_info(&info
);
170 if (ret
!= B_OK
|| info
.max_pages
<= 0)
173 *size
= (uint64_t)info
.max_pages
* (uint64_t)B_PAGE_SIZE
;
175 #elif DETECT_OS_WINDOWS
176 MEMORYSTATUSEX status
;
179 status
.dwLength
= sizeof(status
);
180 ret
= GlobalMemoryStatusEx(&status
);
181 *size
= status
.ullTotalPhys
;
182 return (ret
== TRUE
);
184 #error unexpected platform in os_sysinfo.c
190 os_get_available_system_memory(uint64_t *size
)
193 char *meminfo
= os_read_file("/proc/meminfo", NULL
);
197 char *str
= strstr(meminfo
, "MemAvailable:");
203 uint64_t kb_mem_available
;
204 if (sscanf(str
, "MemAvailable: %" PRIx64
, &kb_mem_available
) == 1) {
206 *size
= kb_mem_available
<< 10;