Skip to content

Commit 4ba8055

Browse files
curuyonghong-song
authored andcommitted
tools/syscount: add --ppid option
we may need to run some one time command and trace its syscount, it's not easy to get the pid. but we can trace the ppid, usually the pid of current shell. eg: 1. get pid of current shell(shell 1) grep -i ppid /proc/self/status | cut -f2 2. run syscount in other shell(shell 2) syscount --ppid $pid_of_step_1 3. run the target command in shell 1 curl https://github.com/ we can trace the one time curl command with the --ppid option
1 parent a0fe2bc commit 4ba8055

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

man/man8/syscount.8

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
.SH NAME
33
syscount \- Summarize syscall counts and latencies.
44
.SH SYNOPSIS
5-
.B syscount [-h] [-p PID] [-t TID] [-i INTERVAL] [-d DURATION] [-T TOP] [-x] [-e ERRNO] [-L] [-m] [-P] [-l] [--syscall SYSCALL]
5+
.B syscount [-h] [-p PID] [-t TID] [-c PPID] [-i INTERVAL] [-d DURATION] [-T TOP] [-x] [-e ERRNO] [-L] [-m] [-P] [-l] [--syscall SYSCALL]
66
.SH DESCRIPTION
77
This tool traces syscall entry and exit tracepoints and summarizes either the
88
number of syscalls of each type, or the number of syscalls per process. It can
@@ -23,6 +23,9 @@ Trace only this process.
2323
\-t TID
2424
Trace only this thread.
2525
.TP
26+
\-c PPID
27+
Trace only child of this pid.
28+
.TP
2629
\-i INTERVAL
2730
Print the summary at the specified interval (in seconds).
2831
.TP

tools/syscount.py

+20
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def handle_errno(errstr):
4949
help="trace only this pid")
5050
parser.add_argument("-t", "--tid", type=int,
5151
help="trace only this tid")
52+
parser.add_argument("-c", "--ppid", type=int,
53+
help="trace only child of this pid")
5254
parser.add_argument("-i", "--interval", type=int,
5355
help="print summary at this interval (seconds)")
5456
parser.add_argument("-d", "--duration", type=int,
@@ -94,6 +96,8 @@ def handle_errno(errstr):
9496
sys.exit(0)
9597

9698
text = """
99+
#include <linux/sched.h>
100+
97101
#ifdef LATENCY
98102
struct data_t {
99103
u64 count;
@@ -127,6 +131,13 @@ def handle_errno(errstr):
127131
return 0;
128132
#endif
129133
134+
#ifdef FILTER_PPID
135+
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
136+
u32 ppid = task->real_parent->tgid;
137+
if (ppid != FILTER_PPID)
138+
return 0;
139+
#endif
140+
130141
u64 t = bpf_ktime_get_ns();
131142
start.update(&pid_tgid, &t);
132143
return 0;
@@ -153,6 +164,13 @@ def handle_errno(errstr):
153164
return 0;
154165
#endif
155166
167+
#ifdef FILTER_PPID
168+
struct task_struct *task = (struct task_struct *)bpf_get_current_task();
169+
u32 ppid = task->real_parent->tgid;
170+
if (ppid != FILTER_PPID)
171+
return 0;
172+
#endif
173+
156174
#ifdef FILTER_FAILED
157175
if (args->ret >= 0)
158176
return 0;
@@ -195,6 +213,8 @@ def handle_errno(errstr):
195213
text = ("#define FILTER_PID %d\n" % args.pid) + text
196214
elif args.tid:
197215
text = ("#define FILTER_TID %d\n" % args.tid) + text
216+
elif args.ppid:
217+
text = ("#define FILTER_PPID %d\n" % args.ppid) + text
198218
if args.failures:
199219
text = "#define FILTER_FAILED\n" + text
200220
if args.errno:

tools/syscount_example.txt

+1
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ optional arguments:
165165
-h, --help show this help message and exit
166166
-p PID, --pid PID trace only this pid
167167
-t TID, --tid TID trace only this tid
168+
-c PPID, --ppid PPID trace only child of this pid
168169
-i INTERVAL, --interval INTERVAL
169170
print summary at this interval (seconds)
170171
-d DURATION, --duration DURATION

0 commit comments

Comments
 (0)