From 1bcfb2baad517947e1bd68309f982567e823d964 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 14 Jan 2025 13:33:14 -0400 Subject: [PATCH] MT#55283 use bencode buffer for log limiter This allows us to deallocate all held strings in one go without having to iterate much. Requires adjusting tests, Makefiles and dependencies. Change-Id: Ic214f9dd2cd9609cb472301f52bb10d1918d859e --- {include => lib}/helpers.h | 0 lib/loglib.c | 26 +++++++++++++------------- perf-tester/Makefile | 6 +++++- recording-daemon/Makefile | 5 ++++- t/Makefile | 20 ++++++++++---------- 5 files changed, 32 insertions(+), 25 deletions(-) rename {include => lib}/helpers.h (100%) diff --git a/include/helpers.h b/lib/helpers.h similarity index 100% rename from include/helpers.h rename to lib/helpers.h diff --git a/lib/loglib.c b/lib/loglib.c index a2febb137..d49b81c2b 100644 --- a/lib/loglib.c +++ b/lib/loglib.c @@ -10,6 +10,7 @@ #include #include #include "auxlib.h" +#include "bencode.h" struct log_limiter_entry { @@ -90,6 +91,7 @@ int ilog_facility = LOG_DAEMON; static GHashTable *__log_limiter; static mutex_t __log_limiter_lock = MUTEX_STATIC_INIT; static unsigned int __log_limiter_count; +static bencode_buffer_t __log_limiter_buffer; @@ -170,16 +172,20 @@ void __vpilog(int prio, const char *prefix, const char *fmt, va_list ap) { if (__log_limiter_count > 10000) { g_hash_table_remove_all(__log_limiter); __log_limiter_count = 0; + bencode_buffer_free(&__log_limiter_buffer); + bencode_buffer_init(&__log_limiter_buffer); } time_t now = time(NULL); llep = g_hash_table_lookup(__log_limiter, &lle); if (!llep || (now - llep->when) >= 15) { - llep = g_slice_alloc0(sizeof(*llep)); - llep->prefix = strdup(prefix); - llep->msg = strdup(msg); - llep->when = now; + llep = bencode_buffer_alloc(&__log_limiter_buffer, sizeof(*llep)); + *llep = (__typeof(*llep)) { + .prefix = bencode_strdup(&__log_limiter_buffer, prefix), + .msg = bencode_strdup(&__log_limiter_buffer, msg), + .when = now, + }; g_hash_table_insert(__log_limiter, llep, llep); __log_limiter_count++; llep = NULL; @@ -246,16 +252,9 @@ static int log_limiter_entry_equal(const void *a, const void *b) { return 1; } -static void log_limiter_entry_free(void *p) { - struct log_limiter_entry *lle = p; - free(lle->prefix); - free(lle->msg); - g_slice_free1(sizeof(*lle), lle); -} - void log_init(const char *handle) { - __log_limiter = g_hash_table_new_full(log_limiter_entry_hash, log_limiter_entry_equal, - log_limiter_entry_free, NULL); + __log_limiter = g_hash_table_new(log_limiter_entry_hash, log_limiter_entry_equal); + bencode_buffer_init(&__log_limiter_buffer); if (!rtpe_common_config_ptr->log_stderr) openlog(handle, LOG_PID | LOG_NDELAY, ilog_facility); @@ -263,6 +262,7 @@ void log_init(const char *handle) { void log_free(void) { g_hash_table_destroy(__log_limiter); + bencode_buffer_free(&__log_limiter_buffer); } int parse_log_facility(const char *name, int *dst) { diff --git a/perf-tester/Makefile b/perf-tester/Makefile index 399560d86..387edbd12 100644 --- a/perf-tester/Makefile +++ b/perf-tester/Makefile @@ -8,8 +8,10 @@ CFLAGS ?= -g -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -Wstrict-prot CFLAGS += -pthread CFLAGS += -std=c11 CFLAGS += -I. -I../kernel-module/ -I../lib/ +CFLAGS += -DPCRE2_CODE_UNIT_WIDTH=8 CFLAGS += -DFIXTURES_PATH="\"$(FIXTURES_PATH)\"" CFLAGS += $(shell pkg-config --cflags glib-2.0) +CFLAGS += $(shell pkg-config --cflags json-glib-1.0) CFLAGS += $(shell pkg-config --cflags gthread-2.0) CFLAGS += -D_GNU_SOURCE CFLAGS += $(shell pkg-config --cflags libavcodec) @@ -25,6 +27,7 @@ CFLAGS += $(shell pkg-config --cflags openssl) LDLIBS = -lm -ldl LDLIBS += $(shell pkg-config --libs glib-2.0) +LDLIBS += $(shell pkg-config --libs json-glib-1.0) LDLIBS += $(shell pkg-config --libs gthread-2.0) LDLIBS += $(shell pkg-config --libs libavcodec) LDLIBS += $(shell pkg-config --libs libavformat) @@ -40,7 +43,8 @@ include ../lib/g729.Makefile include ../lib/codec-chain.Makefile SRCS = main.c log.c -LIBSRCS = codeclib.strhash.c loglib.c auxlib.c resample.c str.c dtmflib.c rtplib.c poller.c ssllib.c bufferpool.c +LIBSRCS = codeclib.strhash.c loglib.c auxlib.c resample.c str.c dtmflib.c rtplib.c poller.c ssllib.c bufferpool.c \ + bencode.c LIBASM = mvr2s_x64_avx2.S mvr2s_x64_avx512.S OBJS = $(SRCS:.c=.o) $(LIBSRCS:.c=.o) $(LIBASM:.S=.o) diff --git a/recording-daemon/Makefile b/recording-daemon/Makefile index 6e28fc840..22fa86c52 100644 --- a/recording-daemon/Makefile +++ b/recording-daemon/Makefile @@ -5,7 +5,9 @@ CFLAGS?= -g -Wall -Wextra -Wno-sign-compare -Wno-unused-parameter -Wstrict-proto CFLAGS+= -pthread -I. -I../lib/ -I../kernel-module/ CFLAGS+= -std=c11 CFLAGS+= -D_GNU_SOURCE -D_POSIX_SOURCE -D_POSIX_C_SOURCE +CFLAGS+= -DPCRE2_CODE_UNIT_WIDTH=8 CFLAGS+= $(shell pkg-config --cflags glib-2.0) +CFLAGS+= $(shell pkg-config --cflags json-glib-1.0) CFLAGS+= $(shell pkg-config --cflags gthread-2.0) CFLAGS+= $(shell pkg-config --cflags libavcodec) CFLAGS+= $(shell pkg-config --cflags libavformat) @@ -19,6 +21,7 @@ CFLAGS+= $(shell pkg-config --cflags libcurl) LDLIBS= -lm -ldl LDLIBS+= $(shell pkg-config --libs glib-2.0) +LDLIBS+= $(shell pkg-config --libs json-glib-1.0) LDLIBS+= $(shell pkg-config --libs gthread-2.0) LDLIBS+= $(shell pkg-config --libs libavcodec) LDLIBS+= $(shell pkg-config --libs libavformat) @@ -35,7 +38,7 @@ include ../lib/g729.Makefile SRCS= epoll.c garbage.c inotify.c main.c metafile.c stream.c recaux.c packet.c \ decoder.c output.c mix.c db.c log.c forward.c tag.c poller.c notify.c LIBSRCS= loglib.c auxlib.c rtplib.c codeclib.strhash.c resample.c str.c socket.c streambuf.c ssllib.c \ - dtmflib.c bufferpool.c + dtmflib.c bufferpool.c bencode.c LIBASM= mvr2s_x64_avx2.S mvr2s_x64_avx512.S mix_in_x64_avx2.S mix_in_x64_avx512bw.S mix_in_x64_sse2.S OBJS= $(SRCS:.c=.o) $(LIBSRCS:.c=.o) $(LIBASM:.S=.o) diff --git a/t/Makefile b/t/Makefile index d0ae716db..f5377db35 100644 --- a/t/Makefile +++ b/t/Makefile @@ -67,7 +67,7 @@ endif include ../lib/codec-chain.Makefile SRCS= test-bitstr.c aes-crypt.c aead-aes-crypt.c test-const_str_hash.strhash.c aead-decrypt.c -LIBSRCS= loglib.c auxlib.c str.c rtplib.c ssllib.c mix_buffer.c bufferpool.c +LIBSRCS= loglib.c auxlib.c str.c rtplib.c ssllib.c mix_buffer.c bufferpool.c bencode.c DAEMONSRCS= crypto.c ssrc.c helpers.c rtp.c HASHSRCS= @@ -223,7 +223,7 @@ test-bitstr: test-bitstr.o test-mix-buffer: test-mix-buffer.o $(COMMONOBJS) mix_buffer.o ssrc.o rtp.o crypto.o helpers.o \ mix_in_x64_avx2.o mix_in_x64_sse2.o mix_in_x64_avx512bw.o codeclib.strhash.o dtmflib.o \ - mvr2s_x64_avx2.o mvr2s_x64_avx512.o resample.o bufferpool.o uring.o poller.o + mvr2s_x64_avx2.o mvr2s_x64_avx512.o resample.o bufferpool.o uring.o poller.o bencode.o spandsp_send_fax_pcm: spandsp_send_fax_pcm.o @@ -236,18 +236,18 @@ spandsp_recv_fax_t38: spandsp_recv_fax_t38.o spandsp_raw_fax_tests: spandsp_send_fax_pcm spandsp_recv_fax_pcm spandsp_send_fax_t38 spandsp_recv_fax_t38 test-amr-decode: test-amr-decode.o $(COMMONOBJS) codeclib.strhash.o resample.o dtmflib.o resample.o \ - mvr2s_x64_avx2.o mvr2s_x64_avx512.o + mvr2s_x64_avx2.o mvr2s_x64_avx512.o bencode.o test-amr-encode: test-amr-encode.o $(COMMONOBJS) codeclib.strhash.o resample.o dtmflib.o \ - mvr2s_x64_avx2.o mvr2s_x64_avx512.o + mvr2s_x64_avx2.o mvr2s_x64_avx512.o bencode.o test-dtmf-detect: test-dtmf-detect.o -aes-crypt: aes-crypt.o $(COMMONOBJS) crypto.o +aes-crypt: aes-crypt.o $(COMMONOBJS) crypto.o bencode.o -aead-decrypt: aead-decrypt.o $(COMMONOBJS) crypto.o rtp.o +aead-decrypt: aead-decrypt.o $(COMMONOBJS) crypto.o rtp.o bencode.o -aead-aes-crypt: aead-aes-crypt.o $(COMMONOBJS) crypto.o +aead-aes-crypt: aead-aes-crypt.o $(COMMONOBJS) crypto.o bencode.o test-stats: test-stats.o $(COMMONOBJS) codeclib.strhash.o resample.o codec.o ssrc.o call.o ice.o helpers.o \ kernel.o media_socket.o stun.o bencode.o socket.o poller.o dtls.o recording.o statistics.o \ @@ -268,14 +268,14 @@ test-transcode: test-transcode.o $(COMMONOBJS) codeclib.strhash.o resample.o cod mix_in_x64_avx2.o mix_in_x64_sse2.o mix_in_x64_avx512bw.o bufferpool.o uring.o arena.o test-resample: test-resample.o $(COMMONOBJS) codeclib.strhash.o resample.o dtmflib.o mvr2s_x64_avx2.o \ - mvr2s_x64_avx512.o + mvr2s_x64_avx512.o bencode.o test-payload-tracker: test-payload-tracker.o $(COMMONOBJS) ssrc.o helpers.o auxlib.o rtp.o crypto.o codeclib.strhash.o \ - resample.o dtmflib.o mvr2s_x64_avx2.o mvr2s_x64_avx512.o bufferpool.o uring.o poller.o + resample.o dtmflib.o mvr2s_x64_avx2.o mvr2s_x64_avx512.o bufferpool.o uring.o poller.o bencode.o test-kernel-module: test-kernel-module.o $(COMMONOBJS) kernel.o -test-const_str_hash.strhash: test-const_str_hash.strhash.o $(COMMONOBJS) +test-const_str_hash.strhash: test-const_str_hash.strhash.o $(COMMONOBJS) bencode.o PRELOAD_CFLAGS += -D_GNU_SOURCE -std=c11 PRELOAD_LIBS += -ldl