| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | module LinuxCPUs | 
					
						
							| 
									
										
										
										
											2014-06-04 16:12:20 +02:00
										 |  |  |   OPTIMIZATION_FLAGS = { | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |     :penryn => "-march=core2 -msse4.1", | 
					
						
							|  |  |  |     :core2 => "-march=core2", | 
					
						
							|  |  |  |     :core => "-march=prescott" | 
					
						
							| 
									
										
										
										
											2014-06-04 16:12:20 +02:00
										 |  |  |   }.freeze | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def optimization_flags | 
					
						
							|  |  |  |     OPTIMIZATION_FLAGS | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 20:05:54 -07:00
										 |  |  |   # Linux supports x86 only, and universal archs do not apply | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   def arch_32_bit | 
					
						
							|  |  |  |     :i386 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def arch_64_bit | 
					
						
							|  |  |  |     :x86_64 | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def universal_archs | 
					
						
							|  |  |  |     [].extend ArchitectureListExtension | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-08-14 20:05:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |   def cpuinfo | 
					
						
							|  |  |  |     @cpuinfo ||= File.read("/proc/cpuinfo") | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def type | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |     @type ||= if cpuinfo =~ /Intel|AMD/ | 
					
						
							|  |  |  |       :intel | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |       :dunno | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def family | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |     cpuinfo[/^cpu family\s*: ([0-9]+)/, 1].to_i | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2013-03-23 20:15:51 +01:00
										 |  |  |   alias_method :intel_family, :family | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   def cores | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |     cpuinfo.scan(/^processor/).size | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |   def flags | 
					
						
							|  |  |  |     @flags ||= cpuinfo[/^flags.*/, 0].split | 
					
						
							| 
									
										
										
										
											2013-03-17 13:30:12 -05:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-21 21:57:03 -07:00
										 |  |  |   # Compatibility with Mac method, which returns lowercase symbols | 
					
						
							|  |  |  |   # instead of strings | 
					
						
							|  |  |  |   def features | 
					
						
							|  |  |  |     @features ||= flags[1..-1].map(&:intern) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   %w[aes altivec avx avx2 lm sse3 ssse3 sse4 sse4_2].each do |flag| | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |     define_method(flag + "?") { flags.include? flag } | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2014-06-05 11:05:48 +02:00
										 |  |  |   alias_method :is_64_bit?, :lm? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def bits | 
					
						
							|  |  |  |     is_64_bit? ? 64 : 32
 | 
					
						
							| 
									
										
										
										
											2013-03-10 17:33:06 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | end |