Skip to content

Commit

Permalink
Correctly count real vs. total vs. cores on darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
tas50 committed Dec 5, 2015
1 parent fa5ab03 commit 3a4e696
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/ohai/plugins/darwin/cpu.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# Author:: Nathan L Smith (<nlloyds@gmail.com>)
# Author:: Tim Smith (<tsmith@limelight.com>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# Author:: Tim Smith (<tsmith@chef.io>)
# Copyright:: Copyright (c) 2013-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -22,10 +22,12 @@

collect_data(:darwin) do
cpu Mash.new
so = shell_out("sysctl -n hw.physicalcpu")
so = shell_out("sysctl -n hw.packages")
cpu[:real] = so.stdout.to_i
so = shell_out("sysctl -n hw.logicalcpu")
so = shell_out("sysctl -n hw.physicalcpu")
cpu[:total] = so.stdout.to_i
so = shell_out("sysctl -n hw.logicalcpu")
cpu[:cores] = so.stdout.to_i
so = shell_out("sysctl -n hw.cpufrequency")
cpu[:mhz] = so.stdout.to_i / 1000000
so = shell_out("sysctl -n machdep.cpu.vendor")
Expand Down
11 changes: 8 additions & 3 deletions spec/unit/plugins/darwin/cpu_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#
# Author:: Nathan L Smith (<nlloyds@gmail.com>)
# Copyright:: Copyright (c) 2013 Opscode, Inc.
# Copyright:: Copyright (c) 2013-2015 Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -23,6 +23,7 @@
before(:each) do
@plugin = get_plugin("darwin/cpu")
allow(@plugin).to receive(:collect_os).and_return(:darwin)
allow(@plugin).to receive(:shell_out).with("sysctl -n hw.packages").and_return(mock_shell_out(0, "1", ""))
allow(@plugin).to receive(:shell_out).with("sysctl -n hw.physicalcpu").and_return(mock_shell_out(0, "4", ""))
allow(@plugin).to receive(:shell_out).with("sysctl -n hw.logicalcpu").and_return(mock_shell_out(0, "8", ""))
allow(@plugin).to receive(:shell_out).with("sysctl -n hw.cpufrequency").and_return(mock_shell_out(0, "2300000000", ""))
Expand All @@ -35,14 +36,18 @@
@plugin.run
end

it "should set cpu[:total] to 8" do
it "should set cpu[:cores] to 8" do
expect(@plugin[:cpu][:total]).to eq(8)
end

it "should set cpu[:real] to 4" do
it "should set cpu[:total] to 4" do
expect(@plugin[:cpu][:real]).to eq(4)
end

it "should set cpu[:real] to 1" do
expect(@plugin[:cpu][:real]).to eq(1)
end

it "should set cpu[:mhz] to 2300" do
expect(@plugin[:cpu][:mhz]).to eq(2300)
end
Expand Down

0 comments on commit 3a4e696

Please sign in to comment.