module Sanzang::Platform
The Sanzang::Platform
module includes information about the underlying system that is needed by the Sanzang system. This includes information about the machine architecture and OS, the number of processors available, encodings that are supported, and encodings that are optimal.
Public Class Methods
Default text data encoding on this platform. This is usually the default external encoding of the Ruby interpreter; however, if the encoding is an ASCII variant or an old IBM DOS encoding, then it should default to UTF-8 since these are effectively obsolete, or they are subsets of UTF-8.
# File lib/sanzang/platform.rb, line 124 def data_encoding if ENV["SANZANG_ENCODING"] and ENV["SANZANG_ENCODING"].strip != "" encoding = Encoding.find(ENV["SANZANG_ENCODING"]) if encoding != Encoding::UTF_8 Encoding::Converter.search_convpath(encoding, Encoding::UTF_8) end encoding elsif Encoding.default_external.to_s =~ /ASCII|IBM/ Encoding::UTF_8 elsif RUBY_PLATFORM == "java" Encoding::UTF_8 else Encoding.default_external end end
CPU architecture of the underlying machine
# File lib/sanzang/platform.rb, line 30 def machine_arch RbConfig::CONFIG["target_cpu"] end
Operating system, which may be different from RUBY_PLATFORM
# File lib/sanzang/platform.rb, line 36 def os_name RbConfig::CONFIG["target_os"] end
Find the number of logical processors seen by the system. This may be different from the number of physical processors or CPU cores. If the number of processors cannot be detected, nil is returned. For Windows, this is detected through an OLE lookup, and for Unix systems, a heuristic approach is taken. Supported Unix types include:
-
AIX: pmcycles (AIX 5+), lsdev
-
BSD: /sbin/sysctl
-
Cygwin: /proc/cpuinfo
-
Darwin: hwprefs, /usr/sbin/sysctl
-
HP-UX: ioscan
-
IRIX: sysconf
-
Linux: /proc/cpuinfo
-
Minix 3+: /proc/cpuinfo
-
Solaris: psrinfo
-
Tru64 UNIX: psrinfo
-
UnixWare: psrinfo
# File lib/sanzang/platform.rb, line 69 def processor_count if os_name =~ /mingw|mswin/ require 'win32ole' result = WIN32OLE.connect("winmgmts://").ExecQuery( "select NumberOfLogicalProcessors from Win32_Processor") result.to_enum.collect(&:NumberOfLogicalProcessors).reduce(:+) elsif File.readable?("/proc/cpuinfo") IO.read("/proc/cpuinfo").scan(/^processor/).size elsif File.executable?("/usr/bin/hwprefs") IO.popen("/usr/bin/hwprefs thread_count").read.to_i elsif File.executable?("/usr/sbin/psrinfo") IO.popen("/usr/sbin/psrinfo").read.scan(/^.*on-*line/).size elsif File.executable?("/usr/sbin/ioscan") IO.popen("/usr/sbin/ioscan -kC processor") do |out| out.read.scan(/^.*processor/).size end elsif File.executable?("/usr/sbin/pmcycles") IO.popen("/usr/sbin/pmcycles -m").read.count("\n") elsif File.executable?("/usr/sbin/lsdev") IO.popen("/usr/sbin/lsdev -Cc processor -S 1").read.count("\n") elsif File.executable?("/usr/sbin/sysconf") and os_name =~ /IRIX/i IO.popen("/usr/sbin/sysconf NPROC_ONLN").read.to_i elsif File.executable?("/usr/sbin/sysctl") IO.popen("/usr/sbin/sysctl -n hw.ncpu").read.to_i elsif File.executable?("/sbin/sysctl") IO.popen("/sbin/sysctl -n hw.ncpu").read.to_i else nil end end
Does this Ruby VM support Unix-style process handling?
# File lib/sanzang/platform.rb, line 42 def unix_processes? [:fork, :wait, :kill].each do |f| if not Process.respond_to?(f) return false end end true end
Text encodings that can be converted to UTF-8. MRI still lacks some converter implementations for obscure encodings.
# File lib/sanzang/platform.rb, line 103 def valid_encodings if RUBY_PLATFORM == "java" return [Encoding::UTF_8] end Encoding.list.find_all do |e| begin Encoding::Converter.search_convpath(e, Encoding::UTF_8) true rescue Encoding::ConverterNotFoundError e == Encoding::UTF_8 ? true : false rescue false end end.sort_by! {|e| e.to_s.upcase } end