Reading sensors and inputs from CP library gets slower over time

Play with it! Please tell us which board you're using.
For CircuitPython issues, ask in the Adafruit CircuitPython forum.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
bink
 
Posts: 1
Joined: Mon Dec 27, 2010 6:30 pm

Reading sensors and inputs from CP library gets slower over time

Post by bink »

I have a project that monitors a Circuit Playground Bluefruit for accelerometer and button press events. Based on what it sees, it may flash the LEDs or emit some beeps. I have noticed that, after a few days of uptime, it starts to get less responsive. It can take a noticeable amount of time for an accelerometer event to be acted on, and even when no state has changed, other time-sensitive operations (like the timing of flashing the LEDs or manually timed beeps) are clearly starved for runtime.

I added timing measurements around some of the operations and logged them for a while. The data was clear: over time, some calls to cp.acceleration and cp.button_a/cp.button_b are taking a very long time. Attached is a graph showing the time it takes to read the buttons. The X axis is time in days, the Y axis is number of operations that took at least as long as that bucket, and each line represents a time bucket (in milliseconds). The tall blue line shows that some measurements are still returning in <1ms even after weeks of uptime. The latest rising orange-red line, OTOH, shows that tens or hundreds of thousands of measurements took at least half a second.

I did a more targeted test with the minimum code needed to time reading the buttons. Right away, the max time to read them climbs to 8ms. 24 hours later, that's 32ms. By 36 hours, it's 64ms. Restarting the code or pressing the reset button does not change the behavior, but power cycling the device does.

Attached is the entirety of my test program. I threw in some delay around reading the buttons, in case measuring the buttons over and over in a tight loop was responsible for the slowness, but this did not turn out to be the case. The calls just get slower the longer the device is powered on. The fact that some calls are fast and some are slow for both the accelerometer and the buttons makes me think that it's something _else_ that gets slower over time, but these are all I can see.

CircuitPython 7.3.3 and circuit_playground_base version 5.2.3.

Anyone seen similar behavior? Any patterns I should use or avoid to work around this? Thanks!
Attachments
test.py
(1.85 KiB) Downloaded 6 times
buttom_times_3.png
buttom_times_3.png (68.36 KiB) Viewed 121 times

User avatar
dastels
 
Posts: 15659
Joined: Tue Oct 20, 2015 3:22 pm

Re: Reading sensors and inputs from CP library gets slower over time

Post by dastels »

A few thoughts:

1) using the circuitplayground module to read a couple buttons is drastic overkill. Even also reading the accel. Try reading them more directly (e.g. using the digitalio module for the buttons)
2) using metaprogramming to call the button properties (i.e. getattr) has added overhead
3) you could try doing gc.collect() now & then. You're not getting memory allocation errors but the heap is likely getting fragmented leading to longer allocation times.

Dave

Locked
Please be positive and constructive with your questions and comments.

Return to “Circuit Playground Classic, Circuit Playground Express, Circuit Playground Bluefruit”