agent: isDir, isFile and Taken make no os.FileInfo on unix

Detection asks isDir, isFile and Taken of about a thousand paths on each
of the window's 15s /api/drift asks. os.Stat made an os.FileInfo on the
heap for each, a fifth of what forty asks allocated with real accounts.
statKind calls syscall.Stat into a Stat_t on the stack (retrying EINTR,
following links as os.Stat does); only the path made a C string is
left. Windows keeps os.Stat (statkind_other.go).

Run:
- TestStatKindAnswersWithoutAFileInfo: folder, file, gone, a path under
  a file, "" and a link to a folder answer as os.Stat did; two looks
  take 2 allocations. Broken by hand (statKind through os.Stat): red, 4.
- go test ./internal/agent on mote; go vet ./internal/agent for darwin,
  windows and linux.
- Real accounts (sandbox HOME on mote, 40 /api/drift asks), paired:
  os.Stat's allocations 4.6 and 4.1 MB before (t24), 1.0 and 1.5 MB
  after (t25); totals 25.0 and 22.6 MB before, 18.4 and 22.0 MB after.
This commit is contained in:
yetone
2026-11-10 06:00:10 +08:00
parent b93dd12441
commit e3ab7f3bf7
5 changed files with 87 additions and 4 deletions
+2 -2
View File
@@ -494,8 +494,8 @@ var leftover = map[string]bool{"logs": true, "log": true, "debug": true, "diagno
// p, or one it is in, would be: nothing can be written under it.
func Taken(p string) bool {
for d := filepath.Clean(p); ; {
if st, err := os.Stat(d); err == nil {
return !st.IsDir()
if dir, ok := statKind(d); ok {
return !dir
}
up := filepath.Dir(d)
if up == d {
+2 -2
View File
@@ -518,8 +518,8 @@ func exe(name string) string {
return name
}
func isFile(p string) bool { st, err := os.Stat(p); return err == nil && !st.IsDir() }
func isDir(p string) bool { st, err := os.Stat(p); return err == nil && st.IsDir() }
func isFile(p string) bool { dir, ok := statKind(p); return ok && !dir }
func isDir(p string) bool { dir, ok := statKind(p); return ok && dir }
// ---- versions --------------------------------------------------------------
+14
View File
@@ -0,0 +1,14 @@
//go:build !unix
package agent
import "os"
// statKind is what is at p, following links (statkind_unix.go).
func statKind(p string) (dir, ok bool) {
st, err := os.Stat(p)
if err != nil {
return false, false
}
return st.IsDir(), true
}
+46
View File
@@ -0,0 +1,46 @@
package agent
import (
"os"
"path/filepath"
"runtime"
"testing"
)
// isDir, isFile and Taken answer as os.Stat would, and on unix make no
// os.FileInfo: detection asks them of some thousand paths a drift ask.
func TestStatKindAnswersWithoutAFileInfo(t *testing.T) {
d := t.TempDir()
f := filepath.Join(d, "f")
if err := os.WriteFile(f, nil, 0o644); err != nil {
t.Fatal(err)
}
link := filepath.Join(d, "to-dir")
linked := os.Symlink(d, link) == nil
gone := filepath.Join(d, "gone")
for _, c := range []struct {
p string
dir, file bool
taken bool
}{
{d, true, false, false},
{f, false, true, true},
{gone, false, false, false},
{filepath.Join(f, "under"), false, false, true},
{"", false, false, false},
} {
if isDir(c.p) != c.dir || isFile(c.p) != c.file || Taken(c.p) != c.taken {
t.Errorf("%q: isDir %v isFile %v Taken %v", c.p, isDir(c.p), isFile(c.p), Taken(c.p))
}
}
if linked && !isDir(link) {
t.Error("a link to a folder is a folder, as os.Stat follows it")
}
if runtime.GOOS == "windows" {
return
}
// the path made a C string is all there is to it
if n := testing.AllocsPerRun(50, func() { isDir(d); isFile(f) }); n > 2 {
t.Fatalf("%v allocations for two looks", n)
}
}
+23
View File
@@ -0,0 +1,23 @@
//go:build unix
package agent
import "syscall"
// statKind is what is at p, following links, as os.Stat's IsDir would say,
// without the os.FileInfo os.Stat makes on the heap: detection asks it of
// some thousand paths on each of the window's 15s drift asks, and the
// FileInfos were a fifth of what forty asks allocated with real accounts.
func statKind(p string) (dir, ok bool) {
var st syscall.Stat_t
for {
err := syscall.Stat(p, &st)
if err == syscall.EINTR {
continue
}
if err != nil {
return false, false
}
return uint32(st.Mode)&syscall.S_IFMT == syscall.S_IFDIR, true
}
}