Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gstreamer causing 100% cpu #7515

Closed
supfors opened this issue May 9, 2017 · 20 comments · Fixed by #17568
Closed

Gstreamer causing 100% cpu #7515

supfors opened this issue May 9, 2017 · 20 comments · Fixed by #17568

Comments

@supfors
Copy link

supfors commented May 9, 2017

Home Assistant release (hass --version):
0.44.1

Python release (python3 --version):
Python 3.5.2

Component/platform:
Gstreamer

Ubuntu 16.04.2 LTS
4.4.0-62-generic

Description of problem:
When enabling gstreamer, cpu load jumps to 100%. I've noticed hass spawned 2 extra processes in addition to its leader process:

homeassistant@hass:~$  top
PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND                                                                            
2655 homeass+  20   0 1292424  60808   6380 R 100.0  2.8  11:46.58 hass
homeassistant@hass:~$ ps aux |grep hass
homeass+   2407  0.3  3.2 1292664 71452 ?       Ssl  20:49   0:01 /opt/hass/bin/python3 /opt/hass/bin/hass --config /opt/hass
homeass+   2492  0.0  2.7 1108704 60700 ?       Sl   20:49   0:00 /opt/hass/bin/python3 /opt/hass/bin/hass --config /opt/hass
homeass+   2500 99.9  2.7 1292412 60756 ?       R    20:49   5:50 /opt/hass/bin/python3 /opt/hass/bin/hass --config /opt/hass

I've tested multiple pipelines (and even without pipeline) but this does not seem to cause the issue.

Daemon running on the player/speaker:

supfors@speaker:~$ gst-launch-1.0 udpsrc port=5555 caps="application/x-rtp" ! queue ! rtppcmudepay ! mulawdec ! alsasink

The streaming of audio (tts.google_say f.e.) works fine.

I do not see this kind of behaviour when testing from command line using gst-launch-1.0:
homeassistant@hass:~$ gst-launch-1.0 audiotestsrc freq=1000 ! mulawenc ! rtppcmupay ! udpsink host=speaker port=5555

Expected:
Near idle cpu-load when using gstreamer.

Problem-relevant configuration.yaml entries and steps to reproduce:

media_player:
  - platform: gstreamer
    pipeline: "mulawenc ! rtppcmupay ! udpsink host=speaker port=5555"

Traceback (if applicable):
This is all I see in home-assistant.log:

homeassistant@hass:~$ cat log |grep gstr
2017-05-09 21:06:02 INFO (MainThread) [homeassistant.loader] Loaded media_player.gstreamer from homeassistant.components.media_player.gstreamer
2017-05-09 21:06:02 INFO (MainThread) [homeassistant.components.media_player] Setting up media_player.gstreamer
2017-05-09 21:06:02 INFO (Thread-7) [gsp] starting gstreamer
2017-05-09 21:06:02 INFO (MainThread) [homeassistant.core] Bus:Handling <Event state_changed[L]: entity_id=media_player.gstreamer, old_state=None, new_state=<state media_player.gstreamer=idle; friendly_name=gstreamer, supported_features=16933 @ 2017-05-09T21:06:02.991053+02:00>>

Additional info:

homeassistant@hass:~$ pip3 show gi |grep Vers
Metadata-Version: 1.0
Version: 1.2
(hass) homeassistant@hass:~$ pip3 list
aiohttp (2.0.7)
appdirs (1.4.3)
async-timeout (1.2.0)
chardet (3.0.2)
homeassistant (0.44.1)
Jinja2 (2.9.6)
MarkupSafe (1.0)
multidict (2.1.4)
packaging (16.8)
pip (9.0.1)
pyparsing (2.2.0)
pytz (2017.2)
PyYAML (3.12)
requests (2.14.0)
setuptools (35.0.2)
six (1.10.0)
typing (3.6.1)
voluptuous (0.10.5)
wheel (0.29.0)
yarl (0.10.2)
homeassistant@hass:~$ dpkg -l |grep gst
ii  gir1.2-gst-plugins-base-1.0          1.8.3-1ubuntu0.2                         amd64        GObject introspection data for the GStreamer Plugins Base library
ii  gir1.2-gstreamer-1.0                 1.8.3-1~ubuntu0.1                        amd64        GObject introspection data for the GStreamer library
ii  gstreamer1.0-alsa:amd64              1.8.3-1ubuntu0.2                         amd64        GStreamer plugin for ALSA
ii  gstreamer1.0-plugins-base:amd64      1.8.3-1ubuntu0.2                         amd64        GStreamer plugins from the "base" set
ii  gstreamer1.0-plugins-good:amd64      1.8.3-1ubuntu0.4                         amd64        GStreamer plugins from the "good" set
ii  gstreamer1.0-plugins-ugly:amd64      1.8.3-1ubuntu0.1                         amd64        GStreamer plugins from the "ugly" set
ii  gstreamer1.0-plugins-ugly-amr:amd64  1.8.3-1ubuntu0.1                         amd64        GStreamer plugins from the "ugly" set
ii  gstreamer1.0-tools                   1.8.3-1~ubuntu0.1                        amd64        Tools for use with GStreamer
ii  gstreamer1.0-x:amd64                 1.8.3-1ubuntu0.2                         amd64        GStreamer plugins for X11 and Pango
ii  libgstreamer-plugins-base1.0-0:amd64 1.8.3-1ubuntu0.2                         amd64        GStreamer libraries from the "base" set
ii  libgstreamer-plugins-good1.0-0:amd64 1.8.3-1ubuntu0.4                         amd64        GStreamer development files for libraries from the "good" set
ii  libgstreamer1.0-0:amd64              1.8.3-1~ubuntu0.1                        amd64        Core GStreamer libraries and elements
ii  python3-gst-1.0
@happyleavesaoc
Copy link
Contributor

Yeah, that's a known issue at this point, for what it's worth. You could downgrade gstreamer to the earlier version if that one worked for you.

@supfors
Copy link
Author

supfors commented May 10, 2017

Thanks @happyleavesaoc Tried version 1.0.0. No joy.

@maxx
Copy link

maxx commented Jun 6, 2017

I tried reverting the gstreamer-player pip package to 1.0 as well as backing out a738767

Unfortunately both still produce 100% CPU.

I need to find another way to get TTS to pulseaudio.. (for mixing).

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@balloobbot
Copy link

This issue will be auto-closed because there hasn't been any activity for a few months. Feel free to open a new one if you still experience this problem 👍

@maxx
Copy link

maxx commented Sep 13, 2017

This is actually still an issue exactly as described above. As @happyleavesaoc mentions above, it apparently remains a known issue.

Could someone please re-open so we can keep this in-context rather than making a new issue?

@cgevans
Copy link

cgevans commented Dec 2, 2017

This is still an issue. Are we really supposed to create a new issue, despite the discussion here, just because a bot auto-closed a known issue?

@skogsmaskin
Copy link

skogsmaskin commented Jan 9, 2018

I see the component is based on gstreamer-player. I don't know Python that much, but there must certainly be better supported player modules for python/gstreamer around (one fork, zero stars)? I am also experiencing this problem. 100% on one core. I'm using Arch Linux btw.

@tschmidty69
Copy link
Contributor

I have the same issue so had to disable it. Unfortunately it is the only way I have tts working to test some other stuff. FWIW, happyleaves created that gstream-player and also the home assistant component. There don't seem to be any good alternatives so...

@pzabiel
Copy link

pzabiel commented Jan 20, 2018

It is still actually with Home Assistant 0.61.1 and RPI3 (with up-to-date Raspbian).

@happyleavesaoc
Copy link
Contributor

I've made some attempts to solve this problem, but I didn't come to a good solution, because gstreamer needs a second event loop, incompatible with the home assistant main loop. I encourage someone to either find a better gstreamer-based player to integrate (I could not), or submit a PR to gstreamer-player which I will gladly merge.

@zhujisheng
Copy link
Contributor

the gstreamer-player has a while true loop, with nonblock in it. This causes the 100% CPU.
The simplest is to add time.sleep(0.1) in the loop.
.homeassistant/deps/lib/python3.4/site-packages/gsp/__init__.py

    def run(self):
        """Run the process.
        Iterate the GLib main loop and process the task queue.
        """
        loop = GLib.MainLoop()
        context = loop.get_context()
        while True:
            time.sleep(0.1)
            if context.pending():
                context.iteration()
                self._manager[ATTR_POSITION] = self._position()
            try:
                method, args = self._task_queue.get(False)
                getattr(self, method)(**args)
            except queue.Empty:
                pass
            if self.state != STATE_IDLE:
                continue
            try:
                uri = self._media_queue.get(False)
                self.media(uri)
            except queue.Empty:
                pass

@skalavala
Copy link
Contributor

@zhujisheng that's an interesting fix. Have you checked the CPU usage before and after? Do you notice any difference? Unfortunately I can't test it as I just upgraded to Python3.6, and Gstreamer wouldn't setup.

2018-03-16 09:33:33 ERROR (MainThread) [homeassistant.components.media_player] Error while setting up platform gstreamer
Traceback (most recent call last):
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/helpers/entity_platform.py", line 82, in async_setup
    SLOW_SETUP_MAX_WAIT, loop=hass.loop)
  File "/usr/lib/python3.6/asyncio/tasks.py", line 358, in wait_for
    return fut.result()
  File "/usr/lib/python3.6/asyncio/futures.py", line 245, in result
    raise self._exception
  File "/usr/lib/python3.6/concurrent/futures/thread.py", line 56, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/srv/homeassistant/lib/python3.6/site-packages/homeassistant/components/media_player/gstreamer.py", line 40, in setup_platform
    from gsp import GstreamerPlayer
  File "/srv/homeassistant/lib/python3.6/site-packages/gsp/__init__.py", line 11, in <module>
    import gi  # pylint: disable=import-error
  File "/srv/homeassistant/lib/python3.6/site-packages/gi/__init__.py", line 42, in <module>
    from . import _gi
ImportError: cannot import name '_gi'

Anyone run into this problem?

CC: @happyleavesaoc

@zhujisheng
Copy link
Contributor

I tested it in python3.5, it's ok.
There's many Pypis can't work properly under pytyon3.6, so I haven't use it.

@skalavala
Copy link
Contributor

skalavala commented Apr 30, 2018

@zhujisheng I never acknowledged since your last response... My apologies!

I tested it on my end, and the CPU is no longer utilized at it's peak. Thank you for the simple fix!
I am now having one of those "Why didn't I think of that?" moment. 😄

It went down from an average 100% utilization to an average 2% utilization... What a big difference it makes!

@balloobbot
Copy link

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates.

Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment 👍

@partofthething
Copy link
Contributor

That fixed it for me too in Python 3.6. We should file a bug in the dependency. Ok I made one. happyleavesaoc/gstreamer-player#4. This can be tagged as problem in dependency.

@partofthething
Copy link
Contributor

Issue in dependency is now closed. This can be closed too.

@MartinHjelmare
Copy link
Member

@partofthething maybe we should wait until the fix is released and the version of the library is bumped in home assistant? Is that in progress?

@partofthething
Copy link
Contributor

@MartinHjelmare excellent point, yes we should. I'll see if i can get some info on the next release.

@ghost ghost added the in progress label Oct 18, 2018
@ghost ghost removed the in progress label Oct 19, 2018
@home-assistant home-assistant locked and limited conversation to collaborators Feb 5, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.