blob: 2d3c731fe3e58e25b9b0e5d2bd7698f98c51fca3 [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/usr/bin/python
Brenden Blanco8e40c232015-08-23 22:59:38 -07002# vim: ts=8 noet sw=8
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07003#
4# pidpersec Count new processes (via fork).
5# For Linux, uses BCC, eBPF. See .c file.
6#
7# USAGE: pidpersec
8#
9# Written as a basic example of counting an event.
10#
11# Copyright (c) 2015 Brendan Gregg.
12# Licensed under the Apache License, Version 2.0 (the "License")
13#
14# 11-Aug-2015 Brendan Gregg Created this.
15
16from bpf import BPF
17from ctypes import c_ushort, c_int, c_ulonglong
18from time import sleep, strftime
19
20# load BPF program
21b = BPF(src_file = "pidpersec.c")
Brenden Blanco5eef65e2015-08-19 15:39:19 -070022b.attach_kprobe(event="sched_fork", fn_name="do_count")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070023
24# stat indexes
Brendan Gregg94210542015-08-20 12:52:30 -070025S_COUNT = c_int(1)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070026
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:
Brenden Blanco8e40c232015-08-23 22:59:38 -070036 exit()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070037
Brendan Gregg762b1b42015-08-18 16:49:48 -070038 print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
Brenden Blanco8e40c232015-08-23 22:59:38 -070039 (b["stats"][S_COUNT].value)))
40 b["stats"].clear()