jvmargs 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/jvmargs.rb ADDED
@@ -0,0 +1,12 @@
1
+
2
+ require 'jvmargs/args'
3
+ require 'jvmargs/directive'
4
+ require 'jvmargs/standard'
5
+ require 'jvmargs/nonstandard'
6
+ require 'jvmargs/unstable'
7
+ require 'jvmargs/rules'
8
+ require 'jvmargs/util'
9
+
10
+ module JVMArgs
11
+ Types = [:directive, :nonstandard, :standard, :unstable ]
12
+ end
@@ -0,0 +1,117 @@
1
+ module JVMArgs
2
+ class Args
3
+
4
+ def initialize(*initial_args,&block)
5
+ @args = Hash.new
6
+ @rules = JVMArgs::Rules.new
7
+ Types.each {|type| @args[type] = Hash.new }
8
+ server_arg = JVMArgs::Standard.new("-server")
9
+ @args[:standard][server_arg.key] = server_arg
10
+ set_default_heap_size
11
+ # in case user passed in an array
12
+ initial_args.flatten!
13
+ parse_args(initial_args) unless initial_args.empty?
14
+ self.instance_exec &block if block
15
+ end
16
+
17
+ def [](key)
18
+ @args[key]
19
+ end
20
+
21
+ def add(*args)
22
+ args.flatten!
23
+ parse_args(args)
24
+ end
25
+
26
+ def process_rules
27
+ @rules.rules.each do |rule|
28
+ @rules.send(rule, @args)
29
+ end
30
+ end
31
+
32
+ def parse_args(args)
33
+ args.each do |arg|
34
+ type = nil
35
+ jvm_arg = case arg
36
+ when /^-?XX.*/
37
+ type = :unstable
38
+ JVMArgs::Unstable.new(arg)
39
+ when /^-?X.*/
40
+ type = :nonstandard
41
+ JVMArgs::NonStandard.new(arg)
42
+ when /^-?D.*/
43
+ type = :directive
44
+ JVMArgs::Directive.new(arg)
45
+ else
46
+ type = :standard
47
+ JVMArgs::Standard.new(arg)
48
+ end
49
+ @args[type][jvm_arg.key] = jvm_arg
50
+ end
51
+ process_rules
52
+ end
53
+
54
+ def set_default_heap_size
55
+ if defined? node
56
+ total_ram = node['memory']['total'].sub(/kB/, '')
57
+ else
58
+ total_ram = (JVMArgs::Util.get_system_ram_m.sub(/M/,'').to_i * 0.4).to_i
59
+ end
60
+ @args[:nonstandard]['Xmx'] = JVMArgs::NonStandard.new("Xmx#{total_ram}M")
61
+ end
62
+
63
+ def to_s
64
+ process_rules
65
+ args_str = ""
66
+ Types.each do |type|
67
+ type_str = @args[type].map {|k,v| v.to_s }
68
+ args_str << " " + type_str.join(' ') + " "
69
+ end
70
+ args_str
71
+ end
72
+
73
+ def parse_named_args(named_args)
74
+ named_args.each do |k,v|
75
+ case k.to_sym
76
+ when :jmx
77
+ add_default_jmx
78
+ end
79
+ end
80
+ end
81
+
82
+ def jmx(boolean)
83
+ if boolean
84
+ [
85
+ "-Djava.rmi.server.hostname=127.0.0.1",
86
+ "-Dcom.sun.management.jmxremote",
87
+ "-Dcom.sun.management.jmxremote",
88
+ "-Dcom.sun.management.jmxremote.port=9000",
89
+ "-Dcom.sun.management.jmxremote.authenticate=false",
90
+ "-Dcom.sun.management.jmxremote.ssl=false"
91
+ ].each do |arg|
92
+ directive = JVMArgs::Directive.new arg
93
+ @args[:directive][directive.key] = directive
94
+ end
95
+ end
96
+ end
97
+
98
+ def heap_size(percentage)
99
+ percentage =~ /([0-9]+)%/
100
+ percent = $1.to_i * 0.01
101
+ if percent > 1
102
+ raise ArgumentError, "heap_size percentage must be less than 100%, you provided #{$1}%"
103
+ elsif percent < 0.01
104
+ raise ArgumentError, "heap_size percentage must be between 100% - 1%, you provided #{$1}%"
105
+ end
106
+ percent_int = JVMArgs::Util.get_system_ram_m.sub(/M/,'').to_i
107
+ percentage_ram = (percent_int * percent).to_i
108
+ @args[:nonstandard]["Xmx"] = JVMArgs::NonStandard.new("-Xmx#{percentage_ram}M")
109
+ @args[:nonstandard]["Xms"] = JVMArgs::NonStandard.new("-Xms#{percentage_ram}M")
110
+ end
111
+
112
+ def add_rule(rule_name, &block)
113
+ @rules.add(rule_name, block)
114
+ end
115
+
116
+ end
117
+ end
@@ -0,0 +1,29 @@
1
+ module JVMArgs
2
+
3
+ class Directive
4
+
5
+ attr_accessor :key, :value
6
+
7
+ def initialize(arg)
8
+ arg.sub!(/^-?D?/, '')
9
+
10
+ if arg =~ /(.*?)=(.*)/
11
+ @key = $1
12
+ @value = $2
13
+ else
14
+ @key = arg
15
+ @value = true
16
+ end
17
+ end
18
+
19
+ def to_s
20
+ if @value == true
21
+ "-D#{@key}"
22
+ else
23
+ "-D#{@key}=#{@value}"
24
+ end
25
+ end
26
+
27
+ end
28
+
29
+ end
@@ -0,0 +1,32 @@
1
+ module JVMArgs
2
+ module Rules
3
+
4
+ def self.heap_too_big(key="Xmx",args)
5
+ total_ram = JVMArgs::Util.get_raw_num(JVMArgs::Util.get_system_ram_m)
6
+ new_ram = JVMArgs::Util.get_raw_num(args[:nonstandard][key].value)
7
+ if new_ram > total_ram
8
+ raise ArgumentError, "You can't set #{key} to larger than the available system RAM of #{total_ram}"
9
+ end
10
+ end
11
+
12
+ def self.max_smaller_than_min(key="Xmx",args)
13
+ if args[:nonstandard]["Xms"].nil?
14
+ return
15
+ else
16
+ max_heap = JVMArgs::Util.get_raw_num(args[:nonstandard]["Xmx"].value)
17
+ min_heap = JVMArgs::Util.get_raw_num(args[:nonstandard]["Xms"].value)
18
+ if max_heap < min_heap
19
+ raise ArgumentError, "Max heap size #{max_heap}M cannot be smaller than Minimum heap size #{min_heap}M"
20
+ end
21
+ end
22
+ end
23
+
24
+ end
25
+ end
26
+
27
+ # HeapRules = [
28
+ # method(:heap_too_big).to_proc,
29
+ # method(:max_smaller_than_min).to_proc
30
+ # ]
31
+
32
+
@@ -0,0 +1,24 @@
1
+ module JVMArgs
2
+ class NonStandard
3
+ attr_accessor :key, :value
4
+
5
+ def initialize(arg)
6
+ arg.sub!(/^-/, '')
7
+ arg =~ /(X[a-z]+)([0-9]+[a-zA-Z])?/
8
+ @key = $1
9
+ if $2.nil?
10
+ @value = true
11
+ else
12
+ @value = JVMArgs::Util.convert_to_m($2)
13
+ end
14
+ end
15
+
16
+ def to_s
17
+ if @value == true
18
+ "-#{@key}"
19
+ else
20
+ "-#{@key}#{@value}"
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,51 @@
1
+ module JVMArgs
2
+ class Rules
3
+
4
+ def initialize(*args)
5
+ @rules = []
6
+ end
7
+
8
+ def each
9
+ @rules.each do |rule|
10
+ yield(rule)
11
+ end
12
+ end
13
+
14
+ def add(rule_name, block)
15
+ new_rule_name = "rule_#{rule_name.to_s}".to_sym
16
+ JVMArgs::Rules.define_method(new_rule_name) {|args| block.call(args) }
17
+ end
18
+
19
+ def rules
20
+ JVMArgs::Rules.public_instance_methods(false).select {|rule| rule.to_s =~ /^rule_.*/ }
21
+ end
22
+
23
+ def [](index)
24
+ @rules[index]
25
+ end
26
+
27
+
28
+ def rule_heap_too_big(key="Xmx",args)
29
+ total_ram = JVMArgs::Util.get_raw_num(JVMArgs::Util.get_system_ram_m)
30
+ new_ram = JVMArgs::Util.get_raw_num(args[:nonstandard][key].value)
31
+ if new_ram > total_ram
32
+ raise ArgumentError, "You can't set #{key} to larger than the available system RAM of #{total_ram}"
33
+ end
34
+ end
35
+
36
+ def rule_max_smaller_than_min(key="Xmx",args)
37
+ if args[:nonstandard]["Xms"].nil?
38
+ return
39
+ else
40
+ max_heap = JVMArgs::Util.get_raw_num(args[:nonstandard]["Xmx"].value)
41
+ min_heap = JVMArgs::Util.get_raw_num(args[:nonstandard]["Xms"].value)
42
+ if max_heap < min_heap
43
+ raise ArgumentError, "Max heap size #{max_heap}M cannot be smaller than Minimum heap size #{min_heap}M"
44
+ end
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+
@@ -0,0 +1,22 @@
1
+ module JVMArgs
2
+
3
+ class Standard
4
+
5
+ attr_accessor :key, :value
6
+
7
+ def initialize(arg)
8
+ arg.sub!(/^-/, '')
9
+ @key = arg
10
+ @value = true
11
+ end
12
+
13
+ def to_s
14
+ if @value
15
+ "-#{@key}"
16
+ else
17
+ ""
18
+ end
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,30 @@
1
+ module JVMArgs
2
+ class Unstable
3
+
4
+ attr_accessor :key, :value
5
+
6
+ def initialize(arg)
7
+ arg.sub!(/^-?XX:/, '')
8
+ # check it if a boolean option
9
+ arg =~ /(\+|-)(.*)/
10
+ if !$1.nil?
11
+ @key = $2
12
+ @value = $1 == '+' ? true : false
13
+ else
14
+ arg =~ /(.*)=(.*)/
15
+ @key = $1
16
+ @value = $2
17
+ end
18
+ end
19
+
20
+ def to_s
21
+ if @value.class == TrueClass or @value.class == FalseClass
22
+ symbol = @value ? '+' : '-'
23
+ "-XX:#{symbol}#{@key}"
24
+ else
25
+ "-XX:#{@key}=#{@value}"
26
+ end
27
+ end
28
+ end
29
+
30
+ end
@@ -0,0 +1,43 @@
1
+ module JVMArgs
2
+ module Util
3
+
4
+
5
+ def self.get_system_ram_m
6
+ if defined? node
7
+ total_ram = node['memory']['total']
8
+ else
9
+ require 'ohai'
10
+ ohai = Ohai::System.new
11
+ ohai.require_plugin "linux::memory"
12
+ total_ram = ohai["memory"]["total"]
13
+ end
14
+ self.convert_to_m(total_ram)
15
+ end
16
+
17
+ def self.get_raw_num(number)
18
+ number.sub(/M/, '').to_i
19
+ end
20
+
21
+ def self.convert_to_m(number)
22
+ raw_num, unit = self.normalize_units(number)
23
+ case unit
24
+ when "K"
25
+ converted_num = (raw_num/1024).to_int
26
+ when "G"
27
+ converted_num = (raw_num * 1024).to_int
28
+ when "M"
29
+ converted_num = raw_num
30
+ end
31
+ "#{converted_num}M"
32
+ end
33
+
34
+ def self.normalize_units(number)
35
+ number =~ /^([0-9]+)\s*([kKmMgG])[bB]?/
36
+ if $2.nil?
37
+ raise ArgumentError, "You must indicate whether number is KiloBytes(K), MegaBytes (M), or Gigabytes(G)"
38
+ end
39
+ [$1.to_i, $2.upcase]
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,4 @@
1
+ module JVMArgs
2
+ # The gem version
3
+ VERSION = '0.0.2'
4
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jvmargs
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Bryan W. Berry
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: chef
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '10.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '10.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rspec
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: 2.13.0
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: 2.13.0
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: 0.9.2.2
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.9.2.2
62
+ - !ruby/object:Gem::Dependency
63
+ name: pry
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: Sanity check command-line arguments to the JVM
79
+ email:
80
+ executables: []
81
+ extensions: []
82
+ extra_rdoc_files: []
83
+ files:
84
+ - lib/jvmargs.rb
85
+ - lib/jvmargs/args.rb
86
+ - lib/jvmargs/nonstandard.rb
87
+ - lib/jvmargs/version.rb
88
+ - lib/jvmargs/rules.rb
89
+ - lib/jvmargs/util.rb
90
+ - lib/jvmargs/heaprules.rb
91
+ - lib/jvmargs/directive.rb
92
+ - lib/jvmargs/standard.rb
93
+ - lib/jvmargs/unstable.rb
94
+ homepage: https://github.com/bryanwb/jvmargs
95
+ licenses:
96
+ - Apache2
97
+ post_install_message:
98
+ rdoc_options: []
99
+ require_paths:
100
+ - lib
101
+ required_ruby_version: !ruby/object:Gem::Requirement
102
+ none: false
103
+ requirements:
104
+ - - ! '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ! '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 1.8.24
116
+ signing_key:
117
+ specification_version: 3
118
+ summary: jvmargs-0.0.2
119
+ test_files: []
120
+ has_rdoc: