Skip to content

perf(daemon): refresh before each request from native file watcher events - #217

Draft
sergej-koscejev wants to merge 9 commits into
mainfrom
perf/188-native-file-watcher
Draft

sergej-koscejev wants to merge 9 commits into
mainfrom
perf/188-native-file-watcher

Conversation

@sergej-koscejev

@sergej-koscejev sergej-koscejev commented Nov 5, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #188

Before each request, the daemon refreshes known, watched folders from native file watcher (fsnotifier) events. It walks the folders only when it cannot trust the watcher.

 ExternalProjectRefresh.refresh
+  assessFileWatcher(NativeFileWatcherProbe)            # outside the EDT
+    wait until the watch roots are set
+    fence: create <system>/mops-file-watcher-fence/<uuid>/fence, wait until the watcher reports it
+    flush the EDT (watcher failure notifications arrive there)
+    Trusted if the fence was reported, no failure, and the watch roots and fsnotifier starts are unchanged
+    since the last refresh; otherwise FullWalk(FullWalkReason)
-  IFile.refresh(.mps, forced)
-  markDirtyAndRefresh(module folders)
+  for .mps, then module folders:
+    planFolderRefresh -> walk:       new folders, folders outside every recursive watch root,
+                                     (Windows) folders on another drive than the fence, everything when FullWalk
+                         fromEvents: the rest -> refreshFiles(non-forced)
+    executeFolderRefresh
   StoreReloadManager.reloadChangedStorageFiles, archives, ReloadManager.flush, class path   # unchanged
+  log "refreshed external changes using <file watcher events | a full walk because …>: …; watcher wait N ms, refresh N ms"

Also:

  • Watcher in the daemon: the plugin registers HeadlessNativeFileWatcher, a second pluggableFileWatcher that stays on in a headless application. Before MPS starts, the daemon copies bin/<os>/fsnotifier into <workspace>/fsnotifier/ and makes it executable. With no binary for the platform it turns the watcher off. MOPS_DAEMON_JAVA_OPTIONS=-Didea.filewatcher.disabled=true turns it off by hand.
  • Request gate: only readers queued before a refresh started share it. Before, a reader that arrived during a refresh shared it and could miss an external change made just before it arrived. The fence cannot close that gap.

Decisions the issue left open: the daemon copies fsnotifier, not the CLI launcher, so daemon tests get the same watcher. The fence has its own watch root in the IDEA system directory. The startup walk is the first request's refresh, so the first request pays for it. After a watcher failure notification, every refresh walks until the daemon restarts.

Changes since the first version of this PR

The refresh behaves the same except where a point below says otherwise. The changes follow the repo's rules for commits and design standards:

  • The history is rebuilt on main as nine commits with no merges. Tests come before the code that makes them pass.

  • A refactor commit first splits ExternalProjectRefresh as it is on main, so that it follows one-kind:

    • RefreshBaseline (Value): what a refresh saw (class paths, archive stamps, folders with stored files loaded).
    • externalChangePolicy.kt (Logic): which archives to refresh, which folders to load stored files for, and which runtimes to replace, with unit tests in ExternalChangePolicyTest.
    • ExternalProjectRefresh (Adapter): the VFS and MPS work. It keeps the baseline of the latest refresh as a private cache (adapter-cache): without it, a refresh would refresh every archive, load every folder's stored files and replace every runtime.
    • moduleRefresh.kt is split: moduleLocations.kt (Adapter) reads where modules are loaded from, now including the class paths of MPS-managed modules, and moduleRefresh.kt (Adapter) keeps the VFS refresh functions.

    One behavior differs: a refresh that throws keeps the whole previous baseline. Before, it kept the parts it had updated before the failure.

  • WatchedFolderRefresh mixed policy, I/O and state. It is split into the daemon.filewatcher package:

    • Logic: assessFileWatcher and planFolderRefresh, with unit tests in FolderRefreshPolicyTest.
    • Adapters: FolderRefresher, NativeFileWatcherProbe, FileWatcherFence, FileWatcherExecutable and executeFolderRefresh. FolderRefresher keeps the watcher's change count and the folders refreshed so far as a private cache, and updates it only when a refresh finishes; without it, every folder is walked.
    • Values: WatcherAssessment, WatchCoverage, FolderRefreshPlan, FolderRefreshReport and FullWalkReason.
    • The fence result carries the watch coverage (Fence.Reported(coverage)), so the coverage is read only after a reported fence without a call-order contract.
  • Every new or changed class has a kind word and a Hides: line. Where a class breaks a rule, its KDoc says which rule and why:

    • ProjectRequestGate breaks one-kind.
    • The probe and the plugin's watcher break connascence-in-types, because they reach each other by name across the plugin class loader, as JavaParser reaches JavaParserBridge.
    • The plugin's watcher breaks adapter-cache: it keeps the watch roots and whether a failure was reported, because the platform tells it about them only through calls and notifications.
  • DaemonWorkspace.fileWatcherDir names the fsnotifier/ directory.

  • LibraryModelRefreshIntegrationTest is now a smoke test, so the full tier also runs it on macOS and Windows.

  • Two log reasons changed:

    • the file watcher did not report the fence in time replaces … within 2000 ms.
    • the file watcher reported a failure now adds or its restarts cannot be counted, because a missing restart counter gives the same result as a reported failure.

Where to look

  1. ProjectRequestGate.run: the batch rule. Under concurrent reads there are more refreshes, and each one can wait for the fence (up to about 300 ms on macOS right after an edit).
  2. assessFileWatcher in filewatcher/folderRefreshPolicy.kt: the trust conditions and their order. Look also at which change count is kept when a check fails early.
  3. ExternalProjectRefresh.refresh: the order of the refresh steps and which baseline each step compares with. Its baseline and FolderRefresher's cache are written only when the refresh finishes. It is an Adapter for "MPS's view of the project's files on disk", though it drives several platform services (VFS, StoreReloadManager, ReloadManager, ClassLoaderManager).

Left for later: #298 (runtime checks on Windows, Linux aarch64 and the watcher failure path) and #301 (ClassPathFingerprints under adapter-cache).

Evidence

  • Fence (ExternalProjectRefreshSemanticsTest): 20 times in a row, the test renames a node in a model file, refreshes right away, and reads the node. It runs once for a project model and once for a library model outside the project folder. On macOS and Linux x86-64, it also asserts that every refresh after the first used watcher events.

    Before: with awaitFence returning REPORTED without waiting, both tests fail at edit 0 is visible after the refresh.

    After: both pass (MPS 2026.1.2, macOS arm64).

  • Gate (ProjectRequestGateTest): a reader arriving during a refresh waits for its own refresh.

    Before: fails on main.

    After: passes.

  • Watcher on and off (LibraryModelRefreshIntegrationTest, through the installed CLI): with the watcher on, the log says using file watcher events. With -Didea.filewatcher.disabled=true, edits stay visible, the log says a full walk because the file watcher is not operational, and no refresh used watcher events. Both pass on 2026.1.1.

  • Suites: check passes on macOS arm64: :daemon:test (499), :cli:test (385), :protocol:test (111), :architecture-tests:test and the CLI integration tests for each MPS version. Every commit compiles and passes detektMain.

  • Per-request cost (measured on the first version of this PR, which has the same refresh logic, not measured again): a copy of MPS-extensions with MPS 2024.1.6 (260 modules, 51,757 files, built), macOS arm64, on a busy machine.

    Per request main / watcher off Watcher on
    Folder refresh (daemon log) 158–354 ms, median about 190 ms 4–10 ms (one at 54 ms)
    Watcher wait (roots + fence + EDT flush) — 30–308 ms, median about 80 ms
    mops list --depth 0, wall time, 25 runs, median 651 ms 588 ms
    Same, right after an external model edit, 10 runs median 820 ms median 620 ms

Merge Danger

Blast Radius: daemon-wide

Every daemon now starts an fsnotifier process. Whether a request sees external changes now depends on the fence and the trust checks. If a check is wrong, a request misses an external edit and nothing reports it. The tests assert watcher use only on macOS and Linux x86-64; #298 lists what has not run.

The scenario takes the way it runs the CLI as a parameter, so another test can
run it with different daemon options.
ExternalProjectRefresh kept what earlier refreshes saw and decided what
changed since then. It now follows `one-kind`:

- RefreshBaseline (Value) holds what a refresh saw.
- externalChangePolicy.kt (Logic) decides which archives to refresh,
  which folders to load stored files for, and which runtimes to replace.
- ExternalProjectRefresh (Adapter) does the VFS and MPS work. It keeps
  the baseline of the latest refresh as a private cache
  (`adapter-cache`).

moduleRefresh.kt is split. moduleLocations.kt (Adapter) reads where
modules are loaded from, now including the class paths of MPS-managed
modules. moduleRefresh.kt (Adapter) keeps the VFS refresh functions.

A refresh that throws now keeps the whole previous baseline. Before, it
kept whatever parts it had updated before the failure.
…fresh

The existing gate tests now queue their readers behind running exclusive work, so
they still describe batches of readers queued before a refresh. The new test
fails until the gate stops admitting readers that arrived during the refresh.
A reader that arrived while a refresh ran shared that refresh, so it could miss an
external change made just before it arrived.
Twenty times in a row, for a project model and for a library model outside the
project folder.
The daemon plugin registers a native file watcher that also runs in a headless
application. Before MPS starts, the daemon copies fsnotifier from the MPS
distribution into its workspace and makes it executable, because the generic
distribution archive drops the executable bit. Without a binary for the platform
it turns the watcher off. Refreshes still walk every folder.
With the native file watcher available, refreshes use its events. With the
watcher turned off through the daemon's Java options, edits stay visible and
every refresh walks the folders. Both tests fail until refreshes log their mode.
Logic and Values only; nothing calls them yet. assessFileWatcher decides from the
watcher's signals whether a refresh can rely on its events, and planFolderRefresh
splits the folders into those to walk and those to refresh from events.
Before each request, the daemon waits for the watcher to report a fence
file, then refreshes known, watched folders from the watcher's events
instead of walking them. It walks the folders when the watcher is not
trusted; the daemon log names the mode and cost of each refresh.

FolderRefresher (Adapter) refreshes the folders. It keeps the watcher's
change count and the folders refreshed before as a private cache
(`adapter-cache`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

daemon: refresh before each request driven by the native file watcher, with the full walk as fallback

1 participant