blob: 6976ba612c20430f6e9f33b4c2c2bfbdaea9028f [file] [log] [blame]
Brendan Greggebdb2422015-08-18 16:53:41 -07001#!/usr/bin/python
Brendan Gregg48fbc3e2015-08-18 14:56:14 -07002#
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
15from bpf import BPF
16from ctypes import c_ushort, c_int, c_ulonglong
17from time import sleep, strftime
18
19# load BPF program
20b = BPF(src_file = "pidpersec.c")
Brenden Blanco5eef65e2015-08-19 15:39:19 -070021b.attach_kprobe(event="sched_fork", fn_name="do_count")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070022
23# stat indexes
Brendan Gregg94210542015-08-20 12:52:30 -070024S_COUNT = c_int(1)
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070025
26# header
Brendan Gregg762b1b42015-08-18 16:49:48 -070027print("Tracing... Ctrl-C to end.")
Brendan Gregg48fbc3e2015-08-18 14:56:14 -070028
29# output
30last = 0
31while (1):
32 try:
33 sleep(1)
34 except KeyboardInterrupt:
35 pass; exit()
36
Brendan Gregg762b1b42015-08-18 16:49:48 -070037 print("%s: PIDs/sec: %d" % (strftime("%H:%M:%S"),
Brendan Gregg94210542015-08-20 12:52:30 -070038 (b["stats"][S_COUNT].value - last)))
39 last = b["stats"][S_COUNT].value