Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call syscall readahead in Python?

How can I call the readahead syscall in Python 3?

readahead() initiates readahead on a file so that subsequent reads from that file will be satisfied from the cache, and not block on disk I/O

like image 456
ArekBulski Avatar asked Jul 18 '16 10:07

ArekBulski


1 Answers

Use ctypes and pull in the system call from libc:

    import ctypes, os

    # load ourselves, we already have libc
    libc = ctypes.CDLL(None, use_errno=True)

    # XXX - YMMV, ctypes doesn't have c_off_t much less c_off64_t.
    # Assume it's c_longlong, but don't count on that.
    off64_t = ctypes.c_longlong

    def readahead(fobj, offset, count):
        fno = fobj if isinstance(fobj, int) else fobj.fileno()

        code = libc.readahead(
            ctypes.c_int(fno),
            off64_t(offset),
            ctypes.c_size_t(count)
        )
        if code != 0:
            errno = ctypes.get_errno()
            raise OSError(errno, os.strerror(errno))
like image 163
dhke Avatar answered Nov 14 '22 23:11

dhke