dirent: one probe vouches for every folder a poll watches (#2278)
A Sessions poll watched each Codex day folder and each Claude Code
project folder through dirent.Watch, and every call ran its own sync1
probe (a mkdir, an unlink and a wait for the write to be heard). On a
real history that was ~130ms of 1.47s CPU per 40 polls, and the
syscalls around it more. WatchSince(dir, mark) skips the probe when a
probe begun after mark was heard; sessions takes the mark when a hold
begins, so one poll makes one probe. A folder watched for the first
time still probes, since its watch hears only what comes after.
Run, real account (mote, the owner's Claude Code and Codex history,
40 back-to-back /api/sessions polls, sandbox HOME):
- CPU: w10 1.47s -> w11 0.54s; under sync1: 130ms -> 10ms.
- Allocated: 25.98 MB -> 25.08 MB.
Tests (mica): go test ./internal/dirent ./internal/sessions ok at the
pushed head. TestWatchesFromOneMarkProbeOnce is red with covered
forced false ("round 0: 3 probes from one mark") and green on this.
./internal/gui: TestPanelHiddenByWailsMarksPageOutOfSight fails under
-count=3 on an origin/main copy (4ae833f84) too and passes -count=1;
recorded on #6203.
This commit is contained in:
+37
-14
@@ -28,16 +28,17 @@ import (
|
||||
// does a listing older than listedFor, should an event never come.
|
||||
var watched struct {
|
||||
sync.Mutex
|
||||
roots []watchRoot
|
||||
seq uint64 // counts listings begun and changes heard
|
||||
all uint64 // seq when everything last changed
|
||||
dirty map[string]uint64 // a folder's seq when it last changed
|
||||
at map[uintptr]listed // a listing's seq and time, by its array
|
||||
since uint64 // seq when the watch last started
|
||||
active bool
|
||||
tried map[string]bool // roots asked for
|
||||
synced time.Time // when every change before was last heard
|
||||
probe probe
|
||||
roots []watchRoot
|
||||
seq uint64 // counts listings begun and changes heard
|
||||
all uint64 // seq when everything last changed
|
||||
dirty map[string]uint64 // a folder's seq when it last changed
|
||||
at map[uintptr]listed // a listing's seq and time, by its array
|
||||
since uint64 // seq when the watch last started
|
||||
active bool
|
||||
tried map[string]bool // roots asked for
|
||||
synced time.Time // when every change before was last heard
|
||||
vouched int // the number of the last probe whose write was heard
|
||||
probe probe
|
||||
}
|
||||
|
||||
type watchRoot struct {
|
||||
@@ -83,17 +84,38 @@ const (
|
||||
// heard: a reader calls it before each read of dir's folders, and Stats
|
||||
// of a folder in it then skips listing one that hasn't changed. A Stats
|
||||
// with no Watch of its root in the last trustFor reads its folder.
|
||||
func Watch(dir string) {
|
||||
func Watch(dir string) { WatchSince(dir, Mark()) }
|
||||
|
||||
// Mark is the point a WatchSince waits from: every change made before it.
|
||||
func Mark() int {
|
||||
watched.Lock()
|
||||
defer watched.Unlock()
|
||||
return watched.probe.n
|
||||
}
|
||||
|
||||
// WatchSince is Watch, waiting only until every change made before mark
|
||||
// is heard: a probe begun after mark, its own or another's, vouches for
|
||||
// them. A Sessions poll watches each Claude Code and Codex folder in turn,
|
||||
// and each Watch wrote, waited on and removed a probe of its own, ~1/6 of
|
||||
// the poll's CPU (#2278); the poll marks once, and its first probe does
|
||||
// for every folder after.
|
||||
func WatchSince(dir string, mark int) {
|
||||
dir = filepath.Clean(dir)
|
||||
starting.Lock()
|
||||
defer starting.Unlock()
|
||||
watched.Lock()
|
||||
tried := watched.tried[dir]
|
||||
covered := watched.active && watched.vouched > mark
|
||||
watched.Unlock()
|
||||
if !tried && !add(dir) {
|
||||
return
|
||||
if !tried {
|
||||
if !add(dir) {
|
||||
return
|
||||
}
|
||||
covered = false // a watch begun again hears only what comes after
|
||||
}
|
||||
if !covered {
|
||||
sync1()
|
||||
}
|
||||
sync1()
|
||||
}
|
||||
|
||||
// starting is held while a watch is made or waited on, outside watched:
|
||||
@@ -189,6 +211,7 @@ func sync1() {
|
||||
watched.probe.heard, watched.probe.want = nil, ""
|
||||
if ok {
|
||||
watched.synced = at
|
||||
watched.vouched = max(watched.vouched, p.n)
|
||||
return
|
||||
}
|
||||
watched.seq++
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
//go:build darwin && cgo
|
||||
|
||||
package dirent
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A Sessions poll watches each of its folders in turn, and each Watch
|
||||
// wrote, waited on and removed a probe (#2278). Watches from one mark take
|
||||
// one probe between them, and a change made before the mark is still seen
|
||||
// by the next read; a Watch from a later mark probes again.
|
||||
func TestWatchesFromOneMarkProbeOnce(t *testing.T) {
|
||||
root, day, rollout := dayFolder(t)
|
||||
other := filepath.Join(t.TempDir(), "projects")
|
||||
if err := os.MkdirAll(other, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
Watch(other)
|
||||
if !quiet(t, root, day) {
|
||||
t.Fatal("an unchanged watched folder was read again")
|
||||
}
|
||||
probes := func() int { watched.Lock(); defer watched.Unlock(); return watched.probe.n }
|
||||
for i := range 20 {
|
||||
f, err := os.OpenFile(rollout, os.O_APPEND|os.O_WRONLY, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.WriteString(`{"type":"event"}` + "\n")
|
||||
f.Close()
|
||||
aged(t, rollout)
|
||||
fi, _ := os.Stat(rollout)
|
||||
mark, before := Mark(), probes()
|
||||
WatchSince(root, mark)
|
||||
WatchSince(other, mark)
|
||||
WatchSince(root, mark)
|
||||
if n := probes() - before; n != 1 {
|
||||
t.Fatalf("round %d: %d probes from one mark", i, n)
|
||||
}
|
||||
l, _ := statsOf(t, day)
|
||||
if s, _ := l.Get("rollout-a.jsonl"); s.Size != fi.Size() {
|
||||
t.Fatalf("round %d: read %d bytes, the file has %d", i, s.Size, fi.Size())
|
||||
}
|
||||
}
|
||||
before := probes()
|
||||
WatchSince(other, Mark())
|
||||
if probes() == before {
|
||||
t.Fatal("a later mark took no probe of its own")
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package sessions
|
||||
import (
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/yetone/magpie/internal/dirent"
|
||||
)
|
||||
|
||||
// held is the session folders' listings kept while a request that reads
|
||||
@@ -20,6 +22,9 @@ var held struct {
|
||||
// listing as its own (#4114)
|
||||
gen uint64
|
||||
lists map[string]*heldList
|
||||
// mark is dirent's as the latest hold began: its folders are watched
|
||||
// from it, so a poll's first probe vouches for the rest (#2278)
|
||||
mark int
|
||||
}
|
||||
|
||||
type heldList struct {
|
||||
@@ -38,6 +43,7 @@ func OneListing() (done func()) {
|
||||
held.Lock()
|
||||
held.n++
|
||||
held.gen++
|
||||
held.mark = dirent.Mark()
|
||||
held.Unlock()
|
||||
return letGo()
|
||||
}
|
||||
@@ -53,6 +59,7 @@ func ShareListing() (done func()) {
|
||||
defer held.Unlock()
|
||||
if held.n++; held.n == 1 {
|
||||
held.gen++
|
||||
held.mark = dirent.Mark()
|
||||
}
|
||||
return letGo()
|
||||
}
|
||||
@@ -67,6 +74,18 @@ func letGo() func() {
|
||||
})
|
||||
}
|
||||
|
||||
// watch is dirent.Watch of dir, from the latest hold's mark under one.
|
||||
func watch(dir string) {
|
||||
held.Lock()
|
||||
n, mark := held.n, held.mark
|
||||
held.Unlock()
|
||||
if n == 0 {
|
||||
dirent.Watch(dir)
|
||||
return
|
||||
}
|
||||
dirent.WatchSince(dir, mark)
|
||||
}
|
||||
|
||||
// heldWaiting is a test's: called as a read waits for another's listing.
|
||||
var heldWaiting func()
|
||||
|
||||
|
||||
@@ -904,7 +904,7 @@ func joinedIs(s, a, b string, sep ...byte) bool {
|
||||
|
||||
func listCCFiles(agent, dir string) []file {
|
||||
projects := filepath.Join(dir, "projects")
|
||||
dirent.Watch(dir) // statFiles reads only the projects that changed
|
||||
watch(dir) // statFiles reads only the projects that changed
|
||||
last, _ := ccListed.Load(dir)
|
||||
names := ccNames{last: func() []ccName { l, _ := last.([]ccName); return l }()}
|
||||
// listed into a list of its own kept from the last pass, and given as
|
||||
@@ -1234,7 +1234,7 @@ func codexFilesIn(dir string) []file {
|
||||
}
|
||||
// its unchanged day folders aren't read again; a change in dir itself
|
||||
// (history.jsonl, at each prompt) has them all read
|
||||
dirent.Watch(dir)
|
||||
watch(dir)
|
||||
var lists [2][]file
|
||||
for i, folder := range []string{"sessions", "archived_sessions"} {
|
||||
root := filepath.Join(dir, folder)
|
||||
|
||||
Reference in New Issue
Block a user