version, hostinfo: recognize gokrazy as a distro

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 <bradfitz@tailscale.com>
(cherry picked from commit 58a6c9b2b8)
This commit is contained in:
Brad Fitzpatrick 2022-03-01 19:26:57 -08:00 committed by Brad Fitzpatrick
parent bfd7f9d318
commit d0698cfcec
3 changed files with 34 additions and 5 deletions

View File

@ -113,6 +113,8 @@ func osVersionLinux() string {
return fmt.Sprintf("Synology %s%s", m["productversion"], attr) return fmt.Sprintf("Synology %s%s", m["productversion"], attr)
case distro.OpenWrt: case distro.OpenWrt:
return fmt.Sprintf("OpenWrt %s%s", m["DISTRIB_RELEASE"], attr) 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) return fmt.Sprintf("Other%s", attr)
} }

View File

@ -8,6 +8,7 @@
import ( import (
"os" "os"
"runtime" "runtime"
"sync/atomic"
) )
type Distro string type Distro string
@ -22,17 +23,25 @@
Pfsense = Distro("pfsense") Pfsense = Distro("pfsense")
OPNsense = Distro("opnsense") OPNsense = Distro("opnsense")
TrueNAS = Distro("truenas") TrueNAS = Distro("truenas")
Gokrazy = Distro("gokrazy")
) )
var distroAtomic atomic.Value // of Distro
// Get returns the current distro, or the empty string if unknown. // Get returns the current distro, or the empty string if unknown.
func Get() Distro { func Get() Distro {
if runtime.GOOS == "linux" { d, ok := distroAtomic.Load().(Distro)
return linuxDistro() if ok {
return d
} }
if runtime.GOOS == "freebsd" { switch runtime.GOOS {
return freebsdDistro() case "linux":
d = linuxDistro()
case "freebsd":
d = freebsdDistro()
} }
return "" distroAtomic.Store(d) // even if empty
return d
} }
func have(file string) bool { func have(file string) bool {
@ -62,6 +71,8 @@ func linuxDistro() Distro {
return NixOS return NixOS
case have("/etc/config/uLinux.conf"): case have("/etc/config/uLinux.conf"):
return QNAP return QNAP
case haveDir("/gokrazy"):
return Gokrazy
} }
return "" return ""
} }

View File

@ -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
}