Skip to content

Commit 3d237a4

Browse files
author
Glyn Normington
committed
Use floating point for MemorySize division
Using whole number division resulted in close to default memory size warnings in cases where the memory size was far from the default. Using floating point division instead fixes the problem. Add diagnostics and extra tests. [#54997812]
1 parent 1cc2102 commit 3d237a4

5 files changed

Lines changed: 22 additions & 3 deletions

File tree

lib/java_buildpack/jre/memory/memory_bucket.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616

17+
require 'java_buildpack/diagnostics/logger_factory'
1718
require 'java_buildpack/jre'
1819
require 'java_buildpack/jre/memory/memory_size'
1920

@@ -47,6 +48,8 @@ def initialize(name, weighting, size, adjustable, total_memory)
4748
@adjustable = (validate_adjustable adjustable) && !@size_specified && total_memory
4849
@total_memory = total_memory ? validate_memory_size(total_memory, 'total_memory') : nil
4950
@size = @size_specified || default_size
51+
logger = JavaBuildpack::Diagnostics::LoggerFactory.get_logger
52+
logger.debug { inspect }
5053
end
5154

5255
# Returns the excess memory in this memory bucket.

lib/java_buildpack/jre/memory/memory_size.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def initialize(size)
3131
v = size[0..-2]
3232
raise "Invalid memory size '#{size}'" unless MemorySize.is_integer v
3333
v = size.to_i
34-
# Store the number of kilobytes.
34+
# Store the number of bytes.
3535
case unit
3636
when 'b', 'B'
3737
@bytes = v
@@ -107,8 +107,8 @@ def -(other)
107107
# @param [MemorySize, Numeric] other the memory size or numeric value to divide by
108108
# @return [MemorySize, Numeric] the result
109109
def /(other)
110-
return @bytes / other.bytes if other.is_a? MemorySize
111-
return MemorySize.from_numeric((@bytes / other).round) if other.is_a? Numeric
110+
return @bytes / other.bytes.to_f if other.is_a? MemorySize
111+
return MemorySize.from_numeric((@bytes / other.to_f).round) if other.is_a? Numeric
112112
raise "Cannot divide a MemorySize by an instance of #{other.class}"
113113
end
114114

lib/java_buildpack/jre/memory/weight_balancing_memory_heuristic.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ def issue_close_to_default_warnings(buckets, heuristics, memory_limit)
137137
actual_size = bucket.size
138138
if default_size != MemorySize::ZERO
139139
factor = ((actual_size - default_size) / default_size).abs
140+
@logger.debug { "factor for memory size #{type} is #{factor}" }
140141
end
141142
if (default_size == MemorySize::ZERO && actual_size == MemorySize::ZERO) || factor < CLOSE_TO_DEFAULT_FACTOR
142143
@logger.warn "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."

spec/java_buildpack/jre/memory/memory_size_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,18 @@ module JavaBuildpack::Jre
9898
expect(ONE_MEG / 2).to eq(HALF_A_MEG)
9999
end
100100

101+
it 'should divide a memory size by a numeric using floating point' do
102+
expect(MemorySize.new('3B') / 2).to eq(MemorySize.new('2B'))
103+
end
104+
101105
it 'should divide a memory size by another memory size correctly' do
102106
expect(ONE_MEG / HALF_A_MEG).to eq(2)
103107
end
104108

109+
it 'should divide a memory size by another memory size using floating point' do
110+
expect(HALF_A_MEG / ONE_MEG).to eq(0.5)
111+
end
112+
105113
it 'should fail when a memory size is divided by an incorrect type' do
106114
expect { MemorySize.new('1B') / '' }.to raise_error(/Cannot\ divide/)
107115
end

spec/java_buildpack/jre/memory/weight_balancing_memory_heuristic_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,13 @@ module JavaBuildpack::Jre
211211
end
212212
end
213213

214+
it 'should not issue a warning when the specified maximum permgen size is not close to the default' do
215+
with_memory_limit('1G') do
216+
WeightBalancingMemoryHeuristic.new({ 'permgen' => '128M' }, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve
217+
expect(buildpack_log_contents).not_to match(/WARN.*is close to the default/)
218+
end
219+
end
220+
214221
it 'should fail when the specified maximum memory is larger than the total memory size' do
215222
with_memory_limit('4096m') do
216223
expect { WeightBalancingMemoryHeuristic.new({ 'heap' => '5g' }, TEST_WEIGHTINGS, VALID_SIZES, VALID_HEURISTICS, JAVA_OPTS).resolve }.to raise_error(/exceeded/)

0 commit comments

Comments
 (0)