From 0e34c02dd60f8d301a1b4c1bbcd3c90ee05bdb0a Mon Sep 17 00:00:00 2001 From: Matthew Marsh Date: Tue, 22 Dec 2015 11:10:31 +0200 Subject: [PATCH] nvenc: fix high CPU use on initialization of multiple encoders at the same time We need a static lock to protect various NVENC methods in _set_format(). Without this the CPU use increases dramatically on initialisation of the element when there are multiple elements being initialised at the same time. https://bugzilla.gnome.org/show_bug.cgi?id=759742 --- sys/nvenc/gstnvbaseenc.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sys/nvenc/gstnvbaseenc.c b/sys/nvenc/gstnvbaseenc.c index ccf7186a97..8e5b088269 100644 --- a/sys/nvenc/gstnvbaseenc.c +++ b/sys/nvenc/gstnvbaseenc.c @@ -70,6 +70,11 @@ enum PROP_DEVICE_ID, }; +/* This lock is needed to prevent the situation where multiple encoders are + * initialised at the same time which appears to cause excessive CPU usage over + * some period of time. */ +G_LOCK_DEFINE_STATIC (initialization_lock); + #if HAVE_NVENC_GST_GL struct gl_input_resource { @@ -878,10 +883,14 @@ gst_nv_base_enc_set_format (GstVideoEncoder * enc, GstVideoCodecState * state) NVENCSTATUS nv_ret; g_assert (nvenc_class->initialize_encoder); + + G_LOCK (initialization_lock); if (!nvenc_class->initialize_encoder (nvenc, old_state, state)) { GST_ERROR_OBJECT (enc, "Subclass failed to reconfigure encoder"); + G_UNLOCK (initialization_lock); return FALSE; } + G_UNLOCK (initialization_lock); if (!nvenc->max_encode_width && !nvenc->max_encode_height) { gst_nv_base_enc_set_max_encode_size (nvenc, GST_VIDEO_INFO_WIDTH (info), @@ -1020,7 +1029,10 @@ gst_nv_base_enc_set_format (GstVideoEncoder * enc, GstVideoCodecState * state) cout_buf.size = 1024 * 1024; cout_buf.memoryHeap = NV_ENC_MEMORY_HEAP_SYSMEM_CACHED; + G_LOCK (initialization_lock); nv_ret = NvEncCreateBitstreamBuffer (nvenc->encoder, &cout_buf); + G_UNLOCK (initialization_lock); + if (nv_ret != NV_ENC_SUCCESS) { GST_WARNING_OBJECT (enc, "Failed to allocate input buffer: %d", nv_ret); /* FIXME: clean up */