Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 2 | # |
| 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 | |
| 15 | from bpf import BPF |
| 16 | from ctypes import c_ushort, c_int, c_ulonglong |
| 17 | from time import sleep, strftime |
| 18 | |
| 19 | # load BPF program |
| 20 | b = BPF(src_file = "pidpersec.c") |
Brenden Blanco | 5eef65e | 2015-08-19 15:39:19 -0700 | [diff] [blame] | 21 | b.attach_kprobe(event="sched_fork", fn_name="do_count") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 22 | |
| 23 | # stat indexes |
Brendan Gregg | 9421054 | 2015-08-20 12:52:30 -0700 | [diff] [blame] | 24 | S_COUNT = c_int(1) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 25 | |
| 26 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 27 | print("Tracing... Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 28 | |
| 29 | # output |
| 30 | last = 0 |
| 31 | while (1): |
| 32 | try: |
| 33 | sleep(1) |
| 34 | except KeyboardInterrupt: |
| 35 | pass; exit() |
| 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 | 9421054 | 2015-08-20 12:52:30 -0700 | [diff] [blame] | 38 | (b["stats"][S_COUNT].value - last))) |
| 39 | last = b["stats"][S_COUNT].value |