| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "os" | 
					
						
							| 
									
										
										
										
											2013-10-18 12:56:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | module Hardware | 
					
						
							|  |  |  |   class CPU | 
					
						
							| 
									
										
										
										
											2013-08-11 19:11:58 -07:00
										 |  |  |     INTEL_32BIT_ARCHS = [:i386].freeze | 
					
						
							|  |  |  |     INTEL_64BIT_ARCHS = [:x86_64].freeze | 
					
						
							|  |  |  |     PPC_32BIT_ARCHS   = [:ppc, :ppc7400, :ppc7450, :ppc970].freeze | 
					
						
							|  |  |  |     PPC_64BIT_ARCHS   = [:ppc64].freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |     class << self | 
					
						
							| 
									
										
										
										
											2016-07-06 10:43:54 +01:00
										 |  |  |       OPTIMIZATION_FLAGS = { | 
					
						
							| 
									
										
										
										
											2016-09-17 15:32:44 +01:00
										 |  |  |         penryn: "-march=core2 -msse4.1", | 
					
						
							|  |  |  |         core2: "-march=core2", | 
					
						
							|  |  |  |         core: "-march=prescott", | 
					
						
							|  |  |  |         dunno: "", | 
					
						
							| 
									
										
										
										
											2016-07-06 10:43:54 +01:00
										 |  |  |       }.freeze | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def optimization_flags | 
					
						
							|  |  |  |         OPTIMIZATION_FLAGS | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def arch_32_bit | 
					
						
							|  |  |  |         :i386 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def arch_64_bit | 
					
						
							|  |  |  |         :x86_64 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def type | 
					
						
							| 
									
										
										
										
											2016-07-16 21:05:07 +01:00
										 |  |  |         case RUBY_PLATFORM | 
					
						
							|  |  |  |         when /x86_64/, /i\d86/ then :intel | 
					
						
							|  |  |  |         when /ppc\d+/ then :ppc | 
					
						
							|  |  |  |         else :dunno | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def family | 
					
						
							|  |  |  |         :dunno | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def cores | 
					
						
							|  |  |  |         1
 | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def bits | 
					
						
							| 
									
										
										
										
											2016-07-16 21:05:07 +01:00
										 |  |  |         case RUBY_PLATFORM | 
					
						
							|  |  |  |         when /x86_64/, /ppc64/ then 64
 | 
					
						
							|  |  |  |         when /i\d86/, /ppc/ then 32
 | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       def sse4? | 
					
						
							|  |  |  |         RUBY_PLATFORM.to_s.include?("x86_64") | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def is_32_bit? | 
					
						
							|  |  |  |         bits == 32
 | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def is_64_bit? | 
					
						
							|  |  |  |         bits == 64
 | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def intel? | 
					
						
							|  |  |  |         type == :intel | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def ppc? | 
					
						
							|  |  |  |         type == :ppc | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-23 21:38:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-29 20:02:39 -06:00
										 |  |  |       def arm? | 
					
						
							|  |  |  |         type == :arm | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def features | 
					
						
							|  |  |  |         [] | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-23 21:38:36 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       def feature?(name) | 
					
						
							|  |  |  |         features.include?(name) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-02-23 21:38:36 -05:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-16 09:35:19 -08:00
										 |  |  |   def self.cores_as_words | 
					
						
							| 
									
										
										
										
											2013-06-13 12:04:58 -05:00
										 |  |  |     case Hardware::CPU.cores | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     when 1 then "single" | 
					
						
							|  |  |  |     when 2 then "dual" | 
					
						
							|  |  |  |     when 4 then "quad" | 
					
						
							| 
									
										
										
										
											2016-06-14 15:08:04 +02:00
										 |  |  |     when 6 then "hexa" | 
					
						
							|  |  |  |     when 8 then "octa" | 
					
						
							| 
									
										
										
										
											2009-11-16 09:35:19 -08:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2013-06-13 12:04:58 -05:00
										 |  |  |       Hardware::CPU.cores | 
					
						
							| 
									
										
										
										
											2009-11-16 09:35:19 -08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-06-06 16:02:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def self.oldest_cpu | 
					
						
							| 
									
										
										
										
											2013-11-26 20:23:19 -06:00
										 |  |  |     if Hardware::CPU.intel? | 
					
						
							| 
									
										
										
										
											2013-06-06 16:02:27 -05:00
										 |  |  |       if Hardware::CPU.is_64_bit? | 
					
						
							|  |  |  |         :core2 | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         :core | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       Hardware::CPU.family | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2009-09-02 13:17:15 -06:00
										 |  |  | end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | require "extend/os/hardware" |