Skip to content

Commit

Permalink
Merge pull request opensearch-project#345 from kakkoyun/increase_rlimit
Browse files Browse the repository at this point in the history
profiler: Increase rlimit
  • Loading branch information
kakkoyun authored Apr 13, 2022
2 parents fa2861c + 4e80b02 commit 1e935f4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/containerd/cgroups v1.0.3
github.com/containerd/containerd v1.6.2
github.com/docker/docker v20.10.14+incompatible
github.com/dustin/go-humanize v1.0.0
github.com/go-kit/log v0.2.0
github.com/goburrow/cache v0.1.4
github.com/google/pprof v0.0.0-20220218203455-0368bd9e19a7
Expand Down Expand Up @@ -72,7 +73,6 @@ require (
github.com/docker/distribution v2.7.1+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/go-delve/delve v1.8.2 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
29 changes: 29 additions & 0 deletions pkg/profiler/profiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (
"C" //nolint:typecheck

bpf "github.com/aquasecurity/libbpfgo"
"github.com/dustin/go-humanize"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/google/pprof/profile"
Expand All @@ -58,6 +59,8 @@ var bpfObj []byte
const (
stackDepth = 127 // Always needs to be sync with MAX_STACK_DEPTH in parca-agent.bpf.c
doubleStackDepth = 254

defaultRlimit = 1024 << 20 // ~1GB
)

type bpfMaps struct {
Expand Down Expand Up @@ -247,6 +250,11 @@ func (p *CgroupProfiler) Run(ctx context.Context) error {
}
defer m.Close()

// Always need to be used after bpf.NewModuleFromBufferArgs to avoid limit override.
if err := p.bumpMemlockRlimit(); err != nil {
return fmt.Errorf("bump memlock rlimit: %w", err)
}

if err := m.BPFLoadObject(); err != nil {
return fmt.Errorf("load bpf object: %w", err)
}
Expand Down Expand Up @@ -640,3 +648,24 @@ func (p *CgroupProfiler) sendProfile(ctx context.Context, prof *profile.Profile)

return err
}

// bumpMemlockRlimit increases the current memlock limit to a value more reasonable for the profiler's needs.
func (p *CgroupProfiler) bumpMemlockRlimit() error {
rLimit := syscall.Rlimit{
Cur: uint64(defaultRlimit),
Max: uint64(defaultRlimit),
}

// RLIMIT_MEMLOCK is 0x8.
if err := syscall.Setrlimit(unix.RLIMIT_MEMLOCK, &rLimit); err != nil {
return fmt.Errorf("failed to increase rlimit: %w", err)
}

rLimit = syscall.Rlimit{}
if err := syscall.Getrlimit(unix.RLIMIT_MEMLOCK, &rLimit); err != nil {
return fmt.Errorf("failed to get rlimit: %w", err)
}
level.Debug(p.logger).Log("msg", "increased max memory locked rlimit", "limit", humanize.Bytes(rLimit.Cur))

return nil
}

0 comments on commit 1e935f4

Please sign in to comment.