blob: 7e23fbbde7db0412ff7953dad9653568f0a57b03 [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
Brenden Blancoc35989d2015-09-02 18:04:07 -070016from bcc import BPF
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070017from 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
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070031while (1):
32 try:
33 sleep(1)
34 except KeyboardInterrupt:
Brenden Blanco8e40c232015-08-23 22:59:38 -070035 exit()
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070036
Brendan Gregg762b1b42015-08-18 16:49:48 -070037 print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
Brendan Greggef2452d2015-08-26 20:15:18 +100038 b["stats"][S_COUNT].value))
Brenden Blanco8e40c232015-08-23 22:59:38 -070039 b["stats"].clear()