1b8e80941Smrg/************************************************************************** 2b8e80941Smrg * 3b8e80941Smrg * Copyright 2008-2010 Vmware, Inc. 4b8e80941Smrg * All Rights Reserved. 5b8e80941Smrg * 6b8e80941Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7b8e80941Smrg * copy of this software and associated documentation files (the 8b8e80941Smrg * "Software"), to deal in the Software without restriction, including 9b8e80941Smrg * without limitation the rights to use, copy, modify, merge, publish, 10b8e80941Smrg * distribute, sub license, and/or sell copies of the Software, and to 11b8e80941Smrg * permit persons to whom the Software is furnished to do so, subject to 12b8e80941Smrg * the following conditions: 13b8e80941Smrg * 14b8e80941Smrg * The above copyright notice and this permission notice (including the 15b8e80941Smrg * next paragraph) shall be included in all copies or substantial portions 16b8e80941Smrg * of the Software. 17b8e80941Smrg * 18b8e80941Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19b8e80941Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20b8e80941Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21b8e80941Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22b8e80941Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23b8e80941Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24b8e80941Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25b8e80941Smrg * 26b8e80941Smrg **************************************************************************/ 27b8e80941Smrg 28b8e80941Smrg 29b8e80941Smrg#include "os_misc.h" 30b8e80941Smrg 31b8e80941Smrg#include <stdarg.h> 32b8e80941Smrg 33b8e80941Smrg 34b8e80941Smrg#if defined(PIPE_SUBSYSTEM_WINDOWS_USER) 35b8e80941Smrg 36b8e80941Smrg#ifndef WIN32_LEAN_AND_MEAN 37b8e80941Smrg#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers 38b8e80941Smrg#endif 39b8e80941Smrg#include <windows.h> 40b8e80941Smrg#include <stdio.h> 41b8e80941Smrg 42b8e80941Smrg#else 43b8e80941Smrg 44b8e80941Smrg#include <stdio.h> 45b8e80941Smrg#include <stdlib.h> 46b8e80941Smrg 47b8e80941Smrg#endif 48b8e80941Smrg 49b8e80941Smrg 50b8e80941Smrg#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD) 51b8e80941Smrg# include <unistd.h> 52b8e80941Smrg#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD) 53b8e80941Smrg# include <sys/sysctl.h> 54b8e80941Smrg#elif defined(PIPE_OS_HAIKU) 55b8e80941Smrg# include <kernel/OS.h> 56b8e80941Smrg#elif defined(PIPE_OS_WINDOWS) 57b8e80941Smrg# include <windows.h> 58b8e80941Smrg#else 59b8e80941Smrg#error unexpected platform in os_sysinfo.c 60b8e80941Smrg#endif 61b8e80941Smrg 62b8e80941Smrg 63b8e80941Smrgvoid 64b8e80941Smrgos_log_message(const char *message) 65b8e80941Smrg{ 66b8e80941Smrg /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename, 67b8e80941Smrg * write all messages to that file. 68b8e80941Smrg */ 69b8e80941Smrg static FILE *fout = NULL; 70b8e80941Smrg 71b8e80941Smrg if (!fout) { 72b8e80941Smrg#ifdef DEBUG 73b8e80941Smrg /* one-time init */ 74b8e80941Smrg const char *filename = os_get_option("GALLIUM_LOG_FILE"); 75b8e80941Smrg if (filename) { 76b8e80941Smrg const char *mode = "w"; 77b8e80941Smrg if (filename[0] == '+') { 78b8e80941Smrg /* If the filename is prefixed with '+' then open the file for 79b8e80941Smrg * appending instead of normal writing. 80b8e80941Smrg */ 81b8e80941Smrg mode = "a"; 82b8e80941Smrg filename++; /* skip the '+' */ 83b8e80941Smrg } 84b8e80941Smrg fout = fopen(filename, mode); 85b8e80941Smrg } 86b8e80941Smrg#endif 87b8e80941Smrg if (!fout) 88b8e80941Smrg fout = stderr; 89b8e80941Smrg } 90b8e80941Smrg 91b8e80941Smrg#if defined(PIPE_SUBSYSTEM_WINDOWS_USER) 92b8e80941Smrg OutputDebugStringA(message); 93b8e80941Smrg if(GetConsoleWindow() && !IsDebuggerPresent()) { 94b8e80941Smrg fflush(stdout); 95b8e80941Smrg fputs(message, fout); 96b8e80941Smrg fflush(fout); 97b8e80941Smrg } 98b8e80941Smrg else if (fout != stderr) { 99b8e80941Smrg fputs(message, fout); 100b8e80941Smrg fflush(fout); 101b8e80941Smrg } 102b8e80941Smrg#else /* !PIPE_SUBSYSTEM_WINDOWS */ 103b8e80941Smrg fflush(stdout); 104b8e80941Smrg fputs(message, fout); 105b8e80941Smrg fflush(fout); 106b8e80941Smrg#endif 107b8e80941Smrg} 108b8e80941Smrg 109b8e80941Smrg 110b8e80941Smrg#if !defined(PIPE_SUBSYSTEM_EMBEDDED) 111b8e80941Smrgconst char * 112b8e80941Smrgos_get_option(const char *name) 113b8e80941Smrg{ 114b8e80941Smrg return getenv(name); 115b8e80941Smrg} 116b8e80941Smrg#endif /* !PIPE_SUBSYSTEM_EMBEDDED */ 117b8e80941Smrg 118b8e80941Smrg 119b8e80941Smrg/** 120b8e80941Smrg * Return the size of the total physical memory. 121b8e80941Smrg * \param size returns the size of the total physical memory 122b8e80941Smrg * \return true for success, or false on failure 123b8e80941Smrg */ 124b8e80941Smrgbool 125b8e80941Smrgos_get_total_physical_memory(uint64_t *size) 126b8e80941Smrg{ 127b8e80941Smrg#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) || defined(PIPE_OS_HURD) 128b8e80941Smrg const long phys_pages = sysconf(_SC_PHYS_PAGES); 129b8e80941Smrg const long page_size = sysconf(_SC_PAGE_SIZE); 130b8e80941Smrg 131b8e80941Smrg if (phys_pages <= 0 || page_size <= 0) 132b8e80941Smrg return false; 133b8e80941Smrg 134b8e80941Smrg *size = (uint64_t)phys_pages * (uint64_t)page_size; 135b8e80941Smrg return true; 136b8e80941Smrg#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD) 137b8e80941Smrg size_t len = sizeof(*size); 138b8e80941Smrg int mib[2]; 139b8e80941Smrg 140b8e80941Smrg mib[0] = CTL_HW; 141b8e80941Smrg#if defined(PIPE_OS_APPLE) 142b8e80941Smrg mib[1] = HW_MEMSIZE; 143b8e80941Smrg#elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD) 144b8e80941Smrg mib[1] = HW_PHYSMEM64; 145b8e80941Smrg#elif defined(PIPE_OS_FREEBSD) 146b8e80941Smrg mib[1] = HW_REALMEM; 147b8e80941Smrg#elif defined(PIPE_OS_DRAGONFLY) 148b8e80941Smrg mib[1] = HW_PHYSMEM; 149b8e80941Smrg#else 150b8e80941Smrg#error Unsupported *BSD 151b8e80941Smrg#endif 152b8e80941Smrg 153b8e80941Smrg return (sysctl(mib, 2, size, &len, NULL, 0) == 0); 154b8e80941Smrg#elif defined(PIPE_OS_HAIKU) 155b8e80941Smrg system_info info; 156b8e80941Smrg status_t ret; 157b8e80941Smrg 158b8e80941Smrg ret = get_system_info(&info); 159b8e80941Smrg if (ret != B_OK || info.max_pages <= 0) 160b8e80941Smrg return false; 161b8e80941Smrg 162b8e80941Smrg *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE; 163b8e80941Smrg return true; 164b8e80941Smrg#elif defined(PIPE_OS_WINDOWS) 165b8e80941Smrg MEMORYSTATUSEX status; 166b8e80941Smrg BOOL ret; 167b8e80941Smrg 168b8e80941Smrg status.dwLength = sizeof(status); 169b8e80941Smrg ret = GlobalMemoryStatusEx(&status); 170b8e80941Smrg *size = status.ullTotalPhys; 171b8e80941Smrg return (ret == TRUE); 172b8e80941Smrg#else 173b8e80941Smrg#error unexpected platform in os_sysinfo.c 174b8e80941Smrg return false; 175b8e80941Smrg#endif 176b8e80941Smrg} 177