This repository has been archived by the owner on Jun 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Add support for processors facts on *BSD #489
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Bsd | ||
module Processors | ||
class Count | ||
FACT_NAME = 'processors.count' | ||
ALIASES = 'processorcount' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Bsd::Processors.resolve(:logical_count) | ||
[Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Bsd | ||
module Processors | ||
class Models | ||
FACT_NAME = 'processors.models' | ||
ALIASES = 'processor.*' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Bsd::Processors.resolve(:models) | ||
return nil unless fact_value | ||
|
||
facts = [Facter::ResolvedFact.new(FACT_NAME, fact_value)] | ||
fact_value.each_with_index do |value, index| | ||
facts.push(Facter::ResolvedFact.new("processor#{index}", value, :legacy)) | ||
end | ||
facts | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Bsd | ||
module Processors | ||
class Speed | ||
FACT_NAME = 'processors.speed' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Bsd::Processors.resolve(:speed) | ||
speed = Facter::FactsUtils::UnitConverter.hertz_to_human_readable(fact_value) | ||
Facter::ResolvedFact.new(FACT_NAME, speed) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Freebsd | ||
module Processors | ||
class Count | ||
FACT_NAME = 'processors.count' | ||
ALIASES = 'processorcount' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Freebsd::Processors.resolve(:logical_count) | ||
[Facter::ResolvedFact.new(FACT_NAME, fact_value), Facter::ResolvedFact.new(ALIASES, fact_value, :legacy)] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Freebsd | ||
module Processors | ||
class Models | ||
FACT_NAME = 'processors.models' | ||
ALIASES = 'processor.*' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Freebsd::Processors.resolve(:models) | ||
return nil unless fact_value | ||
|
||
facts = [Facter::ResolvedFact.new(FACT_NAME, fact_value)] | ||
fact_value.each_with_index do |value, index| | ||
facts.push(Facter::ResolvedFact.new("processor#{index}", value, :legacy)) | ||
end | ||
facts | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facts | ||
module Freebsd | ||
module Processors | ||
class Speed | ||
FACT_NAME = 'processors.speed' | ||
|
||
def call_the_resolver | ||
fact_value = Facter::Resolvers::Freebsd::Processors.resolve(:speed) | ||
speed = Facter::FactsUtils::UnitConverter.hertz_to_human_readable(fact_value) | ||
Facter::ResolvedFact.new(FACT_NAME, speed) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# frozen_string_literal: true | ||
|
||
module Facter | ||
module Resolvers | ||
module Bsd | ||
class Processors < BaseResolver | ||
@log = Facter::Log.new(self) | ||
@semaphore = Mutex.new | ||
@fact_list ||= {} | ||
class << self | ||
private | ||
|
||
def post_resolve(fact_name) | ||
@fact_list.fetch(fact_name) { collect_processors_info(fact_name) } | ||
end | ||
|
||
def collect_processors_info(fact_name) | ||
require "#{ROOT_DIR}/lib/resolvers/bsd/ffi/ffi_helper" | ||
|
||
@fact_list[:logical_count] = logical_count | ||
@fact_list[:models] = Array.new(logical_count, model) if logical_count && model | ||
@fact_list[:speed] = speed * 1000 * 1000 if speed | ||
|
||
@fact_list[fact_name] | ||
end | ||
|
||
CTL_HW = 6 | ||
HW_MODEL = 2 | ||
HW_NCPU = 3 | ||
HW_CPUSPEED = 12 | ||
|
||
def model | ||
@model ||= Facter::Bsd::FfiHelper.sysctl(:string, [CTL_HW, HW_MODEL]) | ||
end | ||
|
||
def logical_count | ||
@logical_count ||= Facter::Bsd::FfiHelper.sysctl(:uint32_t, [CTL_HW, HW_NCPU]) | ||
end | ||
|
||
def speed | ||
@speed ||= Facter::Bsd::FfiHelper.sysctl(:uint32_t, [CTL_HW, HW_CPUSPEED]) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'ffi' | ||
|
||
module Facter | ||
module Freebsd | ||
module FfiHelper | ||
module Libc | ||
extend FFI::Library | ||
|
||
ffi_lib 'c' | ||
attach_function :sysctlbyname, %i[string pointer pointer pointer size_t], :int | ||
end | ||
|
||
def self.sysctl_by_name(type, name) | ||
oldp = FFI::Pointer::NULL | ||
oldlenp = FFI::MemoryPointer.new(:size_t) | ||
|
||
newp = FFI::Pointer::NULL | ||
newlenp = 0 | ||
|
||
if type == :string | ||
res = Libc.sysctlbyname(name, oldp, oldlenp, newp, newlenp) | ||
return nil if res.negative? | ||
else | ||
oldlenp.write(:size_t, FFI.type_size(type)) | ||
end | ||
|
||
oldp = FFI::MemoryPointer.new(:uint8_t, oldlenp.read(:size_t)) | ||
res = Libc.sysctlbyname(name, oldp, oldlenp, newp, newlenp) | ||
return nil if res.negative? | ||
|
||
case type | ||
when :string | ||
oldp.read_string | ||
else | ||
oldp.read(type) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'resolvers/bsd/processors' | ||
|
||
module Facter | ||
module Resolvers | ||
module Freebsd | ||
class Processors < BaseResolver | ||
@log = Facter::Log.new(self) | ||
@semaphore = Mutex.new | ||
@fact_list ||= {} | ||
class << self | ||
private | ||
|
||
def post_resolve(fact_name) | ||
@fact_list.fetch(fact_name) { collect_processors_info(fact_name) } | ||
end | ||
|
||
def collect_processors_info(fact_name) | ||
require "#{ROOT_DIR}/lib/resolvers/freebsd/ffi/ffi_helper" | ||
|
||
@fact_list[:logical_count] = logical_count | ||
@fact_list[:models] = Array.new(logical_count, model) if logical_count && model | ||
@fact_list[:speed] = speed * 1000 * 1000 if speed | ||
|
||
@fact_list[fact_name] | ||
end | ||
|
||
def model | ||
@model ||= Facter::Freebsd::FfiHelper.sysctl_by_name(:string, 'hw.model') | ||
end | ||
|
||
def logical_count | ||
@logical_count ||= Facter::Freebsd::FfiHelper.sysctl_by_name(:uint32_t, 'hw.ncpu') | ||
end | ||
|
||
def speed | ||
@speed ||= Facter::Freebsd::FfiHelper.sysctl_by_name(:uint32_t, 'hw.clockrate') | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
describe Facts::Bsd::Processors::Count do | ||
describe '#call_the_resolver' do | ||
subject(:fact) { Facts::Bsd::Processors::Count.new } | ||
|
||
let(:processors) { '4' } | ||
|
||
before do | ||
allow(Facter::Resolvers::Bsd::Processors).to \ | ||
receive(:resolve).with(:logical_count).and_return(processors) | ||
end | ||
|
||
it 'calls Facter::Resolvers::Bsd::Processors' do | ||
fact.call_the_resolver | ||
expect(Facter::Resolvers::Bsd::Processors).to have_received(:resolve).with(:logical_count) | ||
end | ||
|
||
it 'returns a resolved fact' do | ||
expect(fact.call_the_resolver).to be_an_instance_of(Array).and \ | ||
contain_exactly(an_object_having_attributes(name: 'processors.count', value: processors), | ||
an_object_having_attributes(name: 'processorcount', value: processors, type: :legacy)) | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add some tests for
Freebsd :: FfiHelper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did it, but it raise a few concerns for me because these tests just mock everything and we are testing the mock framework more than anything else 😢