Class: Knj::Cpufreq

Inherits:
Object show all
Defined in:
lib/knj/cpufreq.rb

Overview

This class can manipulate the CPU behavior through “cpufreq”.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Cpufreq

Useually called through “Knj::Cpufreq.list”.



7
8
9
10
# File 'lib/knj/cpufreq.rb', line 7

def initialize(data)
  @data = data
  @allowed_govs = ["performance", "ondemand", "powersafe", "conservative"]
end

Instance Attribute Details

#dataObject (readonly)

Data that is used to identify the CPU controlled by this object.



4
5
6
# File 'lib/knj/cpufreq.rb', line 4

def data
  @data
end

Class Method Details

.listObject

Returns a list of CPUs.

Examples

list = Knj::Cpufreq.list list.each do |cpufreq|

cpufreq.governor = "performance"

end



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/knj/cpufreq.rb', line 18

def self.list
  ret = []
  cont = File.read("/proc/cpuinfo")
  
  matches = cont.scan(/processor\s*:[\s\S]+?\n\n/)
  raise "Could not detect CPUs" if !matches or matches.empty?
  
  matches.each do |cpucont|
    cpu_features = {}
    features = cpucont.scan(/(.+)\s*:\s*(.+)\s*/)
    
    features.each do |data|
      cpu_features[data[0].strip] = data[1].strip
    end
    
    ret << Knj::Cpufreq.new(cpu_features)
  end
  
  return ret
end

Instance Method Details

#governor=(newgov) ⇒ Object

Sets the governor.

Examples

cpufreq.governor = “performance”



42
43
44
45
46
47
48
49
50
# File 'lib/knj/cpufreq.rb', line 42

def governor=(newgov)
  raise "Governor not found." if @allowed_govs.index(newgov) == nil
  
  cmd = "cpufreq-set --cpu #{@data["processor"]} --governor #{newgov}"
  res = Knj::Os::shellcmd(cmd)
  if res.index("Error setting new values") != nil
    raise res.strip
  end
end