blob: 767c092f44c060447ab4e367e9c4d5601152f9f3 [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/usr/bin/python
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07002#
3# pidpersec Count new processes (via fork).
4# For Linux, uses BCC, eBPF. See .c file.
5#
6# USAGE: pidpersec
7#
8# Written as a basic example of counting an event.
9#
10# Copyright (c) 2015 Brendan Gregg.
11# Licensed under the Apache License, Version 2.0 (the "License")
12#
13# 11-Aug-2015 Brendan Gregg Created this.
14
15from bpf import BPF
16from ctypes import c_ushort, c_int, c_ulonglong
17from time import sleep, strftime
18
19# load BPF program
20b = BPF(src_file = "pidpersec.c")
Brenden Blanco5eef65e2015-08-19 15:39:19 -070021b.attach_kprobe(event="sched_fork", fn_name="do_count")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070022
23# stat indexes
24S_COUNT = 1
25
26# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070027print("Tracing... Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070028
29# output
30last = 0
31while (1):
32 try:
33 sleep(1)
34 except KeyboardInterrupt:
35 pass; exit()
36
Brendan Gregg762b1b42015-08-18 16:49:48 -070037 print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
Brendan Gregg057b0782015-08-20 12:40:37 -070038 (b["stats"][c_int(S_COUNT)].value - last)))
39 last = b["stats"][c_int(S_COUNT)].value