Skip to content
Merged
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
11 changes: 5 additions & 6 deletions cli_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
defaultProbabilisticThreshold = tracer.ProbabilisticThresholdMax
defaultProbabilisticInterval = 1 * time.Minute
defaultArgSendErrorFrames = false
defaultOffCPUThreshold = support.OffCPUThresholdMax
defaultOffCPUThreshold = 0
Comment thread
rockdaboot marked this conversation as resolved.

// This is the X in 2^(n + x) where n is the default hardcoded map size value
defaultArgMapScaleFactor = 0
Expand Down Expand Up @@ -63,11 +63,10 @@ var (
"If zero, monotonic-realtime clock sync will be performed once, " +
"on agent startup, but not periodically."
sendErrorFramesHelp = "Send error frames (devfiler only, breaks Kibana)"
offCPUThresholdHelp = fmt.Sprintf("If set to a value between 1 and %d will enable "+
"off-cpu profiling: Every time an off-cpu entry point is hit, a random number between "+
"0 and %d is chosen. If the given threshold is greater than this random number, the "+
"off-cpu trace is collected and reported.",
support.OffCPUThresholdMax-1, support.OffCPUThresholdMax-1)
offCPUThresholdHelp = fmt.Sprintf("The per-mille chance for an off-cpu event being recorded. "+
"Valid values are in the range [1..%d], and 0 to disable off-cpu profiling."+
"Default is %d.",
support.OffCPUThresholdMax, defaultOffCPUThreshold)
Comment thread
rockdaboot marked this conversation as resolved.
)

// Package-scope variable, so that conditionally compiled other components can refer
Expand Down
10 changes: 10 additions & 0 deletions internal/controller/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"time"

log "github.com/sirupsen/logrus"

"go.opentelemetry.io/ebpf-profiler/reporter"
"go.opentelemetry.io/ebpf-profiler/support"
"go.opentelemetry.io/ebpf-profiler/tracer"
)

Expand Down Expand Up @@ -89,6 +91,14 @@ func (cfg *Config) Validate() error {
)
}

if cfg.OffCPUThreshold > support.OffCPUThresholdMax {
return fmt.Errorf(
"invalid argument for off-cpu-threshold. Value "+
"should be between 1 and %d, or 0 to disable off-cpu profiling",
support.OffCPUThresholdMax,
)
}

if !cfg.NoKernelVersionCheck {
major, minor, patch, err := tracer.GetCurrentKernelVersion()
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions internal/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"go.opentelemetry.io/ebpf-profiler/host"
"go.opentelemetry.io/ebpf-profiler/metrics"
"go.opentelemetry.io/ebpf-profiler/reporter"
"go.opentelemetry.io/ebpf-profiler/support"
"go.opentelemetry.io/ebpf-profiler/times"
"go.opentelemetry.io/ebpf-profiler/tracehandler"
"go.opentelemetry.io/ebpf-profiler/tracer"
Expand Down Expand Up @@ -108,7 +107,7 @@ func (c *Controller) Start(ctx context.Context) error {
}
log.Info("Attached tracer program")

if c.config.OffCPUThreshold < support.OffCPUThresholdMax {
if c.config.OffCPUThreshold > 0 {
if err := trc.StartOffCPUProfiling(); err != nil {
return fmt.Errorf("failed to start off-cpu profiling: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion support/ebpf/off_cpu.ebpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ int tracepoint__sched_switch(void *ctx)
return ERR_UNREACHABLE;
}

if (bpf_get_prandom_u32() % OFF_CPU_THRESHOLD_MAX > syscfg->off_cpu_threshold) {
if (bpf_get_prandom_u32() % OFF_CPU_THRESHOLD_MAX >= syscfg->off_cpu_threshold) {
return 0;
}

Expand Down
Binary file modified support/ebpf/tracer.ebpf.release.amd64
Binary file not shown.
Binary file modified support/ebpf/tracer.ebpf.release.arm64
Binary file not shown.
13 changes: 6 additions & 7 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ func initializeMapsAndPrograms(kernelSymbols *libpf.SymbolMap, cfg *Config) (
return nil, nil, fmt.Errorf("failed to load perf eBPF programs: %v", err)
}

if cfg.OffCPUThreshold < support.OffCPUThresholdMax {
if cfg.OffCPUThreshold > 0 {
if err = loadKProbeUnwinders(coll, ebpfProgs, ebpfMaps["kprobe_progs"], tailCallProgs,
cfg.BPFVerifierLogLevel, ebpfMaps["perf_progs"].FD()); err != nil {
return nil, nil, fmt.Errorf("failed to load kprobe eBPF programs: %v", err)
Expand Down Expand Up @@ -554,23 +554,22 @@ func loadAllMaps(coll *cebpf.CollectionSpec, cfg *Config,
// On modern systems /proc/sys/kernel/pid_max defaults to 4194304.
// Try to fit this PID space scaled down with cfg.OffCPUThreshold into
// this map.
adaption["sched_times"] = (4194304 / support.OffCPUThresholdMax) * cfg.OffCPUThreshold
adaption["sched_times"] = (4194304 * cfg.OffCPUThreshold) / support.OffCPUThresholdMax
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Multiply first to prevent rounding inaccuracies.

Comment thread
rockdaboot marked this conversation as resolved.

for i := support.StackDeltaBucketSmallest; i <= support.StackDeltaBucketLargest; i++ {
mapName := fmt.Sprintf("exe_id_to_%d_stack_deltas", i)
adaption[mapName] = 1 << uint32(exeIDToStackDeltasSize+cfg.MapScaleFactor)
}

for mapName, mapSpec := range coll.Maps {
if mapName == "sched_times" && cfg.OffCPUThreshold == 0 {
// Off CPU Profiling is disabled. So do not load this map.
continue
}
if newSize, ok := adaption[mapName]; ok {
log.Debugf("Size of eBPF map %s: %v", mapName, newSize)
mapSpec.MaxEntries = newSize
}
if mapName == "sched_times" &&
cfg.OffCPUThreshold >= support.OffCPUThresholdMax {
// Off CPU Profiling is not enabled. So do not load this map.
continue
}
ebpfMap, err := cebpf.NewMap(mapSpec)
if err != nil {
return fmt.Errorf("failed to load %s: %v", mapName, err)
Expand Down