From c2c6ddc00212d4683c511e71dba136aca3e527f5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Adrien=20B=C3=A9raud?= <adrien.beraud@savoirfairelinux.com>
Date: Tue, 28 Nov 2017 16:03:19 +0100
Subject: [PATCH] decoder: limit number of threads

Using too many threads for decoding is not recommended and can
introduce latency.
Also during a call there are 2 live encoding and 2 live decoding,
so using all threads for every decoding is not useful and could
decrease performance and increase latency because of more
CPU context switch.
This is especially visible on machines with many logical cores
like AMD Threadrippers or some Intel CPUs.
Ideally, loads should use in total as many threads as logical cores
to profit from the full machine power while limiting context switches.

In this patch, decoders will use half the number of logical core,
with a maximum of 8 threads.

Change-Id: Ibe6c083c35fba972d930346629de2175625afd8c
Reviewed-by: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
---
 src/media/media_decoder.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/src/media/media_decoder.cpp b/src/media/media_decoder.cpp
index 3f678e2c13..ef02e4d1f8 100644
--- a/src/media/media_decoder.cpp
+++ b/src/media/media_decoder.cpp
@@ -40,6 +40,7 @@
 #include <unistd.h>
 #include <thread> // hardware_concurrency
 #include <chrono>
+#include <algorithm>
 
 namespace ring {
 
@@ -206,7 +207,7 @@ int MediaDecoder::setupFromAudioData(const AudioFormat format)
     }
 #endif
 
-    decoderCtx_->thread_count = std::thread::hardware_concurrency();
+    decoderCtx_->thread_count = std::max(1u, std::min(8u, std::thread::hardware_concurrency()/2));
     decoderCtx_->channels = format.nb_channels;
     decoderCtx_->sample_rate = format.sample_rate;
 
@@ -299,7 +300,7 @@ int MediaDecoder::setupFromVideoData()
 #endif
     RING_DBG("Decoding video using %s (%s)", inputDecoder_->long_name, inputDecoder_->name);
 
-    decoderCtx_->thread_count = std::thread::hardware_concurrency();
+    decoderCtx_->thread_count = std::max(1u, std::min(8u, std::thread::hardware_concurrency()/2));
 
 #ifdef RING_ACCEL
     if (enableAccel_) {
-- 
GitLab