linux-media.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] qv4l2: Prevent high CPU usage on device disconnect
@ 2019-05-09 13:32 Tasos Sahanidis
  2019-05-27 12:20 ` Hans Verkuil
  0 siblings, 1 reply; 3+ messages in thread
From: Tasos Sahanidis @ 2019-05-09 13:32 UTC (permalink / raw)
  To: linux-media

On device disconnect, ApplicationWindow::ctrlEvent() gets called
continuously, causing high CPU usage.
Closing the device handles once ENODEV is returned fixes this.
---
 utils/qv4l2/alsa_stream.c |  2 +-
 utils/qv4l2/qv4l2.cpp     | 24 +++++++++++++++++++-----
 2 files changed, 20 insertions(+), 6 deletions(-)

diff --git a/utils/qv4l2/alsa_stream.c b/utils/qv4l2/alsa_stream.c
index 05944822..2dca283b 100644
--- a/utils/qv4l2/alsa_stream.c
+++ b/utils/qv4l2/alsa_stream.c
@@ -433,7 +433,7 @@ static snd_pcm_sframes_t readbuf(snd_pcm_t *handle, char *buf, long len)
     snd_pcm_uframes_t frames;
     snd_pcm_htimestamp(handle, &frames, &timestamp);
     r = snd_pcm_readi(handle, buf, len);
-    if (r < 0 && r != -EAGAIN) {
+    if (r < 0 && !(r == -EAGAIN || r == -ENODEV)) {
 	r = snd_pcm_recover(handle, r, 0);
 	if (r < 0)
 	    fprintf(error_fp, "alsa: overrun recover error: %s\n", snd_strerror(r));
diff --git a/utils/qv4l2/qv4l2.cpp b/utils/qv4l2/qv4l2.cpp
index b52a3b60..9ac2c332 100644
--- a/utils/qv4l2/qv4l2.cpp
+++ b/utils/qv4l2/qv4l2.cpp
@@ -495,8 +495,9 @@ void ApplicationWindow::setAudioBufferSize()
 void ApplicationWindow::ctrlEvent()
 {
 	v4l2_event ev;
+	int event_ret = 0;
 
-	while (dqevent(ev) == 0) {
+	while ((event_ret = dqevent(ev)) == 0) {
 		if (ev.type == V4L2_EVENT_SOURCE_CHANGE) {
 			m_genTab->sourceChange(ev);
 			continue;
@@ -551,6 +552,15 @@ void ApplicationWindow::ctrlEvent()
 			setString(ev.id, c.string);
 		free(c.string);
 	}
+
+	if (event_ret && errno == ENODEV) {
+		closeDevice();
+		if (m_capture != NULL) {
+			m_capture->stop();
+			delete m_capture;
+			m_capture = NULL;
+		}
+	}
 }
 
 void ApplicationWindow::newCaptureWin()
@@ -558,6 +568,7 @@ void ApplicationWindow::newCaptureWin()
 	if (m_capture != NULL) {
 		m_capture->stop();
 		delete m_capture;
+		m_capture = NULL;
 	}
 
 	switch (m_renderMethod) {
@@ -1135,7 +1146,7 @@ void ApplicationWindow::stopStreaming()
 	if (!m_genTab->isSDR() && m_genTab->isRadio())
 		return;
 
-	if (v4l_type_is_capture(g_type()))
+	if (v4l_type_is_capture(g_type()) && m_capture != NULL)
 		m_capture->stop();
 
 	m_snapshotAct->setDisabled(true);
@@ -1561,8 +1572,10 @@ void ApplicationWindow::makeFullScreen(bool checked)
 void ApplicationWindow::closeDevice()
 {
 	stopAudio();
-	delete m_sigMapper;
-	m_sigMapper = NULL;
+	if(m_sigMapper != NULL) {
+		m_sigMapper->deleteLater();
+		m_sigMapper = NULL;
+	}
 	m_capStartAct->setEnabled(false);
 	m_capStartAct->setChecked(false);
 	m_capStepAct->setEnabled(false);
@@ -1579,7 +1592,7 @@ void ApplicationWindow::closeDevice()
 			m_outNotifier = NULL;
 		}
 		if (m_ctrlNotifier) {
-			delete m_ctrlNotifier;
+			m_ctrlNotifier->deleteLater();
 			m_ctrlNotifier = NULL;
 		}
 		delete [] m_frameData;
@@ -1740,6 +1753,7 @@ void ApplicationWindow::closeEvent(QCloseEvent *event)
 {
 	closeDevice();
 	delete m_capture;
+	m_capture = NULL;
 	event->accept();
 }
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] qv4l2: Prevent high CPU usage on device disconnect
  2019-05-09 13:32 [PATCH] qv4l2: Prevent high CPU usage on device disconnect Tasos Sahanidis
@ 2019-05-27 12:20 ` Hans Verkuil
  2019-05-28 11:00   ` Tasos Sahanidis
  0 siblings, 1 reply; 3+ messages in thread
From: Hans Verkuil @ 2019-05-27 12:20 UTC (permalink / raw)
  To: Tasos Sahanidis, linux-media

Hi Tasos,

On 5/9/19 3:32 PM, Tasos Sahanidis wrote:
> On device disconnect, ApplicationWindow::ctrlEvent() gets called
> continuously, causing high CPU usage.
> Closing the device handles once ENODEV is returned fixes this.

I'm missing your Signed-of-by. Can you reply to this and add your SoB so I
can merge this patch?

Regards,

	Hans

> ---
>  utils/qv4l2/alsa_stream.c |  2 +-
>  utils/qv4l2/qv4l2.cpp     | 24 +++++++++++++++++++-----
>  2 files changed, 20 insertions(+), 6 deletions(-)
> 
> diff --git a/utils/qv4l2/alsa_stream.c b/utils/qv4l2/alsa_stream.c
> index 05944822..2dca283b 100644
> --- a/utils/qv4l2/alsa_stream.c
> +++ b/utils/qv4l2/alsa_stream.c
> @@ -433,7 +433,7 @@ static snd_pcm_sframes_t readbuf(snd_pcm_t *handle, char *buf, long len)
>      snd_pcm_uframes_t frames;
>      snd_pcm_htimestamp(handle, &frames, &timestamp);
>      r = snd_pcm_readi(handle, buf, len);
> -    if (r < 0 && r != -EAGAIN) {
> +    if (r < 0 && !(r == -EAGAIN || r == -ENODEV)) {
>  	r = snd_pcm_recover(handle, r, 0);
>  	if (r < 0)
>  	    fprintf(error_fp, "alsa: overrun recover error: %s\n", snd_strerror(r));
> diff --git a/utils/qv4l2/qv4l2.cpp b/utils/qv4l2/qv4l2.cpp
> index b52a3b60..9ac2c332 100644
> --- a/utils/qv4l2/qv4l2.cpp
> +++ b/utils/qv4l2/qv4l2.cpp
> @@ -495,8 +495,9 @@ void ApplicationWindow::setAudioBufferSize()
>  void ApplicationWindow::ctrlEvent()
>  {
>  	v4l2_event ev;
> +	int event_ret = 0;
>  
> -	while (dqevent(ev) == 0) {
> +	while ((event_ret = dqevent(ev)) == 0) {
>  		if (ev.type == V4L2_EVENT_SOURCE_CHANGE) {
>  			m_genTab->sourceChange(ev);
>  			continue;
> @@ -551,6 +552,15 @@ void ApplicationWindow::ctrlEvent()
>  			setString(ev.id, c.string);
>  		free(c.string);
>  	}
> +
> +	if (event_ret && errno == ENODEV) {
> +		closeDevice();
> +		if (m_capture != NULL) {
> +			m_capture->stop();
> +			delete m_capture;
> +			m_capture = NULL;
> +		}
> +	}
>  }
>  
>  void ApplicationWindow::newCaptureWin()
> @@ -558,6 +568,7 @@ void ApplicationWindow::newCaptureWin()
>  	if (m_capture != NULL) {
>  		m_capture->stop();
>  		delete m_capture;
> +		m_capture = NULL;
>  	}
>  
>  	switch (m_renderMethod) {
> @@ -1135,7 +1146,7 @@ void ApplicationWindow::stopStreaming()
>  	if (!m_genTab->isSDR() && m_genTab->isRadio())
>  		return;
>  
> -	if (v4l_type_is_capture(g_type()))
> +	if (v4l_type_is_capture(g_type()) && m_capture != NULL)
>  		m_capture->stop();
>  
>  	m_snapshotAct->setDisabled(true);
> @@ -1561,8 +1572,10 @@ void ApplicationWindow::makeFullScreen(bool checked)
>  void ApplicationWindow::closeDevice()
>  {
>  	stopAudio();
> -	delete m_sigMapper;
> -	m_sigMapper = NULL;
> +	if(m_sigMapper != NULL) {
> +		m_sigMapper->deleteLater();
> +		m_sigMapper = NULL;
> +	}
>  	m_capStartAct->setEnabled(false);
>  	m_capStartAct->setChecked(false);
>  	m_capStepAct->setEnabled(false);
> @@ -1579,7 +1592,7 @@ void ApplicationWindow::closeDevice()
>  			m_outNotifier = NULL;
>  		}
>  		if (m_ctrlNotifier) {
> -			delete m_ctrlNotifier;
> +			m_ctrlNotifier->deleteLater();
>  			m_ctrlNotifier = NULL;
>  		}
>  		delete [] m_frameData;
> @@ -1740,6 +1753,7 @@ void ApplicationWindow::closeEvent(QCloseEvent *event)
>  {
>  	closeDevice();
>  	delete m_capture;
> +	m_capture = NULL;
>  	event->accept();
>  }
>  
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] qv4l2: Prevent high CPU usage on device disconnect
  2019-05-27 12:20 ` Hans Verkuil
@ 2019-05-28 11:00   ` Tasos Sahanidis
  0 siblings, 0 replies; 3+ messages in thread
From: Tasos Sahanidis @ 2019-05-28 11:00 UTC (permalink / raw)
  To: Hans Verkuil, linux-media

Signed-off-by: Tasos Sahanidis <tasos@tasossah.com>

On 2019-05-27 15:20, Hans Verkuil wrote:
> Hi Tasos,
> 
> On 5/9/19 3:32 PM, Tasos Sahanidis wrote:
>> On device disconnect, ApplicationWindow::ctrlEvent() gets called
>> continuously, causing high CPU usage.
>> Closing the device handles once ENODEV is returned fixes this.
> 
> I'm missing your Signed-of-by. Can you reply to this and add your SoB so I
> can merge this patch?
> 
> Regards,
> 
> 	Hans
> 
>> ---
>>  utils/qv4l2/alsa_stream.c |  2 +-
>>  utils/qv4l2/qv4l2.cpp     | 24 +++++++++++++++++++-----
>>  2 files changed, 20 insertions(+), 6 deletions(-)
>>
>> diff --git a/utils/qv4l2/alsa_stream.c b/utils/qv4l2/alsa_stream.c
>> index 05944822..2dca283b 100644
>> --- a/utils/qv4l2/alsa_stream.c
>> +++ b/utils/qv4l2/alsa_stream.c
>> @@ -433,7 +433,7 @@ static snd_pcm_sframes_t readbuf(snd_pcm_t *handle, char *buf, long len)
>>      snd_pcm_uframes_t frames;
>>      snd_pcm_htimestamp(handle, &frames, &timestamp);
>>      r = snd_pcm_readi(handle, buf, len);
>> -    if (r < 0 && r != -EAGAIN) {
>> +    if (r < 0 && !(r == -EAGAIN || r == -ENODEV)) {
>>  	r = snd_pcm_recover(handle, r, 0);
>>  	if (r < 0)
>>  	    fprintf(error_fp, "alsa: overrun recover error: %s\n", snd_strerror(r));
>> diff --git a/utils/qv4l2/qv4l2.cpp b/utils/qv4l2/qv4l2.cpp
>> index b52a3b60..9ac2c332 100644
>> --- a/utils/qv4l2/qv4l2.cpp
>> +++ b/utils/qv4l2/qv4l2.cpp
>> @@ -495,8 +495,9 @@ void ApplicationWindow::setAudioBufferSize()
>>  void ApplicationWindow::ctrlEvent()
>>  {
>>  	v4l2_event ev;
>> +	int event_ret = 0;
>>  
>> -	while (dqevent(ev) == 0) {
>> +	while ((event_ret = dqevent(ev)) == 0) {
>>  		if (ev.type == V4L2_EVENT_SOURCE_CHANGE) {
>>  			m_genTab->sourceChange(ev);
>>  			continue;
>> @@ -551,6 +552,15 @@ void ApplicationWindow::ctrlEvent()
>>  			setString(ev.id, c.string);
>>  		free(c.string);
>>  	}
>> +
>> +	if (event_ret && errno == ENODEV) {
>> +		closeDevice();
>> +		if (m_capture != NULL) {
>> +			m_capture->stop();
>> +			delete m_capture;
>> +			m_capture = NULL;
>> +		}
>> +	}
>>  }
>>  
>>  void ApplicationWindow::newCaptureWin()
>> @@ -558,6 +568,7 @@ void ApplicationWindow::newCaptureWin()
>>  	if (m_capture != NULL) {
>>  		m_capture->stop();
>>  		delete m_capture;
>> +		m_capture = NULL;
>>  	}
>>  
>>  	switch (m_renderMethod) {
>> @@ -1135,7 +1146,7 @@ void ApplicationWindow::stopStreaming()
>>  	if (!m_genTab->isSDR() && m_genTab->isRadio())
>>  		return;
>>  
>> -	if (v4l_type_is_capture(g_type()))
>> +	if (v4l_type_is_capture(g_type()) && m_capture != NULL)
>>  		m_capture->stop();
>>  
>>  	m_snapshotAct->setDisabled(true);
>> @@ -1561,8 +1572,10 @@ void ApplicationWindow::makeFullScreen(bool checked)
>>  void ApplicationWindow::closeDevice()
>>  {
>>  	stopAudio();
>> -	delete m_sigMapper;
>> -	m_sigMapper = NULL;
>> +	if(m_sigMapper != NULL) {
>> +		m_sigMapper->deleteLater();
>> +		m_sigMapper = NULL;
>> +	}
>>  	m_capStartAct->setEnabled(false);
>>  	m_capStartAct->setChecked(false);
>>  	m_capStepAct->setEnabled(false);
>> @@ -1579,7 +1592,7 @@ void ApplicationWindow::closeDevice()
>>  			m_outNotifier = NULL;
>>  		}
>>  		if (m_ctrlNotifier) {
>> -			delete m_ctrlNotifier;
>> +			m_ctrlNotifier->deleteLater();
>>  			m_ctrlNotifier = NULL;
>>  		}
>>  		delete [] m_frameData;
>> @@ -1740,6 +1753,7 @@ void ApplicationWindow::closeEvent(QCloseEvent *event)
>>  {
>>  	closeDevice();
>>  	delete m_capture;
>> +	m_capture = NULL;
>>  	event->accept();
>>  }
>>  
>>
> 

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-05-28 11:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-05-09 13:32 [PATCH] qv4l2: Prevent high CPU usage on device disconnect Tasos Sahanidis
2019-05-27 12:20 ` Hans Verkuil
2019-05-28 11:00   ` Tasos Sahanidis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).