intelcpu
1.0.0
該軟件包允許與Intel_pstate驅動程序進行互動以驅動CPU頻率和政治。
必須安裝和激活intel_pstate驅動程序。通常,它已經包含在Linux內核4.13中,用於Sandy Bridge和後來的CPU。
從github獲取它:
go get -u https://github.com/gopowersupply/intelcpu可以在這裡找到文檔
intel_pstate CPU性能縮放驅動程序更改Turboboost狀態的簡單示例:
cpu := intelcpu . New ()
turbo , _ := cpu . GetTurbo ()
if turbo {
cpu . SetTurbo ( false )
} else {
cpu . SetTurbo ( true )
}在實際的項目中,強烈建議檢查駕駛員及其狀態:
cpu := intelcpu . New ()
if err := cpu . CheckDriver (); err != nil {
// [...] Some troubles or driver not installed
}
status , _ := cpu . GetStatus ()
switch status {
case intelcpu . PStateStatusActive :
// [...] All is ok
case intelcpu . PStateStatusPassive :
// [...] Something wrong, working partially
case intelcpu . PStateStatusOff :
// [...] Driver disabled, nothing to work
}您可以啟用或禁用某些內核。當然,除了第一:
cpu := intelcpu . New ()
cores , _ := cpu . GetCores ()
for _ , core := range cores {
// First core will return false and its status always will be online
isOfflineAvailable , _ := core . IsOfflineAvailable ()
isOnline , _ := core . IsOnline ()
fmt . Printf ( "Core %d is online: %v" , isOnline )
// If core can be offline then do it
if isOfflineAvailable {
core . SetOnline ( false )
}
}您還可以更改CPU頻率限制:
cpu := intelcpu . New ()
cpu . SetMaxPerf ( 0.5 ) // 50% of max核心績效和州長政治也可以改變:
cpu := intelcpu . New ()
cores , _ := cpu . GetCores ()
for _ , core := range cores {
core . SetGovernor ( intelcpu . CPUGovernorPerformance )
core . SetPreference ( intelcpu . CPUPreferencePerformance )
}簡短的方式:
cpu := intelcpu . New ()
cores , _ := cpu . GetCores ()
cores . SetGovernor ( intelcpu . CPUGovernorPerformance )
cores . SetPreference ( intelcpu . CPUPreferencePerformance )此軟件包有自己的錯誤類型CPUError
您可以通過函數傳遞軟件包錯誤,然後通過errors.As檢測到它。
func ExecUnexpected () error {
// [...] Here your other returns with own errors
cpu := intelcpu . New ()
_ , err := cpu . GetCore ( 20000 )
if err != nil {
return err
}
// [...] Here your other returns with own errors
}
func main () {
err := ExecUnexpected ()
if intelcpu . IsCPUError ( err ) {
// [...] to do anything
} else {
// [...] to do something other
}
}您可以使用errors.As(err, &intelcpu.CPUError{})作為替代方案。