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

Set system property for experimental memory-limit based heap settings #324

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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-324.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
type: feature
feature:
description: Set system property for experimental memory-limit based heap settings.
Enabling this will allow gathering data on real workloads without any behavioral
changes.
links:
- https://github.com/palantir/go-java-launcher/pull/324
1 change: 1 addition & 0 deletions launchlib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type CustomLauncherConfig struct {

type ExperimentalLauncherConfig struct {
OverrideActiveProcessorCount bool `yaml:"overrideActiveProcessorCount"`
DynamicMemoryLimits bool `yaml:"dynamicMemoryLimits"`
}

type PrimaryCustomLauncherConfig struct {
Expand Down
12 changes: 12 additions & 0 deletions launchlib/launcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ func createJvmOpts(combinedJvmOpts []string, customConfig *CustomLauncherConfig,
if customConfig.Experimental.OverrideActiveProcessorCount {
combinedJvmOpts = ensureActiveProcessorCount(combinedJvmOpts, logger)
}
if customConfig.Experimental.DynamicMemoryLimits {
combinedJvmOpts = addDynamicRAMPercentageSystemProps(combinedJvmOpts, logger)
}
return combinedJvmOpts
}

Expand Down Expand Up @@ -359,6 +362,15 @@ func getActiveProcessorCountArg(logger io.Writer) (string, error) {
return fmt.Sprintf("-XX:ActiveProcessorCount=%d", cgroupProcessorCount), nil
}

func addDynamicRAMPercentageSystemProps(args []string, logger io.Writer) []string {
memoryLimit, err := DefaultMemoryLimit.MemoryLimit()
if err != nil {
_, _ = fmt.Fprintln(logger, "Failed to detect cgroup memory configuration, not setting dynamic RAM percentage system property", err.Error())
return args
}
return append(args, fmt.Sprintf("-DDynamicXMX=%d", uint64(float64(memoryLimit)*0.75)))
}

func isActiveProcessorCount(arg string) bool {
return strings.HasPrefix(arg, "-XX:ActiveProcessorCount=")
}
Expand Down
57 changes: 57 additions & 0 deletions launchlib/memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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 {
MemoryLimit() (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) MemoryLimit() (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.
//
// 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 TestRAMPercenter_DefaultCGroupV1RAMPercenter(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.MemoryLimit()
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)
})
}
}