1*387f9dfdSAndroid Build Coastguard Worker#!/usr/bin/env python 2*387f9dfdSAndroid Build Coastguard Worker# @lint-avoid-python-3-compatibility-imports 3*387f9dfdSAndroid Build Coastguard Worker# 4*387f9dfdSAndroid Build Coastguard Worker# pidpersec Count new processes (via fork). 5*387f9dfdSAndroid Build Coastguard Worker# For Linux, uses BCC, eBPF. See .c file. 6*387f9dfdSAndroid Build Coastguard Worker# 7*387f9dfdSAndroid Build Coastguard Worker# USAGE: pidpersec 8*387f9dfdSAndroid Build Coastguard Worker# 9*387f9dfdSAndroid Build Coastguard Worker# Written as a basic example of counting an event. 10*387f9dfdSAndroid Build Coastguard Worker# 11*387f9dfdSAndroid Build Coastguard Worker# Copyright (c) 2015 Brendan Gregg. 12*387f9dfdSAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License") 13*387f9dfdSAndroid Build Coastguard Worker# 14*387f9dfdSAndroid Build Coastguard Worker# 11-Aug-2015 Brendan Gregg Created this. 15*387f9dfdSAndroid Build Coastguard Worker 16*387f9dfdSAndroid Build Coastguard Workerfrom bcc import BPF 17*387f9dfdSAndroid Build Coastguard Workerfrom ctypes import c_int 18*387f9dfdSAndroid Build Coastguard Workerfrom time import sleep, strftime 19*387f9dfdSAndroid Build Coastguard Worker 20*387f9dfdSAndroid Build Coastguard Worker# load BPF program 21*387f9dfdSAndroid Build Coastguard Workerb = BPF(text=""" 22*387f9dfdSAndroid Build Coastguard Worker#include <uapi/linux/ptrace.h> 23*387f9dfdSAndroid Build Coastguard Worker 24*387f9dfdSAndroid Build Coastguard Workerenum stat_types { 25*387f9dfdSAndroid Build Coastguard Worker S_COUNT = 1, 26*387f9dfdSAndroid Build Coastguard Worker S_MAXSTAT 27*387f9dfdSAndroid Build Coastguard Worker}; 28*387f9dfdSAndroid Build Coastguard Worker 29*387f9dfdSAndroid Build Coastguard WorkerBPF_ARRAY(stats, u64, S_MAXSTAT); 30*387f9dfdSAndroid Build Coastguard Worker 31*387f9dfdSAndroid Build Coastguard Workerstatic void stats_increment(int key) { 32*387f9dfdSAndroid Build Coastguard Worker stats.atomic_increment(key); 33*387f9dfdSAndroid Build Coastguard Worker} 34*387f9dfdSAndroid Build Coastguard Worker 35*387f9dfdSAndroid Build Coastguard Workervoid do_count(struct pt_regs *ctx) { stats_increment(S_COUNT); } 36*387f9dfdSAndroid Build Coastguard Worker""") 37*387f9dfdSAndroid Build Coastguard Workerb.attach_kprobe(event="sched_fork", fn_name="do_count") 38*387f9dfdSAndroid Build Coastguard Worker 39*387f9dfdSAndroid Build Coastguard Worker# stat indexes 40*387f9dfdSAndroid Build Coastguard WorkerS_COUNT = c_int(1) 41*387f9dfdSAndroid Build Coastguard Worker 42*387f9dfdSAndroid Build Coastguard Worker# header 43*387f9dfdSAndroid Build Coastguard Workerprint("Tracing... Ctrl-C to end.") 44*387f9dfdSAndroid Build Coastguard Worker 45*387f9dfdSAndroid Build Coastguard Worker# output 46*387f9dfdSAndroid Build Coastguard Workerwhile (1): 47*387f9dfdSAndroid Build Coastguard Worker try: 48*387f9dfdSAndroid Build Coastguard Worker sleep(1) 49*387f9dfdSAndroid Build Coastguard Worker except KeyboardInterrupt: 50*387f9dfdSAndroid Build Coastguard Worker exit() 51*387f9dfdSAndroid Build Coastguard Worker 52*387f9dfdSAndroid Build Coastguard Worker print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"), 53*387f9dfdSAndroid Build Coastguard Worker b["stats"][S_COUNT].value)) 54*387f9dfdSAndroid Build Coastguard Worker b["stats"].clear() 55