Move the os_get_total_physical_memory function from gallium to util, simplify it (all BSDs support sysconf, no need for sysctl), use it in gallium, anv, dri/i915, dri/i965 and dri/swrast. --- src/gallium/auxiliary/os/os_misc.c | 60 ----------------------- src/gallium/auxiliary/os/os_misc.h | 9 +--- src/intel/vulkan/anv_device.c | 12 +++-- src/mesa/drivers/dri/i915/intel_screen.c | 9 ++-- src/mesa/drivers/dri/i965/intel_screen.c | 9 ++-- src/mesa/drivers/dri/swrast/swrast.c | 23 ++------- src/util/Makefile.sources | 1 + src/util/meson.build | 1 + src/util/phys_memory.h | 81 ++++++++++++++++++++++++++++++++ 9 files changed, 100 insertions(+), 105 deletions(-) create mode 100644 src/util/phys_memory.h
diff --git a/src/gallium/auxiliary/os/os_misc.c b/src/gallium/auxiliary/os/os_misc.c index 09d4400e08..45ae0a1f5b 100644 --- a/src/gallium/auxiliary/os/os_misc.c +++ b/src/gallium/auxiliary/os/os_misc.c @@ -114,63 +114,3 @@ os_get_option(const char *name) return getenv(name); } #endif /* !PIPE_SUBSYSTEM_EMBEDDED */ - - -/** - * Return the size of the total physical memory. - * \param size returns the size of the total physical memory - * \return true for success, or false on failure - */ -bool -os_get_total_physical_memory(uint64_t *size) -{ -#if defined(PIPE_OS_LINUX) || defined(PIPE_OS_CYGWIN) || defined(PIPE_OS_SOLARIS) - const long phys_pages = sysconf(_SC_PHYS_PAGES); - const long page_size = sysconf(_SC_PAGE_SIZE); - - if (phys_pages <= 0 || page_size <= 0) - return false; - - *size = (uint64_t)phys_pages * (uint64_t)page_size; - return true; -#elif defined(PIPE_OS_APPLE) || defined(PIPE_OS_BSD) - size_t len = sizeof(*size); - int mib[2]; - - mib[0] = CTL_HW; -#if defined(PIPE_OS_APPLE) - mib[1] = HW_MEMSIZE; -#elif defined(PIPE_OS_NETBSD) || defined(PIPE_OS_OPENBSD) - mib[1] = HW_PHYSMEM64; -#elif defined(PIPE_OS_FREEBSD) - mib[1] = HW_REALMEM; -#elif defined(PIPE_OS_DRAGONFLY) - mib[1] = HW_PHYSMEM; -#else -#error Unsupported *BSD -#endif - - return (sysctl(mib, 2, size, &len, NULL, 0) == 0); -#elif defined(PIPE_OS_HAIKU) - system_info info; - status_t ret; - - ret = get_system_info(&info); - if (ret != B_OK || info.max_pages <= 0) - return false; - - *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE; - return true; -#elif defined(PIPE_OS_WINDOWS) - MEMORYSTATUSEX status; - BOOL ret; - - status.dwLength = sizeof(status); - ret = GlobalMemoryStatusEx(&status); - *size = status.ullTotalPhys; - return (ret == TRUE); -#else -#error unexpected platform in os_sysinfo.c - return false; -#endif -} diff --git a/src/gallium/auxiliary/os/os_misc.h b/src/gallium/auxiliary/os/os_misc.h index 403c8ee6ec..df3206252c 100644 --- a/src/gallium/auxiliary/os/os_misc.h +++ b/src/gallium/auxiliary/os/os_misc.h @@ -36,6 +36,7 @@ #include "pipe/p_compiler.h" +#include "util/phys_memory.h" #if defined(PIPE_OS_UNIX) @@ -86,14 +87,6 @@ os_log_message(const char *message); const char * os_get_option(const char *name); - -/* - * Get the total amount of physical memory available on the system. - */ -bool -os_get_total_physical_memory(uint64_t *size); - - #ifdef __cplusplus } #endif diff --git a/src/intel/vulkan/anv_device.c b/src/intel/vulkan/anv_device.c index 777abd8757..606f8b7549 100644 --- a/src/intel/vulkan/anv_device.c +++ b/src/intel/vulkan/anv_device.c @@ -25,7 +25,6 @@ #include <stdbool.h> #include <string.h> #include <sys/mman.h> -#include <sys/sysinfo.h> #include <unistd.h> #include <fcntl.h> #include <xf86drm.h> @@ -36,6 +35,7 @@ #include "util/debug.h" #include "util/build_id.h" #include "util/mesa-sha1.h" +#include "util/phys_memory.h" #include "vk_util.h" #include "genxml/gen7_pack.h" @@ -74,11 +74,13 @@ anv_compute_heap_size(int fd, uint64_t *heap_size) } } - /* Query the total ram from the system */ - struct sysinfo info; - sysinfo(&info); + uint64_t total_ram; + + if (!os_get_total_physical_memory(&total_ram)) { + return vk_errorf(NULL, NULL, VK_ERROR_INITIALIZATION_FAILED, + "failed to get physical memory size"); + } - uint64_t total_ram = (uint64_t)info.totalram * (uint64_t)info.mem_unit; /* We don't want to burn too much ram with the GPU. If the user has 4GiB * or less, we use at most half. If they have more than 4GiB, we use 3/4. diff --git a/src/mesa/drivers/dri/i915/intel_screen.c b/src/mesa/drivers/dri/i915/intel_screen.c index 5024a69bd4..5182238bfa 100644 --- a/src/mesa/drivers/dri/i915/intel_screen.c +++ b/src/mesa/drivers/dri/i915/intel_screen.c @@ -40,6 +40,7 @@ #include "utils.h" #include "util/xmlpool.h" +#include "util/phys_memory.h" static const __DRIconfigOptionsExtension i915_config_options = { .base = { __DRI_CONFIG_OPTIONS, 1 }, @@ -738,15 +739,11 @@ i915_query_renderer_integer(__DRIscreen *psp, int param, unsigned int *value) const unsigned gpu_mappable_megabytes = (aper_size / (1024 * 1024)) * 3 / 4; - const long system_memory_pages = sysconf(_SC_PHYS_PAGES); - const long system_page_size = sysconf(_SC_PAGE_SIZE); + uint64_t system_memory_bytes; - if (system_memory_pages <= 0 || system_page_size <= 0) + if (!os_get_total_physical_memory(&system_memory_bytes)) return -1; - const uint64_t system_memory_bytes = (uint64_t) system_memory_pages - * (uint64_t) system_page_size; - const unsigned system_memory_megabytes = (unsigned) (system_memory_bytes / (1024 * 1024)); diff --git a/src/mesa/drivers/dri/i965/intel_screen.c b/src/mesa/drivers/dri/i965/intel_screen.c index 9dbda5142e..0c081f49b2 100644 --- a/src/mesa/drivers/dri/i965/intel_screen.c +++ b/src/mesa/drivers/dri/i965/intel_screen.c @@ -42,6 +42,7 @@ #include "utils.h" #include "util/xmlpool.h" +#include "util/phys_memory.h" static const __DRIconfigOptionsExtension brw_config_options = { .base = { __DRI_CONFIG_OPTIONS, 1 }, @@ -1419,15 +1420,11 @@ brw_query_renderer_integer(__DRIscreen *dri_screen, const unsigned gpu_mappable_megabytes = screen->aperture_threshold / (1024 * 1024); - const long system_memory_pages = sysconf(_SC_PHYS_PAGES); - const long system_page_size = sysconf(_SC_PAGE_SIZE); + uint64_t system_memory_bytes; - if (system_memory_pages <= 0 || system_page_size <= 0) + if (!os_get_total_physical_memory(&system_memory_bytes)) return -1; - const uint64_t system_memory_bytes = (uint64_t) system_memory_pages - * (uint64_t) system_page_size; - const unsigned system_memory_megabytes = (unsigned) (system_memory_bytes / (1024 * 1024)); diff --git a/src/mesa/drivers/dri/swrast/swrast.c b/src/mesa/drivers/dri/swrast/swrast.c index 4bee01ad30..eecdbc1566 100644 --- a/src/mesa/drivers/dri/swrast/swrast.c +++ b/src/mesa/drivers/dri/swrast/swrast.c @@ -51,6 +51,7 @@ #include "vbo/vbo.h" #include "drivers/common/driverfuncs.h" #include "drivers/common/meta.h" +#include "util/phys_memory.h" #include "utils.h" #include "main/teximage.h" @@ -142,29 +143,11 @@ swrast_query_renderer_integer(__DRIscreen *psp, int param, value[0] = 0; return 0; case __DRI2_RENDERER_VIDEO_MEMORY: { - /* This should probably share code with os_get_total_physical_memory() - * from src/gallium/auxiliary/os/os_misc.c - */ -#if defined(CTL_HW) && defined(HW_MEMSIZE) - int mib[2] = { CTL_HW, HW_MEMSIZE }; - unsigned long system_memory_bytes; - size_t len = sizeof(system_memory_bytes); - if (sysctl(mib, 2, &system_memory_bytes, &len, NULL, 0) != 0) - return -1; -#elif defined(_SC_PHYS_PAGES) && defined(_SC_PAGE_SIZE) - /* XXX: Do we want to return the full amount of system memory ? */ - const long system_memory_pages = sysconf(_SC_PHYS_PAGES); - const long system_page_size = sysconf(_SC_PAGE_SIZE); + uint64_t system_memory_bytes; - if (system_memory_pages <= 0 || system_page_size <= 0) + if (!os_get_total_physical_memory(&system_memory_bytes)) return -1; - const uint64_t system_memory_bytes = (uint64_t) system_memory_pages - * (uint64_t) system_page_size; -#else -#error "Unsupported platform" -#endif - const unsigned system_memory_megabytes = (unsigned) (system_memory_bytes / (1024 * 1024)); diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources index f30a063630..8ef7b7ab87 100644 --- a/src/util/Makefile.sources +++ b/src/util/Makefile.sources @@ -25,6 +25,7 @@ MESA_UTIL_FILES := \ mesa-sha1.h \ os_time.c \ os_time.h \ + phys_memory.h \ sha1/sha1.c \ sha1/sha1.h \ ralloc.c \ diff --git a/src/util/meson.build b/src/util/meson.build index 4e9ba14a2c..00d2018f2d 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -49,6 +49,7 @@ files_mesa_util = files( 'mesa-sha1.h', 'os_time.c', 'os_time.h', + 'phys_memory.h', 'sha1/sha1.c', 'sha1/sha1.h', 'ralloc.c', diff --git a/src/util/phys_memory.h b/src/util/phys_memory.h new file mode 100644 index 0000000000..f0822b4fec --- /dev/null +++ b/src/util/phys_memory.h @@ -0,0 +1,81 @@ +/* + * Copyright © 2008-2010 Vmware, Inc. + * Copyright © 2018 Greg V + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + */ + +#include <stdbool.h> +#ifdef __APPLE__ +#include <sys/sysctl.h> +#elif __HAIKU__ +#include <kernel/OS.h> +#elif defined(_WIN32) || defined(WIN32) +#include <windows.h> +#else +#include <unistd.h> +#endif + +/** + * Return the size of the total physical memory. + * \param size returns the size of the total physical memory + * \return true for success, or false on failure + */ +static inline bool +os_get_total_physical_memory(uint64_t *size) +{ +#ifdef __APPLE__ + // Use sysctl because _SC_PHYS_PAGES only started working in 10.11: https://twitter.com/cperciva/status/711799247817945088 + size_t len = sizeof(*size); + int mib[2]; + + mib[0] = CTL_HW; + mib[1] = HW_MEMSIZE; + + return (sysctl(mib, 2, size, &len, NULL, 0) == 0); +#elif __HAIKU__ + system_info info; + status_t ret; + + ret = get_system_info(&info); + if (ret != B_OK || info.max_pages <= 0) + return false; + + *size = (uint64_t)info.max_pages * (uint64_t)B_PAGE_SIZE; + return true; +#elif defined(_WIN32) || defined(WIN32) + MEMORYSTATUSEX status; + BOOL ret; + + status.dwLength = sizeof(status); + ret = GlobalMemoryStatusEx(&status); + *size = status.ullTotalPhys; + return (ret == TRUE); +#else + const long phys_pages = sysconf(_SC_PHYS_PAGES); + const long page_size = sysconf(_SC_PAGESIZE); + + if (phys_pages <= 0 || page_size <= 0) + return false; + + *size = (uint64_t)phys_pages * (uint64_t)page_size; + return true; +#endif +} -- 2.15.1 _______________________________________________ mesa-dev mailing list mesa-dev@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/mesa-dev