linuxmint / blueberry

A Bluetooth configuration tool
GNU General Public License v3.0
182 stars 53 forks source link

Blueberry may be continually scanning for devices causing high CPU usage #55

Closed RyanNerd closed 5 years ago

RyanNerd commented 6 years ago

This is a copy of the issue from linuxmint/cinnamon as this may be a Blueberry issue and not a Cinnamon issue per se.

 * Cinnamon version (3.6.7)
 * Distribution - (Mint 18.3 Debian)
 * Graphics hardware *and* driver used: Nvidia-390 390.25-0ubuntu0-gpu16.04.1 NVIDIA binary driver version 390.25
 * 64 bit Intel Core i7 - HP Envy laptop

Issue Anytime the Bluetooth applet is used cinnamon CPU usage skyrockets.

Steps to reproduce

  1. In terminal run top
  2. Note that the CPU % for cinnamon is 10% or less.
  3. Click on the Bluetooth applet and make sure the Devices tab is selected.
  4. Watch the CPU % for cinnamon go up around 80%+.
  5. Using the Bluetooth applet turn Bluetooth off (or if you click on the Settings tab).
  6. Note that CPU % for cinnamon drops down to 10% or less.
  7. Using the Bluetooth applet turn Bluetooth back on (or click back on the Devices tab).
  8. Note that CPU % for cinnamon skyrockets back to around 80%+

image

Expected behaviour

CPU % usage for cinnamon shouldn't be impacted by using Bluetooth

Other information This is a recent and clean install of Linux Mint 18.3 to my new SSD. My previous install didn't have this issue but I was using Blueman and never used the "blueberry" applet before. I installed Blueman and using this instead of the applet the CPU usage problem does not occur.

IndigoJay commented 6 years ago

Perhaps a solution would be a radio-button to turn-on or turn-off device scanning.

python2 /usr/lib/blueberry/blueberry.pl

As long as "Devices" is "spinning" my CPU uses ~6% - ~9%.

Closing the Blueberry window immediately stops this CPU usage.

airbusa320 commented 6 years ago

I can reproduce this issue in Mint 19. On further inspection, I noticed it could be a graphic interface issue. When I scroll down the device list the CPU usage remains high until the spinning "scanning for devices" icon disappears, as soon as the icon disappears from the list box the CPU usage goes down to a normal level. Scrolling up again the CPU usage skyrockets again.

I think it could be an issue calculating the rotation of the image.

netikras commented 5 years ago

yepp, Mint 19 still has this problem. I wouldn't notice it if it wasn't lagging my dell xps with i7 so much lately. bluebery.py consumes >20% Xorg's time as well as 12% CPU% on its own. The lag was VERY annoying. I'm mentioning this because 40% CPU should not cause this much trouble.

FrogMcRibbit commented 5 years ago

I can also confirm this in 2019 on my side, and I use a completely different distro: Manjaro Cinnamon.

I have a way higher CPU usage while using Blueberry, see this screenshot. screenshot from 2019-03-08 12-28-06

When using Blueman (I replaced Blueberry for Blueman because of a different issue), the CPU is not used that much. screenshot from 2019-03-08 12-48-08

Laptop Model: Lenovo Ideapad 310 OS: Manjaro (Testing branch) DE: Cinnamon 4.0.9 (package: cinnamon 4.0.9-1.3) Kernel: Linux 4.19.26 (package: linux 4.19.26-1) Bluetooth-related packages:

Smartphone Model: LG G7 ThinQ OS: Android 8.0.0 (Security patch level: Feb 1 2019)

RyanNerd commented 5 years ago

The problem seems to be that when you activate Bluetooth in Blueberry it immediately starts searching for Bluetooth devices all the time -- There's not a toggle switch for: Search for Bluetooth devices: Yes/No (as suggested by @IndigoJay). This constant searching for devices can in some instances drive up the CPU usage even to 100%. If I was more skilled with Python I'd put in a PR to fix this. For now if you are having trouble the suggestion is to uninstall Blueberry and use Blueman instead.

As a side note. It appears that Blueberry is using a port from the Blueman project: https://github.com/linuxmint/blueberry/blob/master/usr/lib/blueberry/blueberry-obex-agent.py I couldn't find a feature to leave Bluetooth on and switch scanning off. Hopefully someone with some Python-Script-Fu skills can look at this.

airbusa320 commented 5 years ago

On further inspection, I noticed it could be a graphic interface issue. When I scroll down the device list the CPU usage remains high until the spinning "scanning for devices" icon disappears, as soon as the icon disappears from the list box the CPU usage goes down to a normal level. Scrolling up again the CPU usage skyrockets again.

I have found a workaround: in blueberry.py:update_status I've added spinner = vbox.get_children()[1].get_children()[0].get_children()[1] if spinner.props.active: spinner.stop() to stop the spinner, doing so the process doesn't waste CPU cycles any more

RyanNerd commented 5 years ago

@airbusa320 Have you created a PR for this?

airbusa320 commented 5 years ago

@RyanNerd I have, but I don't expect it to be merged, it's a dirty workaround and turning off the spinner deprives the user of feedback from the app. The root of the problem is a bug in GTK

IndigoJay commented 5 years ago

Looked at the Python code for /usr/lib/blueberry/blueberry.py and /BlueberrySettingsWidgets.py and did not discover how the 'spinner' is implemented. And didn't see anything interesting in the Cinnamon Debugger\Melange\Looking Glass logs. My suspicion is in line with @airbusa320 's, that issue is caused by a perpetual rotation transformation calculation.

Could we simply show a spinning .gif ?

Or implement a GTK+3 spinner https://developer.gnome.org/gtk3/stable/GtkSpinner.html

airbusa320 commented 5 years ago

@IndigoJay in blueberry the bottom part of the interface (highlighted in the picture below) is not defined at all and it's included "as is" from GnomeBluetooth (self.lib_widget) and the instances inside could be accessed by the get_children method. image

(And the spinner is already a gtkspinner)

IndigoJay commented 5 years ago

@airbusa320 , wow! you weren't kidding when you said the GtkSpinner's

The root of the problem is a bug in GTK

"GtkSpinner animation eats too much CPU" Issue opened in 2012 - https://gitlab.gnome.org/GNOME/gtk/issues/408

Plus related tickets on bugzilla.gnome.org https://bugzilla.redhat.com and https://bugs.launchpad.net/ - suggesting the spinner is queuing re-layouts at 60 fps.

GnomeBluetooth links - https://wiki.gnome.org/Projects/GnomeBluetooth and https://ftp.gnome.org/pub/gnome/sources/gnome-bluetooth/

RyanNerd commented 5 years ago

Steps to see this GTK bug in action that @IndigoJay @airbusa320 are talking about:

  1. Install python3.6
  2. Copy the code below into spinner.py
  3. Execute chmod +x spinner.py
  4. Start two terminal windows.
  5. In the first terminal window execute top in the second execute python3.6 spinner.py This will bring up a window with a GTK spinner you can start and stop.
  6. Start the spinner and watch the Cinnamon CPU usage climb. Stop the spinner and watch the CPU usage dive.

Note: This bug originally reported that using mint-x theme does not show the GTK spinner. This may be why not all Mint users are experiencing this bug (I guess different themes may implement the GTK spinner differently 😕).

spinner.py

from gi.repository import Gtk
import sys

class MyWindow(Gtk.ApplicationWindow):
    # a window
    def __init__(self, app):
        Gtk.Window.__init__(self, title="ToggleButton Example", application=app)
        self.set_default_size(300, 300)
        self.set_border_width(30)

        # a spinner animation
        self.spinner = Gtk.Spinner()
        # with extra horizontal space
        self.spinner.set_hexpand(True)
        # with extra vertical space
        self.spinner.set_vexpand(True)

        # a togglebutton
        button = Gtk.ToggleButton.new_with_label("Start/Stop")
        # connect the signal "toggled" emitted by the togglebutton
        # when its state is changed to the callback function toggled_cb
        button.connect("toggled", self.toggled_cb)

        # a grid to allocate the widgets
        grid = Gtk.Grid()
        grid.set_row_homogeneous(False);
        grid.set_row_spacing(15);
        grid.attach(self.spinner, 0, 0, 1, 1);
        grid.attach(button, 0, 1, 1, 1);

        # add the grid to the window
        self.add(grid)

    # callback function for the signal "toggled"
    def toggled_cb(self, button):
        # if the togglebutton is active, start the spinner
        if button.get_active():
            self.spinner.start()
        # else, stop it
        else:
            self.spinner.stop()

class MyApplication(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self)

    def do_activate(self):
        win = MyWindow(self)
        win.show_all()

    def do_startup(self):
        Gtk.Application.do_startup(self)

app = MyApplication()
exit_status = app.run(sys.argv)
sys.exit(exit_status)

This bug in action (Note: I am using peek/ffmpeg to create this GIF which is why top is showing high CPU usage for ffmpeg -- but even with this Cinnamon is showing high CPU usage when the GTK spinner is active): spinner

RyanNerd commented 5 years ago

@clefebvre Why did this get closed when it is still an issue?

airbusa320 commented 5 years ago

@RyanNerd because yesterday Clefebvre merged my PR. #85 We can't do much more since it's not a Blueberry's issue. The spinner instance is created by GnomeBluetooth so the issue is on their side... but using a spinner is not a bug! Maybe is a GTK3 bug, or maybe it depends on the resolution of the spinner asset... in any case, we disabled the spinner.