os_misc.c revision b8e80941
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 31#include <stdarg.h> 32 33 34#if defined(PIPE_SUBSYSTEM_WINDOWS_USER) 35 36#ifndef WIN32_LEAN_AND_MEAN 37#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 38#endif 39#include <windows.h> 40#include <stdio.h> 41 42#else 43 44#include <stdio.h> 45#include <stdlib.h> 46 47#endif 48 49 50#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD) 51# include <unistd.h> 52#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD) 53# include <sys/sysctl.h> 54#elif defined(PIPE_OS_HAIKU) 55# include <kernel/OS.h> 56#elif defined(PIPE_OS_WINDOWS) 57# include <windows.h> 58#else 59#error unexpected platform in os_sysinfo.c 60#endif 61 62 63void 64os_log_message(const char *message) 65{ 66 /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename, 67 * write all messages to that file. 68 */ 69 static FILE *fout = NULL; 70 71 if (!fout) { 72#ifdef DEBUG 73 /* one-time init */ 74 const char *filename = os_get_option("GALLIUM_LOG_FILE"); 75 if (filename) { 76 const char *mode = "w"; 77 if (filename[0] == '+') { 78 /* If the filename is prefixed with '+' then open the file for 79 * appending instead of normal writing. 80 */ 81 mode = "a"; 82 filename++; /* skip the '+' */ 83 } 84 fout = fopen(filename, mode); 85 } 86#endif 87 if (!fout) 88 fout = stderr; 89 } 90 91#if defined(PIPE_SUBSYSTEM_WINDOWS_USER) 92 OutputDebugStringA(message); 93 if(GetConsoleWindow() && !IsDebuggerPresent()) { 94 fflush(stdout); 95 fputs(message, fout); 96 fflush(fout); 97 } 98 else if (fout != stderr) { 99 fputs(message, fout); 100 fflush(fout); 101 } 102#else /* !PIPE_SUBSYSTEM_WINDOWS */ 103 fflush(stdout); 104 fputs(message, fout); 105 fflush(fout); 106#endif 107} 108 109 110#if !defined(PIPE_SUBSYSTEM_EMBEDDED) 111const char * 112os_get_option(const char *name) 113{ 114 return getenv(name); 115} 116#endif /* !PIPE_SUBSYSTEM_EMBEDDED */ 117 118 119/** 120 * Return the size of the total physical memory. 121 * \param size returns the size of the total physical memory 122 * \return true for success, or false on failure 123 */ 124bool 125os_get_total_physical_memory(uint64_t *size) 126{ 127#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD) 128 const long phys_pages = sysconf(_SC_PHYS_PAGES); 129 const long page_size = sysconf(_SC_PAGE_SIZE); 130 131 if (phys_pages <= 0 || page_size <= 0) 132 return false; 133 134 *size = (uint64_t)phys_pages * (uint64_t)page_size; 135 return true; 136#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD) 137 size_t len = sizeof(*size); 138 int mib[2]; 139 140 mib[0] = CTL_HW; 141#if defined(PIPE_OS_APPLE) 142 mib[1] = HW_MEMSIZE; 143#elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD) 144 mib[1] = HW_PHYSMEM64; 145#elif defined(PIPE_OS_FREEBSD) 146 mib[1] = HW_REALMEM; 147#elif defined(PIPE_OS_DRAGONFLY) 148 mib[1] = HW_PHYSMEM; 149#else 150#error Unsupported *BSD 151#endif 152 153 return (sysctl(mib, 2, size, &len, NULL, 0) == 0); 154#elif defined(PIPE_OS_HAIKU) 155 system_info info; 156 status_t ret; 157 158 ret = get_system_info(&info); 159 if (ret != B_OK || info.max_pages <= 0) 160 return false; 161 162 *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE; 163 return true; 164#elif defined(PIPE_OS_WINDOWS) 165 MEMORYSTATUSEX status; 166 BOOL ret; 167 168 status.dwLength = sizeof(status); 169 ret = GlobalMemoryStatusEx(&status); 170 *size = status.ullTotalPhys; 171 return (ret == TRUE); 172#else 173#error unexpected platform in os_sysinfo.c 174 return false; 175#endif 176} 177