]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MEDIUM: pool: Following up on previous pool trimming update.
authorDavid CARLIER <devnexen@gmail.com>
Fri, 26 Nov 2021 20:44:44 +0000 (20:44 +0000)
committerWilly Tarreau <w@1wt.eu>
Wed, 1 Dec 2021 09:38:31 +0000 (10:38 +0100)
Apple libmalloc has its own notion of memory arenas as malloc_zone with
rich API having various callbacks for various allocations strategies but
here we just use the defaults.
In trim_all_pools,  we advise to purge each zone as much as possible, called "greedy" mode.

include/haproxy/compat.h
src/pool.c

index 25b15a1f0f7b0cb1a308d25fdfdc996969e2d00f..f8afe1b5c32972c9db15aec77d8f040f9f8451a7 100644 (file)
@@ -274,9 +274,10 @@ typedef struct { } empty_t;
 #endif
 
 /* macOS has a call similar to malloc_usable_size */
-#if defined(USE_MEMORY_PROFILING) && defined(__APPLE__)
+#if defined(__APPLE__)
 #include <malloc/malloc.h>
 #define malloc_usable_size malloc_size
+#define HA_HAVE_MALLOC_ZONE
 #endif
 
 /* Max number of file descriptors we send in one sendmsg(). Linux seems to be
index 4d1d7d2ec6fc1b97b4d942fc76de04c0ac2a52b4..984c8909d47d57fd3cf6ecc0df41f5ae804ae256 100644 (file)
@@ -63,6 +63,21 @@ static void trim_all_pools(void)
 #if defined(HA_HAVE_MALLOC_TRIM)
                if (using_default_allocator)
                        malloc_trim(0);
+#elif defined(HA_HAVE_MALLOC_ZONE)
+               if (using_default_allocator) {
+                       vm_address_t *zones;
+                       unsigned int i, nzones;
+
+                       if (malloc_get_all_zones(0, NULL, &zones, &nzones) == KERN_SUCCESS) {
+                               for (i = 0; i < nzones; i ++) {
+                                       malloc_zone_t *zone = (malloc_zone_t *)zones[i];
+
+                                       /* we cannot purge anonymous zones */
+                                       if (zone->zone_name)
+                                               malloc_zone_pressure_relief(zone, 0);
+                               }
+                       }
+               }
 #endif
        }
 }
@@ -111,6 +126,8 @@ static void detect_allocator(void)
                free(DISGUISE(ptr));
 
                using_default_allocator = !!memcmp(&mi1, &mi2, sizeof(mi1));
+#elif defined(HA_HAVE_MALLOC_ZONE)
+               using_default_allocator = (malloc_default_zone() != NULL);
 #endif
        }
 }