Reputation: 31
I am using bluez 5.37 backported to linux 2.6 due to project restrictions on a custom device. I have noticed gatttool causes a very high cpu usage on disconnection. This is very easy to replicate. Use gatttool in interactive mode to connect to any BLE device and then disconnect. CPU usage is fine till you give the disconnect command. Observe CPU usage, gatttool will cross 70-80%. I can also replicate this on Ubuntu LTS. Has anyone had a go at fixing this? Thank you.
Upvotes: 2
Views: 972
Reputation: 31
I guess I found what is wrong. Gatttool uses glib main event loop and so io channels are attached as sources to the default main context (read glib docs for more clarification). As an example in 'bluez/attrib/interactive.c' in function cmd_connect 'g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL)' is done. This function will return a gsource which has to be removed from watch once io channel is lost or deleted. In absence of this, glib will keep waiting on this io channel (fd) and cause EINVAL as return code. (My knowledge is patchy but I guess that's what happens). So when disconnecting the device this watch needs to be removed.
I do it in following way - define a new global -
guint gsrc;
Change the line in cmd_connect to
gsrc = g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL);
After disconnection, in disconnect_io function before return - g_source_remove(gsrc);
This fixes gatttool cpu usage bug in interactive mode.
Upvotes: 1