-
-
Notifications
You must be signed in to change notification settings - Fork 983
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Libtorrent 2.x memory-mapped files and RAM usage #6667
Comments
Seems like for Windows users it also can cause crashes qbittorrent/qBittorrent#16048. Because in Windows process can't allocate more than virutal memory avaliable (physical RAM + max pagefile size). |
there's no way to avoid Is the confusion among users similar to this? |
pages backed by memory mapped files are not also backed by the pagefile. The page file backs anonymous memory (i.e. normally allocated memory). The issue of windows prioritizing pages backed by memory mapped files very high, failing to flush it when it's running low on memory is known, and there are some work-arounds for that. such as periodically flushing views of files and closing files (forcing a flush to disk).
I don't believe that's true. Do you have a citation for that? |
Yes. It's like
No. I mentioned, that it's not cache.
Well, just search for it. Simply Windows never had overcommit feature. And I personally as programmer faced this fact. |
sorry, I accidentally clicked "edit" instead of "quote reply". And now I'm having a hard time finding the undo button. |
The output from:
would be more helpful.
None of these are microsoft sources, just random people on the internet making claims. One of those claims is a program that (supposedy) demonstrates over-committing on windows.
I think this may be drifting away a bit from your point. Let me ask you this. On a computer that has 16 GB of physical RAM, would you expect it to be possible to memory map a file that's 32 GB? According to one answer on the stack overflow link, it wouldn't be considered over-committing as long as there is enough space in the page file (and presumably in any other file backing the pages, for non anonymous ones). So, mapping a file on disk (according to that definition) wouldn't be over-committing. With that definition of over-committing, whether windows does it or not isn't relevant, so long as it allows more virtual address space than there is physical memory. |
Of course. (File name is redacted.)
Sorry. By overcommit I mean exceeding the virtual memory amount, as I said earlier physical RAM + pagefile. Windows don't allow that. If this happens, Windows will expand the pagefile with empty space to ensure full commit capacity and will raise OOM if max pagefile size exceeded. So if you have e.g. 16G RAM and 16G max pagefile size, maximum possible virtual memory amount for the whole system will be 32G. This can be easily tested. |
Ok. When memory mapping a file, that file itself is backing the pages, so (presumably) they won't affect the pagefile, or be relevant for purposes of extending the pagefile. That I would also expect those pages to be some of the first to be evicted (especially some of the 99.3 % of the non-dirty pages, that are cheap to evict). Do you experience this not happening? Does it slow down the system as a whole? |
I found a more clear analogy. Windows always behaves like Linux with kernel option
This was a download. It was just easier to make a fast showcase for a such large memory scale. Because when downloading the memory consumption seems to grow indefinitely. When seeding, memory consumption depends on peers activity. Only certain file parts are loaded, as shown in this output (file names omitted):
When seeding RSS are freeing when file are not in use. But qBt memory consumption is still very large. With intensive seeding tasks it easily grows to gigabytes. |
I performed better test downloading very large file that is larger than my RAM.
RSS caps at RAM amount avaliable. No system slowdown, OOM events or such. Don't have the swap though. |
My impression is that Linux is one of the most sophisticated kernels when it comes to managing the page cache. Windows is having more problems where the system won't flush dirty pages early enough, requiring libtorrent to force flushing. But that leads to waves of disk thread lock-ups while flushing. There's some work to do on windows still, to get this to work well (unless persistent memory and the unification of RAM and SSD happens before then :) ) |
Yeah. Linux at least can be tweaked in all aspects and debugged. I made some more research in per-process stats.
Mapped files obviously represented as |
This is an issue on Linux, as libtorrent's memory-mapped files implementation affects file eviction (swaping out) functionality. Please watch the video where rechecking the torrent in qBittorrent forces the kernel to swap out 3 GB of RAM. qbittorrent-440-recheck2-2022-01-16_20.18.30.mp4Regular Relevant issue in qBittorrent repo: qbittorrent/qBittorrent#16146 |
I also found this behavior strange. It shouldn't work this way I think. But diving in the source, not found anything suspicious. Line 580 in 55111d2
And flags are normal Line 326 in 55111d2
I also tried to play around with the flags, but nothing changes. But I'm not an expert in C++ and Linux programming, so definitely can miss something. |
Ugh.
|
|
It seems I was not entirely correct in my previous comment. For some reason, my Linux system also swaps out anonymous memory upon reading huge files with the regular means (read/fread), and decreasing vm.swappiness does not help much. This might be recent change as I don't remember such behavior in the past. So take my comment with a grain of salt: everything might work fine, I need to do more tests. |
@ValdikSS, try to adjust |
Well, this is how the OS treats memory-mapped files. This question should be addressed to Linux kernel devs, I suppose. |
For the mmap issue, how about creating many smaller maps instead of the large map of the entire file. And let the size of mapped chunks pool configurable, or default to a reasonable value. It's similar to the original |
I don't think it's obvious that it would make a difference. It would make it support 32 bit systems for sure, since you could stay within the 3 GB virtual address space. But whether unmapping a range of a file causes those pages to be forcefully flushed or evicted, would have to be demonstrated first. If that would work, simply unmapping and remapping the file regularly might help, just like the periodic file close functionality. |
I believe the default behavior is to flush pages or delayed flush on sync, unless special flags were used such as MADV_DONTNEED. But you can always use msync(2) to force the flush. |
Just my 2 cents qbittorrent 4.4.0 started using libtorrent 2.0. Windows 10 x64 Based on this https://libtorrent.org/upgrade_to_2.0-ref.html#cache-size i feel something is not quite right since it says that libtorrent 2.0 should exclusively use the OS cache. |
libtorrent 2 features memory mapped file. So it request the OS to virtually maps the file to the memory and let the OS to decide when to do actual read, write and release through this cpu cache - physical memory - disk stack. OS will report a high memory usage but most of these usage should actually just be cached binaries that don't need to be freed at the moment. (Unless under some scenario windows didn't flush its cache early enough, which is what #6522 and this issue is talking about). But I do think I observed disk usage and IO get higher than before when I first use a libtorrent 2 build. Don't sure if it's still the case for windows now as I've migrated to linux. |
Finally some updates to this problem. Idk if the last part will help my setup. We'll see if pwrite fixes my problems on my NFS share. |
Any idea of a timeline as to when 2.0.8 will be released? |
Probably this week or this coming weekend. |
Cool, thanks for the response and your hard work! |
Tried the fresh 2.0.8, it didn't change anything for me. Staying on 1.2 for now. Sorry... |
Same problem. |
Workaround: set https://www.libtorrent.org/single-page-ref.html#default-disk-io-constructor to posix-compliant |
How about the concept of mapping smaller regions (1~16MB) of the file instead of mapping the entire file, and penalize/deprioritized IO requests that accessing non-mmaped regions. This method is essentially converting a torrent with large files (large mmap) to torrent with many smaller/medium files (smaller mmap). The address space of all mappings could be restricted by the pool size. It's common practice to expose a limit on usage of mmap, such as sqlite's mmap_size and lmdb. The libtorrent-rakshasa is using chunked mmap instead of mmap the entire file. Although this will bring more overhead of mmap/mumap, the smaller RSS could improve the page cache hit ratio and UX for non-tech-savvy users to accept lt2.x isn't eating all the ram. |
On windows it literally causes freezes, so it's not just an "UX" of "non-tech-savvy users". |
Having the same issue here. 2GB Download slowed down to speeds of like 50KBs and than even stopped even tho at least 20 Seeders were available, i checked the RAM and saw that i have a wonderful 20MB of free RAM from a total of 12GB. This happened on Ubuntu Server 22.04LTS. Using QBittorrent-nox 4.5.2, came here from the issue on their side that mentioned this. I changed the Disk IO Type to "POSIX-compliant" in the advanced settings of QBittorrent and rebooted the whole thing. Will edit this comment in a few hours probably with the result of if that fixed the issue or not. This needs fixing, since no matter how big the download, after some time the whole thing just stops downloading and needs a actual reboot to work again for an hour or less. EDIT: This seems to have fixed the issue. RAM usage is stable and below 1GB for over an hour now. |
…sterbar 2.x * Disable memmory mapped file handling and use "POSIX-compliant" * Backport commit 8bcac1bed28f93c0712e915f43294b1e5fd03659 which reduces FilePoolSize * Change status to experimental This change only applies to new installs, if you have a configuration already you need to apply these changes by hand References: qbittorrent/qBittorrent@8bcac1b arvidn/libtorrent#6667 (comment) PR: 270765 Reviewed by: yuri (previous revision)
POSIX-compliant disk I/O type in the advanced configuration makes qBittorrent interface very sluggish. Adding the torrent resulting it up to 1 minute interface freeze, and the files are seeded definitely slower than with libtorrent 1 or memory-mapped I/O. Anyone else having the same issue? |
@ValdikSS Yes. I have the exact same issue and tried the same things to fix it, also without success. So I'm sitting here (just like you) having to choose if I want my QBT instance to crash and not work at all, or have it work, but be completely uncontrollable while downloading large files (because the Web ui just times out until its done downloading) and have very slow download and upload speeds as well. |
I have been using POSIX-compliant I/O on my seedbox for a while and now tried to use memory-mapped I/O again.
|
Workaround if you want to use memory-mapped IO, but still want to limit memory usage:
|
I "solved" the issue for me, by using qbittorrent-nox with libtorrent V1, that way it's not as fast as V2 (when it's working properly), but at least it doesn't grind to a halt when doing anything besides looking pretty. |
This will OOM still. Not sure why you downvoted my workaround? |
It doesn't seem to get OOM killed for me? If I use memory-mapped file I/O and use the If I set I/O to POSIX-compliant, then the memory usage will be good but checking a very large torrent causes qBittorrent to become very laggy (almost freezing) until it's done checking and rechecking speed is way lower (~1 GB/s). I think rechecking speed is so much lower because hashing threads seem to be limited to a single thread with POSIX-compliant I/O. There is no such limit with memory-mapped file I/O, and using 24 threads maximizes checking speed on my 6 core/12 thread processor. |
I'm a win10/Arch dual boot user, and shares same torrent folder in a NTFS partition on my hdd. As I migrated to qbittorrent on linux I suffer from qbit totally stuck all the time I add any large torrent, and my system blocks for a long time with no response, ps or htop doesn't response so I find it hard to trace the bug. I've suspected ntfs-3g might not be stable with heavy r/w before.
After I read the issue I see where problem really comes from. BTW the frozen qbit process seems not able to trigger oom-killer now on my latest kernel v6.6.1-arch1-1, while it sometimes triggers before. Hoping for a native fix for high RAM usage! |
Someone need to actually deal with it. There are still fair amount of options that can be done. On Linux side of things,
Unless someone wants to ask the kernel devs directly. Also At the end of the day, we probably can fallback to manually calling I would probably try all of it myself, but I'm too bad with C++, especially considering complicated libtorrent codebase. |
@PriitUring |
libtorrent version (or branch): 2.0.5 (from Arch Linux official repo)
platform/architecture: Arch Linux x86-64, kernel ver 5.16
compiler and compiler version: gcc 11.1.0
Since
qBittorrent
started to actively migrate tolibtorrent
2.x, there a lot of concerns from users about extensive RAM usage.I not tested
libtorrent
with other frontends, but think result will be similar to qBt.The thing is,
libtorrent
2.x memory-mapped files model may be great improvement for I/O performance, but users are confused about RAM usage. And seems like this is not related to particular platform, both Windows and Linux users are affected.libtorrent
2.x causes strange memory monitoring behavior. For me the process RAM usage is reported very high, but in fact memory is not consumed and overall system RAM usage reported is low.Not critical in my case, but kinda confusing.
This counter does not include OS filesystem cache, just in case.
Also this is not some write sync/flush issue, because also present when only seeding.
I'm not an expert in this topic, but maybe there are some flags can be tweaked for
mmap
to avoid this?The text was updated successfully, but these errors were encountered: