Skip to content

Commit

Permalink
support: use cgo to generate Go constants from eBPF
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Lehner <florian.lehner@elastic.co>
  • Loading branch information
florianl committed Jan 30, 2025
1 parent de19b87 commit 449ca73
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 78 deletions.
19 changes: 19 additions & 0 deletions support/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash

set -eu

echo "////go:build linux" > types_gen.go
echo "" >> types_gen.go
echo "// Copyright The OpenTelemetry Authors" >> types_gen.go
echo "// SPDX-License-Identifier: Apache-2.0" >> types_gen.go
echo "" >> types_gen.go
GOOS=linux go tool cgo -godefs types_def.go >> types_gen.go

sed -i 's/^package support$/package support \/\/ import "go.opentelemetry.io\/ebpf-profiler\/support"/' types_gen.go

if ! diff types_gen.go types.go; then
echo "Auto generated and existing code differ"
exit 1
fi

rm -rf _obj/ types_gen.go
29 changes: 29 additions & 0 deletions support/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:generate bash -c "./generate.sh"

// support maps the definitions from headers in the C world into a nice go way
package support // import "go.opentelemetry.io/ebpf-profiler/support"

import "fmt"

// EncodeBiasAndUnwindProgram encodes a bias_and_unwind_program value (for C.PIDPageMappingInfo)
// from a bias and unwind program values.
// This currently assumes a non-negative bias: this encoding may have to be changed if bias can be
// negative.
func EncodeBiasAndUnwindProgram(bias uint64,
unwindProgram uint8) (uint64, error) {
if (bias >> 56) > 0 {
return 0, fmt.Errorf("unsupported bias value (too large): 0x%x", bias)
}
return bias | (uint64(unwindProgram) << 56), nil
}

// DecodeBiasAndUnwindProgram decodes the contents of the `bias_and_unwind_program` field in
// C.PIDPageMappingInfo and returns the corresponding bias and unwind program.
func DecodeBiasAndUnwindProgram(biasAndUnwindProgram uint64) (bias uint64, unwindProgram uint8) {
bias = biasAndUnwindProgram & 0x00FFFFFFFFFFFFFF
unwindProgram = uint8(biasAndUnwindProgram >> 56)
return bias, unwindProgram
}
129 changes: 51 additions & 78 deletions support/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

95 changes: 95 additions & 0 deletions support/types_def.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
//go:build ignore

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package support // import "go.opentelemetry.io/ebpf-profiler/support"

/*
#include "./ebpf/types.h"
#include "./ebpf/frametypes.h"
*/
import "C"

const (
FrameMarkerUnknown = C.FRAME_MARKER_UNKNOWN
FrameMarkerErrorBit = C.FRAME_MARKER_ERROR_BIT
FrameMarkerPython = C.FRAME_MARKER_PYTHON
FrameMarkerNative = C.FRAME_MARKER_NATIVE
FrameMarkerPHP = C.FRAME_MARKER_PHP
FrameMarkerPHPJIT = C.FRAME_MARKER_PHP_JIT
FrameMarkerKernel = C.FRAME_MARKER_KERNEL
FrameMarkerHotSpot = C.FRAME_MARKER_HOTSPOT
FrameMarkerRuby = C.FRAME_MARKER_RUBY
FrameMarkerPerl = C.FRAME_MARKER_PERL
FrameMarkerV8 = C.FRAME_MARKER_V8
FrameMarkerDotnet = C.FRAME_MARKER_DOTNET
FrameMarkerAbort = C.FRAME_MARKER_ABORT
)

const (
ProgUnwindStop = C.PROG_UNWIND_STOP
ProgUnwindNative = C.PROG_UNWIND_NATIVE
ProgUnwindHotspot = C.PROG_UNWIND_HOTSPOT
ProgUnwindPython = C.PROG_UNWIND_PYTHON
ProgUnwindPHP = C.PROG_UNWIND_PHP
ProgUnwindRuby = C.PROG_UNWIND_RUBY
ProgUnwindPerl = C.PROG_UNWIND_PERL
ProgUnwindV8 = C.PROG_UNWIND_V8
ProgUnwindDotnet = C.PROG_UNWIND_DOTNET
)

const (
DeltaCommandFlag = C.STACK_DELTA_COMMAND_FLAG

MergeOpcodeNegative = C.MERGEOPCODE_NEGATIVE
)

const (
EventTypeGenericPID = C.EVENT_TYPE_GENERIC_PID
)

const MaxFrameUnwinds = C.MAX_FRAME_UNWINDS

const (
MetricIDBeginCumulative = C.metricID_BeginCumulative
)

const (
BitWidthPID = C.BIT_WIDTH_PID
BitWidthPage = C.BIT_WIDTH_PAGE
)

const (
// StackDeltaBucket[Smallest|Largest] define the boundaries of the bucket sizes of the various
// nested stack delta maps.
StackDeltaBucketSmallest = C.STACK_DELTA_BUCKET_SMALLEST
StackDeltaBucketLargest = C.STACK_DELTA_BUCKET_LARGEST

// StackDeltaPage[Bits|Mask] determine the paging size of stack delta map information
StackDeltaPageBits = C.STACK_DELTA_PAGE_BITS
StackDeltaPageMask = C.STACK_DELTA_PAGE_MASK
)

const (
HSTSIDIsStubBit = C.HS_TSID_IS_STUB_BIT
HSTSIDHasFrameBit = C.HS_TSID_HAS_FRAME_BIT
HSTSIDStackDeltaBit = C.HS_TSID_STACK_DELTA_BIT
HSTSIDStackDeltaMask = C.HS_TSID_STACK_DELTA_MASK
HSTSIDStackDeltaScale = C.HS_TSID_STACK_DELTA_SCALE
HSTSIDSegMapBit = C.HS_TSID_SEG_MAP_BIT
HSTSIDSegMapMask = C.HS_TSID_SEG_MAP_MASK
)

const (
// PerfMaxStackDepth is the bpf map data array length for BPF_MAP_TYPE_STACK_TRACE traces
PerfMaxStackDepth = C.PERF_MAX_STACK_DEPTH
)

const (
TraceOriginUnknown = C.TRACE_UNKNOWN
TraceOriginSampling = C.TRACE_SAMPLING
TraceOriginOffCPU = C.TRACE_OFF_CPU
)

const OffCPUThresholdMax = C.OFF_CPU_THRESHOLD_MAX

0 comments on commit 449ca73

Please sign in to comment.