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 #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
61 #elif DETECT_OS_OPENBSD
62 # include <sys/resource.h>
63 # include <sys/sysctl.h>
64 #elif DETECT_OS_APPLE || DETECT_OS_BSD
65 # include <sys/sysctl.h>
67 # include <kernel/OS.h>
68 #elif DETECT_OS_WINDOWS
71 #error unexpected platform in os_sysinfo.c
76 os_log_message(const char *message
)
78 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
79 * write all messages to that file.
81 static FILE *fout
= NULL
;
86 const char *filename
= os_get_option("GALLIUM_LOG_FILE");
88 const char *mode
= "w";
89 if (filename
[0] == '+') {
90 /* If the filename is prefixed with '+' then open the file for
91 * appending instead of normal writing.
94 filename
++; /* skip the '+' */
96 fout
= fopen(filename
, mode
);
103 #if DETECT_OS_WINDOWS
104 OutputDebugStringA(message
);
105 if(GetConsoleWindow() && !IsDebuggerPresent()) {
107 fputs(message
, fout
);
110 else if (fout
!= stderr
) {
111 fputs(message
, fout
);
114 #else /* !DETECT_OS_WINDOWS */
116 fputs(message
, fout
);
118 # if DETECT_OS_ANDROID
119 LOG_PRI(ANDROID_LOG_ERROR
, LOG_TAG
, "%s", message
);
125 #if !defined(EMBEDDED_DEVICE)
127 os_get_option(const char *name
)
131 #endif /* !EMBEDDED_DEVICE */
135 * Return the size of the total physical memory.
136 * \param size returns the size of the total physical memory
137 * \return true for success, or false on failure
140 os_get_total_physical_memory(uint64_t *size
)
142 #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
143 const long phys_pages
= sysconf(_SC_PHYS_PAGES
);
144 const long page_size
= sysconf(_SC_PAGE_SIZE
);
146 if (phys_pages
<= 0 || page_size
<= 0)
149 *size
= (uint64_t)phys_pages
* (uint64_t)page_size
;
151 #elif DETECT_OS_APPLE || DETECT_OS_BSD
152 size_t len
= sizeof(*size
);
158 #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
159 mib
[1] = HW_PHYSMEM64
;
160 #elif DETECT_OS_FREEBSD
162 #elif DETECT_OS_DRAGONFLY
165 #error Unsupported *BSD
168 return (sysctl(mib
, 2, size
, &len
, NULL
, 0) == 0);
169 #elif DETECT_OS_HAIKU
173 ret
= get_system_info(&info
);
174 if (ret
!= B_OK
|| info
.max_pages
<= 0)
177 *size
= (uint64_t)info
.max_pages
* (uint64_t)B_PAGE_SIZE
;
179 #elif DETECT_OS_WINDOWS
180 MEMORYSTATUSEX status
;
183 status
.dwLength
= sizeof(status
);
184 ret
= GlobalMemoryStatusEx(&status
);
185 *size
= status
.ullTotalPhys
;
186 return (ret
== TRUE
);
188 #error unexpected platform in os_sysinfo.c
194 os_get_available_system_memory(uint64_t *size
)
197 char *meminfo
= os_read_file("/proc/meminfo", NULL
);
201 char *str
= strstr(meminfo
, "MemAvailable:");
207 uint64_t kb_mem_available
;
208 if (sscanf(str
, "MemAvailable: %" PRIx64
, &kb_mem_available
) == 1) {
210 *size
= kb_mem_available
<< 10;
216 #elif DETECT_OS_OPENBSD
218 int mib
[] = { CTL_HW
, HW_USERMEM64
};
219 int64_t mem_available
;
220 size_t len
= sizeof(mem_available
);
222 /* physmem - wired */
223 if (sysctl(mib
, 2, &mem_available
, &len
, NULL
, 0) == -1)
226 /* static login.conf limit */
227 if (getrlimit(RLIMIT_DATA
, &rl
) == -1)
230 *size
= MIN2(mem_available
, rl
.rlim_cur
);