Skip to content

Instantly share code, notes, and snippets.

@vStone
Created February 9, 2018 07:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vStone/69ccbaa75ab1fc0def6d5b90ad76429a to your computer and use it in GitHub Desktop.
Save vStone/69ccbaa75ab1fc0def6d5b90ad76429a to your computer and use it in GitHub Desktop.
Puppet function to calculate memory usage.
# frozen_string_literal: true
# File goes into lib/puppet/functions/ of some module
# You can use this function to dynamically calculate memory use for a certain process.
# The java JVM for example.
#
# Its possible to calculate memory based upon fixed numbers (would like 512M but with a minimum of 256M) or
# with percentages (give me half but with a minimum of 256M).
#
# If you want to make sure the puppet run fails if there is not enough memory available on a machine,
# you can set the `minimum` argument to the same value as the `use` parameter.
#
# The returned value will always be on the same base as the provided `memorysize`. In other words: If the given
# memorysize is in MBs, so will the result be. All other parameters (if not percentages) should also be in the
# same base.
#
# @example Use half ot he non-reserved (512M) but with a minimum of 256M
# $use_half_of_not_required = calculate_memory_use($facts['memorysize_mb'], 0.5, 512, 256)
#
# @example The following call will make puppet fail since there is not enough memory available.
# $failing = calculate_memory_use(1024, 768, 512, 512)
#
# @raise [Puppet::ParseError] If we can not allocate the required amount of minimal memory.
Puppet::Functions.create_function(:calculate_memory_use) do
# @param memorysize The memory to use as base. This should be the memorysize_mb fact.
# @param use How much memory we would like to use of the non-reserved space left.
# If it is a float smaller than 1, it will be interpreted as a percentage.
# @param reserved How much memory to reserve for other processes.
# If it is a float smaller than 1, it will be interpreted as a percentage.
# @param minimum The minimum memory to allocate.
# @param round If `true`, round the number and return as an integer. Otherwise return
# the float value. Defaults to `true`.
# @return Calculated memory as an integer or float depending on the `round` boolean.
dispatch :calculate_memory_use do
param 'Numeric', :memorysize
param 'Numeric', :use
param 'Numeric', :reserved
optional_param 'Numeric', :minimum
optional_param 'Boolean', :round
return_type 'Numeric'
end
def calculate_memory_use(memorysize, use, reserved, minimum = 0, round = true)
use = use.to_f
reserved = reserved.to_f
minimum = minimum.to_f
total_size = memorysize.to_f
reserved = total_size * reserved if reserved < 1.0
useable_size = total_size - reserved
use = useable_size * use if use < 1.0
try = [useable_size, use].min
actual_use = [minimum, try].max
if actual_use > useable_size
err_insufficient_free = 'calculate_memory_use(): insufficient free memory (%s) to provide the requested amount: %s'
raise Puppet::ParseError, format(err_insufficient_free, useable_size.to_s, actual_use.to_s)
end
round ? actual_use.round : actual_use
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment