1 Star 0 Fork 0

wangcichen/bcc

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
pidpersec.py 1.09 KB
Copy Edit Raw Blame History
chantra authored 2022-08-14 08:35 +08:00 . [test][bionic] Force python3 and utf-8 encoding
#!/usr/bin/env python
# @lint-avoid-python-3-compatibility-imports
#
# pidpersec Count new processes (via fork).
# For Linux, uses BCC, eBPF. See .c file.
#
# USAGE: pidpersec
#
# Written as a basic example of counting an event.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 11-Aug-2015 Brendan Gregg Created this.
from bcc import BPF
from ctypes import c_int
from time import sleep, strftime
# load BPF program
b = BPF(text="""
#include <uapi/linux/ptrace.h>
enum stat_types {
S_COUNT = 1,
S_MAXSTAT
};
BPF_ARRAY(stats, u64, S_MAXSTAT);
static void stats_increment(int key) {
stats.atomic_increment(key);
}
void do_count(struct pt_regs *ctx) { stats_increment(S_COUNT); }
""")
b.attach_kprobe(event="sched_fork", fn_name="do_count")
# stat indexes
S_COUNT = c_int(1)
# header
print("Tracing... Ctrl-C to end.")
# output
while (1):
try:
sleep(1)
except KeyboardInterrupt:
exit()
print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
b["stats"][S_COUNT].value))
b["stats"].clear()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/wangcichen/bcc.git
git@gitee.com:wangcichen/bcc.git
wangcichen
bcc
bcc
master

Search