Skip to content

Commit

Permalink
replace strace with perf
Browse files Browse the repository at this point in the history
Signed-off-by: Alessio Greggi <[email protected]>
  • Loading branch information
alegrey91 committed Aug 20, 2024
1 parent 5820e15 commit 30dc9fc
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 27 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ jobs:
with:
go-version: '1.20'

- name: Install perf
run: |
sudo apt install -y linux-tools-generic
- name: Build coverage-instrumented binary
run: |
make build-cover && sudo make -B install
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
)

require (
github.com/benhoyt/goawk v1.27.0
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/olekukonko/tablewriter v0.0.5
github.com/rogpeppe/go-internal v1.12.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3f
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/benhoyt/goawk v1.27.0 h1:y69/kwLrLWlwXjRi9WMGgGIzL/qM97HAHLhXnsiKb7E=
github.com/benhoyt/goawk v1.27.0/go.mod h1:FjIAicXvrv3wbqAhSTo5bn4mIM5y1iy3lcnIynlJvoI=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
Expand Down
38 changes: 11 additions & 27 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/alegrey91/fwdctl/pkg/iptables"
"github.com/benhoyt/goawk/interp"
goiptables "github.com/coreos/go-iptables/iptables"
"github.com/rogpeppe/go-internal/testscript"
)
Expand Down Expand Up @@ -62,9 +63,9 @@ func strace(ts *testscript.TestScript, neg bool, args []string) {
outputFile := filepath.Join(outputDir, fmt.Sprintf("strace-%s.log", timestamp))

// Prepare the command to execute with strace, filtering only system calls
straceArgs := []string{"-f", "-e", "trace=all"}
straceArgs := []string{"stat", "-o", "/dev/stdout", "-e", "'syscalls:sys_enter_*'"}
straceArgs = append(straceArgs, args...)
strace := exec.Command("strace", straceArgs...)
strace := exec.Command("perf", straceArgs...)

var stdoutBuf bytes.Buffer
strace.Stdout = &stdoutBuf
Expand All @@ -85,38 +86,21 @@ func strace(ts *testscript.TestScript, neg bool, args []string) {
fmt.Fprintf(ts.Stdout(), "%s", stdoutBuf.String())

fmt.Fprintf(ts.Stdout(), "%s", stderrBuf.String())
syscalls := processStraceOutput(stderrBuf.String())
syscalls := processPerfOutput(stderrBuf.String())
if err := os.WriteFile(outputFile, []byte(syscalls), 0644); err != nil {
ts.Fatalf("error saving strace output to %s", outputFile)
}
ts.Logf("strace output saved to %s", outputFile)
}

func processStraceOutput(output string) string {
lines := strings.Split(output, "\n")

// Use a map to store unique system call names
syscalls := make(map[string]struct{})

// Iterate over each line and extract the system call name
for _, line := range lines {
// Extract the system call name before the first '('
if idx := strings.Index(line, "("); idx != -1 {
syscall := strings.TrimSpace(line[:idx])
if syscall != "" {
syscalls[syscall] = struct{}{}
}
}
}

// Convert the map keys to a slice to get unique system call names
var syscallList []string
for syscall := range syscalls {
syscallList = append(syscallList, syscall)
func processPerfOutput(input string) string {
inputReader := strings.NewReader(input)
outputWriter := new(bytes.Buffer)
err := interp.Exec(`$1 && $2 ~ /syscalls:/ { sub("syscalls:sys_enter_", ""); sub(":", "") gsub(",", ""); printf "%s ",$2 }`, " ", inputReader, outputWriter)
if err != nil {
return ""
}

// Join the unique system call names into a single string, one per line
return strings.Join(syscallList, "\n")
return outputWriter.String()
}

func customCommands() map[string]func(ts *testscript.TestScript, neg bool, args []string) {
Expand Down

0 comments on commit 30dc9fc

Please sign in to comment.