Skip to content

Commit e76b65f

Browse files
author
Glyn Normington
committed
Merge 52692569-warn-if-memory-close-to-default to master
[Completes #52692569]
2 parents 519e6b9 + a0c3382 commit e76b65f

3 files changed

Lines changed: 52 additions & 2 deletions

File tree

lib/java_buildpack/jre/memory/stack_memory_bucket.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,29 @@ class StackMemoryBucket < MemoryBucket
3333
# @param [Numeric] total_memory the total virtual memory size of the operating system process in KB
3434
def initialize(weighting, size, total_memory)
3535
super('stack', weighting, size, false, total_memory)
36+
@weighting = weighting
37+
@total_memory = total_memory
3638
set_size(DEFAULT_STACK_SIZE) unless size
3739
end
3840

3941
# Returns the excess memory in this memory bucket.
4042
#
4143
# @return [Numeric] the excess memory in KB
4244
def excess
43-
if default_size
44-
size ? default_size * ((size - DEFAULT_STACK_SIZE) / DEFAULT_STACK_SIZE) : 0
45+
if @total_memory
46+
size ? @total_memory * @weighting * ((size - DEFAULT_STACK_SIZE) / DEFAULT_STACK_SIZE) : 0
4547
else
4648
MemorySize.ZERO
4749
end
4850
end
4951

52+
# Returns the default stack size.
53+
#
54+
# @return [MemorySize, nil] the default memory size or nil if there is no default
55+
def default_size
56+
DEFAULT_STACK_SIZE
57+
end
58+
5059
private
5160

5261
DEFAULT_STACK_SIZE = MemorySize.new('1024K') # 1 MB

lib/java_buildpack/jre/memory/weight_balancing_memory_heuristic.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def resolve
5858

5959
balance_buckets(@sizes, buckets, memory_limit)
6060
issue_memory_wastage_warning(buckets) if memory_limit
61+
issue_close_to_default_warnings(buckets, @heuristics, memory_limit)
6162

6263
buckets.map { |type, bucket| "#{@java_opts[type]}#{bucket.size}" if bucket.size && @java_opts.has_key?(type) }.compact
6364
end
@@ -66,6 +67,8 @@ def resolve
6667

6768
NATIVE_MEMORY_WARNING_FACTOR = 3
6869

70+
CLOSE_TO_DEFAULT_FACTOR = 0.1
71+
6972
def balance_buckets(sizes, buckets, memory_limit)
7073
total_excess = MemorySize.ZERO
7174
total_adjustable_weighting = 0
@@ -122,6 +125,22 @@ def validate(type, expected, actual)
122125
end
123126
end
124127

128+
def issue_close_to_default_warnings(buckets, heuristics, memory_limit)
129+
# Check each specified memory size to see if it is close to the default.
130+
buckets.each do |type, bucket|
131+
if @sizes[type]
132+
default_size = bucket.default_size
133+
actual_size = bucket.size
134+
if default_size != MemorySize.ZERO
135+
factor = ((actual_size - default_size) / default_size).abs
136+
end
137+
if (default_size == MemorySize.ZERO && actual_size == MemorySize.ZERO) || factor < CLOSE_TO_DEFAULT_FACTOR
138+
$stderr.puts "-----> WARNING: the configured value #{actual_size} of memory size #{type} is close to the default value #{default_size}. Consider deleting the configured value and taking the default."
139+
end
140+
end
141+
end
142+
end
143+
125144
end
126145

127146
end

spec/java_buildpack/jre/memory/weight_balancing_memory_heuristic_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,29 @@ module JavaBuildpack::Jre
183183
expect(output).to include('-Xss2M')
184184
expect($stderr.string).to match(/WARNING:/)
185185
end
186+
end
187+
188+
it 'should issue a warning when the specified stack size is close to the default' do
189+
with_memory_limit('4096m') do
190+
WeightBalancingMemoryHeuristic.new({'stack' => '1025k'}, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
191+
expect($stderr.string).to match(/WARNING:.*close to the default/)
192+
end
193+
end
194+
195+
it 'should issue a warning when the specified maximum heap size is close to the default' do
196+
with_memory_limit('4096m') do
197+
WeightBalancingMemoryHeuristic.new({'heap' => '2049m'}, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
198+
expect($stderr.string).to match(/WARNING:.*close to the default/)
186199
end
200+
end
201+
202+
it 'should issue a warning when the specified maximum permgen size is close to the default' do
203+
with_memory_limit('4096m') do
204+
WeightBalancingMemoryHeuristic.new({'permgen' => '1339m'}, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
205+
expect($stderr.string).to match(/WARNING:.*close to the default/)
206+
end
207+
end
208+
187209

188210
it 'should fail when the specified maximum memory is larger than the total memory size' do
189211
with_memory_limit('4096m') do

0 commit comments

Comments
 (0)