-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leon Hwang <[email protected]>
- Loading branch information
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2024 Leon Hwang. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package bpflbr | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/cilium/ebpf" | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
type LbrPerfEvent struct { | ||
fds []int | ||
} | ||
|
||
func OpenLbrPerfEvent() (*LbrPerfEvent, error) { | ||
var p LbrPerfEvent | ||
var err error | ||
|
||
defer func() { | ||
if err != nil { | ||
p.Close() | ||
} | ||
}() | ||
|
||
numCPU, err := ebpf.PossibleCPU() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get number of CPUs: %w", err) | ||
} | ||
|
||
p.fds = make([]int, 0, numCPU) | ||
for i := 0; i < numCPU; i++ { | ||
fd, err := openLbrPerfEvent(i) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open LBR perf event: %w", err) | ||
} | ||
|
||
p.fds = append(p.fds, fd) | ||
} | ||
|
||
return &p, nil | ||
} | ||
|
||
func (p *LbrPerfEvent) Close() { | ||
for _, fd := range p.fds { | ||
_ = unix.Close(fd) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2024 Leon Hwang. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build linux | ||
|
||
package bpflbr | ||
|
||
import ( | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func openLbrPerfEvent(cpu int) (int, error) { | ||
var attr unix.PerfEventAttr | ||
attr.Size = uint32(unsafe.Sizeof(attr)) | ||
attr.Type = unix.PERF_TYPE_HARDWARE | ||
attr.Config = unix.PERF_COUNT_HW_CPU_CYCLES | ||
attr.Sample = 4000 | ||
attr.Bits |= unix.PerfBitFreq | ||
attr.Sample_type = unix.PERF_SAMPLE_BRANCH_STACK | ||
attr.Branch_sample_type = unix.PERF_SAMPLE_BRANCH_KERNEL | | ||
unix.PERF_SAMPLE_BRANCH_ANY | ||
|
||
return unix.PerfEventOpen(&attr, -1, cpu, -1, unix.PERF_FLAG_FD_CLOEXEC) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2024 Leon Hwang. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
//go:build !linux | ||
|
||
package bpflbr | ||
|
||
import ( | ||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func openLbrPerfEvent(cpu int) (int, error) { | ||
return 0, unix.EOPNOTSUPP | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters