Brendan Gregg | 177e07e | 2015-08-18 16:11:35 -0700 | [diff] [blame] | 1 | #!/usr/bin/env 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") |
| 21 | BPF.attach_kprobe(b.load_func("do_count", BPF.KPROBE), "sched_fork") |
Brendan Gregg | ef4e1fc | 2015-08-18 15:34:56 -0700 | [diff] [blame] | 22 | stats = b.get_table("stats") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 23 | |
| 24 | # stat indexes |
| 25 | S_COUNT = 1 |
| 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: |
| 36 | pass; exit() |
| 37 | |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame^] | 38 | print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"), |
| 39 | (stats[c_int(S_COUNT)].value - last))) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 40 | last = stats[c_int(S_COUNT)].value |