José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 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 | |
Eric Engestrom | 8c52bca | 2019-08-01 22:49:05 +0100 | [diff] [blame] | 34 | #if DETECT_OS_WINDOWS |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 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> |
Jose Fonseca | 46f7b36 | 2019-08-27 11:51:00 +0100 | [diff] [blame] | 41 | #include <stdlib.h> |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 42 | |
| 43 | #else |
| 44 | |
| 45 | #include <stdio.h> |
| 46 | #include <stdlib.h> |
| 47 | |
| 48 | #endif |
| 49 | |
| 50 | |
Rob Clark | f9f7cbc | 2019-09-03 11:43:40 -0700 | [diff] [blame] | 51 | #if DETECT_OS_ANDROID |
| 52 | # define LOG_TAG "MESA" |
| 53 | # include <unistd.h> |
| 54 | # include <log/log.h> |
| 55 | #elif DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 56 | # include <unistd.h> |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 57 | #elif DETECT_OS_APPLE || DETECT_OS_BSD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 58 | # include <sys/sysctl.h> |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 59 | #elif DETECT_OS_HAIKU |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 60 | # include <kernel/OS.h> |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 61 | #elif DETECT_OS_WINDOWS |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 62 | # include <windows.h> |
| 63 | #else |
| 64 | #error unexpected platform in os_sysinfo.c |
| 65 | #endif |
| 66 | |
| 67 | |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 68 | void |
| 69 | os_log_message(const char *message) |
| 70 | { |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 71 | /* If the GALLIUM_LOG_FILE environment variable is set to a valid filename, |
| 72 | * write all messages to that file. |
| 73 | */ |
| 74 | static FILE *fout = NULL; |
| 75 | |
| 76 | if (!fout) { |
Brian Paul | bb1292e | 2016-06-14 20:35:53 -0600 | [diff] [blame] | 77 | #ifdef DEBUG |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 78 | /* one-time init */ |
| 79 | const char *filename = os_get_option("GALLIUM_LOG_FILE"); |
Brian Paul | bb1292e | 2016-06-14 20:35:53 -0600 | [diff] [blame] | 80 | if (filename) { |
| 81 | const char *mode = "w"; |
| 82 | if (filename[0] == '+') { |
| 83 | /* If the filename is prefixed with '+' then open the file for |
| 84 | * appending instead of normal writing. |
| 85 | */ |
| 86 | mode = "a"; |
| 87 | filename++; /* skip the '+' */ |
| 88 | } |
| 89 | fout = fopen(filename, mode); |
| 90 | } |
| 91 | #endif |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 92 | if (!fout) |
| 93 | fout = stderr; |
| 94 | } |
| 95 | |
Eric Engestrom | 8c52bca | 2019-08-01 22:49:05 +0100 | [diff] [blame] | 96 | #if DETECT_OS_WINDOWS |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 97 | OutputDebugStringA(message); |
| 98 | if(GetConsoleWindow() && !IsDebuggerPresent()) { |
| 99 | fflush(stdout); |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 100 | fputs(message, fout); |
| 101 | fflush(fout); |
| 102 | } |
| 103 | else if (fout != stderr) { |
| 104 | fputs(message, fout); |
| 105 | fflush(fout); |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 106 | } |
Eric Engestrom | 8c52bca | 2019-08-01 22:49:05 +0100 | [diff] [blame] | 107 | #else /* !DETECT_OS_WINDOWS */ |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 108 | fflush(stdout); |
Brian Paul | 9c85687 | 2012-05-22 09:32:50 -0600 | [diff] [blame] | 109 | fputs(message, fout); |
| 110 | fflush(fout); |
Rob Clark | f9f7cbc | 2019-09-03 11:43:40 -0700 | [diff] [blame] | 111 | # if DETECT_OS_ANDROID |
| 112 | LOG_PRI(ANDROID_LOG_ERROR, LOG_TAG, "%s", message); |
| 113 | # endif |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 114 | #endif |
| 115 | } |
| 116 | |
| 117 | |
Eric Engestrom | e740e7a | 2019-08-01 21:45:25 +0100 | [diff] [blame] | 118 | #if !defined(EMBEDDED_DEVICE) |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 119 | const char * |
| 120 | os_get_option(const char *name) |
| 121 | { |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 122 | return getenv(name); |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 123 | } |
Eric Engestrom | e740e7a | 2019-08-01 21:45:25 +0100 | [diff] [blame] | 124 | #endif /* !EMBEDDED_DEVICE */ |
José Fonseca | 0b0e705 | 2010-02-03 12:11:58 +0000 | [diff] [blame] | 125 | |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 126 | |
| 127 | /** |
| 128 | * Return the size of the total physical memory. |
| 129 | * \param size returns the size of the total physical memory |
| 130 | * \return true for success, or false on failure |
| 131 | */ |
| 132 | bool |
| 133 | os_get_total_physical_memory(uint64_t *size) |
| 134 | { |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 135 | #if DETECT_OS_LINUX || DETECT_OS_CYGWIN || DETECT_OS_SOLARIS || DETECT_OS_HURD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 136 | const long phys_pages = sysconf(_SC_PHYS_PAGES); |
| 137 | const long page_size = sysconf(_SC_PAGE_SIZE); |
| 138 | |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 139 | if (phys_pages <= 0 || page_size <= 0) |
| 140 | return false; |
| 141 | |
Axel Davy | 197cdd1 | 2016-10-11 18:57:17 +0200 | [diff] [blame] | 142 | *size = (uint64_t)phys_pages * (uint64_t)page_size; |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 143 | return true; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 144 | #elif DETECT_OS_APPLE || DETECT_OS_BSD |
Jonathan Gray | 7983a3d | 2015-02-22 19:19:25 +1100 | [diff] [blame] | 145 | size_t len = sizeof(*size); |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 146 | int mib[2]; |
| 147 | |
| 148 | mib[0] = CTL_HW; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 149 | #if DETECT_OS_APPLE |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 150 | mib[1] = HW_MEMSIZE; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 151 | #elif DETECT_OS_NETBSD || DETECT_OS_OPENBSD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 152 | mib[1] = HW_PHYSMEM64; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 153 | #elif DETECT_OS_FREEBSD |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 154 | mib[1] = HW_REALMEM; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 155 | #elif DETECT_OS_DRAGONFLY |
Vinson Lee | a2fd55c | 2014-10-10 22:40:21 -0700 | [diff] [blame] | 156 | mib[1] = HW_PHYSMEM; |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 157 | #else |
| 158 | #error Unsupported *BSD |
| 159 | #endif |
| 160 | |
Jonathan Gray | 7983a3d | 2015-02-22 19:19:25 +1100 | [diff] [blame] | 161 | return (sysctl(mib, 2, size, &len, NULL, 0) == 0); |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 162 | #elif DETECT_OS_HAIKU |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 163 | system_info info; |
| 164 | status_t ret; |
| 165 | |
| 166 | ret = get_system_info(&info); |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 167 | if (ret != B_OK || info.max_pages <= 0) |
| 168 | return false; |
| 169 | |
Axel Davy | 197cdd1 | 2016-10-11 18:57:17 +0200 | [diff] [blame] | 170 | *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE; |
Axel Davy | 2184597 | 2016-10-06 19:35:04 +0200 | [diff] [blame] | 171 | return true; |
Eric Engestrom | 7f12a66 | 2019-08-01 22:33:05 +0100 | [diff] [blame] | 172 | #elif DETECT_OS_WINDOWS |
Emil Velikov | 8d27457 | 2014-02-28 03:34:51 +0000 | [diff] [blame] | 173 | MEMORYSTATUSEX status; |
| 174 | BOOL ret; |
| 175 | |
| 176 | status.dwLength = sizeof(status); |
| 177 | ret = GlobalMemoryStatusEx(&status); |
| 178 | *size = status.ullTotalPhys; |
| 179 | return (ret == TRUE); |
| 180 | #else |
| 181 | #error unexpected platform in os_sysinfo.c |
| 182 | return false; |
| 183 | #endif |
| 184 | } |