Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the containerV2 flag to compute initial heap percentage using processor count, and disable setting the ActiveProcessorCount JVM option #361

Merged
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
0381bc3
Add the UseProcessorAwareInitialHeapPercentage and set the initial he…
Nov 13, 2023
9885551
Update integration test file
Nov 13, 2023
2a25467
add a comment
Nov 13, 2023
8047f50
Round percentage to 2 decimal places
Nov 13, 2023
694c0c5
Update heap pct calculation
Nov 13, 2023
b5311e2
update flag name
Nov 13, 2023
cd02b48
round to 1 decimal place
Nov 13, 2023
f2c041a
Add generated changelog entries
svc-changelog Nov 13, 2023
b311ee5
review changes
Nov 14, 2023
c4f02cb
Merge branch 'pmarupaka/updateActiveProcessorCountFlagAndInitialHeapP…
Nov 14, 2023
3c7b572
fix computation
Nov 14, 2023
52908f8
review changes p2
Nov 14, 2023
7b517e5
wip: adding test
Nov 14, 2023
b0b7776
wip tests
Nov 14, 2023
9a09312
update integration tests
Nov 14, 2023
2c28dd7
Add generated changelog entries
svc-changelog Nov 14, 2023
b3649aa
integration test: check for existence of keys
Nov 14, 2023
6890a1a
add test for ComputeJVMHeapSizeInBytes
Nov 14, 2023
89c0f6e
update TestComputeJVMHeapSize test case
Nov 14, 2023
8489e7a
Autorelease 1.93.0-rc1
carterkozak Nov 15, 2023
17043f5
review changes: rename flag to containerV2 and fallback to setting in…
Nov 16, 2023
5d68069
Add generated changelog entries
svc-changelog Nov 16, 2023
d11eaf4
review changes: add comment justifying fallback. heapLimit -> memoryL…
Nov 16, 2023
1a34acf
remove check for containerV2 in 'ensureActiveProcessorCount' and move…
Nov 16, 2023
03480a1
update comment
Nov 16, 2023
ad969c4
review comments: update readme. Don't needlessly propagate a nil error
Nov 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions changelog/@unreleased/pr-361.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: improvement
improvement:
description: When the experimental UseProcessorAwareInitialHeapSize is set, 1) compute
the heap percentage as 75% of the heap minus 3mb per processor, with a minimum
value of 50%, and 2) don't set the -XX:ActiveProcessorCount JVM option.
links:
- https://github.com/palantir/go-java-launcher/pull/361
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ configVersion: 1
jvmOpts:
- '-Xmx1g'
experimental:
overrideActiveProcessorCount: true
useProcessorAwareInitialHeapSize: true
mpritham marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 4 additions & 1 deletion launchlib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ type CustomLauncherConfig struct {
DisableContainerSupport bool `yaml:"dangerousDisableContainerSupport"`
}

type ExperimentalLauncherConfig struct{}
type ExperimentalLauncherConfig struct {
// Also disables setting the active processor count JVM argument.
UseProcessorAwareInitialHeapSize bool `yaml:"useProcessorAwareInitialHeapSize"`
mpritham marked this conversation as resolved.
Show resolved Hide resolved
}

type PrimaryCustomLauncherConfig struct {
VersionedConfig `yaml:",inline"`
Expand Down
40 changes: 33 additions & 7 deletions launchlib/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
TemplateDelimsClose = "}}"
// ExecPathBlackListRegex matches characters disallowed in paths we allow to be passed to exec()
ExecPathBlackListRegex = `[^\w.\/_\-]`
BytesInMebibyte = 1048576
)

type ServiceCmds struct {
Expand Down Expand Up @@ -279,8 +280,8 @@ func delim(str string) string {
func createJvmOpts(combinedJvmOpts []string, customConfig *CustomLauncherConfig, logger io.WriteCloser) []string {
if isEnvVarSet("CONTAINER") && !customConfig.DisableContainerSupport && !hasMaxRAMOverride(combinedJvmOpts) {
_, _ = fmt.Fprintln(logger, "Container support enabled")
combinedJvmOpts = filterHeapSizeArgs(combinedJvmOpts)
combinedJvmOpts = ensureActiveProcessorCount(combinedJvmOpts, logger)
combinedJvmOpts = filterHeapSizeArgs(customConfig, combinedJvmOpts)
combinedJvmOpts = ensureActiveProcessorCount(customConfig, combinedJvmOpts, logger)
mpritham marked this conversation as resolved.
Show resolved Hide resolved
return combinedJvmOpts
}

Expand All @@ -295,7 +296,7 @@ func createJvmOpts(combinedJvmOpts []string, customConfig *CustomLauncherConfig,
return combinedJvmOpts
}

func filterHeapSizeArgs(args []string) []string {
func filterHeapSizeArgs(customConfig *CustomLauncherConfig, args []string) []string {
var filtered []string
var hasMaxRAMPercentage, hasInitialRAMPercentage bool
for _, arg := range args {
Expand All @@ -311,13 +312,17 @@ func filterHeapSizeArgs(args []string) []string {
}

if !hasInitialRAMPercentage && !hasMaxRAMPercentage {
filtered = append(filtered, "-XX:InitialRAMPercentage=75.0")
filtered = append(filtered, "-XX:MaxRAMPercentage=75.0")
initialHeapPercentage, err := computeInitialHeapPercentage(customConfig)
if err != nil {
initialHeapPercentage = 75.0
}
filtered = append(filtered, fmt.Sprintf("-XX:InitialRAMPercentage=%.1f", initialHeapPercentage))
mpritham marked this conversation as resolved.
Show resolved Hide resolved
filtered = append(filtered, fmt.Sprintf("-XX:MaxRAMPercentage=%.1f", initialHeapPercentage))
}
return filtered
}

func ensureActiveProcessorCount(args []string, logger io.Writer) []string {
func ensureActiveProcessorCount(customConfig *CustomLauncherConfig, args []string, logger io.Writer) []string {
filtered := make([]string, 0, len(args)+1)

var hasActiveProcessorCount bool
Expand All @@ -328,7 +333,7 @@ func ensureActiveProcessorCount(args []string, logger io.Writer) []string {
filtered = append(filtered, arg)
}

if !hasActiveProcessorCount {
if !hasActiveProcessorCount && !customConfig.Experimental.UseProcessorAwareInitialHeapSize {
processorCountArg, err := getActiveProcessorCountArg(logger)
if err == nil {
filtered = append(filtered, processorCountArg)
Expand Down Expand Up @@ -376,3 +381,24 @@ func isMaxRAMPercentage(arg string) bool {
func isInitialRAMPercentage(arg string) bool {
return strings.HasPrefix(arg, "-XX:InitialRAMPercentage=")
}

// If the experimental `UseProcessorAwareInitialHeapSize` is set, compute the heap percentage as 75% of the
// heap minus 3mb per processor, with a minimum value of 50%.
func computeInitialHeapPercentage(customConfig *CustomLauncherConfig) (float64, error) {
if !customConfig.Experimental.UseProcessorAwareInitialHeapSize {
return 75.0, nil
}

cgroupProcessorCount, err := DefaultCGroupV1ProcessorCounter.ProcessorCount()
if err != nil {
return 0, errors.Wrap(err, "failed to get cgroup processor count")
}
cgroupMemoryLimitInBytes, err := DefaultMemoryLimit.MemoryLimitInBytes()
if err != nil {
return 0, errors.Wrap(err, "failed to get cgroup memory limit")
}
var heapLimit = float64(cgroupMemoryLimitInBytes)
mpritham marked this conversation as resolved.
Show resolved Hide resolved
var processorAdjustment = 3 * BytesInMebibyte * float64(cgroupProcessorCount)

return max(50.0, (0.75*heapLimit-processorAdjustment)/heapLimit*100.0), nil
}
71 changes: 71 additions & 0 deletions launchlib/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2023 Palantir Technologies, Inc.
mpritham marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package launchlib

import (
"io"
"io/fs"
"os"
"path/filepath"
"strconv"
"strings"

"github.com/pkg/errors"
)

const (
memGroupName = "memory"
memLimitName = "memory.limit_in_bytes"
)

type MemoryLimit interface {
MemoryLimitInBytes() (uint64, error)
}

var DefaultMemoryLimit = NewCGroupMemoryLimit(os.DirFS("/"))

type CGroupMemoryLimit struct {
pather CGroupPather
fs fs.FS
}

func NewCGroupMemoryLimit(filesystem fs.FS) MemoryLimit {
return CGroupMemoryLimit{
pather: NewCGroupV1Pather(filesystem),
fs: filesystem,
}
}

func (c CGroupMemoryLimit) MemoryLimitInBytes() (uint64, error) {
memoryCGroupPath, err := c.pather.Path(memGroupName)
if err != nil {
return 0, errors.Wrap(err, "failed to get memory cgroup path")
}

memLimitFilepath := filepath.Join(memoryCGroupPath, memLimitName)
memLimitFile, err := c.fs.Open(convertToFSPath(memLimitFilepath))
if err != nil {
return 0, errors.Wrapf(err, "unable to open memory.limit_in_bytes at expected location: %s", memLimitFilepath)
}
memLimitBytes, err := io.ReadAll(memLimitFile)
if err != nil {
return 0, errors.Wrapf(err, "unable to read memory.limit_in_bytes")
}
memLimit, err := strconv.Atoi(strings.TrimSpace(string(memLimitBytes)))
if err != nil {
return 0, errors.New("unable to convert memory.limit_in_bytes value to expected type")
}
return uint64(memLimit), nil
}
95 changes: 95 additions & 0 deletions launchlib/memory_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright 2023 Palantir Technologies, Inc.
mpritham marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package launchlib_test

import (
"io/fs"
"testing"
"testing/fstest"

"github.com/palantir/go-java-launcher/launchlib"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

var (
memoryLimitContent = []byte("2147483648\n")
badMemoryLimitContent = []byte(``)
)

func TestMemoryLimit_DefaultMemoryLimit(t *testing.T) {
for _, test := range []struct {
name string
filesystem fs.FS
expectedMemoryLimit uint64
expectedError error
}{
{
name: "fails when unable to read memory.limit_in_bytes",
filesystem: fstest.MapFS{
"proc/self/cgroup": &fstest.MapFile{
Data: CGroupContent,
},
"proc/self/mountinfo": &fstest.MapFile{
Data: MountInfoContent,
},
},
expectedError: errors.New("unable to open memory.limit_in_bytes at expected location"),
},
{
name: "fails when unable to parse memory.limit_in_bytes",
filesystem: fstest.MapFS{
"proc/self/cgroup": &fstest.MapFile{
Data: CGroupContent,
},
"proc/self/mountinfo": &fstest.MapFile{
Data: MountInfoContent,
},
"sys/fs/cgroup/memory/memory.limit_in_bytes": &fstest.MapFile{
Data: badMemoryLimitContent,
},
},
expectedError: errors.New("unable to convert memory.limit_in_bytes value to expected type"),
},
{
name: "returns expected RAM percentage when memory.limit_in_bytes under 2 GiB",
filesystem: fstest.MapFS{
"proc/self/cgroup": &fstest.MapFile{
Data: CGroupContent,
},
"proc/self/mountinfo": &fstest.MapFile{
Data: MountInfoContent,
},
"sys/fs/cgroup/memory/memory.limit_in_bytes": &fstest.MapFile{
Data: memoryLimitContent,
},
},
expectedMemoryLimit: 1 << 31,
},
} {
t.Run(test.name, func(t *testing.T) {
limit := launchlib.NewCGroupMemoryLimit(test.filesystem)
memoryLimit, err := limit.MemoryLimitInBytes()
if test.expectedError != nil {
require.Error(t, err)
assert.Contains(t, err.Error(), test.expectedError.Error())
return
}
assert.NoError(t, err)
assert.Equal(t, test.expectedMemoryLimit, memoryLimit)
})
}
}