Guest User

Untitled

a guest
Feb 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. # File goes into lib/puppet/functions/ of some module
  4.  
  5. # You can use this function to dynamically calculate memory use for a certain process.
  6. # The java JVM for example.
  7. #
  8. # Its possible to calculate memory based upon fixed numbers (would like 512M but with a minimum of 256M) or
  9. # with percentages (give me half but with a minimum of 256M).
  10. #
  11. # If you want to make sure the puppet run fails if there is not enough memory available on a machine,
  12. # you can set the `minimum` argument to the same value as the `use` parameter.
  13. #
  14. # The returned value will always be on the same base as the provided `memorysize`. In other words: If the given
  15. # memorysize is in MBs, so will the result be. All other parameters (if not percentages) should also be in the
  16. # same base.
  17. #
  18. # @example Use half ot he non-reserved (512M) but with a minimum of 256M
  19. # $use_half_of_not_required = calculate_memory_use($facts['memorysize_mb'], 0.5, 512, 256)
  20. #
  21. # @example The following call will make puppet fail since there is not enough memory available.
  22. # $failing = calculate_memory_use(1024, 768, 512, 512)
  23. #
  24. # @raise [Puppet::ParseError] If we can not allocate the required amount of minimal memory.
  25. Puppet::Functions.create_function(:calculate_memory_use) do
  26. # @param memorysize The memory to use as base. This should be the memorysize_mb fact.
  27. # @param use How much memory we would like to use of the non-reserved space left.
  28. # If it is a float smaller than 1, it will be interpreted as a percentage.
  29. # @param reserved How much memory to reserve for other processes.
  30. # If it is a float smaller than 1, it will be interpreted as a percentage.
  31. # @param minimum The minimum memory to allocate.
  32. # @param round If `true`, round the number and return as an integer. Otherwise return
  33. # the float value. Defaults to `true`.
  34. # @return Calculated memory as an integer or float depending on the `round` boolean.
  35. dispatch :calculate_memory_use do
  36. param 'Numeric', :memorysize
  37. param 'Numeric', :use
  38. param 'Numeric', :reserved
  39. optional_param 'Numeric', :minimum
  40. optional_param 'Boolean', :round
  41. return_type 'Numeric'
  42. end
  43.  
  44. def calculate_memory_use(memorysize, use, reserved, minimum = 0, round = true)
  45. use = use.to_f
  46. reserved = reserved.to_f
  47. minimum = minimum.to_f
  48.  
  49. total_size = memorysize.to_f
  50.  
  51. reserved = total_size * reserved if reserved < 1.0
  52.  
  53. useable_size = total_size - reserved
  54. use = useable_size * use if use < 1.0
  55.  
  56. try = [useable_size, use].min
  57. actual_use = [minimum, try].max
  58. if actual_use > useable_size
  59. err_insufficient_free = 'calculate_memory_use(): insufficient free memory (%s) to provide the requested amount: %s'
  60. raise Puppet::ParseError, format(err_insufficient_free, useable_size.to_s, actual_use.to_s)
  61. end
  62.  
  63. round ? actual_use.round : actual_use
  64. end
  65. end
Add Comment
Please, Sign In to add comment