Sha256: 53a1bd5cf873fbc4d27918fdd33fc5235f0cb90ce5dbf4121a90890b6d920082

Contents?: true

Size: 940 Bytes

Versions: 4

Compression:

Stored size: 940 Bytes

Contents

#This class can manipulate the CPU behavior through "cpufreq".
class Knj::Cpufreq
	attr_reader :data
	
	def initialize(data)
		@data = data
		@allowed_govs = ["performance", "ondemand", "powersafe", "conservative"]
	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
	
	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

4 entries across 4 versions & 1 rubygems

Version Path
knjrbfw-0.0.8 lib/knj/cpufreq.rb
knjrbfw-0.0.7 lib/knj/cpufreq.rb
knjrbfw-0.0.4 lib/knj/cpufreq.rb
knjrbfw-0.0.3 lib/knj/cpufreq.rb