From 6ff1f175eec35c220c6df70053e30a9c59c3c23b Mon Sep 17 00:00:00 2001 From: Federico Lorenzi Date: Sat, 18 Jun 2022 11:20:32 +0200 Subject: [PATCH] Flush the page cache every 100 packets while recording --- record.c | 14 ++++++++++++++ record.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/record.c b/record.c index b92bf59d7b..30a96485f3 100644 --- a/record.c +++ b/record.c @@ -21,6 +21,7 @@ #include #include #include +#include #include #include @@ -464,6 +465,19 @@ int janus_recorder_save_frame(janus_recorder *recorder, char *buffer, uint lengt header->seq_number = htons(seq); header->timestamp = htonl(timestamp); } + + /* Mark the file as POSIX_FADV_DONTNEED, since we won't be reading from it at all. This + * prevents polution of the page cache, but also fixes a nasty bug where CGroupsV2 + * considers the page cache to be used kernel memory, and won't allocate new UDP + * buffer space if it's full, leading to dropped packets. Can't just do this once + * at the start unfortunately, so we do it every ~100 packets. + */ + recorder->unflushed_count++; + if (recorder->unflushed_count == 100) { + posix_fadvise(fileno(recorder->file), 0, 0, POSIX_FADV_DONTNEED); + recorder->unflushed_count = 0; + } + /* Done */ janus_mutex_unlock_nodebug(&recorder->mutex); return 0; diff --git a/record.h b/record.h index 354474f340..06a2741cbe 100644 --- a/record.h +++ b/record.h @@ -73,6 +73,8 @@ typedef struct janus_recorder { volatile gint destroyed; /*! \brief Reference counter for this instance */ janus_refcount ref; + /*! \brief Used to flush the page cache every n packets */ + int unflushed_count; } janus_recorder; /*! \brief Initialize the recorder code