Skip to content

Commit

Permalink
fix: detect python apps better (#3712)
Browse files Browse the repository at this point in the history
  • Loading branch information
korniltsev authored Nov 22, 2024
1 parent 06c720e commit 543d655
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 11 deletions.
16 changes: 16 additions & 0 deletions ebpf/python/procinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package python
import (
"bufio"
"fmt"
"os"
"path/filepath"
"regexp"
"strconv"
Expand All @@ -21,6 +22,21 @@ type ProcInfo struct {

var rePython = regexp.MustCompile("/.*/((?:lib)?python)(\\d+)\\.(\\d+)(?:[mu]?(-pyston\\d.\\d)?(?:\\.so)?)?(?:.1.0)?$")

func GetProcInfoForPid(pid uint32) (ProcInfo, error) {
mapsFD, err := os.Open(fmt.Sprintf("/proc/%d/maps", pid))
if err != nil {
return ProcInfo{}, fmt.Errorf("reading proc maps %d: %w", pid, err)
}
defer mapsFD.Close()

info, err := GetProcInfo(bufio.NewScanner(mapsFD))

if err != nil {
return ProcInfo{}, fmt.Errorf("GetPythonProcInfo error %s: %w", fmt.Sprintf("/proc/%d/maps", pid), err)
}
return info, nil
}

// GetProcInfo parses /proc/pid/map of a python process.
func GetProcInfo(s *bufio.Scanner) (ProcInfo, error) {
res := ProcInfo{}
Expand Down
12 changes: 2 additions & 10 deletions ebpf/python/pyperf_pid_data.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package python

import (
"bufio"
"debug/elf"
"fmt"
"os"
Expand All @@ -12,16 +11,9 @@ import (
)

func GetPyPerfPidData(l log.Logger, pid uint32, collectKernel bool) (*PerfPyPidData, error) {
mapsFD, err := os.Open(fmt.Sprintf("/proc/%d/maps", pid))
info, err := GetProcInfoForPid(pid)
if err != nil {
return nil, fmt.Errorf("reading proc maps %d: %w", pid, err)
}
defer mapsFD.Close()

info, err := GetProcInfo(bufio.NewScanner(mapsFD))

if err != nil {
return nil, fmt.Errorf("GetPythonProcInfo error %s: %w", fmt.Sprintf("/proc/%d/maps", pid), err)
return nil, err
}
var pythonMeat []*symtab.ProcMap
if info.LibPythonMaps == nil {
Expand Down
1 change: 1 addition & 0 deletions ebpf/sd/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
OptionCollectKernel = labelMetaPyroscopeOptionsPrefix + "collect_kernel"
OptionPythonFullFilePath = labelMetaPyroscopeOptionsPrefix + "python_full_file_path"
OptionPythonEnabled = labelMetaPyroscopeOptionsPrefix + "python_enabled"
OptionPythonProfilingType = labelMetaPyroscopeOptionsPrefix + "python_profiling_type"
OptionPythonBPFDebugLogEnabled = labelMetaPyroscopeOptionsPrefix + "python_bpf_debug_log"
OptionPythonBPFErrorLogEnabled = labelMetaPyroscopeOptionsPrefix + "python_bpf_error_log"
OptionDemangle = labelMetaPyroscopeOptionsPrefix + "demangle"
Expand Down
25 changes: 24 additions & 1 deletion ebpf/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func (s *session) selectProfilingType(pid uint32, target *sd.Target) procInfoLit
}
exe := filepath.Base(exePath)

if s.pythonEnabled(target) && strings.HasPrefix(exe, "python") || exe == "uwsgi" {
if s.pythonEnabled(target) && isPythonProfilingType(pid, exe, target) {
return procInfoLite{pid: pid, comm: string(comm), typ: pyrobpf.ProfilingTypePython}
}
return procInfoLite{pid: pid, comm: string(comm), typ: pyrobpf.ProfilingTypeFramepointers}
Expand Down Expand Up @@ -903,6 +903,29 @@ func (s *session) pythonEnabled(target *sd.Target) bool {
return enabled
}

func isPythonProfilingType(pid uint32, exe string, target *sd.Target) bool {
if v, present := target.GetFlag(sd.OptionPythonProfilingType); present {
return v
}
if strings.HasPrefix(exe, "python") {
return true
}
if exe == "uwsgi" {
return true
}
if exe == "streamlit" {
return true
}
pi, err := python.GetProcInfoForPid(pid) // todo(korniltsev): reorganize code to not do this twice
if err != nil {
return false
}
if pi.PythonMaps != nil || pi.LibPythonMaps != nil {
return true
}
return false
}

func (s *session) pythonBPFDebugLogEnabled(target *sd.Target) bool {
enabled := s.options.PythonBPFDebugLogEnabled
if v, present := target.GetFlag(sd.OptionPythonBPFDebugLogEnabled); present {
Expand Down

0 comments on commit 543d655

Please sign in to comment.