Closed Hermann-SW closed 2 years ago
WHOOPS! Yeah. That's a bit silly of me.
I added "usleep(100)" at end of main loop, resulted in 16.5% CPU. Replaced with "usleep(1000)", resulting in <4% CPU on one core. Finally used 10000, sleeping for 1/100th second should be OK for keyboard and mouse, resulting in <1% CPU utilization.
Just verified that it is OK by running cpuburn-a53 on same Pi400 and still getting fast keyboard as well as mouse reaction.
Could be enhanced by doing usleep() in a loop only if neither key nor mouse events happened in that loop.
pi@raspberrypi400:~/pi400kb/build $ top -b | grep pi400kb$
32191 root 20 0 2040 404 344 S 0.0 0.0 0:00.12 pi400kb
32191 root 20 0 2040 404 344 S 0.3 0.0 0:00.13 pi400kb
32191 root 20 0 2040 404 344 S 0.7 0.0 0:00.15 pi400kb
32191 root 20 0 2040 404 344 S 0.0 0.0 0:00.15 pi400kb
32191 root 20 0 2040 404 344 S 0.7 0.0 0:00.17 pi400kb
32191 root 20 0 2040 404 344 S 0.3 0.0 0:00.18 pi400kb
32191 root 20 0 2040 404 344 S 0.3 0.0 0:00.19 pi400kb
^C
pi@raspberrypi400:~/pi400kb/build $
I would recommend using poll
or epoll
(or even select
) instead of a usleep
since it is designed exactly to solve this problem... it will wait (efficiently) until there is activity on any of the file descriptors you are watching.
@gpetersson agreed- forgot about those! This grew from a very rough gist prototype into a more mature project quite suddenly 🤣
I made a PR that uses poll(), if only for testing. Seems to work fine for me, basically 0% CPU load.
Could be fixed by adding an usleep() in main processing loop: https://github.com/Gadgetoid/pi400kb/blob/feature/toggle-hook/pi400.c#L202-L248