Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Brenden Blanco | 8e40c23 | 2015-08-23 22:59:38 -0700 | [diff] [blame] | 2 | # vim: ts=8 noet sw=8 |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 3 | # |
| 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 Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 16 | from bcc import BPF |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 17 | from ctypes import c_ushort, c_int, c_ulonglong |
| 18 | from time import sleep, strftime |
| 19 | |
| 20 | # load BPF program |
| 21 | b = BPF(src_file = "pidpersec.c") |
Brenden Blanco | 5eef65e | 2015-08-19 15:39:19 -0700 | [diff] [blame] | 22 | b.attach_kprobe(event="sched_fork", fn_name="do_count") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 23 | |
| 24 | # stat indexes |
Brendan Gregg | 9421054 | 2015-08-20 12:52:30 -0700 | [diff] [blame] | 25 | S_COUNT = c_int(1) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 26 | |
| 27 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 28 | print("Tracing... Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 29 | |
| 30 | # output |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 31 | while (1): |
| 32 | try: |
| 33 | sleep(1) |
| 34 | except KeyboardInterrupt: |
Brenden Blanco | 8e40c23 | 2015-08-23 22:59:38 -0700 | [diff] [blame] | 35 | exit() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 36 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 37 | print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"), |
Brendan Gregg | ef2452d | 2015-08-26 20:15:18 +1000 | [diff] [blame] | 38 | b["stats"][S_COUNT].value)) |
Brenden Blanco | 8e40c23 | 2015-08-23 22:59:38 -0700 | [diff] [blame] | 39 | b["stats"].clear() |