blob: 6a3ca8cb40d4a2454a98bee1d78df70ee98ddf74 [file] [log] [blame]
Brendan Gregg177e07e2015-08-18 16:11:35 -07001#!/usr/bin/env 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")
21BPF.attach_kprobe(b.load_func("do_count", BPF.KPROBE), "sched_fork")
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