Object
Encapsulates system CPU information
The version of the sys-cpu library
# File lib/unix/sys/cpu.rb, line 92 def self.architecture if respond_to?(:sysinfo, true) buf = 0.chr * 257 if sysinfo(SI_ARCHITECTURE, buf, buf.size) < 0 raise Error, "sysinfo function failed" end buf.strip elsif respond_to?(:sysctlbyname, true) optr = FFI::MemoryPointer.new(:char, 256) size = FFI::MemoryPointer.new(:size_t) size.write_int(optr.size) if RbConfig::CONFIG['host_os'] =~ /darwin/ name = 'hw.machine' else name = 'hw.machine_arch' end if sysctlbyname(name, optr, size, nil, 0) < 0 raise Error, "sysctlbyname function failed" end optr.read_string else buf = 0.chr * 64 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_MACHINE_ARCH]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip end end
Returns a hash of arrays that contain the number of seconds that the system spent in user mode, user mode with low priority (nice), system mode, and the idle task, respectively.
# File lib/linux/sys/cpu.rb, line 99 def self.cpu_stats cpu_stat_file = "/proc/stat" hash = {} # Hash needed for multi-cpu systems lines = IO.readlines(cpu_stat_file) lines.each_with_index{ |line, i| array = line.split break unless array[0] =~ /cpu/ # 'cpu' entries always on top # Some machines list a 'cpu' and a 'cpu0'. In this case only # return values for the numbered cpu entry. if lines[i].split[0] == "cpu" && lines[i+1].split[0] =~ /cpu\d/ next end vals = array[1..-1].map{ |e| e = e.to_i / 100 } # 100 jiffies/sec. hash[array[0]] = vals } hash end
Returns a string indicating the type of processor, e.g. GenuineIntel.
# File lib/windows/sys/cpu.rb, line 258 def self.cpu_type(host = Socket.gethostname) cs = BASE_CS + "//#{host}/root/cimv2:Win32_Processor='cpu0'" begin wmi = WIN32OLE.connect(cs) rescue WIN32OLERuntimeError => e raise Error, e else return wmi.Manufacturer end end
# File lib/unix/sys/cpu.rb, line 306 def self.fpu_type raise NoMethodError unless respond_to?(:processor_info, true) pinfo = ProcInfo.new if processor_info(0, pinfo) < 0 if processor_info(1, pinfo) < 0 raise Error, "process_info function failed" end end pinfo[:pi_fputypes].to_s end
# File lib/unix/sys/cpu.rb, line 245 def self.freq if respond_to?(:sysctlbyname, true) optr = FFI::MemoryPointer.new(:long) size = FFI::MemoryPointer.new(:size_t) size.write_long(optr.size) if RbConfig::CONFIG['host_os'] =~ /bsd/ name = 'hw.clockrate' else name = 'hw.cpufrequency' end if sysctlbyname(name, optr, size, nil, 0) < 0 raise Error, "sysctlbyname failed" end if RbConfig::CONFIG['host_os'] =~ /darwin/ optr.read_long / 1000000 else optr.read_long end elsif respond_to?(:sysctl, true) buf = 0.chr * 16 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_CPU_FREQ]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.unpack("I*").first / 1000000 else pinfo = ProcInfo.new # Some systems start at 0, some at 1 if processor_info(0, pinfo) < 0 if processor_info(1, pinfo) < 0 raise Error, "process_info function failed" end end pinfo[:pi_clock].to_i end end
# File lib/unix/sys/cpu.rb, line 294 def self.load_avg if respond_to?(:getloadavg, true) loadavg = FFI::MemoryPointer.new(:double, 3) if getloadavg(loadavg, loadavg.size) < 0 raise Error, "getloadavg function failed" end loadavg.get_array_of_double(0, 3) end end
# File lib/unix/sys/cpu.rb, line 170 def self.machine if respond_to?(:sysctl, true) buf = 0.chr * 32 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_MACHINE]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip else buf = 0.chr * 257 if sysinfo(SI_MACHINE, buf, buf.size) < 0 raise Error, "sysinfo function failed" end buf.strip end end
# File lib/unix/sys/cpu.rb, line 195 def self.model if RbConfig::CONFIG['host_os'] =~ /darwin/ ptr = FFI::MemoryPointer.new(:long) size = FFI::MemoryPointer.new(:size_t) size.write_long(ptr.size) if sysctlbyname("hw.cputype", ptr, size, nil, 0) < 0 raise "sysctlbyname function failed" end case ptr.read_long when CPU_TYPE_X86, CPU_TYPE_X86_64 "Intel" when CPU_TYPE_SPARC "Sparc" when CPU_TYPE_POWERPC, CPU_TYPE_POWERPC64 "PowerPC" else "Unknown" end else if respond_to?(:sysctl, true) buf = 0.chr * 64 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_MODEL]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip else pinfo = ProcInfo.new # Some systems start at 0, some at 1 if processor_info(0, pinfo) < 0 if processor_info(1, pinfo) < 0 raise Error, "process_info function failed" end end pinfo[:pi_processor_type].to_s end end end
# File lib/unix/sys/cpu.rb, line 134 def self.num_cpu if respond_to?(:sysctlbyname, true) optr = FFI::MemoryPointer.new(:long) size = FFI::MemoryPointer.new(:size_t) size.write_long(optr.size) if sysctlbyname('hw.ncpu', optr, size, nil, 0) < 0 raise Error, "sysctlbyname failed" end optr.read_long elsif respond_to?(:sysconf, true) num = sysconf(SC_NPROCESSORS_ONLN) if num < 0 raise Error, "sysconf function failed" end num else buf = 0.chr * 4 mib = FFI::MemoryPointer.new(:int, 2) size = FFI::MemoryPointer.new(:long, 1) mib.write_array_of_int([CTL_HW, HW_NCPU]) size.write_int(buf.size) if sysctl(mib, 2, buf, size, nil, 0) < 0 raise Error, "sysctl function failed" end buf.strip.unpack("C").first end end
In block form, yields a CPUStruct for each CPU on the system. In non-block form, returns an Array of CPUStruct's.
The exact members of the struct vary on Linux systems.
# File lib/linux/sys/cpu.rb, line 58 def self.processors array = [] $cpu_array.each{ |hash| struct = CPUStruct.new struct.members.each{ |m| struct.send("#{m}=", hash[m]) } if block_given? yield struct else array << struct end } array unless block_given? end
# File lib/unix/sys/cpu.rb, line 320 def self.state(num = 0) raise NoMethodError unless respond_to?(:processor_info, true) pinfo = ProcInfo.new if processor_info(num, pinfo) < 0 raise Error, "process_info function failed" end case pinfo[:pi_state].to_i when P_ONLINE "online" when P_OFFLINE "offline" when P_POWEROFF "poweroff" when P_FAULTED "faulted" when P_NOINTR "nointr" when P_SPARE "spare" else "unknown" end end
Generated with the Darkfish Rdoc Generator 2.