-
Notifications
You must be signed in to change notification settings - Fork 566
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from pyroscope-io/ebpf3
* adds eBPF spy * adds a new new connect command
- Loading branch information
Showing
19 changed files
with
356 additions
and
64 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 |
---|---|---|
|
@@ -10,6 +10,7 @@ pkged.go | |
/scripts/packages/ | ||
.eslintcache | ||
*.pyc | ||
*.internal | ||
.idea | ||
.vscode | ||
vendor/ |
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
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
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
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,69 @@ | ||
// +build ebpfspy | ||
|
||
// Package ebpfspy provides integration with Linux eBPF. It calls profile.py from BCC tools: | ||
// https://github.com/iovisor/bcc/blob/master/tools/profile.py | ||
// TODO: At some point we might extract the part that starts another process because it has good potential to be reused by similar profiling tools. | ||
package ebpfspy | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/pyroscope-io/pyroscope/pkg/agent/spy" | ||
) | ||
|
||
type EbpfSpy struct { | ||
resetMutex sync.Mutex | ||
reset bool | ||
stop bool | ||
|
||
profilingSession *session | ||
|
||
stopCh chan struct{} | ||
} | ||
|
||
func Start(pid int) (spy.Spy, error) { | ||
s := newSession(pid) | ||
err := s.Start() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &EbpfSpy{ | ||
profilingSession: s, | ||
stopCh: make(chan struct{}), | ||
}, nil | ||
} | ||
|
||
func (s *EbpfSpy) Stop() error { | ||
s.stop = true | ||
<-s.stopCh | ||
return nil | ||
} | ||
|
||
func (s *EbpfSpy) Snapshot(cb func([]byte, uint64, error)) { | ||
s.resetMutex.Lock() | ||
defer s.resetMutex.Unlock() | ||
|
||
if !s.reset { | ||
return | ||
} | ||
|
||
s.reset = false | ||
s.profilingSession.Reset(func(name []byte, v uint64) { | ||
cb(name, v, nil) | ||
}) | ||
if s.stop { | ||
s.stopCh <- struct{}{} | ||
s.profilingSession.Stop() | ||
} | ||
} | ||
|
||
func (s *EbpfSpy) Reset() { | ||
s.resetMutex.Lock() | ||
defer s.resetMutex.Unlock() | ||
|
||
s.reset = true | ||
} | ||
|
||
func init() { | ||
spy.RegisterSpy("ebpfspy", Start) | ||
} |
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,3 @@ | ||
// +build !ebpfspy | ||
|
||
package ebpfspy |
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,100 @@ | ||
// +build ebpfspy | ||
|
||
// Package ebpfspy provides integration with Linux eBPF. | ||
package ebpfspy | ||
|
||
import ( | ||
"fmt" | ||
"os/exec" | ||
"strconv" | ||
"sync" | ||
"syscall" | ||
|
||
"github.com/pyroscope-io/pyroscope/pkg/convert" | ||
"github.com/pyroscope-io/pyroscope/pkg/util/file" | ||
) | ||
|
||
type line struct { | ||
name []byte | ||
val int | ||
} | ||
|
||
type session struct { | ||
pid int | ||
|
||
cmd *exec.Cmd | ||
ch chan line | ||
|
||
stopMutex sync.Mutex | ||
stop bool | ||
} | ||
|
||
const helpURL = "https://github.com/iovisor/bcc/blob/master/INSTALL.md" | ||
|
||
var command = "/usr/share/bcc/tools/profile" | ||
|
||
// TODO: make these configurable | ||
var commandArgs = []string{"-F", "100", "-f", "11"} | ||
|
||
func newSession(pid int) *session { | ||
return &session{pid: pid} | ||
} | ||
|
||
func (s *session) Start() error { | ||
if !file.Exists(command) { | ||
return fmt.Errorf("Could not find profile.py at '%s'. Visit %s for instructions on how to install it", command, helpURL) | ||
} | ||
|
||
args := commandArgs | ||
if s.pid != -1 { | ||
args = append(commandArgs, "-p", strconv.Itoa(s.pid)) | ||
} | ||
|
||
s.cmd = exec.Command(command, args...) | ||
stdout, err := s.cmd.StdoutPipe() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
s.ch = make(chan line) | ||
|
||
go func() { | ||
convert.ParseGroups(stdout, func(name []byte, val int) { | ||
s.ch <- line{ | ||
name: name, | ||
val: val, | ||
} | ||
}) | ||
stdout.Close() | ||
close(s.ch) | ||
}() | ||
|
||
err = s.cmd.Start() | ||
return err | ||
} | ||
|
||
func (s *session) Reset(cb func([]byte, uint64)) error { | ||
s.cmd.Process.Signal(syscall.SIGINT) | ||
|
||
for v := range s.ch { | ||
cb(v.name, uint64(v.val)) | ||
} | ||
s.cmd.Wait() | ||
|
||
s.stopMutex.Lock() | ||
defer s.stopMutex.Unlock() | ||
|
||
if s.stop { | ||
return nil | ||
} else { | ||
return s.Start() | ||
} | ||
} | ||
|
||
func (s *session) Stop() error { | ||
s.stopMutex.Lock() | ||
defer s.stopMutex.Unlock() | ||
|
||
s.stop = true | ||
return nil | ||
} |
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
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
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
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
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
Oops, something went wrong.