blob: 32afaa1da48423680657246295bda3d8a5d1022f [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 Greggef4e1fc2015-08-18 15:34:56 -070022stats = b.get_table("stats")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070023
24# stat indexes
25S_COUNT = 1
26
27# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070028print("Tracing... Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070029
30# output
31last = 0
32while (1):
33 try:
34 sleep(1)
35 except KeyboardInterrupt:
36 pass; exit()
37
Brendan Gregg762b1b42015-08-18 16:49:48 -070038 print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
39 (stats[c_int(S_COUNT)].value - last)))
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070040 last = stats[c_int(S_COUNT)].value