| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | module Hardware | 
					
						
							|  |  |  |   class CPU | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |     class << self | 
					
						
							|  |  |  |       def universal_archs | 
					
						
							|  |  |  |         [].extend ArchitectureListExtension | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def cpuinfo | 
					
						
							|  |  |  |         @cpuinfo ||= File.read("/proc/cpuinfo") | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def type | 
					
						
							|  |  |  |         @type ||= if cpuinfo =~ /Intel|AMD/ | 
					
						
							|  |  |  |           :intel | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           :dunno | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def family | 
					
						
							|  |  |  |         cpuinfo[/^cpu family\s*: ([0-9]+)/, 1].to_i | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       alias_method :intel_family, :family | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def cores | 
					
						
							|  |  |  |         cpuinfo.scan(/^processor/).size | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def flags | 
					
						
							|  |  |  |         @flags ||= cpuinfo[/^flags.*/, 0].split | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       # Compatibility with Mac method, which returns lowercase symbols | 
					
						
							|  |  |  |       # instead of strings | 
					
						
							|  |  |  |       def features | 
					
						
							|  |  |  |         @features ||= flags[1..-1].map(&:intern) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       %w[aes altivec avx avx2 lm sse3 ssse3 sse4 sse4_2].each do |flag| | 
					
						
							|  |  |  |         define_method(flag + "?") { flags.include? flag } | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       alias_method :is_64_bit?, :lm? | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-09 09:41:30 +02:00
										 |  |  |       def bits | 
					
						
							|  |  |  |         is_64_bit? ? 64 : 32
 | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2016-04-25 18:00:01 +01:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |