From 9f1da11792da66a0ce6c267c1c62a0bd609f8c80 Mon Sep 17 00:00:00 2001 From: wwqgtxx Date: Wed, 23 Jul 2025 18:02:46 +0800 Subject: [PATCH] chore: use the compile-time GOAMD64 flag in the updater --- component/updater/cpu_amd64.go | 4 ++++ component/updater/cpu_amd64.s | 22 ++++++++++++++++++++++ component/updater/cpu_others.go | 8 ++++++++ component/updater/cpu_test.go | 20 ++++++++++++++++++++ component/updater/update_core.go | 4 +--- go.mod | 2 +- 6 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 component/updater/cpu_amd64.go create mode 100644 component/updater/cpu_amd64.s create mode 100644 component/updater/cpu_others.go create mode 100644 component/updater/cpu_test.go diff --git a/component/updater/cpu_amd64.go b/component/updater/cpu_amd64.go new file mode 100644 index 00000000..b3b29bbe --- /dev/null +++ b/component/updater/cpu_amd64.go @@ -0,0 +1,4 @@ +package updater + +// getGOAMD64level is implemented in cpu_amd64.s. Returns number in [1,4]. +func getGOAMD64level() int32 diff --git a/component/updater/cpu_amd64.s b/component/updater/cpu_amd64.s new file mode 100644 index 00000000..bfe068a8 --- /dev/null +++ b/component/updater/cpu_amd64.s @@ -0,0 +1,22 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +#include "textflag.h" + +// func getGOAMD64level() int32 +TEXT ·getGOAMD64level(SB),NOSPLIT,$0-4 +#ifdef GOAMD64_v4 + MOVL $4, ret+0(FP) +#else +#ifdef GOAMD64_v3 + MOVL $3, ret+0(FP) +#else +#ifdef GOAMD64_v2 + MOVL $2, ret+0(FP) +#else + MOVL $1, ret+0(FP) +#endif +#endif +#endif + RET diff --git a/component/updater/cpu_others.go b/component/updater/cpu_others.go new file mode 100644 index 00000000..7abf61c5 --- /dev/null +++ b/component/updater/cpu_others.go @@ -0,0 +1,8 @@ +//go:build !amd64 + +package updater + +// getGOAMD64level is always return 0 when not in amd64 platfrom. +func getGOAMD64level() int32 { + return 0 +} diff --git a/component/updater/cpu_test.go b/component/updater/cpu_test.go new file mode 100644 index 00000000..b7359e0a --- /dev/null +++ b/component/updater/cpu_test.go @@ -0,0 +1,20 @@ +package updater + +import ( + "fmt" + "runtime" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestGOAMD64level(t *testing.T) { + level := getGOAMD64level() + fmt.Printf("GOAMD64=%d\n", level) + if runtime.GOARCH == "amd64" { + assert.True(t, level > 0) + assert.True(t, level <= 4) + } else { + assert.Equal(t, level, int32(0)) + } +} diff --git a/component/updater/update_core.go b/component/updater/update_core.go index 2aab7833..97ae0d50 100644 --- a/component/updater/update_core.go +++ b/component/updater/update_core.go @@ -18,8 +18,6 @@ import ( mihomoHttp "github.com/metacubex/mihomo/component/http" C "github.com/metacubex/mihomo/constant" "github.com/metacubex/mihomo/log" - - "github.com/klauspost/cpuid/v2" ) // modify from https://github.com/AdguardTeam/AdGuardHome/blob/595484e0b3fb4c457f9bb727a6b94faa78a66c5f/internal/updater/updater.go @@ -48,7 +46,7 @@ var ( ) func init() { - if runtime.GOARCH == "amd64" && cpuid.CPU.X64Level() < 3 { + if runtime.GOARCH == "amd64" && getGOAMD64level() < 3 { amd64Compatible = "-compatible" } if !strings.HasPrefix(C.Version, "alpha") { diff --git a/go.mod b/go.mod index 9df55093..24b656c4 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,6 @@ require ( github.com/gofrs/uuid/v5 v5.3.2 github.com/insomniacslk/dhcp v0.0.0-20250109001534-8abf58130905 github.com/klauspost/compress v1.17.9 // lastest version compatible with golang1.20 - github.com/klauspost/cpuid/v2 v2.2.9 // lastest version compatible with golang1.20 github.com/lunixbochs/struc v0.0.0-20200707160740-784aaebc1d40 github.com/mdlayher/netlink v1.7.2 github.com/metacubex/amneziawg-go v0.0.0-20240922133038-fdf3a4d5a4ab @@ -86,6 +85,7 @@ require ( github.com/google/pprof v0.0.0-20210407192527-94a9f03dee38 // indirect github.com/hashicorp/yamux v0.1.2 // indirect github.com/josharian/native v1.1.0 // indirect + github.com/klauspost/cpuid/v2 v2.2.9 // indirect github.com/kr/text v0.2.0 // indirect github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect github.com/mailru/easyjson v0.7.7 // indirect