Skip to content

Commit

Permalink
move traceCacheSize back into a private method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Oct 30, 2024
1 parent 0d9ca53 commit 12fb84a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 39 deletions.
38 changes: 0 additions & 38 deletions internal/controller/cache_size.go

This file was deleted.

33 changes: 32 additions & 1 deletion internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"github.com/tklauser/numcpus"

"go.opentelemetry.io/ebpf-profiler/host"
"go.opentelemetry.io/ebpf-profiler/hostmetadata"
Expand All @@ -16,6 +17,7 @@ import (
"go.opentelemetry.io/ebpf-profiler/tracehandler"
"go.opentelemetry.io/ebpf-profiler/tracer"
tracertypes "go.opentelemetry.io/ebpf-profiler/tracer/types"
"go.opentelemetry.io/ebpf-profiler/util"
)

const MiB = 1 << 20
Expand Down Expand Up @@ -51,7 +53,7 @@ func (c *Controller) Start(ctx context.Context) error {
}

traceHandlerCacheSize, err :=
TraceCacheSize(c.config.MonitorInterval, c.config.SamplesPerSecond)
traceCacheSize(c.config.MonitorInterval, c.config.SamplesPerSecond)
if err != nil {
return fmt.Errorf("retrieve trace cache size: %w", err)
}
Expand Down Expand Up @@ -180,6 +182,35 @@ func startTraceHandling(ctx context.Context, rep reporter.TraceReporter,
return err
}

// traceCacheSize defines the maximum number of elements for the caches in tracehandler.
// The caches in tracehandler have a size-"processing overhead" trade-off: Every cache miss will
// trigger additional processing for that trace in userspace (Go). For most maps, we use
// maxElementsPerInterval as a base sizing factor. For the tracehandler caches, we also multiply
// with traceCacheIntervals. For typical/small values of maxElementsPerInterval, this can lead to
// non-optimal map sizing (reduced cache_hit:cache_miss ratio and increased processing overhead).
// Simply increasing traceCacheIntervals is problematic when maxElementsPerInterval is large
// (e.g. too many CPU cores present) as we end up using too much memory. A minimum size is
// therefore used here.
func traceCacheSize(monitorInterval time.Duration, samplesPerSecond int) (uint32, error) {
const (
traceCacheIntervals = 6
traceCacheMinSize = 65536
)

presentCores, err := numcpus.GetPresent()
if err != nil {
return 0, fmt.Errorf("failed to read CPU file: %w", err)
}

maxElements := maxElementsPerInterval(monitorInterval, samplesPerSecond, uint16(presentCores))

size := maxElements * uint32(traceCacheIntervals)
if size < traceCacheMinSize {
size = traceCacheMinSize
}
return util.NextPowerOfTwo(size), nil
}

func maxElementsPerInterval(monitorInterval time.Duration, samplesPerSecond int,
presentCPUCores uint16) uint32 {
return uint32(uint16(samplesPerSecond) * uint16(monitorInterval.Seconds()) * presentCPUCores)
Expand Down

0 comments on commit 12fb84a

Please sign in to comment.