2020require 'java_buildpack/util/shell'
2121require 'java_buildpack/util/qualify_path'
2222require 'open3'
23+ require 'tmpdir'
2324
2425module JavaBuildpack
2526 module Jre
@@ -32,15 +33,19 @@ class OpenJDKLikeMemoryCalculator < JavaBuildpack::Component::VersionedDependenc
3233 def compile
3334 download ( @version , @uri ) do |file |
3435 FileUtils . mkdir_p memory_calculator . parent
36+
3537 if @version [ 0 ] < '2'
3638 unpack_calculator file
3739 else
3840 unpack_compressed_calculator file
3941 end
42+
4043 memory_calculator . chmod 0o755
41- end
4244
43- show_settings memory_calculation_string ( Pathname . new ( Dir . pwd ) )
45+ puts " Loaded Classes: #{ class_count @configuration } , " \
46+ "Threads: #{ stack_threads @configuration } , " \
47+ "JAVA_OPTS: '#{ java_opts } '"
48+ end
4449 end
4550
4651 # Returns a fully qualified memory calculation command to be prepended to the buildpack's command sequence
@@ -64,6 +69,24 @@ def supports?
6469
6570 private
6671
72+ def actual_class_count ( root )
73+ ( root + '**/*.class' ) . glob . count +
74+ ( root + '**/*.groovy' ) . glob . count +
75+ ( root + '**/*.jar' ) . glob . inject ( 0 ) { |a , e | a + archive_class_count ( e ) }
76+ end
77+
78+ def archive_class_count ( archive )
79+ `unzip -l #{ archive } | grep '\\ (\\ .class\\ |\\ .groovy\\ )$' | wc -l` . to_i
80+ end
81+
82+ def class_count ( configuration )
83+ configuration [ 'class_count' ] || ( 0.2 * actual_class_count ( @application . root ) ) . ceil + 5500
84+ end
85+
86+ def java_opts
87+ ENV [ 'JAVA_OPTS' ]
88+ end
89+
6790 def memory_calculator
6891 @droplet . sandbox + "bin/java-buildpack-memory-calculator-#{ @version } "
6992 end
@@ -74,24 +97,22 @@ def memory_calculator_tar
7497 end
7598
7699 def memory_calculation_string ( relative_path )
77- " #{ qualify_path memory_calculator , relative_path } -memorySizes= #{ memory_sizes @configuration } " \
78- "-memoryWeights= #{ memory_weights @configuration } -memoryInitials= #{ memory_initials @configuration } " \
79- " #{ stack_threads @configuration } -totMemory=$MEMORY_LIMIT "
80- end
81-
82- def memory_sizes ( configuration )
83- memory_sizes = version_specific configuration [ 'memory_sizes' ]
84- memory_sizes . map { | k , v | " #{ k } : #{ v } " } . join ( ', ' )
100+ memory_calculation_string = [ qualify_path ( memory_calculator , relative_path ) ]
101+ memory_calculation_string << '-totMemory=$MEMORY_LIMIT'
102+ memory_calculation_string << "-stackThreads= #{ stack_threads @configuration } "
103+ memory_calculation_string << "-loadedClasses= #{ class_count @configuration } "
104+ memory_calculation_string << "-poolType= #{ pool_type } "
105+ memory_calculation_string << "-vmOptions=' #{ java_opts } '" if java_opts
106+
107+ memory_calculation_string . join ( ' ' )
85108 end
86109
87- def memory_weights ( configuration )
88- memory_heuristics = version_specific configuration [ 'memory_heuristics' ]
89- memory_heuristics . map { |k , v | "#{ k } :#{ v } " } . join ( ',' )
110+ def pool_type
111+ @droplet . java_home . java_8_or_later? ? 'metaspace' : 'permgen'
90112 end
91113
92- def memory_initials ( configuration )
93- memory_initials = version_specific configuration [ 'memory_initials' ]
94- memory_initials . map { |k , v | "#{ k } :#{ v } " } . join ( ',' )
114+ def stack_threads ( configuration )
115+ configuration [ 'stack_threads' ]
95116 end
96117
97118 def unpack_calculator ( file )
@@ -103,33 +124,6 @@ def unpack_compressed_calculator(file)
103124 FileUtils . mv ( memory_calculator_tar , memory_calculator )
104125 end
105126
106- def stack_threads ( configuration )
107- configuration [ 'stack_threads' ] ? " -stackThreads=#{ configuration [ 'stack_threads' ] } " : ''
108- end
109-
110- def version_specific ( configuration )
111- if @droplet . java_home . java_8_or_later?
112- configuration . delete 'permgen'
113- else
114- configuration . delete 'metaspace'
115- end
116-
117- configuration
118- end
119-
120- def show_settings ( *args )
121- Open3 . popen3 ( *args ) do |_stdin , stdout , stderr , wait_thr |
122- status = wait_thr . value
123- stderr_content = stderr . gets nil
124- stdout_content = stdout . gets nil
125-
126- puts " #{ stderr_content } " if stderr_content
127-
128- raise unless status . success?
129- puts " Memory Settings: #{ stdout_content } "
130- end
131- end
132-
133127 end
134128
135129 end
0 commit comments