From 828fe8ca9164f07ab9d0c615498a47fa7e2a353c Mon Sep 17 00:00:00 2001 From: xhe Date: Thu, 6 Feb 2025 12:03:20 +0800 Subject: [PATCH] telemetry: fix loong64 build (#2493) * telemetry: fix loong64 build Signed-off-by: xhe * address comments Signed-off-by: xhe --------- Signed-off-by: xhe --- pkg/telemetry/node_info.go | 49 --------------------- pkg/telemetry/node_info_generic.go | 70 ++++++++++++++++++++++++++++++ pkg/telemetry/node_info_loong64.go | 61 ++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 49 deletions(-) create mode 100644 pkg/telemetry/node_info_generic.go create mode 100644 pkg/telemetry/node_info_loong64.go diff --git a/pkg/telemetry/node_info.go b/pkg/telemetry/node_info.go index 6dfb58e0f7..aa96e65513 100644 --- a/pkg/telemetry/node_info.go +++ b/pkg/telemetry/node_info.go @@ -15,12 +15,6 @@ package telemetry import ( "context" - "runtime" - - "github.com/shirou/gopsutil/cpu" - "github.com/shirou/gopsutil/host" - "github.com/shirou/gopsutil/load" - "github.com/shirou/gopsutil/mem" ) // FillNodeInfo fill HardwareInfo and Os info. @@ -37,46 +31,3 @@ func FillNodeInfo(ctx context.Context, info *NodeInfo) (err error) { return nil } - -// GetHardwareInfo get the HardwareInfo. -func GetHardwareInfo(ctx context.Context) (info HardwareInfo, err error) { - if virt, role, err := host.VirtualizationWithContext(ctx); err == nil && role == "guest" { - info.Virtualization = virt - } - - if l, err := load.AvgWithContext(ctx); err == nil { - info.Loadavg15 = float32(l.Load15) - } - - // Fill cpu info - info.Cpu.Numcpu = int32(runtime.NumCPU()) - if cpus, err := cpu.InfoWithContext(ctx); err == nil && len(cpus) > 0 { - info.Cpu.Sockets = int32(len(cpus)) - c := cpus[0] - info.Cpu.Cores = c.Cores - info.Cpu.Model = c.ModelName - info.Cpu.Mhz = float32(c.Mhz) - info.Cpu.Features = c.Flags - } - // Fill mem info - if m, err := mem.VirtualMemory(); err == nil { - info.Mem.Available = m.Available - info.Mem.Total = m.Total - } - - return -} - -// GetOSInfo get the OSInfo. -func GetOSInfo(ctx context.Context) (info OSInfo, err error) { - platform, family, version, err := host.PlatformInformationWithContext(ctx) - if err != nil { - return - } - - info.Platform = platform - info.Family = family - info.Version = version - - return -} diff --git a/pkg/telemetry/node_info_generic.go b/pkg/telemetry/node_info_generic.go new file mode 100644 index 0000000000..796ebf196b --- /dev/null +++ b/pkg/telemetry/node_info_generic.go @@ -0,0 +1,70 @@ +//go:build !loong64 +// +build !loong64 +// +// Copyright 2025 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package telemetry + +import ( + "context" + "runtime" + + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/host" + "github.com/shirou/gopsutil/load" + "github.com/shirou/gopsutil/mem" +) + +// GetHardwareInfo get the HardwareInfo. +func GetHardwareInfo(ctx context.Context) (info HardwareInfo, err error) { + if virt, role, err := host.VirtualizationWithContext(ctx); err == nil && role == "guest" { + info.Virtualization = virt + } + + if l, err := load.AvgWithContext(ctx); err == nil { + info.Loadavg15 = float32(l.Load15) + } + + // Fill cpu info + info.Cpu.Numcpu = int32(runtime.NumCPU()) + if cpus, err := cpu.InfoWithContext(ctx); err == nil && len(cpus) > 0 { + info.Cpu.Sockets = int32(len(cpus)) + c := cpus[0] + info.Cpu.Cores = c.Cores + info.Cpu.Model = c.ModelName + info.Cpu.Mhz = float32(c.Mhz) + info.Cpu.Features = c.Flags + } + // Fill mem info + if m, err := mem.VirtualMemory(); err == nil { + info.Mem.Available = m.Available + info.Mem.Total = m.Total + } + + return +} + +// GetOSInfo get the OSInfo. +func GetOSInfo(ctx context.Context) (info OSInfo, err error) { + platform, family, version, err := host.PlatformInformationWithContext(ctx) + if err != nil { + return + } + + info.Platform = platform + info.Family = family + info.Version = version + + return +} diff --git a/pkg/telemetry/node_info_loong64.go b/pkg/telemetry/node_info_loong64.go new file mode 100644 index 0000000000..1caa915fc5 --- /dev/null +++ b/pkg/telemetry/node_info_loong64.go @@ -0,0 +1,61 @@ +//go:build loong64 +// +build loong64 +// +// Copyright 2025 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// See the License for the specific language governing permissions and +// limitations under the License. + +package telemetry + +import ( + "context" + "runtime" + + "github.com/shirou/gopsutil/cpu" + "github.com/shirou/gopsutil/load" + "github.com/shirou/gopsutil/mem" +) + +// GetHardwareInfo get the HardwareInfo. +func GetHardwareInfo(ctx context.Context) (info HardwareInfo, err error) { + info.Virtualization = "" + + if l, err := load.AvgWithContext(ctx); err == nil { + info.Loadavg15 = float32(l.Load15) + } + + // Fill cpu info + info.Cpu.Numcpu = int32(runtime.NumCPU()) + if cpus, err := cpu.InfoWithContext(ctx); err == nil && len(cpus) > 0 { + info.Cpu.Sockets = int32(len(cpus)) + c := cpus[0] + info.Cpu.Cores = c.Cores + info.Cpu.Model = c.ModelName + info.Cpu.Mhz = float32(c.Mhz) + info.Cpu.Features = c.Flags + } + // Fill mem info + if m, err := mem.VirtualMemory(); err == nil { + info.Mem.Available = m.Available + info.Mem.Total = m.Total + } + + return +} + +// GetOSInfo get the OSInfo. +func GetOSInfo(ctx context.Context) (info OSInfo, err error) { + info.Platform = "Loong64" + info.Family = "unknown" + info.Version = "unknown" + return +}