Skip to content

Commit a76bb02

Browse files
committed
Merge 52129161-remove-system-properties to master
[Completes #52129161]
2 parents 77680ef + f188f30 commit a76bb02

9 files changed

Lines changed: 225 additions & 686 deletions

config/openjdk.yml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,7 @@
1818
repository_root: "http://download.pivotal.io.s3.amazonaws.com/openjdk/lucid/x86_64"
1919
version: 1.7.0_+
2020
memory_heuristics:
21-
post_8:
22-
heap: 0.75
23-
metaspace: 0.1
24-
stack: 0.05
25-
native: 0.1
26-
pre_8:
27-
heap: 0.75
28-
permgen: 0.1
29-
stack: 0.05
30-
native: 0.1
21+
heap: 0.75
22+
permgen: 0.1
23+
stack: 0.05
24+
native: 0.1

lib/java_buildpack/jre/memory/memory_heuristics_openjdk.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@ class MemoryHeuristicsOpenJDK < WeightBalancingMemoryHeuristic
2424
# Creates an instance based on a hash containing memory settings, a configuration file containing weightings, and
2525
# the application's memory size in $MEMORY_LIMIT.
2626
#
27-
# @param [Hash<String, Numeric>] specified_sizes any sizings specified by the user
28-
# @param [Hash<Stirng, Numeric>] memory_heuristics the memory heuristics for OpenJDK
29-
def initialize(specified_sizes, memory_heuristics)
30-
super(specified_sizes, memory_heuristics[WEIGHTINGS_NAME])
27+
# @param [Hash<String, Numeric>] sizes any sizings specified by the user
28+
# @param [Hash<Stirng, Numeric>] heuristics the memory heuristics for OpenJDK
29+
def initialize(sizes, heuristics)
30+
super(sizes, heuristics, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS)
3131
end
3232

3333
private
3434

35-
WEIGHTINGS_NAME = 'post_8'
35+
JAVA_OPTS = {
36+
'heap' => '-Xmx',
37+
'metaspace' => '-XX:MaxMetaspaceSize=',
38+
'stack' => '-Xss',
39+
}.freeze
40+
41+
VALID_HEURISTICS = ['heap', 'metaspace', 'stack', 'native']
42+
43+
VALID_SIZES = ['heap', 'metaspace', 'stack']
3644

3745
end
3846
end

lib/java_buildpack/jre/memory/memory_heuristics_openjdk_pre8.rb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,24 @@ class MemoryHeuristicsOpenJDKPre8 < WeightBalancingMemoryHeuristic
2424
# Creates an instance based on a hash containing memory settings, a configuration file containing weightings, and
2525
# the application's memory size in $MEMORY_LIMIT.
2626
#
27-
# @param [Hash<String, Numeric>] specified_sizes any sizings specified by the user
28-
# @param [Hash<Stirng, Numeric>] memory_heuristics the memory heuristics for OpenJDK
29-
def initialize(specified_sizes, memory_heuristics)
30-
super(specified_sizes, memory_heuristics[WEIGHTINGS_NAME])
27+
# @param [Hash<String, Numeric>] sizes any sizings specified by the user
28+
# @param [Hash<String, Numeric>] heuristics the memory heuristics for OpenJDK
29+
def initialize(sizes, heuristics)
30+
super(sizes, heuristics, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS)
3131
end
3232

3333
private
3434

35-
WEIGHTINGS_NAME = 'pre_8'
35+
JAVA_OPTS = {
36+
'heap' => '-Xmx',
37+
'permgen' => '-XX:MaxPermSize=',
38+
'stack' => '-Xss',
39+
}.freeze
40+
41+
VALID_HEURISTICS = ['heap', 'permgen', 'stack', 'native']
42+
43+
VALID_SIZES = ['heap', 'permgen', 'stack']
3644

3745
end
46+
3847
end

lib/java_buildpack/jre/memory/weight_balancing_memory_heuristic.rb

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,50 +24,41 @@ module JavaBuildpack::Jre
2424
# A utility for defaulting Java memory settings.
2525
class WeightBalancingMemoryHeuristic
2626

27-
# @!attribute [r] output
28-
# @return [Hash] a hash of memory types and corresponding memory sizes, e.g. {'memory-type' => '1M'}
29-
attr_reader :output
30-
31-
# Creates an instance based on user-specified sizes weightings, and the application's memory size in $MEMORY_LIMIT.
27+
# Creates an instance based on a hash containing memory settings, and the application's memory size in
28+
# $MEMORY_LIMIT.
3229
#
33-
# @param [Hash<String, Numeric>] specified_sizes any sizings specified by the user
34-
# @param [Hash<String, Numeric>] weightings the weightings for this version of the JRE
35-
def initialize(specified_sizes, weightings)
36-
memory_limit = MemoryLimit.memory_limit
37-
38-
buckets = WeightBalancingMemoryHeuristic.create_memory_buckets(specified_sizes, weightings, memory_limit)
30+
# @param [Hash<String, Numeric>] sizes any sizings specified by the user
31+
# @param [Hash<String, Numeric>] heuristics the memory heuristics specified by the user
32+
# @param [Array<String>] valid_sizes the valid size keys
33+
# @param [Array<String>] valid_heuristics the valid heuristics keys
34+
# @param [Hash<String, String>] java_opts a mapping from a memory type to a +JAVA_OPTS+ option
35+
def initialize(sizes, heuristics, valid_sizes, valid_heuristics, java_opts)
36+
validate 'size', valid_sizes, sizes.keys
37+
validate 'heuristic', valid_heuristics, heuristics.keys
38+
39+
@sizes = sizes
40+
@heuristics = heuristics
41+
@java_opts = java_opts
42+
end
3943

40-
WeightBalancingMemoryHeuristic.balance_buckets(specified_sizes, buckets, memory_limit)
44+
def resolve
45+
memory_limit = MemoryLimit.memory_limit
46+
buckets = create_memory_buckets(@sizes, @heuristics, memory_limit)
4147

42-
WeightBalancingMemoryHeuristic.issue_memory_wastage_warning(buckets) if memory_limit
48+
balance_buckets(@sizes, buckets, memory_limit)
49+
issue_memory_wastage_warning(buckets) if memory_limit
4350

44-
@output = {}
45-
buckets.each_pair do |memory_type, bucket|
46-
@output[memory_type] = bucket.size.to_s if bucket.size
47-
end
51+
buckets.map { |type, bucket| "#{@java_opts[type]}#{bucket.size}" if bucket.size && @java_opts.has_key?(type) }.compact
4852
end
4953

5054
private
5155

52-
def self.create_memory_buckets(specified_sizes, weightings, memory_limit)
53-
buckets = {}
54-
total_weighting = 0
55-
weightings.each_pair do |memory_type, weighting|
56-
value = specified_sizes[memory_type]
57-
58-
buckets[memory_type] = WeightBalancingMemoryHeuristic.create_memory_bucket(
59-
memory_type, weighting, value ? MemorySize.new(value) : nil, memory_limit)
60-
61-
total_weighting += weighting
62-
end
63-
64-
raise "Invalid configuration: sum of weightings is greater than 1" if total_weighting > 1
65-
buckets
66-
end
56+
NATIVE_MEMORY_WARNING_FACTOR = 3
6757

68-
def self.balance_buckets(specified_sizes, buckets, memory_limit)
58+
def balance_buckets(sizes, buckets, memory_limit)
6959
total_excess = MemorySize.ZERO
7060
total_adjustable_weighting = 0
61+
7162
buckets.each_value do |bucket|
7263
xs = bucket.excess
7364
total_excess = total_excess + xs
@@ -76,26 +67,50 @@ def self.balance_buckets(specified_sizes, buckets, memory_limit)
7667

7768
buckets.each_value do |bucket|
7869
bucket.adjust(total_excess, total_adjustable_weighting)
79-
raise "Total memory #{memory_limit} exceeded by configured memory #{specified_sizes}" if bucket.size && bucket.size < MemorySize.ZERO
70+
raise "Total memory #{memory_limit} exceeded by configured memory #{sizes}" if bucket.size && bucket.size < MemorySize.ZERO
8071
end
8172
end
8273

83-
NATIVE_MEMORY_WARNING_FACTOR = 3
84-
85-
def self.create_memory_bucket(memory_type, weighting, size, total_memory)
86-
if memory_type == 'stack'
87-
StackMemoryBucket.new(weighting, size, total_memory)
74+
def create_memory_bucket(type, weighting, size, memory_limit)
75+
if type == 'stack'
76+
StackMemoryBucket.new(weighting, size, memory_limit)
8877
else
89-
MemoryBucket.new(memory_type, weighting, size, true, total_memory)
78+
MemoryBucket.new(type, weighting, size, true, memory_limit)
9079
end
9180
end
9281

93-
def self.issue_memory_wastage_warning(buckets)
82+
def create_memory_buckets(sizes, heuristics, memory_limit)
83+
buckets = {}
84+
total_weighting = 0
85+
86+
heuristics.each_pair do |type, weighting|
87+
size = nil_safe_size sizes[type]
88+
buckets[type] = create_memory_bucket(type, weighting, size, memory_limit)
89+
total_weighting += weighting
90+
end
91+
92+
raise "Invalid configuration: sum of weightings is greater than 1" if total_weighting > 1
93+
94+
buckets
95+
end
96+
97+
def issue_memory_wastage_warning(buckets)
9498
native_bucket = buckets['native']
9599
if native_bucket && native_bucket.size > native_bucket.default_size * NATIVE_MEMORY_WARNING_FACTOR
96100
$stderr.puts "-----> WARNING: there is #{NATIVE_MEMORY_WARNING_FACTOR} times more spare native memory than the default, so configured Java memory may be too small."
97101
end
98102
end
99103

104+
def nil_safe_size(size)
105+
size ? MemorySize.new(size) : nil
106+
end
107+
108+
def validate(type, expected, actual)
109+
actual.each do |key|
110+
raise "'#{key}' is not a valid memory #{type}" unless expected.include? key
111+
end
112+
end
113+
100114
end
115+
101116
end

lib/java_buildpack/jre/openjdk.rb

Lines changed: 7 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ class OpenJdk
3636
def initialize(context)
3737
@app_dir = context[:app_dir]
3838
@java_opts = context[:java_opts]
39-
@java_opts << "-XX:OnOutOfMemoryError='kill -9 %p'"
4039
@configuration = context[:configuration]
4140
@version, @uri = OpenJdk.find_openjdk(@configuration)
4241

@@ -48,7 +47,6 @@ def initialize(context)
4847
#
4948
# @return [String, nil] returns +openjdk-<version>+.
5049
def detect
51-
memory_sizes @configuration # drive out errors early
5250
id @version
5351
end
5452

@@ -69,7 +67,8 @@ def compile
6967
#
7068
# @return [void]
7169
def release
72-
@java_opts.concat to_java_opts(memory_sizes(@configuration))
70+
@java_opts << "-XX:OnOutOfMemoryError='kill -9 %p'"
71+
@java_opts.concat memory(@configuration)
7372
end
7473

7574
private
@@ -78,24 +77,7 @@ def release
7877

7978
KEY_MEMORY_HEURISTICS = 'memory_heuristics'
8079

81-
MAPPINGS = {
82-
'heap' => {
83-
:switch => '-Xmx',
84-
:system_property => 'java.heap.size'.freeze
85-
},
86-
'metaspace' => {
87-
:switch => '-XX:MaxMetaspaceSize=',
88-
:system_property => 'java.metaspace.size'.freeze
89-
},
90-
'permgen' => {
91-
:switch => '-XX:MaxPermSize=',
92-
:system_property => 'java.permgen.size'.freeze
93-
},
94-
'stack' => {
95-
:switch => '-Xss',
96-
:system_property => 'java.stack.size'.freeze
97-
}
98-
}
80+
KEY_MEMORY_SIZES = 'memory_sizes'
9981

10082
def expand(file)
10183
expand_start_time = Time.now
@@ -122,40 +104,18 @@ def java_home
122104
File.join @app_dir, JAVA_HOME
123105
end
124106

125-
def memory_sizes(configuration)
126-
specified_sizes = specified_sizes(configuration)
127-
memory_heuristics = configuration[KEY_MEMORY_HEURISTICS]
107+
def memory(configuration)
108+
heuristics = configuration[KEY_MEMORY_HEURISTICS] || {}
109+
sizes = configuration[KEY_MEMORY_SIZES] || {}
128110

129111
heuristic_class = pre_8 ? MemoryHeuristicsOpenJDKPre8 : MemoryHeuristicsOpenJDK
130-
heuristic_class.new(specified_sizes, memory_heuristics).output
112+
heuristic_class.new(sizes, heuristics).resolve
131113
end
132114

133115
def pre_8
134116
@version < JavaBuildpack::Util::TokenizedVersion.new("1.8.0")
135117
end
136118

137-
def specified_sizes(configuration)
138-
specified_sizes = {}
139-
140-
MAPPINGS.each_pair do |key, mapping|
141-
system_property = mapping[:system_property]
142-
specified_sizes[key] = configuration[system_property] if configuration.has_key? system_property
143-
end
144-
145-
specified_sizes
146-
end
147-
148-
def to_java_opts(memory_values)
149-
java_opts = []
150-
151-
memory_values.each_pair do |key, memory_value|
152-
mapping = MAPPINGS[key]
153-
java_opts << "#{mapping[:switch]}#{memory_value}" if mapping
154-
end
155-
156-
java_opts
157-
end
158-
159119
end
160120

161121
end

0 commit comments

Comments
 (0)