Module: JVMArgs::Util

Defined in:
lib/jvmargs/util.rb

Class Method Summary collapse

Class Method Details

.convert_to_m(number) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/jvmargs/util.rb', line 25

def self.convert_to_m(number)
  raw_num, unit = self.normalize_units(number)
  case unit
  when "K"
    converted_num = (raw_num/1024).to_int
  when "G"
    converted_num = (raw_num * 1024).to_int
  when "M"
    converted_num = raw_num
  end
  "#{converted_num}M"
end

.get_raw_num(number) ⇒ Object



21
22
23
# File 'lib/jvmargs/util.rb', line 21

def self.get_raw_num(number)
  number.sub(/M/, '').to_i
end

.get_system_ram_mObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/jvmargs/util.rb', line 5

def self.get_system_ram_m
  if defined? node
    total_ram = node['memory']['total']
  else
    require 'ohai'
    ohai = Ohai::System.new
    begin
      ohai.all_plugins "memory"
    rescue ArgumentError
      ohai.require_plugin "linux::memory"
    end
    total_ram = ohai["memory"]["total"]
  end
  self.convert_to_m(total_ram)
end

.normalize_units(number) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/jvmargs/util.rb', line 38

def self.normalize_units(number)
  number =~ /^([0-9]+)\s*([kKmMgG])[bB]?/
  if $2.nil?
    raise ArgumentError, "You must indicate whether number is KiloBytes(K), MegaBytes (M), or Gigabytes(G)"
  end
  [$1.to_i, $2.upcase]
end

.parse_ram_size_to_m(size) ⇒ Object

Accepts a ‘size’ value such as 70% or 5G and converts this to the size in M.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/jvmargs/util.rb', line 48

def self.parse_ram_size_to_m(size)
  if (size =~ /([0-9]+)%/)
    percent = $1.to_i * 0.01
    if percent > 1
      raise ArgumentError, "heap_size percentage must be less than 100%, you provided #{$1}%"
    elsif percent < 0.01
      raise ArgumentError, "heap_size percentage must be between 100% - 1%, you provided #{$1}%"
    end
    system_ram = JVMArgs::Util.get_system_ram_m.sub(/M/,'').to_i
    size_ram = "#{(system_ram * percent).to_i}M"
  else
    begin
      size_ram = JVMArgs::Util.convert_to_m(size)
    rescue ArgumentError
       raise ArgumentError, "heap_size percentage must be a percentage or a fixed size in KiloBytes(K), MegaBytes (M), or Gigabytes(G)"
    end 
  end      
end