Brendan Gregg | ebdb242 | 2015-08-18 16:53:41 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 3 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 4 | # pidpersec Count new processes (via fork). |
| 5 | # For Linux, uses BCC, eBPF. See .c file. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 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 | # |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 14 | # 11-Aug-2015 Brendan Gregg Created this. |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 15 | |
Brenden Blanco | c35989d | 2015-09-02 18:04:07 -0700 | [diff] [blame] | 16 | from bcc import BPF |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 17 | from ctypes import c_int |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 18 | from time import sleep, strftime |
| 19 | |
| 20 | # load BPF program |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 21 | b = BPF(text=""" |
| 22 | #include <uapi/linux/ptrace.h> |
| 23 | |
| 24 | enum stat_types { |
| 25 | S_COUNT = 1, |
| 26 | S_MAXSTAT |
| 27 | }; |
| 28 | |
Brenden Blanco | d51870b | 2017-08-16 11:29:23 -0700 | [diff] [blame] | 29 | BPF_ARRAY(stats, u64, S_MAXSTAT); |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 30 | |
Brenden Blanco | 3f28e7b | 2017-04-20 09:33:44 -0700 | [diff] [blame] | 31 | static void stats_increment(int key) { |
Brendan Gregg | b90bbab | 2016-02-15 15:55:08 -0800 | [diff] [blame] | 32 | u64 *leaf = stats.lookup(&key); |
| 33 | if (leaf) (*leaf)++; |
| 34 | } |
| 35 | |
| 36 | void do_count(struct pt_regs *ctx) { stats_increment(S_COUNT); } |
| 37 | """) |
Brenden Blanco | 5eef65e | 2015-08-19 15:39:19 -0700 | [diff] [blame] | 38 | b.attach_kprobe(event="sched_fork", fn_name="do_count") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 39 | |
| 40 | # stat indexes |
Brendan Gregg | 9421054 | 2015-08-20 12:52:30 -0700 | [diff] [blame] | 41 | S_COUNT = c_int(1) |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 42 | |
| 43 | # header |
Brendan Gregg | 762b1b4 | 2015-08-18 16:49:48 -0700 | [diff] [blame] | 44 | print("Tracing... Ctrl-C to end.") |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 45 | |
| 46 | # output |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 47 | while (1): |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 48 | try: |
| 49 | sleep(1) |
| 50 | except KeyboardInterrupt: |
| 51 | exit() |
Brendan Gregg | 48fbc3e | 2015-08-18 14:56:14 -0700 | [diff] [blame] | 52 | |
Alexei Starovoitov | bdf0773 | 2016-01-14 10:09:20 -0800 | [diff] [blame] | 53 | print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"), |
| 54 | b["stats"][S_COUNT].value)) |
| 55 | b["stats"].clear() |