-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added functionality for Java Native Memory Tracking to new helper - nmt
- Loading branch information
1 parent
adf90db
commit 20f5096
Showing
6 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package helper | ||
|
||
import ( | ||
"fmt" | ||
"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 | ||
} | ||
level := "summary" | ||
if s, ok := os.LookupEnv("BPL_JAVA_NMT_LEVEL"); ok && strings.ToLower(s) == "detail" { | ||
level = "detail" | ||
} | ||
|
||
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", fmt.Sprintf("-XX:NativeMemoryTracking=%s", level), "-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": level, "JAVA_TOOL_OPTIONS": strings.Join(values, " ")}, nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
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()) | ||
}) | ||
|
||
it.After(func() { | ||
Expect(os.Unsetenv("BPL_JAVA_NMT_ENABLED")).To(Succeed()) | ||
Expect(os.Unsetenv("NMT_LEVEL_1")).To(Succeed()) | ||
}) | ||
|
||
it("contributes configuration for summary level", func() { | ||
|
||
Expect(os.Setenv("BPL_JAVA_NMT_LEVEL", "summary")).To(Succeed()) | ||
Expect(n.Execute()).To(Equal(map[string]string{"NMT_LEVEL_1": "summary", | ||
"JAVA_TOOL_OPTIONS": "-XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=summary -XX:+PrintNMTStatistics", | ||
})) | ||
Expect(os.Unsetenv("BPL_JAVA_NMT_LEVEL")).To(Succeed()) | ||
}) | ||
|
||
it("contributes configuration for detail level", func() { | ||
|
||
Expect(os.Setenv("BPL_JAVA_NMT_LEVEL", "detail")).To(Succeed()) | ||
Expect(n.Execute()).To(Equal(map[string]string{"NMT_LEVEL_1": "detail", | ||
"JAVA_TOOL_OPTIONS": "-XX:+UnlockDiagnosticVMOptions -XX:NativeMemoryTracking=detail -XX:+PrintNMTStatistics", | ||
})) | ||
Expect(os.Unsetenv("BPL_JAVA_NMT_LEVEL")).To(Succeed()) | ||
}) | ||
|
||
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 - level defaults to summary", 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", | ||
})) | ||
}) | ||
}) | ||
}) | ||
|
||
} |