From 3a4698e63a800d1a412f5533bd9bc22436ab74b7 Mon Sep 17 00:00:00 2001 From: Andy Green Date: Mon, 24 Feb 2020 10:32:59 +0000 Subject: [PATCH] malloc_trim: move out of unix plat and into lwsws https://github.com/warmcat/libwebsockets/issues/1849 --- lib/plat/unix/unix-init.c | 7 ------- lwsws/main.c | 28 +++++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/plat/unix/unix-init.c b/lib/plat/unix/unix-init.c index 6204e52d2..565c7cab6 100644 --- a/lib/plat/unix/unix-init.c +++ b/lib/plat/unix/unix-init.c @@ -30,10 +30,6 @@ #endif #include -#if defined(LWS_HAVE_MALLOC_TRIM) -#include -#endif - #if defined(LWS_WITH_NETWORK) static void lws_sul_plat_unix(lws_sorted_usec_list_t *sul) @@ -51,9 +47,6 @@ lws_sul_plat_unix(lws_sorted_usec_list_t *sul) kill(pt->context->started_with_parent, 0) < 0) kill(getpid(), SIGTERM); #endif -#if defined(LWS_HAVE_MALLOC_TRIM) - malloc_trim(4 * 1024); -#endif if (pt->context->deprecated && !pt->context->count_wsi_allocated) { lwsl_notice("%s: ending deprecated context\n", __func__); diff --git a/lwsws/main.c b/lwsws/main.c index 4a193071b..f74d8033c 100644 --- a/lwsws/main.c +++ b/lwsws/main.c @@ -1,7 +1,7 @@ /* * libwebsockets web server application * - * Written in 2010-2019 by Andy Green + * Written in 2010-2020 by Andy Green * * This file is made available under the Creative Commons CC0 1.0 * Universal Public Domain Dedication. @@ -51,7 +51,12 @@ int fork(void) #include +#if defined(LWS_HAVE_MALLOC_TRIM) +#include +#endif + static struct lws_context *context; +static lws_sorted_usec_list_t sul_lwsws; static char config_dir[128]; static int opts = 0, do_reload = 1; static uv_loop_t loop; @@ -111,6 +116,18 @@ void signal_cb(uv_signal_t *watcher, int signum) lws_context_destroy(context); } +static void +lwsws_min(lws_sorted_usec_list_t *sul) +{ + lwsl_debug("%s\n", __func__); + +#if defined(LWS_HAVE_MALLOC_TRIM) + malloc_trim(4 * 1024); +#endif + + lws_sul_schedule(context, 0, &sul_lwsws, lwsws_min, 60 * LWS_US_PER_SEC); +} + static int context_creation(void) { @@ -162,6 +179,8 @@ context_creation(void) if (lwsws_get_config_vhosts(context, &info, config_dir, &cs, &cs_len)) return 1; + lws_sul_schedule(context, 0, &sul_lwsws, lwsws_min, 60 * LWS_US_PER_SEC); + return 0; init_failed: @@ -287,8 +306,8 @@ int main(int argc, char **argv) lws_set_log_level(debug_level, lwsl_emit_stderr_notimestamp); - lwsl_notice("lwsws libwebsockets web server - license CC0 + LGPL2.1\n"); - lwsl_notice("(C) Copyright 2010-2018 Andy Green \n"); + lwsl_notice("lwsws libwebsockets web server - license CC0 + MIT\n"); + lwsl_notice("(C) Copyright 2010-2020 Andy Green \n"); #if (UV_VERSION_MAJOR > 0) // Travis... uv_loop_init(&loop); @@ -315,6 +334,9 @@ int main(int argc, char **argv) uv_close((uv_handle_t *)&signal_outer[n], NULL); } + /* cancel the per-minute sul */ + lws_sul_schedule(context, 0, &sul_lwsws, NULL, LWS_SET_TIMER_USEC_CANCEL); + lws_context_destroy(context); (void)budget; #if (UV_VERSION_MAJOR > 0) // Travis...