atlanticwave-sdx / sdx-controller

Central Controller for AtlanticWave SDX.
https://www.atlanticwave-sdx.net
MIT License
2 stars 3 forks source link

Avoid a busy wait #175

Closed sajith closed 1 year ago

sajith commented 1 year ago

Issue is #33. These lines seem to cause high CPU usage:

https://github.com/atlanticwave-sdx/sdx-controller/blob/82dca910e94d337c8877b9b695305bcdb1096d9d/swagger_server/__main__.py#L124-L125

coveralls commented 1 year ago

Coverage Status

coverage: 45.352% (+0.06%) from 45.288% when pulling fae1104c330e9cd04bdb234edd8780c01e0fa4fd on 33.cpu-usage into 82dca910e94d337c8877b9b695305bcdb1096d9d on main.

sajith commented 1 year ago

Of the below lines, I think lines 124-125 are not necessary at all, because the subsequent Queue.get() is a blocking call -- it will block until there's an item in the queue (there's also an alternative Queue.get_nowait()).

https://github.com/atlanticwave-sdx/sdx-controller/blob/82dca910e94d337c8877b9b695305bcdb1096d9d/swagger_server/__main__.py#L124-L127

When running sdx-controller on a laptop, my CPU is struggles, the laptop starts to heat up, and the fan spins noisily. I think the busy loop is the problem. I did some profiling with scalene, like so:

$ pip install scalene
$ python3 -m scalene --cpu-only -m swagger_server
# Wait a while and then stop the processes
^C 

Scalene reports that most of the Python time is spent in that busy loop.

image

It gets better without that loop:

image

sajith commented 1 year ago

Did some tests on the testbed (publishing topologies, requesting a connection, etc). It worked fine. This doesn't seem to break anything. :-)