Second refactor: Manage the heap
At Puppet, we manage the Java heap size for the Jenkins app. Production servers didn't have enough memory for heavy use.
The Jenkins module has a jenkins::sysconfig
defined type for managing system properties, so let's use
it:
# Manage the heap size on the controller, in MB. if($::memorysize_mb =~ Number and $::memorysize_mb > 8192) { # anything over 8GB we should keep max 4GB for OS and others $heap = sprintf('%.0f', $::memorysize_mb - 4096) } else { # This is calculated as 50% of the total memory. $heap = sprintf('%.0f', $::memorysize_mb * 0.5) } # Set java params, like heap min and max sizes. See # https://wiki.jenkins-ci.org/display/JENKINS/Features+controlled+by+system+properties jenkins::sysconfig { 'JAVA_ARGS': value => "-Xms${heap}m -Xmx${heap}m -Djava.awt.headless=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -Dhudson.model.DirectoryBrowserSupport.CSP=\\\"default-src 'self'; img-src 'self'; style-src 'self';\\\"", }
Rule 4 again — we couldn't hardcode this, because we have some smaller Jenkins
controllers that can't spare the extra memory. But because our production controllers are
always on more powerful machines, we can calculate the heap based on the machine's memory
size, which we can access as a fact. This lets us avoid extra configuration.
Diff of second refactor
@@ -16,4 +16,20 @@ class profile::jenkins::controller ( # When not using the jenkins module's java version, install java8. unless $install_jenkins_java { include profile::jenkins::usage::java8 } + + # Manage the heap size on the controller, in MB. + if($::memorysize_mb =~ Number and $::memorysize_mb > 8192) + { + # anything over 8GB we should keep max 4GB for OS and others + $heap = sprintf('%.0f', $::memorysize_mb - 4096) + } else { + # This is calculated as 50% of the total memory. + $heap = sprintf('%.0f', $::memorysize_mb * 0.5) + } + # Set java params, like heap min and max sizes. See + # https://wiki.jenkins-ci.org/display/JENKINS/Features+controlled+by+system+properties + jenkins::sysconfig { 'JAVA_ARGS': + value => "-Xms${heap}m -Xmx${heap}m -Djava.awt.headless=true -XX:+UseConcMarkSweepGC -XX:+CMSClassUnloadingEnabled -Dhudson.model.DirectoryBrowserSupport.CSP=\\\"default-src 'self'; img-src 'self'; style-src 'self';\\\"", + } + }