Skip to content

Instantly share code, notes, and snippets.

@stevenwilliamson
Created March 23, 2017 16:36
Show Gist options
  • Select an option

  • Save stevenwilliamson/37cf09c0538d3eccc6797b8e363f9219 to your computer and use it in GitHub Desktop.

Select an option

Save stevenwilliamson/37cf09c0538d3eccc6797b8e363f9219 to your computer and use it in GitHub Desktop.
# Sets the cpu_cap fact to the cpu_cap setting for the zone.
# We use kstat to retrive the value because it is not availble via mdata-get
# If no cap can be found it will be set to 0
Facter.add('cpu_cap') do
confine :operatingsystem => "SmartOS"
setcode do
cpu_cap_value = 0
if Facter::Util::Resolution.which('kstat')
cpu_cap_string = Facter::Util::Resolution.exec('kstat -n /cpucaps/ -c zone_caps -s value -C')
cpu_cap_value = cpu_cap_string.split(":").last unless cpu_cap_string == nil
end
cpu_cap_value
end
end
# Adds a usable_cores fact which represents the number of cores the VM
# actually has access to.
# For KVM this is effectively the number of VCPU's for Zones it is the CPU_CAP setting
#
Facter.add('usable_cores') do
setcode do
cpu_cap = Facter.value('cpu_cap')
# If cpu_cap is set, set the number of usable cores based off it
# If it's a fractional part of a core we always round up to at least 1 core
if cpu_cap != nil && cpu_cap.to_i > 0
usable_cores = (cpu_cap.to_i / 100).ceil
else
# If cpu_cap is not set then we default to all available cores
# which happens to be correct for KVM (physical cores is the number of VCPUS allocated)
# and SmartOS zones, if no cap is set the zone has access to all available cores
usable_cores = Facter.value("processorcount")
end
# we always return at least 1 core
usable_cores || 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment