Sha256: 4b4f8bf533f7c22ffcf2f3caa15dd2eb7fdf805684af58e148a44fbe38d27570

Contents?: true

Size: 1.32 KB

Versions: 79

Compression:

Stored size: 1.32 KB

Contents

#This class can manipulate the CPU behavior through "cpufreq".
class Knj::Cpufreq
  #Data that is used to identify the CPU controlled by this object.
  attr_reader :data
  
  #Useually called through "Knj::Cpufreq.list".
  def initialize(data)
    @data = data
    @allowed_govs = ["performance", "ondemand", "powersafe", "conservative"]
  end
  
  #Returns a list of CPUs.
  #===Examples
  # list = Knj::Cpufreq.list
  # list.each do |cpufreq|
  #   cpufreq.governor = "performance"
  # end
  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
  
  #Sets the governor.
  #===Examples
  # cpufreq.governor = "performance"
  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
end

Version data entries

79 entries across 79 versions & 1 rubygems

Version Path
knjrbfw-0.0.116 lib/knj/cpufreq.rb
knjrbfw-0.0.115 lib/knj/cpufreq.rb
knjrbfw-0.0.114 lib/knj/cpufreq.rb
knjrbfw-0.0.113 lib/knj/cpufreq.rb
knjrbfw-0.0.111 lib/knj/cpufreq.rb
knjrbfw-0.0.110 lib/knj/cpufreq.rb
knjrbfw-0.0.109 lib/knj/cpufreq.rb
knjrbfw-0.0.108 lib/knj/cpufreq.rb
knjrbfw-0.0.107 lib/knj/cpufreq.rb
knjrbfw-0.0.105 lib/knj/cpufreq.rb
knjrbfw-0.0.104 lib/knj/cpufreq.rb
knjrbfw-0.0.103 lib/knj/cpufreq.rb
knjrbfw-0.0.102 lib/knj/cpufreq.rb
knjrbfw-0.0.101 lib/knj/cpufreq.rb
knjrbfw-0.0.100 lib/knj/cpufreq.rb
knjrbfw-0.0.99 lib/knj/cpufreq.rb
knjrbfw-0.0.98 lib/knj/cpufreq.rb
knjrbfw-0.0.97 lib/knj/cpufreq.rb
knjrbfw-0.0.96 lib/knj/cpufreq.rb
knjrbfw-0.0.95 lib/knj/cpufreq.rb