Skip to content

The project provides to control Intel CPUs power. All CPU families are supported including Sandy Bridge and later (including Haswell and Skylake family also). intel_pstate driver needed.

License

Notifications You must be signed in to change notification settings

gopowersupply/intelcpu

Repository files navigation

GitHub Go version Go Report Card code-coverage GoDoc GitHub tag (latest SemVer) GitHub last commit GitHub issues

The package to allow interaction with intel_pstate driver to drive CPU frequency and politics.

intel_pstate driver must be installed and activated. Usually, it already included in Linux kernel 4.13 for Sandy Bridge and later CPUs.

Get it from github:

go get -u https://github.com/gopowersupply/intelcpu

Documentation can be found here

Requirements

  • intel_pstate CPU Performance Scaling Driver

Examples

Simple example to change TurboBoost status:

    cpu := intelcpu.New()
    turbo, _ := cpu.GetTurbo()
    if turbo {
    	cpu.SetTurbo(false)
    } else {
    	cpu.SetTurbo(true)
    }

In real projects strongly recommended to check for driver and its status:

    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
    }

You can enable or disable some cores. Except first, of course:

    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)
    	}    	
    }

You can change CPU frequency limitation also:

    cpu := intelcpu.New()    
    cpu.SetMaxPerf(0.5) // 50% of max

Core performance and governor politics also can be changed:

    cpu := intelcpu.New()
    cores, _ := cpu.GetCores()
    
    for _, core := range cores {
    	core.SetGovernor(intelcpu.CPUGovernorPerformance)
    	core.SetPreference(intelcpu.CPUPreferencePerformance)
    }

Short way:

    cpu := intelcpu.New()
    cores, _ := cpu.GetCores()
    cores.SetGovernor(intelcpu.CPUGovernorPerformance)
    cores.SetPreference(intelcpu.CPUPreferencePerformance)

Errors handling

This package has an own error type CPUError
You can pass the package errors through your functions then detect it via 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    		
    	}
    }

And you can use errors.As(err, &intelcpu.CPUError{}) as alternative.

About

The project provides to control Intel CPUs power. All CPU families are supported including Sandy Bridge and later (including Haswell and Skylake family also). intel_pstate driver needed.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published