954900e3c5d7dd4ceaf5497c2d7e9eb5c15db755
[mesa.git] / src / util / os_misc.c
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 "os_misc.h"
30 #include "os_file.h"
31
32 #include <stdarg.h>
33
34
35 #if DETECT_OS_WINDOWS
36
37 #ifndef WIN32_LEAN_AND_MEAN
38 #define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
39 #endif
40 #include <windows.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43
44 #else
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <inttypes.h>
50
51 #endif
52
53
54 #if DETECT_OS_ANDROID
55 # define LOG_TAG "MESA"
56 # include <unistd.h>
57 # include <log/log.h>
58 #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD
59 # include <unistd.h>
60 #elif DETECT_OS_APPLE || DETECT_OS_BSD
61 # include <sys/sysctl.h>
62 #elif DETECT_OS_HAIKU
63 # include <kernel/OS.h>
64 #elif DETECT_OS_WINDOWS
65 # include <windows.h>
66 #else
67 #error unexpected platform in os_sysinfo.c
68 #endif
69
70
71 void
72 os_log_message(const char *message)
73 {
74 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename,
75 * write all messages to that file.
76 */
77 static FILE *fout = NULL;
78
79 if (!fout) {
80 #ifdef DEBUG
81 /* one-time init */
82 const char *filename = os_get_option("GALLIUM_LOG_FILE");
83 if (filename) {
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.
88 */
89 mode = "a";
90 filename++; /* skip the '+' */
91 }
92 fout = fopen(filename, mode);
93 }
94 #endif
95 if (!fout)
96 fout = stderr;
97 }
98
99 #if DETECT_OS_WINDOWS
100 OutputDebugStringA(message);
101 if(GetConsoleWindow() && !IsDebuggerPresent()) {
102 fflush(stdout);
103 fputs(message, fout);
104 fflush(fout);
105 }
106 else if (fout != stderr) {
107 fputs(message, fout);
108 fflush(fout);
109 }
110 #else /* !DETECT_OS_WINDOWS */
111 fflush(stdout);
112 fputs(message, fout);
113 fflush(fout);
114 # if DETECT_OS_ANDROID
115 LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message);
116 # endif
117 #endif
118 }
119
120
121 #if !defined(EMBEDDED_DEVICE)
122 const char *
123 os_get_option(const char *name)
124 {
125 return getenv(name);
126 }
127 #endif /* !EMBEDDED_DEVICE */
128
129
130 /**
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
134 */
135 bool
136 os_get_total_physical_memory(uint64_t *size)
137 {
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);
141
142 if (phys_pages <= 0 || page_size <= 0)
143 return false;
144
145 *size = (uint64_t)phys_pages * (uint64_t)page_size;
146 return true;
147 #elif DETECT_OS_APPLE || DETECT_OS_BSD
148 size_t len = sizeof(*size);
149 int mib[2];
150
151 mib[0] = CTL_HW;
152 #if DETECT_OS_APPLE
153 mib[1] = HW_MEMSIZE;
154 #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD
155 mib[1] = HW_PHYSMEM64;
156 #elif DETECT_OS_FREEBSD
157 mib[1] = HW_REALMEM;
158 #elif DETECT_OS_DRAGONFLY
159 mib[1] = HW_PHYSMEM;
160 #else
161 #error Unsupported *BSD
162 #endif
163
164 return (sysctl(mib, 2, size, &len, NULL, 0) == 0);
165 #elif DETECT_OS_HAIKU
166 system_info info;
167 status_t ret;
168
169 ret = get_system_info(&info);
170 if (ret != B_OK || info.max_pages <= 0)
171 return false;
172
173 *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE;
174 return true;
175 #elif DETECT_OS_WINDOWS
176 MEMORYSTATUSEX status;
177 BOOL ret;
178
179 status.dwLength = sizeof(status);
180 ret = GlobalMemoryStatusEx(&status);
181 *size = status.ullTotalPhys;
182 return (ret == TRUE);
183 #else
184 #error unexpected platform in os_sysinfo.c
185 return false;
186 #endif
187 }
188
189 bool
190 os_get_available_system_memory(uint64_t *size)
191 {
192 #if DETECT_OS_LINUX
193 char *meminfo = os_read_file("/proc/meminfo", NULL);
194 if (!meminfo)
195 return false;
196
197 char *str = strstr(meminfo, "MemAvailable:");
198 if (!str) {
199 free(meminfo);
200 return false;
201 }
202
203 uint64_t kb_mem_available;
204 if (sscanf(str, "MemAvailable: %" PRIx64, &kb_mem_available) == 1) {
205 free(meminfo);
206 *size = kb_mem_available << 10;
207 return true;
208 }
209
210 free(meminfo);
211 return false;
212 #else
213 return false;
214 #endif
215 }