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

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Golang?獲取系統(tǒng)信息的實(shí)現(xiàn)_Golang

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

本文介紹獲取系統(tǒng)信息的方法,另外給出根據(jù)不同系統(tǒng)編譯的方法。

問題提出

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

在找到第三方庫前,也想過根據(jù)不同的系統(tǒng)編譯不同的源碼文件,經(jīng)過考量,還是直接用現(xiàn)成的庫。

golang 的編譯選項(xiàng)

在進(jìn)入主題前,先了解一下編譯選項(xiàng)。C++可以直接在文件開始和結(jié)束處分別加#if 0和#endif解決,相應(yīng)的,golang 可以在.go文件定義包前添加// +build windows或// +build linux來區(qū)別在哪個(gè)系統(tǒng)編譯。
如果后面跟著注釋,會(huì)提示//go:build comment without // +build comment。

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

實(shí)際中,筆者使用的工程會(huì)調(diào)用 Linux 的動(dòng)態(tài)庫,但在編譯調(diào)試時(shí),還是以 Windows 為主,因?yàn)樯婕?web 前端的設(shè)計(jì),這樣就可以在 Windows 中不調(diào)用動(dòng)態(tài)庫,即接口函數(shù)做空實(shí)現(xiàn)。

獲取系統(tǒng)信息

gopsutil 抽象了不同系統(tǒng),提供統(tǒng)一接口,因?yàn)椴淮嬖谏鲜鰡栴},本節(jié)給出一些示例代碼,可以獲取一些必要的系統(tǒng)信息,如CPU、內(nèi)存、磁盤等。

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,在不同系統(tǒng)中使用率不一樣,因此直接計(jì)算一次
?? ?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內(nèi)的CPU信息,太短不準(zhǔn)確,也可以獲幾秒內(nèi)的,但這樣會(huì)有延時(shí),因?yàn)橐却?
?? ?cc, _ := cpu.Percent(time.Millisecond*200, false)
?? ?info.CpuUsedPercent = cc[0]

?? ?// 獲取開機(jī)時(shí)間
?? ?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)
}

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

某windows系統(tǒng)運(yùn)行結(jié)果如下:

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 服務(wù)器運(yùn)行結(jié)果:

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

欄目分類
最近更新