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{})作为替代方案。