From 58a6c9b2b847934ff4e80203fbec6ba880a42227 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 1 Mar 2022 19:26:57 -0800 Subject: [PATCH] version, hostinfo: recognize gokrazy as a distro MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now: /tmp/breakglass3929186798 # /user/tailscale debug hostinfo { "IPNVersion": "1.23.0-date.20220107", "OS": "linux", "OSVersion": "Gokrazy; kernel=5.16.11", "DeviceModel": "Raspberry Pi 4 Model B Rev 1.2", "Hostname": "gokrazy", "GoArch": "arm64" } Also, cache the distro lookup. It doesn't change while the program is running: name old time/op new time/op delta Get-6 5.21µs ± 5% 0.00µs ± 3% -99.91% (p=0.008 n=5+5) name old alloc/op new alloc/op delta Get-6 792B ± 0% 0B -100.00% (p=0.008 n=5+5) name old allocs/op new allocs/op delta Get-6 8.00 ± 0% 0.00 -100.00% (p=0.008 n=5+5) Updates #1866 Change-Id: Ifb9a63b94287010d3f4c8bfeb6b78119e8a9b203 Signed-off-by: Brad Fitzpatrick --- hostinfo/hostinfo_linux.go | 2 ++ version/distro/distro.go | 21 ++++++++++++++++----- version/distro/distro_test.go | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 5 deletions(-) create mode 100644 version/distro/distro_test.go diff --git a/hostinfo/hostinfo_linux.go b/hostinfo/hostinfo_linux.go index e2a878be6..90ceece90 100644 --- a/hostinfo/hostinfo_linux.go +++ b/hostinfo/hostinfo_linux.go @@ -113,6 +113,8 @@ func osVersionLinux() string { return fmt.Sprintf("Synology %s%s", m["productversion"], attr) case distro.OpenWrt: return fmt.Sprintf("OpenWrt %s%s", m["DISTRIB_RELEASE"], attr) + case distro.Gokrazy: + return fmt.Sprintf("Gokrazy%s", attr) } return fmt.Sprintf("Other%s", attr) } diff --git a/version/distro/distro.go b/version/distro/distro.go index 67c455890..4107b8a77 100644 --- a/version/distro/distro.go +++ b/version/distro/distro.go @@ -8,6 +8,7 @@ package distro import ( "os" "runtime" + "sync/atomic" ) type Distro string @@ -22,17 +23,25 @@ const ( Pfsense = Distro("pfsense") OPNsense = Distro("opnsense") TrueNAS = Distro("truenas") + Gokrazy = Distro("gokrazy") ) +var distroAtomic atomic.Value // of Distro + // Get returns the current distro, or the empty string if unknown. func Get() Distro { - if runtime.GOOS == "linux" { - return linuxDistro() + d, ok := distroAtomic.Load().(Distro) + if ok { + return d } - if runtime.GOOS == "freebsd" { - return freebsdDistro() + switch runtime.GOOS { + case "linux": + d = linuxDistro() + case "freebsd": + d = freebsdDistro() } - return "" + distroAtomic.Store(d) // even if empty + return d } func have(file string) bool { @@ -62,6 +71,8 @@ func linuxDistro() Distro { return NixOS case have("/etc/config/uLinux.conf"): return QNAP + case haveDir("/gokrazy"): + return Gokrazy } return "" } diff --git a/version/distro/distro_test.go b/version/distro/distro_test.go new file mode 100644 index 000000000..bc4e8c5b4 --- /dev/null +++ b/version/distro/distro_test.go @@ -0,0 +1,16 @@ +// Copyright (c) 2022 Tailscale Inc & AUTHORS All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package distro + +import "testing" + +func BenchmarkGet(b *testing.B) { + b.ReportAllocs() + var d Distro + for i := 0; i < b.N; i++ { + d = Get() + } + _ = d +}