diff --git a/internal/pii/arch.go b/internal/pii/arch.go new file mode 100644 index 000000000..32b6dbf1d --- /dev/null +++ b/internal/pii/arch.go @@ -0,0 +1,34 @@ +package pii + +import ( + "runtime" + + "golang.org/x/sys/cpu" +) + +// osArch is the system magpie was built for. +var osArch = runtime.GOOS + "-" + runtime.GOARCH + +// target is this system's name in serveSHA: the one magpie was built +// for, but Apple silicon's where an Intel magpie runs under Rosetta. +// Rosetta before macOS 15 has no AVX, so the Intel pf-serve ended with +// SIGILL on its first instruction of it, on Metal and on the CPU alike +// (echo_yip on X); Apple silicon's runs there natively, on Metal. +func target() string { + if osArch == "darwin-amd64" && translated() { + return "darwin-arm64" + } + return osArch +} + +// fullX86 says the CPU has what pf-serve is built for on x86 (build.sh's +// x86): AVX, AVX2, FMA and BMI2, with F16C, which every CPU with AVX2 +// has. Without them pf-serve ends with SIGILL (a Celeron, a Pentium, a +// Mac before Haswell), and pf-serve-base, built for SSE4.2 alone, is run +// instead. Off x86 it is true: no other system has a pf-serve-base. +var fullX86 = func() bool { + if runtime.GOARCH != "amd64" { + return true + } + return cpu.X86.HasAVX && cpu.X86.HasAVX2 && cpu.X86.HasFMA && cpu.X86.HasBMI2 +} diff --git a/internal/pii/arch_test.go b/internal/pii/arch_test.go new file mode 100644 index 000000000..56269f01c --- /dev/null +++ b/internal/pii/arch_test.go @@ -0,0 +1,148 @@ +package pii + +import ( + "archive/zip" + "bytes" + "net/http/httptest" + "os" + "path/filepath" + "reflect" + "testing" +) + +// noFullX86 holds fullX86 at have for the test. +func noFullX86(t *testing.T, have bool) { + was := fullX86 + fullX86 = func() bool { return have } + t.Cleanup(func() { fullX86 = was }) +} + +// withBase puts the stand-in as pf-serve-base too. +func withBase(t *testing.T) { + t.Helper() + b, err := os.ReadFile(exe("pf-serve")) + if err != nil { + t.Fatal(err) + } + if err := os.WriteFile(exe("pf-serve-base"), b, 0o755); err != nil { + t.Fatal(err) + } +} + +// A CPU without AVX2, FMA or BMI2 ended pf-serve with SIGILL on the GPU +// and on the CPU (echo_yip on X): pf-serve-base runs there, on the CPU +// alone, since pf-serve-vulkan is built like pf-serve. +func TestBaseWhereTheCPULacksAVX2(t *testing.T) { + log := fakeInstall(t, "ok", true) + withBase(t) + noFullX86(t, false) + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-base CPU"}) { + t.Fatalf("starts = %q", starts) + } +} + +// A CPU with all of them never runs pf-serve-base; and a pf-serve from +// before pf-serve-base was built is run as it was. +func TestBaseOnlyWhereNeeded(t *testing.T) { + t.Run("full CPU", func(t *testing.T) { + log := fakeInstall(t, "novulkan", true) + withBase(t) + noFullX86(t, true) + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-vulkan MTL0", "start pf-serve CPU"}) { + t.Fatalf("starts = %q", starts) + } + }) + t.Run("no base on disk", func(t *testing.T) { + log := fakeInstall(t, "novulkan", true) + noFullX86(t, false) + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + if starts := fakeLines(t, log, "start"); !reflect.DeepEqual(starts, []string{"start pf-serve-vulkan MTL0", "start pf-serve CPU"}) { + t.Fatalf("starts = %q", starts) + } + }) +} + +// rosettaInstall is an Intel magpie's pf-serve of serveTag (pf-v9), as +// fetched before magpie knew it ran under Rosetta, and from then on under +// Rosetta, Apple silicon's served at want's paths. +func rosettaInstall(t *testing.T, want map[string][]byte) string { + t.Helper() + wasArch, wasTr := osArch, translated + wasTag, wasOld, wasBase, wasServe := serveTag, oldTags, serveBase, serveSHA + t.Cleanup(func() { + upgrades.Wait() + osArch, translated = wasArch, wasTr + serveTag, oldTags, serveBase, serveSHA = wasTag, wasOld, wasBase, wasServe + }) + osArch, translated = "darwin-amd64", func() bool { return false } + serveTag, oldTags = "pf-v9", nil + log := fakeInstall(t, "ok", false) + translated = func() bool { return true } + if target() != "darwin-arm64" { + t.Fatalf("target = %s under Rosetta", target()) + } + s := httptest.NewServer(&files{m: want}) + t.Cleanup(s.Close) + zipSum := sum([]byte("nothing")) + if z, ok := want["/rel/pf-v9/pf-serve-darwin-arm64.zip"]; ok { + zipSum = sum(z) + } + serveBase, serveSHA = s.URL+"/rel/", map[string]string{"darwin-arm64": zipSum, "darwin-amd64": "x"} + return log +} + +// Under Rosetta, Apple silicon's pf-serve is fetched before the first +// start, run, and the Intel one deleted. +func TestRosettaRunsAppleSiliconsServe(t *testing.T) { + self, err := os.Executable() + if err != nil { + t.Fatal(err) + } + b, err := os.ReadFile(self) + if err != nil { + t.Fatal(err) + } + var zb bytes.Buffer + zw := zip.NewWriter(&zb) + w, _ := zw.Create(filepath.Base(exeIn("", "pf-serve"))) + w.Write(b) + zw.Close() + log := rosettaInstall(t, map[string][]byte{"/rel/pf-v9/pf-serve-darwin-arm64.zip": zb.Bytes()}) + intel := filepath.Join(dir(), "pf-v9-darwin-amd64") + if !Installed() { + t.Fatal("the Intel pf-serve wasn't taken for installed") + } + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + if got := fakeLines(t, log, "path "); !reflect.DeepEqual(got, []string{"path pf-v9-darwin-arm64"}) { + t.Fatalf("started from %q", got) + } + upgrades.Wait() + if _, err := os.Stat(intel); !os.IsNotExist(err) { + t.Fatalf("the Intel pf-serve was left: %v", err) + } +} + +// One that can't be fetched leaves the Intel one to run. +func TestRosettaKeepsIntelsWhereNoneIsFetched(t *testing.T) { + log := rosettaInstall(t, map[string][]byte{}) + if _, err := Find([]string{"hi Alice"}); err != nil { + t.Fatal(err) + } + upgrades.Wait() + if got := fakeLines(t, log, "path "); !reflect.DeepEqual(got, []string{"path pf-v9-darwin-amd64"}) { + t.Fatalf("started from %q", got) + } + if _, err := os.Stat(exeIn(filepath.Join(dir(), "pf-v9-darwin-amd64"), "pf-serve")); err != nil { + t.Fatalf("the Intel pf-serve was deleted: %v", err) + } +} diff --git a/internal/pii/fetch.go b/internal/pii/fetch.go index 68916e78d..999ac9853 100644 --- a/internal/pii/fetch.go +++ b/internal/pii/fetch.go @@ -169,10 +169,16 @@ func upgrade() { removeOld() } -// removeOld deletes oldTags' pf-serve. +// removeOld deletes oldTags' pf-serve, and serveTag's for a system +// other than target (an Intel one under Rosetta). func removeOld() { for _, tag := range oldTags { - os.RemoveAll(tagDir(tag)) + for _, d := range tagDirs(tag) { + os.RemoveAll(d) + } + } + for _, d := range tagDirs(serveTag)[1:] { + os.RemoveAll(d) } } diff --git a/internal/pii/pii.go b/internal/pii/pii.go index 211242019..1cfd37063 100644 --- a/internal/pii/pii.go +++ b/internal/pii/pii.go @@ -52,21 +52,18 @@ var ( // oldTags are the tags pinned before, newest first: one of theirs on disk // is run until serveTag's is downloaded (upgrade). var ( - serveTag = "pf-v2" - oldTags = []string{"pf-v1"} + serveTag = "pf-v3" + oldTags = []string{"pf-v2", "pf-v1"} serveBase = "https://github.com/yetone/magpie/releases/download/" serveSHA = map[string]string{ - "darwin-amd64": "4b68c9f06b14b909ec35e491ba58fcb06df500a5a0985465dd1115f9e7a5d753", - "darwin-arm64": "13ea258cc139dd0fc024f8eab70ee3566aa12ce6b5ab71f890b68bc8507159d3", - "linux-amd64": "ff0af5cd1a3d350e7c93ca45c0b5285088c9666340826e87fbb14e5dcb23f772", - "linux-arm64": "05970f7c5ba202e0e577cb22e0d12f009789c321583574a1ae1196d9522f7181", - "windows-amd64": "f4993000cc09500c907d3dd7042d783fa0b9310f272bc0bdf323ba9d012556f0", + "darwin-amd64": "5d9277d6852b7e3dcc30d102f43c21bf54a5c3199e0f9f998d965ec7a30ea6d7", + "darwin-arm64": "b45951751bb26633a5ca3827fbf3718f10dfd5c313da6b4f6ba786755353cb4b", + "linux-amd64": "b2b231209d8806648ec8dfe2d7b1a06c4cc273d1de4d50325e7a1638e63c7cc4", + "linux-arm64": "9812356fcb311c351c094aa9a587da3d54166382699b59e9f1c92ab08abae792", + "windows-amd64": "8fd8f7295743197311d13ed34aa9fb7be891f4e2586dfda6c4a3335e6cef52f3", } ) -// target is this system's name in serveSHA. -func target() string { return runtime.GOOS + "-" + runtime.GOARCH } - // Supported says pf-serve is built for this system. func Supported() bool { return serveSHA[target()] != "" } @@ -88,6 +85,17 @@ func serveDir() string { return tagDir(serveTag) } func tagDir(tag string) string { return filepath.Join(dir(), tag+"-"+target()) } +// tagDirs are where tag's pf-serve may be on disk: tagDir, and under +// Rosetta also the Intel one's, which magpie fetched there before it +// fetched Apple silicon's (target). +func tagDirs(tag string) []string { + ds := []string{tagDir(tag)} + if osArch != target() { + ds = append(ds, filepath.Join(dir(), tag+"-"+osArch)) + } + return ds +} + // exe is a program of pf-serve's: serveTag's, or, until that is // downloaded, the newest of oldTags' on disk. func exe(name string) string { return exeIn(binDir(), name) } @@ -101,8 +109,10 @@ func exeIn(d, name string) string { func binDir() string { for _, tag := range append([]string{serveTag}, oldTags...) { - if _, err := os.Stat(exeIn(tagDir(tag), "pf-serve")); err == nil { - return tagDir(tag) + for _, d := range tagDirs(tag) { + if _, err := os.Stat(exeIn(d, "pf-serve")); err == nil { + return d + } } } return serveDir() diff --git a/internal/pii/rosetta_darwin.go b/internal/pii/rosetta_darwin.go new file mode 100644 index 000000000..ae96efa6e --- /dev/null +++ b/internal/pii/rosetta_darwin.go @@ -0,0 +1,14 @@ +package pii + +import ( + "sync" + + "golang.org/x/sys/unix" +) + +// translated says magpie runs under Rosetta: an Intel build on Apple +// silicon. +var translated = sync.OnceValue(func() bool { + v, err := unix.SysctlUint32("sysctl.proc_translated") + return err == nil && v == 1 +}) diff --git a/internal/pii/rosetta_other.go b/internal/pii/rosetta_other.go new file mode 100644 index 000000000..9de6b16bd --- /dev/null +++ b/internal/pii/rosetta_other.go @@ -0,0 +1,6 @@ +//go:build !darwin + +package pii + +// translated says magpie runs under Rosetta, which only macOS has. +var translated = func() bool { return false } diff --git a/internal/pii/serve.go b/internal/pii/serve.go index 929f31dae..24450ad4f 100644 --- a/internal/pii/serve.go +++ b/internal/pii/serve.go @@ -3,6 +3,7 @@ package pii import ( "bufio" "bytes" + "context" "encoding/binary" "encoding/json" "errors" @@ -165,7 +166,15 @@ type candidate struct { // which runs on Metal; then pf-serve on the CPU, which runs anywhere. // macOS's was tried on Metal alone: one whose load hung there, or ended, // was tried again on Metal by every request (lazyeo, an M2). +// A CPU without what pf-serve is built for runs pf-serve-base, on the +// CPU alone: pf-serve-vulkan is built like pf-serve, and would end the +// same way. func candidates(cpu bool) []candidate { + if !fullX86() { + if _, err := os.Stat(exe("pf-serve-base")); err == nil { + return []candidate{{exe("pf-serve-base"), true}} + } + } var out []candidate if !cpu { if _, err := os.Stat(exe("pf-serve-vulkan")); err == nil { @@ -183,6 +192,21 @@ func start(cpu bool, stop <-chan struct{}) (*helper, error) { if !Installed() { return nil, ErrNotInstalled } + if !strings.HasSuffix(binDir(), "-"+target()) { + // only the Intel pf-serve under Rosetta, which may not start at + // all: Apple silicon's is fetched first, and one that can't be + // leaves the Intel one to try + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute) + go func() { + select { + case <-stop: + cancel() + case <-ctx.Done(): + } + }() + ensureServe(ctx) + cancel() + } upgrades.Add(1) go func() { defer upgrades.Done()