diff --git a/internal/sessions/dirstat_other.go b/internal/sessions/dirstat_other.go new file mode 100644 index 000000000..7844fd512 --- /dev/null +++ b/internal/sessions/dirstat_other.go @@ -0,0 +1,17 @@ +//go:build !unix + +package sessions + +import ( + "os" + "time" +) + +// dirStat is os.Stat's mod time, size and whether path is a folder. +func dirStat(path string) (mod time.Time, size int64, dir bool, err error) { + st, err := os.Stat(path) + if err != nil { + return time.Time{}, 0, false, err + } + return st.ModTime(), st.Size(), st.IsDir(), nil +} diff --git a/internal/sessions/dirstat_test.go b/internal/sessions/dirstat_test.go new file mode 100644 index 000000000..38f40d055 --- /dev/null +++ b/internal/sessions/dirstat_test.go @@ -0,0 +1,47 @@ +package sessions + +import ( + "errors" + "io/fs" + "os" + "path/filepath" + "runtime" + "testing" + "time" +) + +// A folder a Sessions poll looks at again, unchanged, costs its listing +// nothing and its stat no FileInfo: os.Stat's was ~4.5 MB of garbage per +// 40 polls on a real history (#2278). dirStat says what os.Stat does. +func TestDirStatAsOSStat(t *testing.T) { + dir := t.TempDir() + file := filepath.Join(dir, "f.jsonl") + if err := os.WriteFile(file, []byte("{}\n"), 0o600); err != nil { + t.Fatal(err) + } + old := time.Now().Add(-time.Hour).Truncate(time.Millisecond) + os.Chtimes(dir, old, old) + for _, p := range []string{dir, file} { + st, err := os.Stat(p) + if err != nil { + t.Fatal(err) + } + mod, size, isDir, err := dirStat(p) + if err != nil || !mod.Equal(st.ModTime()) || size != st.Size() || isDir != st.IsDir() { + t.Fatalf("%s: %v %d %t %v, os.Stat %v %d %t", p, mod, size, isDir, err, st.ModTime(), st.Size(), st.IsDir()) + } + } + if _, _, _, err := dirStat(filepath.Join(dir, "gone")); !errors.Is(err, fs.ErrNotExist) { + t.Fatalf("a folder gone: %v", err) + } + if got := readDirectory(dir); len(got) != 1 { + t.Fatalf("listed %d", len(got)) + } + if runtime.GOOS == "windows" { + return // os.Stat there + } + // the path's C string is all an unchanged folder's look allocates + if n := testing.AllocsPerRun(50, func() { readDirectory(dir) }); n > 1 { + t.Fatalf("an unchanged folder's look allocated %.0f times", n) + } +} diff --git a/internal/sessions/dirstat_unix.go b/internal/sessions/dirstat_unix.go new file mode 100644 index 000000000..5cf78ab87 --- /dev/null +++ b/internal/sessions/dirstat_unix.go @@ -0,0 +1,29 @@ +//go:build unix + +package sessions + +import ( + "io/fs" + "time" + + "golang.org/x/sys/unix" +) + +// dirStat is os.Stat's mod time, size and whether path is a folder, read +// into a Stat_t on the stack: os.Stat made a FileInfo of each of the few +// hundred folders a Sessions poll looks at, ~4.5 MB of garbage per 40 polls +// on a real history (#2278). Its errors are the errno, which errors.Is +// matches to fs.ErrNotExist as os.Stat's are. +func dirStat(path string) (mod time.Time, size int64, dir bool, err error) { + var st unix.Stat_t + for { + err = unix.Stat(path, &st) + if err != unix.EINTR { + break + } + } + if err != nil { + return time.Time{}, 0, false, &fs.PathError{Op: "stat", Path: path, Err: err} + } + return time.Unix(st.Mtim.Unix()), st.Size, st.Mode&unix.S_IFMT == unix.S_IFDIR, nil +} diff --git a/internal/sessions/discovery.go b/internal/sessions/discovery.go index 5a3f84b07..962012392 100644 --- a/internal/sessions/discovery.go +++ b/internal/sessions/discovery.go @@ -40,17 +40,17 @@ const racyDirectory = 2 * time.Second // left the Sessions page, and its parses were let go with them, until a // later poll read the folder again and parsed them anew. func readDirectory(path string) []os.DirEntry { - st, err := os.Stat(path) + mod, size, dir, err := dirStat(path) if err != nil { return unreadDirectory(path, err) } - if !st.IsDir() { + if !dir { return nil } directoryCache.Lock() old, ok := directoryCache.entries[path] directoryCache.Unlock() - if ok && old.mod.Equal(st.ModTime()) && old.size == st.Size() && old.at.Sub(old.mod) > racyDirectory { + if ok && old.mod.Equal(mod) && old.size == size && old.at.Sub(old.mod) > racyDirectory { return old.entries } at := time.Now() @@ -65,7 +65,7 @@ func readDirectory(path string) []os.DirEntry { if len(directoryCache.entries) >= 16384 { directoryCache.entries = map[string]directoryEntry{} } - directoryCache.entries[path] = directoryEntry{st.ModTime(), st.Size(), at, entries} + directoryCache.entries[path] = directoryEntry{mod, size, at, entries} directoryCache.Unlock() return entries }