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 | |
| 16 | from bpf import BPF |
| 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 |
| 31 | last = 0 |
| 32 | while (1): |
| 33 | try: |
| 34 | sleep(1) |
| 35 | except KeyboardInterrupt: |
Brenden Blanco | 8e40c23 | 2015-08-23 22:59:38 -0700 | [diff] [blame^] | 36 | exit() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 37 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 38 | print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"), |
Brenden Blanco | 8e40c23 | 2015-08-23 22:59:38 -0700 | [diff] [blame^] | 39 | (b["stats"][S_COUNT].value))) |
| 40 | b["stats"].clear() |