diff options
author | Tim Smith <tsmith@chef.io> | 2017-08-15 10:04:22 -0700 |
---|---|---|
committer | Tim Smith <tsmith@chef.io> | 2017-08-15 10:38:19 -0700 |
commit | 2c8c34b8474b0929fcac85852d53e9e2d56ab968 (patch) | |
tree | 295b5480204d8f6a905406190b0ce4a9a3323b2e | |
parent | 8edc4a9e1fcf7b0bdf61d30b4b2e253f746c1fc7 (diff) | |
download | ohai-sles.tar.gz |
Update testing for systems w/o core count in /proc/cpuinfosles
Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r-- | lib/ohai/plugins/linux/cpu.rb | 32 | ||||
-rw-r--r-- | spec/unit/plugins/linux/cpu_spec.rb | 210 |
2 files changed, 141 insertions, 101 deletions
diff --git a/lib/ohai/plugins/linux/cpu.rb b/lib/ohai/plugins/linux/cpu.rb index be5667aa..85f91b9b 100644 --- a/lib/ohai/plugins/linux/cpu.rb +++ b/lib/ohai/plugins/linux/cpu.rb @@ -86,7 +86,7 @@ Ohai.plugin(:CPU) do cpu[:total] = cpu_number # use data we collected unless cpuinfo is lacking core information - # which is the case on older linux distros (specifically SLES <= 12 SP1) + # which is the case on older linux distros if !real_cpu.empty? && cpu["0"]["cores"] cpu[:real] = real_cpu.keys.length cpu[:cores] = real_cpu.keys.length * cpu["0"]["cores"].to_i @@ -94,22 +94,26 @@ Ohai.plugin(:CPU) do begin Ohai::Log.debug("Plugin CPU: Falling back to aggregate data from lscpu as real cpu & core data is missing in /proc/cpuinfo") so = shell_out("lscpu") - lscpu_data = Mash.new - so.stdout.each_line do |line| - case line - when /^Thread\(s\) per core:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q - lscpu_data[:threads] = $1.to_i - when /^Core\(s\) per socket:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q - lscpu_data[:cores] = $1.to_i - when /^Socket\(s\):\s(.+)/ # http://rubular.com/r/DIzmPtJFvK - lscpu_data[:sockets] = $1.to_i + if so.exitstatus == 0 + lscpu_data = Mash.new + so.stdout.each_line do |line| + case line + when /^Thread\(s\) per core:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q + lscpu_data[:threads] = $1.to_i + when /^Core\(s\) per socket:\s(.+)/ # http://rubular.com/r/lOw2pRrw1q + lscpu_data[:cores] = $1.to_i + when /^Socket\(s\):\s(.+)/ # http://rubular.com/r/DIzmPtJFvK + lscpu_data[:sockets] = $1.to_i + end end + cpu[:total] = lscpu_data[:sockets] * lscpu_data[:cores] * lscpu_data[:threads] + cpu[:real] = lscpu_data[:sockets] + cpu[:cores] = lscpu_data[:sockets] * lscpu_data[:cores] + else + Ohai::Log.debug("Plugin CPU: Error executing lscpu. CPU data may not be available.") end - cpu[:total] = lscpu_data[:sockets] * lscpu_data[:cores] * lscpu_data[:threads] - cpu[:real] = lscpu_data[:sockets] - cpu[:cores] = lscpu_data[:sockets] * lscpu_data[:cores] rescue Ohai::Exceptions::Exec # util-linux isn't installed most likely - Ohai::Log.debug("Plugin CPU: Error executing lshw. util-linux may not be installed") + Ohai::Log.debug("Plugin CPU: Error executing lscpu. util-linux may not be installed.") end end end diff --git a/spec/unit/plugins/linux/cpu_spec.rb b/spec/unit/plugins/linux/cpu_spec.rb index ccaa802d..120b64ae 100644 --- a/spec/unit/plugins/linux/cpu_spec.rb +++ b/spec/unit/plugins/linux/cpu_spec.rb @@ -79,30 +79,6 @@ end describe Ohai::System, "General Linux cpu plugin" do let(:plugin) { get_plugin("linux/cpu") } - let(:cpuinfo_contents) do - <<-EOF -processor : 0 -vendor_id : GenuineIntel -cpu family : 6 -model : 23 -model name : Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz -stepping : 6 -cpu MHz : 1968.770 -cache size : 64 KB -fdiv_bug : no -hlt_bug : no -f00f_bug : no -coma_bug : no -fpu : yes -fpu_exception : yes -cpuid level : 10 -wp : yes -flags : fpu pse tsc msr mce cx8 sep mtrr pge cmov -bogomips : 2575.86 -clflush size : 32 - EOF - end - let(:tempfile_handle) do tempfile = Tempfile.new("ohai-rspec-proc-cpuinfo") tempfile.write cpuinfo_contents @@ -124,81 +100,139 @@ clflush size : 32 end end - it_behaves_like "Common cpu info", 1, 0 + context "with old kernel that doesn't include cores in /proc/cpuinfo" do + let(:cpuinfo_contents) do + <<-EOF + processor : 0 + vendor_id : GenuineIntel + cpu family : 6 + model : 23 + model name : Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz + stepping : 6 + cpu MHz : 1968.770 + cache size : 64 KB + fdiv_bug : no + hlt_bug : no + f00f_bug : no + coma_bug : no + fpu : yes + fpu_exception : yes + cpuid level : 10 + wp : yes + flags : fpu pse tsc msr mce cx8 sep mtrr pge cmov + bogomips : 2575.86 + clflush size : 32 + EOF + end - it "gets total cores" do - plugin.run - expect(plugin[:cpu][:cores]).to eql(0) - end + let(:lscpu) do + <<-EOF +Architecture: x86_64 +CPU op-mode(s): 32-bit, 64-bit +Byte Order: Little Endian +CPU(s): 1 +On-line CPU(s) list: 0 +Thread(s) per core: 1 +Core(s) per socket: 1 +Socket(s): 1 +NUMA node(s): 1 +Vendor ID: GenuineIntel +CPU family: 6 +Model: 23 +Model name: Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz +Stepping: 2 +CPU MHz: 1968.770 +BogoMIPS: 2575.86 +Hypervisor vendor: Xen +Virtualization type: full +L1d cache: 32K +L1i cache: 32K +L2 cache: 256K +L3 cache: 30720K +NUMA node0 CPU(s): 0 +Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx rdtscp lm constant_tsc rep_good nopl xtopology pni pclmulqdq ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm fsgsbase bmi1 avx2 smep bmi2 erms invpcid xsaveopt + EOF + end - it "doesn't have a cpu 1" do - plugin.run - expect(plugin[:cpu]).not_to have_key("1") - end + it_behaves_like "Common cpu info", 1, 1 - it "has a vendor_id for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("vendor_id") - expect(plugin[:cpu]["0"]["vendor_id"]).to eql("GenuineIntel") - end + before(:each) do + allow(plugin).to receive(:shell_out).with("lscpu").and_return(mock_shell_out(0, lscpu, "")) + end - it "has a family for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("family") - expect(plugin[:cpu]["0"]["family"]).to eql("6") - end + it "gets total cores" do + plugin.run + expect(plugin[:cpu][:cores]).to eql(1) + end - it "has a model for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("model") - expect(plugin[:cpu]["0"]["model"]).to eql("23") - end + it "doesn't have a cpu 1" do + plugin.run + expect(plugin[:cpu]).not_to have_key("1") + end - it "has a stepping for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("stepping") - expect(plugin[:cpu]["0"]["stepping"]).to eql("6") - end + it "has a vendor_id for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("vendor_id") + expect(plugin[:cpu]["0"]["vendor_id"]).to eql("GenuineIntel") + end - it "doesn't have a phyiscal_id for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).not_to have_key("physical_id") - end + it "has a family for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("family") + expect(plugin[:cpu]["0"]["family"]).to eql("6") + end - it "doesn't have a core_id for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).not_to have_key("core_id") - end + it "has a model for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("model") + expect(plugin[:cpu]["0"]["model"]).to eql("23") + end - it "doesn't have a cores for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).not_to have_key("cores") - end + it "has a stepping for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("stepping") + expect(plugin[:cpu]["0"]["stepping"]).to eql("6") + end - it "has a model name for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("model_name") - expect(plugin[:cpu]["0"]["model_name"]).to eql("Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz") - end + it "doesn't have a phyiscal_id for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).not_to have_key("physical_id") + end - it "has a mhz for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("mhz") - expect(plugin[:cpu]["0"]["mhz"]).to eql("1968.770") - end + it "doesn't have a core_id for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).not_to have_key("core_id") + end - it "has a cache_size for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("cache_size") - expect(plugin[:cpu]["0"]["cache_size"]).to eql("64 KB") - end + it "doesn't have a cores for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).not_to have_key("cores") + end - it "has flags for cpu 0" do - plugin.run - expect(plugin[:cpu]["0"]).to have_key("flags") - expect(plugin[:cpu]["0"]["flags"]).to eq(%w{fpu pse tsc msr mce cx8 sep mtrr pge cmov}) - end + it "has a model name for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("model_name") + expect(plugin[:cpu]["0"]["model_name"]).to eql("Intel(R) Core(TM)2 Duo CPU T8300 @ 2.40GHz") + end + + it "has a mhz for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("mhz") + expect(plugin[:cpu]["0"]["mhz"]).to eql("1968.770") + end + + it "has a cache_size for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("cache_size") + expect(plugin[:cpu]["0"]["cache_size"]).to eql("64 KB") + end + it "has flags for cpu 0" do + plugin.run + expect(plugin[:cpu]["0"]).to have_key("flags") + expect(plugin[:cpu]["0"]["flags"]).to eq(%w{fpu pse tsc msr mce cx8 sep mtrr pge cmov}) + end + end context "with a dual-core hyperthreaded /proc/cpuinfo" do let(:cpuinfo_contents) do <<-EOF @@ -331,6 +365,7 @@ describe Ohai::System, "S390 linux cpu plugin" do before(:each) do allow(plugin).to receive(:collect_os).and_return(:linux) + allow(plugin).to receive(:shell_out).with("lscpu").and_return(mock_shell_out(1, "", "")) @double_file = double("/proc/cpuinfo") allow(@double_file).to receive(:each). @@ -343,7 +378,7 @@ describe Ohai::System, "S390 linux cpu plugin" do allow(File).to receive(:open).with("/proc/cpuinfo").and_return(@double_file) end - it_behaves_like "Common cpu info", 2, 0 + it_behaves_like "Common cpu info", 2, nil it "has a cpu 1" do plugin.run @@ -377,6 +412,7 @@ describe Ohai::System, "arm64 linux cpu plugin" do before(:each) do allow(plugin).to receive(:collect_os).and_return(:linux) + allow(plugin).to receive(:shell_out).with("lscpu").and_return(mock_shell_out(1, "", "")) @double_file = double("/proc/cpuinfo") allow(@double_file).to receive(:each). @@ -391,7 +427,7 @@ describe Ohai::System, "arm64 linux cpu plugin" do allow(File).to receive(:open).with("/proc/cpuinfo").and_return(@double_file) end - it_behaves_like "Common cpu info", 2, 0 + it_behaves_like "Common cpu info", 2, nil it "has a cpu 1" do plugin.run |