Skip to content

Watching for changes using too many system resources on BSDs (kqueue) #7855

Closed

Description

@imsodin

Kqueue is a very heavyweight mechanism to watch for changes. Issues about systems being brought to their knees by Syncthing's watching for changes with large folders come in every now and then, see e.g. https://forum.syncthing.net/t/truenas-freenas-folder-watcher-unusably-slow-crashes-host/17161

We should somehow prevent this. Either disabling kqueue alltogether or adding a limit for items in a folder (disable kqueue once reached). The latter will be somewhat annoying, as I guess users will want to adjust/override that, and we'd need to enable watching only after the initial scan (so we know how many items there are). However just disabling it will probably make a lot of people mad.

Activity

  1. added
    bug
    needs-triage
    on Jul 29, 2021
  2. alexmarkley commented on Jul 29, 2021

    @alexmarkley

    Thanks for adding this issue @imsodin. As you noted via the above forum post link, this issue bit me hard this week, so I would have benefited from some kind of mitigation.

    Changing the defaults when the host is BSD would have been helpful, but honestly would not have helped me that much, because I probably would have simply turned it on when I set everything up.

    Honestly, as a sysadmin, I would cast my vote for a UI warning along with some clear documentation around the risks and tradeoffs of enabling the feature. I think that would have saved me a couple days of hair-pulling.

    To further clarify, I don't think the biggest issue is the performance impact itself. I think the biggest issue was the mystery of trying to figure out what was causing the performance impact. So a clear UI warning would have been incredibly helpful.

    Thanks again for working on Syncthing. It's pretty incredible!

  3. koskee commented on Nov 23, 2021

    @koskee

    I agree, it took me several weeks of screwing around to finally figure out why anytime Syncthing was enabled my system would CRAWL and be literally unusable (which was tough to troubleshoot because the shell and webgui would actually time out before getting a response.) I was trying to install custom Syncthing jails with resource limits and all kinds of circular goose chasing, only to find out it was just an otherwise innocent looking setting...

  4. calmh commented on Nov 24, 2021

    @calmh
    Member

    So perhaps a one time notification similar to the "you should set up authentication" one, like "my dude, you're on a system with kqueue, use filesystem notifications with care and be aware that enabling it for large folders will use lots of system resources", perhaps with a link to some suitable doc article / faq...

  5. koskee commented on Nov 24, 2021

    @koskee

    Exactly. Just something to make the average user aware of the performance impact. For me, the filesystem watcher works flawlessly on my Windows and my prior Linux setup, but the FreeBSD version (that i only started using/learning a month ago) basically left me completely unaware.

    Otherwise, its a pretty nice bit of code you've got here.

  6. AudriusButkevicius commented on Nov 24, 2021

    @AudriusButkevicius
    Member

    "Average user" and "BSD" in the same paragraph, wat?

  7. koskee commented on Nov 24, 2021

    @koskee

    Right. That is a fair point you have there.. Perhaps rookie, inexperienced or just plain dumbass (me) users might have been more precise - hard to say. Bahaha

  8. bitclick commented on Nov 26, 2021

    @bitclick

    i think, regardless of the user-model, (this) software should give relevant information to the user in order to not waste their time. So a notification should be fine. This would be in the bsd-spirit 😄

  9. pushytoxin commented on Dec 8, 2021

    @pushytoxin

    Is this a scalability issue? Could I fix the hang by adding another 128GB RAM to the box running Syncthing?

  10. AudriusButkevicius commented on Dec 8, 2021

    @AudriusButkevicius
    Member

    Its probably cheaper to move to linux, if you can.

  11. pushytoxin commented on Dec 9, 2021

    @pushytoxin

    I already tried and it didn't help

  12. AudriusButkevicius commented on Dec 9, 2021

    @AudriusButkevicius
    Member

    Tried what and what didn't help?

  13. pushytoxin commented on Dec 10, 2021

    @pushytoxin

    256GB RAM in the box, the issue described above still happens (~11M files)

  14. AudriusButkevicius commented on Dec 10, 2021

    @AudriusButkevicius
    Member

    Yes, on BSD watching large folders does not work, this is what issue talks about.

  15. 17 remaining items

  16. pae-id commented on May 27, 2022

    @pae-id

    Just got this warning for a bunch of Syncthing folders that had 0 files since update and restart (used for auto cloning between systems with manual deletion after move to final destination).

    Seeing that kqueueItemCountThreshold = 10000 and the condition for warning is
    summary.Data.(map[string]interface{})["localTotalItems"].(int) > kqueueItemCountThreshold

    I am now confused as to what localTotalItems is and where exactly I should avoid enabling FS watching.

    Is it Local State (Total) number of files in this instance of Syncthing, 100k in my case, and it is a bad idea to enable FS watching an any of the folders not considering the actual amount of files in them?

  17. imsodin commented on May 27, 2022

    @imsodin
    MemberAuthor

    I am a bit confused as you are saying first 0 files then 100k? Anyway I can't tell you if it's a problem, but I assume if you don't observe anyproblems with your current set up, semms like it's fine. Generally lets please take further discussion on your specific use-case to https://forum.syncthing.net, we handle support-like discussions there.

    Maybe emitting a warning for this is too much and "INFO" would have been the sweet spot. The reason I used a warning is that hardly anyone notices an info level line in the logs. However that might just be a good thing: Dosn't spook anyone, but if issues arise there's a hint in the logs if anyone starts digging.

  18. ajshea commented on May 28, 2022

    @ajshea

    Something broke with this revision.

    I have two FreeBSD systems syncing the same sets of files. I had to increase kern.maxfiles sysctl when first setting them up, but they have worked well up until this revision. Global state is 90,262 files, 8,127 directories, ~343 GiB.

    1. I get the warning even for sets with only 10 files in one directory.
    2. Disabling file system watching does not reduce the system load. Even scanning a set with 10 files bumps CPU to 95% for 15-20 seconds. Previously syncthing CPU usage was less than a few percent almost all the time.
    3. One system has UFS, the other has ZFS. UFS has become almost unusable unless I pause all but the smallest sets. ZFS CPU usage has jumped to between 40-50% during rescans, but otherwise the system is still usable.

    Reverting to v1.19.2 on the UFS system seems to return the CPU usage to a sane level -- peaks to 95% for a few seconds when unpausing large sets, but dropping below 60-70% for 15-20 seconds while scanning all sets, then dropping to less than 1% overall.

    I will leave upgrades disabled and watch this version to confirm, but so far it seems to undo the CPU usage problem.

  19. imsodin commented on May 28, 2022

    @imsodin
    MemberAuthor
    1. That's indeed a bug: The limit also considers deleted items. Fix: lib/model: Don't include deleted items in kqueue warning threshold (ref #7855) #8365

    The cpu usage problem is definitely not related, all the change did is print a warning - no behaviour change otherwise. Please post on the forum to get help with diagnosing your problem: https://forum.syncthing.net

  20. ajshea commented on May 28, 2022

    @ajshea

    Thanks for the clarification.

    Something in 1.20 and following definitely changed the CPU usage. I'll post on the forum to see if we can narrow down what changed.

  21. pae-id commented on May 28, 2022

    @pae-id

    I am a bit confused as you are saying first 0 files then 100k?

    Sorry, for late reply, missed notification. 100k is the total number of files in all of Syncthing's (TrueNAS jail) folders, which includes ZFS mounts from 5 different pools.

    Not that I was seeking support, but rather was trying to clarify how kqueue works in order to understand this limitation, particularly what is the context for this 10k threshold. Is it single Syncthing folder that is being watched, or an underlying filesystem of that mount, or even the filesystem of the whole jail including all mounts.

    For the sake of curious minds that would come across this thread as it is the only thing coming up during research of the warning. Anyway, hope we are talking about the same issue and last commit fixes things.

  22. pae-id commented on Jun 12, 2022

    @pae-id

    It still warns about folders with 0 files after a restart on v1.20.2

    zero
    kqueue

  23. imsodin commented on Jun 18, 2022

    @imsodin
    MemberAuthor

    That's weird, now I can't come up with an explanation anymore. Can you run Syncthing with -verbose or enable events debug logging and look for FolderSummaries and post a few of those here please (those are used to trigger this warning).

  24. pae-id commented on Jun 20, 2022

    @pae-id

    look for FolderSummaries and post a few of those here please (those are used to trigger this warning).

    Here you go:

    Folder "p2p-plex-node1-SYNC"
    2022-06-20 23:11:45 Summary for folder "p2p-plex-node1-SYNC" is &{Errors:0 PullErrors:0 Invalid: GlobalFiles:0 GlobalDirectories:0 GlobalSymlinks:0 GlobalDeleted:5010 GlobalBytes:0 GlobalTotalItems:5010 LocalFiles:0 LocalDirectories:0 LocalSymlinks:0 LocalDeleted:8275 LocalBytes:0 LocalTotalItems:8275 NeedFiles:0 NeedDirectories:0 NeedSymlinks:0 NeedDeletes:0 NeedBytes:0 NeedTotalItems:0 ReceiveOnlyChangedFiles:0 ReceiveOnlyChangedDirectories:0 ReceiveOnlyChangedSymlinks:0 ReceiveOnlyChangedDeletes:0 ReceiveOnlyChangedBytes:0 ReceiveOnlyTotalItems:0 InSyncFiles:0 InSyncBytes:0 State:idle 23:11:39 +0300 MSK Error: Version:17901 Sequence:17901 WatchError:}
    2022-06-20 23:11:45 Completion for folder "p2p-plex-node1-SYNC" on device ZRHYUWI- is 100% (state: valid)
    
    Folder "p2p-plex-node4-CLONE"
    2022-06-20 23:11:45 Summary for folder "p2p-plex-node4-CLONE" is &{Errors:0 PullErrors:0 Invalid: GlobalFiles:0 GlobalDirectories:0 GlobalSymlinks:0 GlobalDeleted:98 GlobalBytes:0 GlobalTotalItems:98 LocalFiles:0 LocalDirectories:0 LocalSymlinks:0 LocalDeleted:97 LocalBytes:0 LocalTotalItems:97 NeedFiles:0 NeedDirectories:0 NeedSymlinks:0 NeedDeletes:0 NeedBytes:0 NeedTotalItems:0 ReceiveOnlyChangedFiles:0 ReceiveOnlyChangedDirectories:0 ReceiveOnlyChangedSymlinks:0 ReceiveOnlyChangedDeletes:0 ReceiveOnlyChangedBytes:0 ReceiveOnlyTotalItems:0 InSyncFiles:0 InSyncBytes:0 State:idle 23:11:39 +0300 MSK Error: Version:210 Sequence:210 WatchError:}
    2022-06-20 23:11:45 Completion for folder "p2p-plex-node4-CLONE" on device ZRHYUWI- is 100% (state: valid)
    
  25. imsodin commented on Jun 21, 2022

    @imsodin
    MemberAuthor

    The condition is:

    const kqueueItemCountThreshold = 10000
    [...]
    			} else if data.Summary.LocalTotalItems-data.Summary.LocalDeleted > kqueueItemCountThreshold {
    

    So clearly not fulfilled for the summaries you showed above.

    You did also get a new warning in the logs? Not just that you didn't press ok in the web UI (grasping at straws here).

  26. pae-id commented on Jun 21, 2022

    @pae-id

    You did also get a new warning in the logs? Not just that you didn't press ok in the web UI (grasping at straws here).

    Yeah, I though that warning could be carried from previous version in case I did not press "OK", but I rechecked that multiple times since then, hit "OK", had restarts, but it persistently pops up again (both GUI and logs) on every restart.

  27. added
    frozen-due-to-age
    on Apr 6, 2023
  28. locked and limited conversation to collaborators on Apr 6, 2023
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

    bugfrozen-due-to-age

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions