You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve file io time by using low level os functions #155
This came up in #130. Currently, reading sysfs attributes takes too long, ~1.25ms in python 2 and ~3.65ms in python 3.
Python 2.7.9 (default, Mar 1 2015, 13:52:09)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.repeat('f.seek(0); f.read()', 'f = open("/sys/class/tacho-motor/motor0/position", "r+")', number=1000)
[1.2495520114898682, 1.2299458980560303, 1.206686019897461]
Python 3.4.2 (default, Oct 8 2014, 14:47:30)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.repeat('f.seek(0); f.read()', 'f = open("/sys/class/tacho-motor/motor0/position", "r+")', number=1000)
[3.638115724999807, 3.6730630889996974, 3.7583993859989278]
This can be significantly improved in python3 by using os.pread() instead.
Python 3.4.2 (default, Oct 8 2014, 14:47:30)
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.repeat('os.pread(fd, 4096, 0).decode().strip()', 'import os; fd = os.open("/sys/class/tacho-motor/motor0/position", os.O_RDWR)', number=1000)
[0.8667515870001807, 0.8680804369996622, 0.86280666499988]
python2 on the other hand uses low level system calls already and does not have popen. Performance is actually slightly worse this way.
Python 2.7.9 (default, Mar 1 2015, 13:52:09)
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import timeit
>>> timeit.repeat('os.lseek(fd, 0, os.SEEK_SET); os.read(fd, 4096).decode().strip()', 'import os; fd = os.open("/sys/class/tacho-motor/motor0/position", os.O_RDWR)', number=1000)
[1.4058640003204346, 1.4420111179351807, 1.3634161949157715]
I did a simple test using a loop directly on a file descriptor, with the following results for seek/read:
10,000 blank loops takes 0.045836
1,000 attribute reads takes 0.385303
1,000 attribute reads with decode takes 0.617118
1,000 attribute reads with decode and strip takes 0.655846
The loop overhead for 1,000 iterations using xrange is about 4 msec. Even with the decode and strip applied, the read time should be about 0.655 msec per attribute read - so the overhead must be somewhere in the file attribute cache lookup. I am going to investigate further tomorrow - maybe using device specific attributes (that's why we have a spec) will help.
Well, good news - the problem is in the file cache. I did a bit more experimentation using file handles opened and cached on demand - but instead of using a device level cache where each attribute is looked up by name, we have device level variables that hold the file handles - no lookup necessary.
Here the empty loop time is irrelevant. Reading an attribute through the existing cache mechanism takes about 2 msec, but reading from a file handle stored in an instance variable takes only 0.5 msec - a significant improvement!
I would still need to put some code around checking to see if the file was already opened - I'll check on the overhead of that later on.
Well, I'm pretty convinced that eliminating the lookups in the file cache is the right thing to do. I now have the Python binding opening attribute files on demand and storing the file handle in an object specific instance variable - the speedup is approximately threefold:
I'll be updating the Python binding generator this weekend and including the changes to attributes for the current kernel level drivers.
The best news? Except for the change in the stop_command attribute to stop_action, no script level changes :-)
This has been a great example of the value of doing real benchmarks and looking for improvements when you have determined performance copuld be better.
It will be interesting to see if BALANC3R can become even more stable (it is already pretty amazing) by running in a tighter loop since the motor reads/writes will be so much faster.
Activity
rhempel commentedon Apr 12, 2016
I did a simple test using a loop directly on a file descriptor, with the following results for seek/read:
The loop overhead for 1,000 iterations using
xrangeis about 4 msec. Even with the decode and strip applied, the read time should be about 0.655 msec per attribute read - so the overhead must be somewhere in the file attribute cache lookup. I am going to investigate further tomorrow - maybe using device specific attributes (that's why we have a spec) will help.rhempel commentedon Apr 13, 2016
Well, good news - the problem is in the file cache. I did a bit more experimentation using file handles opened and cached on demand - but instead of using a device level cache where each attribute is looked up by name, we have device level variables that hold the file handles - no lookup necessary.
Here the empty loop time is irrelevant. Reading an attribute through the existing cache mechanism takes about 2 msec, but reading from a file handle stored in an instance variable takes only 0.5 msec - a significant improvement!
I would still need to put some code around checking to see if the file was already opened - I'll check on the overhead of that later on.
rhempel commentedon Apr 14, 2016
Well, I'm pretty convinced that eliminating the lookups in the file cache is the right thing to do. I now have the Python binding opening attribute files on demand and storing the file handle in an object specific instance variable - the speedup is approximately threefold:
I'll be updating the Python binding generator this weekend and including the changes to attributes for the current kernel level drivers.
The best news? Except for the change in the
stop_commandattribute tostop_action, no script level changes :-)This has been a great example of the value of doing real benchmarks and looking for improvements when you have determined performance copuld be better.
dwalton76 commentedon May 5, 2016
It will be interesting to see if BALANC3R can become even more stable (it is already pretty amazing) by running in a tighter loop since the motor reads/writes will be so much faster.
dwalton76 commentedon Sep 14, 2016
@rhempel did your changes make it in?
dlech commentedon Sep 14, 2016
Looks like it: #162
dwalton76 commentedon Sep 14, 2016
cool