I've been hearing lots of good things about lgpio lately, particularly now it is the default backend for gpiozero, so I thought would be worthwhile to take it for a quick spin.
First thing I tried was the gpiozero button example, but that didn't behave quite as I expected - it is using a lot more CPU than I anticipated:
$ python docs/examples/button_lgpio.py &
[1] 805
$ top -p 805 -H
top - 16:37:27 up 6 min, 1 user, load average: 0.16, 0.21, 0.11
Threads: 5 total, 0 running, 5 sleeping, 0 stopped, 0 zombie
%Cpu(s): 0.2 us, 1.1 sy, 0.0 ni, 98.7 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
MiB Mem : 1845.9 total, 1614.3 free, 148.6 used, 138.5 buff/cache
MiB Swap: 100.0 total, 100.0 free, 0.0 used. 1697.3 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
805 pi 20 0 257248 18304 6016 S 5.3 1.0 0:03.87 python
808 pi 20 0 257248 18304 6016 S 5.3 1.0 0:03.46 python
806 pi 20 0 257248 18304 6016 S 0.0 1.0 0:00.00 python
807 pi 20 0 257248 18304 6016 S 0.0 1.0 0:00.07 python
810 pi 20 0 257248 18304 6016 S 0.0 1.0 0:00.03 python
More than 5% CPU on a Pi4 to monitor one pin seems a tad excessive.
That example is based on the gpiozero _button4.py example, modified to enforce usage of lgpio and to request GPIO22, as my basic test setup is a Pi with a jumper across GPIO22 and GPIO23. The change isn't terribly relevant, so I will omit it for brevity - the unadulterated _button4.py example behaves the same, just on a different pin - as does the lgpiomonitor.py example.
Checking that the line is requested by lgpio as expected (using libgpiod v2 tools):
I've been hearing lots of good things about lgpio lately, particularly now it is the default backend for gpiozero, so I thought would be worthwhile to take it for a quick spin.
First thing I tried was the gpiozero button example, but that didn't behave quite as I expected - it is using a lot more CPU than I anticipated:
More than 5% CPU on a Pi4 to monitor one pin seems a tad excessive.
That example is based on the gpiozero _button4.py example, modified to enforce usage of lgpio and to request
GPIO22
, as my basic test setup is a Pi with a jumper acrossGPIO22
andGPIO23
. The change isn't terribly relevant, so I will omit it for brevity - the unadulterated _button4.py example behaves the same, just on a different pin - as does the lgpio monitor.py example.Checking that the line is requested by lgpio as expected (using libgpiod v2 tools):
so it is definitely using lgpio.
It does see edges, so that is a plus:
I tried the same thing on a Pi0 (sadly an old Zero W, not a Zero 2), and I see similar:
So now over 10% of the CPU just to monitor one line.
For comparison, I tried the gpiod equivalent example, _watch_linevalue.py, from the libgpiod source tree, and got this:
which is more like what I expected - as the monitoring is interrupt driven there is no reason for it to be burning CPU.
Confirming gpiod has requested the line:
And confirming it is working:
All cases above are running on Raspberry Pi OS Bookworm.
I also had a quick look at Bullseye on the Pi0, which uses RPi.GPIO as the default backend, and I get:
That also does not require much CPU.
So why is lgpio using so much CPU?
Cheers, Kent.