Skip to content

Commit

Permalink
pyspy: defaults to non-blocking mode, adds an option to enable blocki…
Browse files Browse the repository at this point in the history
…ng mode
  • Loading branch information
petethepig committed Apr 6, 2021
1 parent 98284f7 commit 0035a99
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 10 deletions.
5 changes: 1 addition & 4 deletions examples/python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ WORKDIR /usr/src/app

COPY --from=pyroscope/pyroscope:latest /usr/bin/pyroscope /usr/bin/pyroscope
COPY main.py ./main.py
COPY util.py ./util.py
COPY __init__.py ./__init__.py
COPY a_module ./a_module
COPY b_module ./b_module

ENV PYROSCOPE_APPLICATION_NAME=simple.python.app
ENV PYROSCOPE_SERVER_ADDRESS=http://pyroscope:4040/
ENV PYROSCOPE_LOG_LEVEL=debug
CMD ["pyroscope", "exec", "python", "main.py"]
3 changes: 3 additions & 0 deletions pkg/agent/pyspy/placeholder.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// +build !pyspy

package pyspy

// TODO: we should probably find a better way of setting this
var Blocking bool
9 changes: 8 additions & 1 deletion pkg/agent/pyspy/pyspy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ import (
// TODO: pass lower level structures between go and rust?
var bufferLength = 1024 * 64

// TODO: we should probably find a better way of setting this
var Blocking bool

type PySpy struct {
dataPtr unsafe.Pointer
dataBuf []byte
Expand All @@ -41,7 +44,11 @@ func Start(pid int) (spy.Spy, error) {
// TODO: handle this better
time.Sleep(1 * time.Second)

r := C.pyspy_init(C.int(pid), errorPtr, C.int(bufferLength))
blocking := 0
if Blocking {
blocking = 1
}
r := C.pyspy_init(C.int(pid), C.int(blocking), errorPtr, C.int(bufferLength))

if r < 0 {
return nil, errors.New(string(errorBuf[:-r]))
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Exec struct {
NoLogging bool `def:"false" desc:"disables logging from pyroscope"`
NoRootDrop bool `def:"false" desc:"disables permissions drop when ran under root. use this one if you want to run your command as root"`
Pid int `def:"0" desc:"PID of the process you want to profile. Pass -1 to profile the whole system (only supported by ebpfspy)"`
PyspyBlocking bool `def:"false" desc:"enables blocking mode for pyspy"`
}

func calculateMaxDepth(min, max time.Duration, multiplier int) int {
Expand Down
7 changes: 7 additions & 0 deletions pkg/exec/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/fatih/color"
"github.com/mitchellh/go-ps"
"github.com/pyroscope-io/pyroscope/pkg/agent"
"github.com/pyroscope-io/pyroscope/pkg/agent/pyspy"
"github.com/pyroscope-io/pyroscope/pkg/agent/spy"
"github.com/pyroscope-io/pyroscope/pkg/agent/upstream/remote"
"github.com/pyroscope-io/pyroscope/pkg/config"
Expand All @@ -37,6 +38,9 @@ func Cli(cfg *config.Config, args []string) error {
return errors.New("no arguments passed")
}

// TODO: this is somewhat hacky, we need to find a better way to configure agents
pyspy.Blocking = cfg.Exec.PyspyBlocking

spyName := cfg.Exec.SpyName
if spyName == "auto" {
if isExec {
Expand Down Expand Up @@ -131,8 +135,11 @@ func Cli(cfg *config.Config, args []string) error {

// TODO: add sample rate, make it configurable
sess := agent.NewSession(u, cfg.Exec.ApplicationName, spyName, 100, 10*time.Second, pid, cfg.Exec.DetectSubprocesses)

sess.Logger = logrus.StandardLogger()
logrus.Debug("sess created")
err = sess.Start()
logrus.Debug("sess started")
if err != nil {
logrus.Errorf("error when starting session: %q", err)
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/server/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package server

import (
"io"
"log"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -97,7 +96,6 @@ func (ctrl *Controller) ingestHandler(w http.ResponseWriter, r *http.Request) {
}

err = ctrl.s.Put(ip.from, ip.until, ip.storageKey, t, ip.spyName, ip.sampleRate)
log.Println("test", err, ip.from, ip.until, ip.storageKey, t, ip.spyName, ip.sampleRate)
if err != nil {
logrus.WithField("err", err).Error("error happened while inserting data")
return
Expand Down
2 changes: 1 addition & 1 deletion third_party/rustdeps/Cargo.lock

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

2 changes: 1 addition & 1 deletion third_party/rustdeps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ crate-type = ["staticlib"]
# py-spy = { path = "../../../py-spy" }
# rbspy = { path = "../../../rbspy" }

py-spy = { git = "https://github.com/pyroscope-io/py-spy", rev = "c1a8008" }
py-spy = { git = "https://github.com/pyroscope-io/py-spy", rev = "f990f0b" }
rbspy = { git = "https://github.com/pyroscope-io/rbspy", rev = "599b90a" }
2 changes: 1 addition & 1 deletion third_party/rustdeps/pyspy.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <sys/types.h>

int pyspy_init(pid_t pid, void* err_ptr, int err_len);
int pyspy_init(pid_t pid, int blocking, void* err_ptr, int err_len);
int pyspy_cleanup(pid_t pid, void* err_ptr, int err_len);
int pyspy_snapshot(pid_t pid, void* ptr, int len, void* err_ptr, int err_len);

0 comments on commit 0035a99

Please sign in to comment.