Skip to content

Improve file io time by using low level os functions #155

Closed

Description

@dlech

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]

Activity

  1. rhempel commented on Apr 12, 2016

    @rhempel
    Member

    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.

  2. rhempel commented on Apr 13, 2016

    @rhempel
    Member

    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.

    10,000 blank loops takes 0.049056
    1,000 cached attribute reads takes 2.016907
    1,000 direct attribute reads takes 0.53761
    

    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.

  3. rhempel commented on Apr 14, 2016

    @rhempel
    Member

    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:

    10,000 blank loops takes 0.054289
    1,000 cached attribute reads takes 2.01207
    1,000 direct attribute reads takes 0.645048
    1,000 cached attribute writes takes 1.771911
    1,000 direct attribute writes takes 0.708666
    

    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.

  4. dwalton76 commented on May 5, 2016

    @dwalton76
    Collaborator

    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.

  5. dwalton76 commented on Sep 14, 2016

    @dwalton76
    Collaborator

    @rhempel did your changes make it in?

  6. dlech commented on Sep 14, 2016

    @dlech
    MemberAuthor

    Looks like it: #162

  7. dwalton76 commented on Sep 14, 2016

    @dwalton76
    Collaborator

    cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions