Skip to content

Commit

Permalink
hostinfo: cache device model to speed up init
Browse files Browse the repository at this point in the history
This was causing a relatively consistent ~10ms of delay on Linux.

Signed-off-by: Kyle Carberry <kyle@carberry.com>
  • Loading branch information
kylecarbs authored and bradfitz committed Apr 2, 2024
1 parent ec87e21 commit 27038ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
22 changes: 16 additions & 6 deletions hostinfo/hostinfo.go
Expand Up @@ -52,7 +52,7 @@ func New() *tailcfg.Hostinfo {
GoArchVar: lazyGoArchVar.Get(),
GoVersion: runtime.Version(),
Machine: condCall(unameMachine),
DeviceModel: deviceModel(),
DeviceModel: deviceModelCached(),
Cloud: string(cloudenv.Get()),
NoLogsNoSupport: envknob.NoLogsNoSupport(),
AllowsUpdate: envknob.AllowsRemoteUpdate(),
Expand All @@ -68,6 +68,7 @@ var (
distroVersion func() string
distroCodeName func() string
unameMachine func() string
deviceModel func() string
)

func condCall[T any](fn func() T) T {
Expand Down Expand Up @@ -176,6 +177,20 @@ var (
// SetDeviceModel sets the device model for use in Hostinfo updates.
func SetDeviceModel(model string) { deviceModelAtomic.Store(model) }

func deviceModelCached() string {
if v, _ := deviceModelAtomic.Load().(string); v != "" {
return v
}
if deviceModel == nil {
return ""
}
v := deviceModel()
if v != "" {
deviceModelAtomic.Store(v)
}
return v
}

// SetOSVersion sets the OS version.
func SetOSVersion(v string) { osVersionAtomic.Store(v) }

Expand All @@ -193,11 +208,6 @@ func SetPackage(v string) { packagingType.Store(v) }
// and "k8s-operator".
func SetApp(v string) { appType.Store(v) }

func deviceModel() string {
s, _ := deviceModelAtomic.Load().(string)
return s
}

// FirewallMode returns the firewall mode for the app.
// It is empty if unset.
func FirewallMode() string {
Expand Down
6 changes: 2 additions & 4 deletions hostinfo/hostinfo_linux.go
Expand Up @@ -22,9 +22,7 @@ func init() {
distroName = distroNameLinux
distroVersion = distroVersionLinux
distroCodeName = distroCodeNameLinux
if v := linuxDeviceModel(); v != "" {
SetDeviceModel(v)
}
deviceModel = deviceModelLinux
}

var (
Expand All @@ -50,7 +48,7 @@ func distroCodeNameLinux() string {
return lazyVersionMeta.Get().DistroCodeName
}

func linuxDeviceModel() string {
func deviceModelLinux() string {
for _, path := range []string{
// First try the Synology-specific location.
// Example: "DS916+-j"
Expand Down

0 comments on commit 27038ee

Please sign in to comment.