Skip to content

Commit

Permalink
telemetry: fix loong64 build
Browse files Browse the repository at this point in the history
Signed-off-by: xhe <[email protected]>
  • Loading branch information
xhebox committed Jan 16, 2025
1 parent c39bd6e commit 7e0ca14
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 49 deletions.
49 changes: 0 additions & 49 deletions pkg/telemetry/node_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
69 changes: 69 additions & 0 deletions pkg/telemetry/node_info_generic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//go:build !loong64
// +build !loong64
// Copyright 2024 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
}
61 changes: 61 additions & 0 deletions pkg/telemetry/node_info_loong64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//go:build loong64
// +build loong64

// Copyright 2024 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
}

0 comments on commit 7e0ca14

Please sign in to comment.