From d436c1d361d62db0771b66bee434afe830a71767 Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Fri, 10 Jan 2025 11:20:27 -0400 Subject: [PATCH] MT#61822 support evicting player cache data Change-Id: I58c1127f2b906a3e238a2dc8579d433abe72c50b --- daemon/cli.c | 7 +++++++ daemon/media_player.c | 40 ++++++++++++++++++++++++++++++++++++++++ include/media_player.h | 1 + 3 files changed, 48 insertions(+) diff --git a/daemon/cli.c b/daemon/cli.c index e264364b8..fe1306289 100644 --- a/daemon/cli.c +++ b/daemon/cli.c @@ -131,6 +131,7 @@ static void cli_incoming_media_evict_db(str *instr, struct cli_writer *cw, const static void cli_incoming_media_evict_dbs(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_media_evict_cache(str *instr, struct cli_writer *cw, const cli_handler_t *); static void cli_incoming_media_evict_caches(str *instr, struct cli_writer *cw, const cli_handler_t *); +static void cli_incoming_media_evict_players(str *instr, struct cli_writer *cw, const cli_handler_t *); #endif @@ -233,6 +234,7 @@ static const cli_handler_t cli_media_evict_handlers[] = { { "dbs", cli_incoming_media_evict_dbs, NULL }, { "cache", cli_incoming_media_evict_cache, NULL }, { "caches", cli_incoming_media_evict_caches, NULL }, + { "players", cli_incoming_media_evict_players, NULL }, { NULL, }, }; static const cli_handler_t cli_media_handlers[] = { @@ -2035,4 +2037,9 @@ static void cli_incoming_media_evict_caches(str *instr, struct cli_writer *cw, c unsigned int num = media_player_evict_caches(); cw->cw_printf(cw, "%u DB cache entries evicted\n", num); } + +static void cli_incoming_media_evict_players(str *instr, struct cli_writer *cw, const cli_handler_t *handler) { + unsigned int num = media_player_evict_player_caches(); + cw->cw_printf(cw, "%u DB cache entries evicted\n", num); +} #endif diff --git a/daemon/media_player.c b/daemon/media_player.c index 6d156904b..45a3a9a4e 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -108,6 +108,7 @@ static void __media_player_cache_entry_free(struct media_player_cache_entry *p); TYPED_GHASHTABLE(media_player_cache_ht, struct media_player_cache_index, struct media_player_cache_entry, media_player_cache_entry_hash, media_player_cache_entry_eq, NULL, __obj_put) +TYPED_GQUEUE(media_player_cache_entry, struct media_player_cache_entry) static media_player_cache_ht media_player_cache; // keys and values only ever freed at shutdown TYPED_GHASHTABLE(media_player_media_files_ht, str, struct media_player_media_file, str_hash, str_equal, @@ -663,6 +664,7 @@ static void media_player_cached_reader_start(struct media_player *mp, str_case_v static void cache_packet_free(struct media_player_cache_packet *p) { + RTPE_GAUGE_ADD(player_cache, -1 * (ssize_t) p->s.len); bufferpool_unref(p->buf); g_slice_free1(sizeof(*p), p); } @@ -2632,3 +2634,41 @@ charp_q media_player_list_player_cache(void) { #endif return ret; } + +#ifdef WITH_TRANSCODING +// lock must not be held +static bool media_player_evict_player_cache(struct media_player_cache_entry *entry) { + LOCK(&media_player_cache_lock); + if (t_hash_table_remove(media_player_cache, &entry->index)) + return true; + return false; +} +#endif + +unsigned int media_player_evict_player_caches(void) { + unsigned int ret = 0; +#ifdef WITH_TRANSCODING + if (!t_hash_table_is_set(media_player_cache)) + return 0; + + // grab references from hash table + media_player_cache_entry_q q = TYPED_GQUEUE_INIT; + media_player_cache_ht_iter iter; + { + LOCK(&media_player_cache_lock); + t_hash_table_iter_init(&iter, media_player_cache); + struct media_player_cache_entry *entry; + while (t_hash_table_iter_next(&iter, NULL, &entry)) + t_queue_push_tail(&q, obj_get(entry)); + } + + // release references + while (q.head) { + __auto_type entry = t_queue_pop_head(&q); + if (media_player_evict_player_cache(entry)) + ret++; + obj_put(entry); + } +#endif + return ret; +} diff --git a/include/media_player.h b/include/media_player.h index 958a9b666..d1df3753f 100644 --- a/include/media_player.h +++ b/include/media_player.h @@ -174,6 +174,7 @@ bool media_player_reload_cache(unsigned long long); unsigned int media_player_reload_caches(void); enum thread_looper_action media_player_refresh_cache(void); charp_q media_player_list_player_cache(void); +unsigned int media_player_evict_player_caches(void); struct send_timer *send_timer_new(struct packet_stream *); void send_timer_push(struct send_timer *, struct codec_packet *);