日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Golang?獲取系統信息的實現_Golang

作者:李遲 ? 更新時間: 2022-07-30 編程語言

本文介紹獲取系統信息的方法,另外給出根據不同系統編譯的方法。

問題提出

由于多年來接觸了不同系統的兼容工程,對使用宏區分不同的代碼一直有一種莫名的感覺。像 Linux 內核中就有很多這樣的代碼,coreboot 中有,nRF52 SDK中也有。在實現的工程庫也要往這方向考慮,比如線程庫和socket庫。當接觸 golang 后,因其跨平臺,編碼快,所以在工作中也使用了。但并不是所有代碼都是跨平臺,像 syscall這樣的包,就無法做到。最近的工程中需要獲取系統信息,但無法只使用 golang 官方的接口達到目的,最終找到了第三方庫github.com/shirou/gopsutil

在找到第三方庫前,也想過根據不同的系統編譯不同的源碼文件,經過考量,還是直接用現成的庫。

golang 的編譯選項

在進入主題前,先了解一下編譯選項。C++可以直接在文件開始和結束處分別加#if 0和#endif解決,相應的,golang 可以在.go文件定義包前添加// +build windows或// +build linux來區別在哪個系統編譯。
如果后面跟著注釋,會提示//go:build comment without // +build comment

另一種方法,是直接使用源碼文件名稱來區分。比如ccall_linux.go和ccall_windows.go分別在 Linux 系統和 Windows 系統下編譯,這種方法非常直觀。golang 中帶_test的文件是測試用例,這其中的設計思想是一致的。

實際中,筆者使用的工程會調用 Linux 的動態庫,但在編譯調試時,還是以 Windows 為主,因為涉及 web 前端的設計,這樣就可以在 Windows 中不調用動態庫,即接口函數做空實現。

獲取系統信息

gopsutil 抽象了不同系統,提供統一接口,因為不存在上述問題,本節給出一些示例代碼,可以獲取一些必要的系統信息,如CPU、內存、磁盤等。

package gin

import (
?? ?"fmt"
?? ?"os"
?? ?"runtime"
?? ?"time"

?? ?"github.com/shirou/gopsutil/cpu"
?? ?"github.com/shirou/gopsutil/disk"
?? ?"github.com/shirou/gopsutil/host"
?? ?"github.com/shirou/gopsutil/net"
?? ?"github.com/shirou/gopsutil/v3/mem"
)

type LSysInfo struct {
?? ?MemAll ? ? ? ? uint64
?? ?MemFree ? ? ? ?uint64
?? ?MemUsed ? ? ? ?uint64
?? ?MemUsedPercent float64
?? ?Days ? ? ? ? ? int64
?? ?Hours ? ? ? ? ?int64
?? ?Minutes ? ? ? ?int64
?? ?Seconds ? ? ? ?int64

?? ?CpuUsedPercent float64
?? ?OS ? ? ? ? ? ? string
?? ?Arch ? ? ? ? ? string
?? ?CpuCores ? ? ? int
}

func GetSysInfo() (info LSysInfo) {
?? ?unit := uint64(1024 * 1024) // MB

?? ?v, _ := mem.VirtualMemory()

?? ?info.MemAll = v.Total
?? ?info.MemFree = v.Free
?? ?info.MemUsed = info.MemAll - info.MemFree
?? ?// 注:使用SwapMemory或VirtualMemory,在不同系統中使用率不一樣,因此直接計算一次
?? ?info.MemUsedPercent = float64(info.MemUsed) / float64(info.MemAll) * 100.0 // v.UsedPercent
?? ?info.MemAll /= unit
?? ?info.MemUsed /= unit
?? ?info.MemFree /= unit

?? ?info.OS = runtime.GOOS
?? ?info.Arch = runtime.GOARCH
?? ?info.CpuCores = runtime.GOMAXPROCS(0)

?? ?// 獲取200ms內的CPU信息,太短不準確,也可以獲幾秒內的,但這樣會有延時,因為要等待
?? ?cc, _ := cpu.Percent(time.Millisecond*200, false)
?? ?info.CpuUsedPercent = cc[0]

?? ?// 獲取開機時間
?? ?boottime, _ := host.BootTime()
?? ?ntime := time.Now().Unix()
?? ?btime := time.Unix(int64(boottime), 0).Unix()
?? ?deltatime := ntime - btime

?? ?info.Seconds = int64(deltatime)
?? ?info.Minutes = info.Seconds / 60
?? ?info.Seconds -= info.Minutes * 60
?? ?info.Hours = info.Minutes / 60
?? ?info.Minutes -= info.Hours * 60
?? ?info.Days = info.Hours / 24
?? ?info.Hours -= info.Days * 24

?? ?fmt.Printf("info: %#v\n", info)

?? ?infoTest()
?? ?os.Exit(0)
?? ?return
}

func infoTest() {
?? ?c, _ := cpu.Info()
?? ?cc, _ := cpu.Percent(time.Second, false) // 1秒
?? ?d, _ := disk.Usage("/")
?? ?n, _ := host.Info()
?? ?nv, _ := net.IOCounters(true)
?? ?physicalCnt, _ := cpu.Counts(false)
?? ?logicalCnt, _ := cpu.Counts(true)
?? ?if len(c) > 1 {
?? ??? ?for _, sub_cpu := range c {
?? ??? ??? ?modelname := sub_cpu.ModelName
?? ??? ??? ?cores := sub_cpu.Cores
?? ??? ??? ?fmt.Printf("CPUs: %v ? %v cores \n", modelname, cores)
?? ??? ?}
?? ?} else {
?? ??? ?sub_cpu := c[0]
?? ??? ?modelname := sub_cpu.ModelName
?? ??? ?cores := sub_cpu.Cores
?? ??? ?fmt.Printf("CPU: %v ? %v cores \n", modelname, cores)
?? ?}
?? ?fmt.Printf("physical count:%d logical count:%d\n", physicalCnt, logicalCnt)
?? ?fmt.Printf("CPU Used: used %f%%\n", cc[0])
?? ?fmt.Printf("HD: %v GB Free: %v GB Usage:%f%%\n", d.Total/1024/1024/1024, d.Free/1024/1024/1024, d.UsedPercent)
?? ?fmt.Printf("OS: %v(%v) %v\n", n.Platform, n.PlatformFamily, n.PlatformVersion)
?? ?fmt.Printf("Hostname: %v\n", n.Hostname)
?? ?fmt.Printf("Network: %v bytes / %v bytes\n", nv[0].BytesRecv, nv[0].BytesSent)
}

需要注意的,計算內存的使用率,是根據已獲取的已用內存除以總內存,而不是直接由gopsutil獲取。計算CPU使用率,需要指定時間間隔,如果秒級別,用戶會感覺卡頓,文中代碼使用 200 毫秒,經測試,有時獲取的值為0。至于運行時間,則通過時間戳直接計算出天數。

某windows系統運行結果如下:

info: gin.LSysInfo{MemAll:0x2ec6, MemFree:0x11a5, MemUsed:0x1d21, MemUsedPercent:62.27692697126946, Days:0, Hours:9, Minutes:26, Seconds:6, CpuUsedPercent:5.882352941068881, OS:"windows", Arch:"amd64", CpuCores:4}
CPU: Intel(R) Core(TM) i5-4210M CPU @ 2.60GHz ? 4 cores
physical count:2 logical count:4
CPU Used: used 8.593750%
HD: 330 GB Free: 32 GB Usage:90.242198%
OS: Microsoft Windows 7 Ultimate Service Pack 1(Standalone Workstation) 6.1.7601 Build 7601
Hostname: SKY-20210126BVC
Network: 0 bytes / 0 bytes

某 linux 服務器運行結果:

info: gin.LSysInfo{MemAll:0xf84b, MemFree:0x527, MemUsed:0xf323, MemUsedPercent:97.92430801663596, Days:0, Hours:1, Minutes:6, Seconds:38, CpuUsedPercent:0.25062656021506197, OS:"linux", Arch:"amd64", CpuCores:20}
CPUs: Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz ? 1 cores?
CPUs: Intel(R) Xeon(R) CPU E5-2640 v4 @ 2.40GHz ? 1 cores?
...
physical count:10 logical count:20
CPU Used: used 0.702459%
HD: 49 GB Free: 38 GB Usage:16.708842%
OS: centos(rhel) 7.6.000
Hostname: localhost.localdomain
Network: 1915935 bytes / 224926648 bytes

原文鏈接:https://juejin.cn/post/7104069654733144078

欄目分類
最近更新