Skip to content

[FEAT] - More intelligent dynamic prefetching #2455

Open

Description

@mxmlnkn

Is this feature for paramiko acting as a client or a server?

Client

What functionality does this feature request relate to?

SFTP

For client-side features, does this relate to a specific type of SSH server?

No response

If you're using paramiko as part of another tool, which tool/version?

No response

Desired behavior

Currently, the SFTPFile.prefetch API can be successfully used to increase bandwidth by a factor of ~25x. However, the prefetching is quite simple and seems to only have been intended for the simple use case of transferring a single file that is not too large. The problem with the prefetching is that it tries to prefetch the whole file at once, possibly generating millions of request messages before the replies can even be read.

I think the prefetching should be done smarter and only prefetch the next i+n chunks after chunk i has been accessed. This would even make prefetching work for more random accesses to the file object, e.g., seek to offset 10 MiB, read 1 MiB, seek to offset 1 GiB, read 10 MiB, etc. The prefetching scheme might even be extended to only prefetch i+n chunks where n is the number of previously sequentially accessed chunks before accessing chunk i, i.e., some kind of sequential read detection similar to those used in processors paired with a least-recently-used cache.

Anything else?

No response

Activity

  1. mxmlnkn commented on Sep 22, 2024

    @mxmlnkn
    Author

    My bad, at least the issue with millions of prefetches can be fixed by specifying max_concurrent_requests. If it is important, then the default should be different, though ... The other idea of making prefetching more dynamic and pairing it with a LRU cache is still valid though in order to increase more random-access-like patterns.

    And also note that max_concurrent_requests is very hard to manually configure because of the fixed value for io_sleep = 0.01. Therefore, if max_concurrent_requests is set too small it comes to a crawl again, making it even slower than the non-prefetched case!

    max_concurrent_requests=64
    
    Read  8.61696 MB in  5.58 s -> 1.54 MB/s
    Read 16.9728  MB in 13.24 s -> 1.28 MB/s
    Read 25.32864 MB in 21.06 s -> 1.20 MB/s
    Read 33.68448 MB in 28.92 s -> 1.16 MB/s
    Read 42.04032 MB in 37.03 s -> 1.14 MB/s
    Read 50.39616 MB in 43.06 s -> 1.17 MB/s
    Read 58.752   MB in 47.05 s -> 1.25 MB/s
    
    max_concurrent_requests=72
    
    Read   8.61696 MB in   0.15 s -> 58.67 MB/s
    Read  16.9728  MB in   0.32 s -> 53.24 MB/s
    Read  25.32864 MB in   0.48 s -> 52.35 MB/s
    Read  33.68448 MB in   0.65 s -> 51.64 MB/s
    Read  42.04032 MB in   0.79 s -> 53.34 MB/s
    Read  50.39616 MB in   0.96 s -> 52.72 MB/s
    Read  58.752   MB in   1.12 s -> 52.36 MB/s
    Read  67.36896 MB in   1.26 s -> 53.26 MB/s
    Read  75.7248  MB in   1.39 s -> 54.37 MB/s
    Read  84.08064 MB in   1.56 s -> 54.00 MB/s
    Read  92.43648 MB in   1.72 s -> 53.88 MB/s
    Read 100.79232 MB in   8.55 s -> 11.78 MB/s
    Read 109.14816 MB in  15.71 s ->  6.95 MB/s
    Read 117.504   MB in  23.60 s ->  4.98 MB/s
    Read 125.85984 MB in  31.66 s ->  3.98 MB/s
    Read 134.4768  MB in  39.65 s ->  3.39 MB/s
    Read 142.83264 MB in  47.37 s ->  3.02 MB/s
    Read 151.18848 MB in  55.31 s ->  2.73 MB/s
    Read 159.54432 MB in  61.94 s ->  2.58 MB/s
    Read 167.90016 MB in  65.95 s ->  2.55 MB/s
    Read 176.256   MB in  70.79 s ->  2.49 MB/s
    Read 184.61184 MB in  75.16 s ->  2.46 MB/s
    Read 192.96768 MB in  79.44 s ->  2.43 MB/s
    Read 201.58464 MB in  84.41 s ->  2.39 MB/s
    Read 209.94048 MB in  89.30 s ->  2.35 MB/s
    Read 218.29632 MB in  94.40 s ->  2.31 MB/s
    Read 226.65216 MB in  98.65 s ->  2.30 MB/s
    Read 235.008   MB in 102.45 s ->  2.29 MB/s
    Read 243.36384 MB in 106.16 s ->  2.29 MB/s
    
    max_concurrent_requests=128
    
    Read 134.4768  MB in  1.67 s -> 80.42 MB/s
    Read 268.69248 MB in  3.23 s -> 83.10 MB/s
    Read 402.90816 MB in  5.21 s -> 77.34 MB/s
    Read 537.12384 MB in  7.28 s -> 73.81 MB/s
    Read 671.33952 MB in  9.30 s -> 72.18 MB/s
    Read 805.5552  MB in 11.26 s -> 71.52 MB/s
    
    max_concurrent_requests=1024
    
    Read 134.4768  MB in  2.01 s -> 66.79 MB/s
    Read 268.69248 MB in  4.11 s -> 65.45 MB/s
    Read 402.90816 MB in  6.10 s -> 66.07 MB/s
    Read 537.12384 MB in  8.24 s -> 65.22 MB/s
    Read 671.33952 MB in 10.24 s -> 65.57 MB/s
    

    For max_concurrent_requests = 72, there is also the weird effect that it slows down after ~90 MB, probably because the first 72 requests are sent without the sleep, and only after that the sleep of 10 ms becomes important after all initially requested replies have been consumed?

    This basically means that I have to somehow guess the expected bandwidth in order to derive the most fitting max_concurrent_requests, and if I get it wron,g I'll have to deal with a 100x performance slowdown. There should be some dynamic prefetching instead that makes use of the knowledge of the most recently accessed chunk.

    Prefetching combined with seek also seems to be wholly untested territory. For example, I might want to try to enable prefetching and then skip the first n bytes of the file:

    import io
    import sys
    import time
    import paramiko
    
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.connect("127.0.0.1")
    
    sftp = client.open_sftp()
    
    remotepath = "wikidata-20220103-all.json.gz"
    file_size = sftp.stat(remotepath).st_size
    with sftp.open(remotepath, "rb", bufsize=255 * 1024) as reader:
        reader.prefetch(file_size, max_concurrent_requests=128)
        t0 = time.time()
        reader.seek(1 * 1024 * 1024 * 1024)
        t1 = time.time()
        print(f"Seek over the first 1 GiB took {t1-t0:.2f} s")
    
        size = 0
        threshold = 8 * 1024 * 1024
        t0 = time.time()
        while True:
            data = reader.read(255 * 1024)
            size += len(data)
    
            if size >= threshold:
                t1 = time.time()
                print(f"Read {size / 1e6} MB in {t1-t0:.2f} s -> {size/(t1-t0)/1e6:.2f} MB/s")
                threshold += 8 * 1024 * 1024
    
            if len(data) == 0:
                break

    Output:

    Seek over the first 2 GiB took 0.00 s
    Read   8.61696 MB in 88.03 s -> 0.10 MB/s
    Read  16.9728  MB in 89.21 s -> 0.19 MB/s
    Read  25.32864 MB in 90.38 s -> 0.28 MB/s
    Read  33.68448 MB in 91.56 s -> 0.37 MB/s
    Read  42.04032 MB in 92.75 s -> 0.45 MB/s
    Read  50.39616 MB in 93.94 s -> 0.54 MB/s
    [...]
    Read 629.2992  MB in 174.87 s -> 3.60 MB/s
    Read 637.65504 MB in 176.02 s -> 3.62 MB/s
    Read 646.01088 MB in 177.18 s -> 3.65 MB/s
    Read 654.36672 MB in 178.34 s -> 3.67 MB/s
    Read 662.72256 MB in 179.50 s -> 3.69 MB/s
    

    The seek is instant, but the next call to read has to wait for all the prefetching to finish. Worse, the memory usage, as queried with grep VmRSS /proc/2046960/status, increases to 2.7 GB and never decreases again because those chunks in the beginning are only deleted after they are read, which they don't. I guess the documentation actually warns about this, but I didn't understand the warning before I went about debugging my performance problems.

    The prefetched data is stored in a buffer until read via the read method. Once data has been read, it’s removed from the buffer. The data may be read in a random order (using seek); chunks of the buffer that haven’t been read will continue to be buffered.

    I am not even sure that the prefetching needs to be done in a separate thread. The message request with _async_request is already asynchronous based on the name, and as seen in the trace, is already sufficiently fast that it probably can also be called from the main thread during any read / seek call as necessary.

  2. denisrohr commented on Nov 28, 2025

    @denisrohr

    the amount of requests "on the wire", meaning the effect of the variable max_concurrent_requests, should be in_window_size / 32kb. Not default window size. A well programmed server will notice more bandwidth is possible and notify the client that it can squeeze in more requests "on the wire".
    If the upper layer that uses paramiko reads from the prefetched buffer too slowly, the read replies from the server will get stuck either in the prefetcher or worst case in the OS tcp receive buffer. If they fill up the tcp receive buffer, the OS will notify the server's OS that the tcp connection is clogged and it should slow down. The server's OS will notify the ssh server and that in turn will slow down it's read replies.
    Paramiko should therefore dynamically adjust how many read requests should remain pending.
    If the server increases the window size, it should increase the amount of pending read requests.
    If the upper layer read()s too slowly, it should decrease the amount of pending read requests.

    Another problem is that the CPU usage spikes to 100% in the file paramiko/file.py in the method "def read(self, size=None)", where the self._rbuffer becomes bigger and bigger and on every append (32kb) the whole buffer needs to be copied. If the upper layer requests a lot of bytes in the read(), 99% of the cpu time will be spent appending buffers and there will be no cpu time left to actually read from the socket.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions