Skip to content

Commit

Permalink
Added functionality for Java Native Memory Tracking to new helper - nmt
Browse files Browse the repository at this point in the history
  • Loading branch information
pivotal-david-osullivan committed Sep 16, 2021
1 parent adf90db commit 203d78a
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (b Build) Build(context libcnb.BuildContext) (libcnb.BuildResult, error) {

if IsLaunchContribution(jrePlanEntry.Metadata) {
helpers := []string{"active-processor-count", "java-opts", "jvm-heap", "link-local-dns", "memory-calculator",
"openssl-certificate-loader", "security-providers-configurer", "jmx"}
"openssl-certificate-loader", "security-providers-configurer", "jmx", "nmt"}

if IsBeforeJava9(depJRE.Version) {
helpers = append(helpers, "security-providers-classpath-8")
Expand Down
2 changes: 2 additions & 0 deletions build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
"openssl-certificate-loader",
"security-providers-configurer",
"jmx",
"nmt",
"security-providers-classpath-8",
"debug-8",
}))
Expand Down Expand Up @@ -143,6 +144,7 @@ func testBuild(t *testing.T, context spec.G, it spec.S) {
"openssl-certificate-loader",
"security-providers-configurer",
"jmx",
"nmt",
"security-providers-classpath-9",
"debug-9",
}))
Expand Down
2 changes: 2 additions & 0 deletions cmd/helper/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func main() {
d8 = helper.Debug8{Logger: l}
d9 = helper.Debug9{Logger: l}
jm = helper.JMX{Logger: l}
n = helper.NMT{Logger: l}
)

file := "/etc/resolv.conf"
Expand All @@ -75,6 +76,7 @@ func main() {
"debug-8": d8,
"debug-9": d9,
"jmx": jm,
"nmt": n,
})
})
}
1 change: 1 addition & 0 deletions helper/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,6 @@ func TestUnit(t *testing.T) {
suite("Debug8", testDebug8)
suite("Debug9", testDebug9)
suite("JMX", testJMX)
suite("NMT", testNMT)
suite.Run(t)
}
32 changes: 32 additions & 0 deletions helper/nmt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package helper

import (
"os"
"strings"

"github.com/paketo-buildpacks/libpak/bard"
)

type NMT struct {
Logger bard.Logger
}

func (n NMT) Execute() (map[string]string, error) {

if s, ok := os.LookupEnv("BPL_JAVA_NMT_ENABLED"); ok && strings.ToLower(s) == "false" {
n.Logger.Info("Disabling Java Native Memory Tracking")
return nil, nil
}

n.Logger.Info("Enabling Java Native Memory Tracking")
var values []string
if s, ok := os.LookupEnv("JAVA_TOOL_OPTIONS"); ok {
values = append(values, s)
}
values = append(values, "-XX:+UnlockDiagnosticVMOptions", "-XX:NativeMemoryTracking=summary", "-XX:+PrintNMTStatistics")

// NMT_LEVEL_1 Required for Java Native Memory Tracking to work due to bug which is not fixed until Java v18 (https://bugs.openjdk.java.net/browse/JDK-8256844)
// '1' = PID of Java process in the container. Value for NMT level should match that passed to '-XX:NativeMemoryTracking' in the NMT helper.
return map[string]string{"NMT_LEVEL_1": "summary", "JAVA_TOOL_OPTIONS": strings.Join(values, " ")}, nil

}
58 changes: 58 additions & 0 deletions helper/nmt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package helper_test

import (
"os"
"testing"

. "github.com/onsi/gomega"
"github.com/paketo-buildpacks/libjvm/helper"
"github.com/sclevine/spec"
)

func testNMT(t *testing.T, context spec.G, it spec.S) {
var (
Expect = NewWithT(t).Expect

n = helper.NMT{}
)

it("returns if $BPL_JAVA_NMT_ENABLED is set to false", func() {
Expect(os.Setenv("BPL_JAVA_NMT_ENABLED", "false")).To(Succeed())
Expect(n.Execute()).To(BeNil())
})

context("$BPL_JAVA_NMT_ENABLED", func() {
it.Before(func() {
Expect(os.Setenv("BPL_JAVA_NMT_ENABLED", "true")).To(Succeed())
Expect(os.Setenv("NMT_LEVEL_1", "summary")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("BPL_JAVA_NMT_ENABLED")).To(Succeed())
Expect(os.Unsetenv("NMT_LEVEL_1")).To(Succeed())
})

it("contributes configuration", func() {
Expect(n.Execute()).To(Equal(map[string]string{"NMT_LEVEL_1": "summary",
"JAVA_TOOL_OPTIONS": "-XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics",
}))
})

context("$JAVA_TOOL_OPTIONS", func() {
it.Before(func() {
Expect(os.Setenv("JAVA_TOOL_OPTIONS", "test-java-tool-options")).To(Succeed())
})

it.After(func() {
Expect(os.Unsetenv("JAVA_TOOL_OPTIONS")).To(Succeed())
})

it("contributes configuration appended to existing $JAVA_TOOL_OPTIONS", func() {
Expect(n.Execute()).To(Equal(map[string]string{"NMT_LEVEL_1": "summary",
"JAVA_TOOL_OPTIONS": "test-java-tool-options -XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics",
}))
})
})
})

}

0 comments on commit 203d78a

Please sign in to comment.