-
Notifications
You must be signed in to change notification settings - Fork 63
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 #108 from coroot/python_gil_monitoring
add `container_python_thread_lock_wait_time_seconds` metric
- Loading branch information
Showing
9 changed files
with
227 additions
and
43 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
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
Large diffs are not rendered by default.
Oops, something went wrong.
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,42 @@ | ||
struct { | ||
__uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); | ||
__uint(key_size, sizeof(int)); | ||
__uint(value_size, sizeof(int)); | ||
} python_thread_events SEC(".maps"); | ||
|
||
struct { | ||
__uint(type, BPF_MAP_TYPE_HASH); | ||
__uint(key_size, sizeof(__u64)); | ||
__uint(value_size, sizeof(__u64)); | ||
__uint(max_entries, 10240); | ||
} python_thread_locks SEC(".maps"); | ||
|
||
SEC("uprobe/pthread_cond_timedwait_enter") | ||
int pthread_cond_timedwait_enter(struct pt_regs *ctx) { | ||
__u64 pid_tgid = bpf_get_current_pid_tgid(); | ||
__u64 timestamp = bpf_ktime_get_ns(); | ||
bpf_map_update_elem(&python_thread_locks, &pid_tgid, ×tamp, BPF_ANY); | ||
return 0; | ||
} | ||
|
||
struct python_thread_event { | ||
__u32 type; | ||
__u32 pid; | ||
__u64 duration; | ||
}; | ||
|
||
SEC("uprobe/pthread_cond_timedwait_exit") | ||
int pthread_cond_timedwait_exit(struct pt_regs *ctx) { | ||
__u64 pid_tgid = bpf_get_current_pid_tgid(); | ||
__u64 *timestamp = bpf_map_lookup_elem(&python_thread_locks, &pid_tgid); | ||
if (!timestamp) { | ||
return 0; | ||
} | ||
struct python_thread_event e = { | ||
.type = EVENT_TYPE_PYTHON_THREAD_LOCK, | ||
.pid = pid_tgid >> 32, | ||
.duration = bpf_ktime_get_ns()-*timestamp, | ||
}; | ||
bpf_perf_event_output(ctx, &python_thread_events, BPF_F_CURRENT_CPU, &e, sizeof(e)); | ||
return 0; | ||
} |
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,84 @@ | ||
package ebpftracer | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"regexp" | ||
"strings" | ||
|
||
"github.com/cilium/ebpf/link" | ||
"github.com/coroot/coroot-node-agent/proc" | ||
"k8s.io/klog/v2" | ||
) | ||
|
||
var ( | ||
libcRegexp = regexp.MustCompile(`libc[\.-]`) | ||
muslRegexp = regexp.MustCompile(`musl[\.-]`) | ||
) | ||
|
||
func (t *Tracer) AttachPythonThreadLockProbes(pid uint32) []link.Link { | ||
exePath := getPthreadLib(pid) | ||
if exePath == "" { | ||
return nil | ||
} | ||
|
||
log := func(msg string, err error) { | ||
if err != nil { | ||
for _, s := range []string{"no such file or directory", "no such process", "permission denied"} { | ||
if strings.HasSuffix(err.Error(), s) { | ||
return | ||
} | ||
} | ||
klog.ErrorfDepth(1, "pid=%d lib=%s: %s: %s", pid, exePath, msg, err) | ||
return | ||
} | ||
klog.InfofDepth(1, "pid=%d lib=%s: %s", pid, exePath, msg) | ||
} | ||
exe, err := link.OpenExecutable(exePath) | ||
if err != nil { | ||
log("failed to open executable", err) | ||
return nil | ||
} | ||
var links []link.Link | ||
uprobe, err := exe.Uprobe("pthread_cond_timedwait", t.uprobes["pthread_cond_timedwait_enter"], nil) | ||
if err != nil { | ||
log("failed to attach uprobe", err) | ||
return nil | ||
} | ||
links = append(links, uprobe) | ||
uretprobe, err := exe.Uretprobe("pthread_cond_timedwait", t.uprobes["pthread_cond_timedwait_exit"], nil) | ||
if err != nil { | ||
log("failed to attach uretprobe", err) | ||
return nil | ||
} | ||
links = append(links, uretprobe) | ||
log("python uprobes attached", nil) | ||
return links | ||
} | ||
|
||
func getPthreadLib(pid uint32) string { | ||
f, err := os.Open(proc.Path(pid, "maps")) | ||
if err != nil { | ||
return "" | ||
} | ||
defer f.Close() | ||
scanner := bufio.NewScanner(f) | ||
scanner.Split(bufio.ScanLines) | ||
libc := "" | ||
for scanner.Scan() { | ||
parts := strings.Fields(scanner.Text()) | ||
if len(parts) <= 5 { | ||
continue | ||
} | ||
libPath := parts[5] | ||
switch { | ||
case libcRegexp.MatchString(libPath): | ||
libc = proc.Path(pid, "root", libPath) | ||
case muslRegexp.MatchString(libPath): | ||
return proc.Path(pid, "root", libPath) | ||
case strings.Contains(libPath, "libpthread"): | ||
return proc.Path(pid, "root", libPath) | ||
} | ||
} | ||
return libc | ||
} |
Oops, something went wrong.