tools: filter using PID intead of TID
authorHengqi Chen <chenhengqi@outlook.com>
Thu, 13 May 2021 13:46:16 +0000 (21:46 +0800)
committeryonghong-song <ys114321@gmail.com>
Fri, 14 May 2021 02:56:55 +0000 (19:56 -0700)
Signed-off-by: Hengqi Chen <chenhengqi@outlook.com>
tools/btrfsdist.py
tools/ext4dist.py
tools/nfsdist.py
tools/xfsdist.py
tools/zfsdist.py

index 4659ab46eeef8c7084336f6605db88555bdd45a3..0aad2d945af998f66281cac38ad96ce39cb3bc84 100755 (executable)
@@ -73,11 +73,14 @@ BPF_HISTOGRAM(dist, dist_key_t);
 // time operation
 int trace_entry(struct pt_regs *ctx)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
@@ -86,7 +89,10 @@ int trace_entry(struct pt_regs *ctx)
 // I do by checking file->f_op.
 int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
 
@@ -96,7 +102,7 @@ int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb)
         return 0;
 
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
@@ -105,8 +111,10 @@ int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb)
 int trace_open_entry(struct pt_regs *ctx, struct inode *inode,
     struct file *file)
 {
-    u32 pid;
-    pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
 
@@ -115,17 +123,19 @@ int trace_open_entry(struct pt_regs *ctx, struct inode *inode,
         return 0;
 
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
 static int trace_return(struct pt_regs *ctx, const char *op)
 {
     u64 *tsp;
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
 
     // fetch timestamp and calculate delta
-    tsp = start.lookup(&pid);
+    tsp = start.lookup(&tid);
     if (tsp == 0) {
         return 0;   // missed start or filtered
     }
@@ -136,7 +146,7 @@ static int trace_return(struct pt_regs *ctx, const char *op)
     __builtin_memcpy(&key.op, op, sizeof(key.op));
     dist.increment(key);
 
-    start.delete(&pid);
+    start.delete(&tid);
     return 0;
 }
 
index 384a4c147bacd8e057bee24a09ccdb4743faf885..49139c8197c6c09dd025144cedf9cd4733460d14 100755 (executable)
@@ -73,11 +73,14 @@ BPF_HISTOGRAM(dist, dist_key_t);
 // time operation
 int trace_entry(struct pt_regs *ctx)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
@@ -86,15 +89,17 @@ EXT4_TRACE_READ_CODE
 static int trace_return(struct pt_regs *ctx, const char *op)
 {
     u64 *tsp;
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
 
     // fetch timestamp and calculate delta
-    tsp = start.lookup(&pid);
+    tsp = start.lookup(&tid);
     if (tsp == 0) {
         return 0;   // missed start or filtered
     }
     u64 delta = bpf_ktime_get_ns() - *tsp;
-    start.delete(&pid);
+    start.delete(&tid);
 
     // Skip entries with backwards time: temp workaround for #728
     if ((s64) delta < 0)
@@ -164,7 +169,10 @@ else:
     ext4_trace_read_code = """
 int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
 
@@ -174,7 +182,7 @@ int trace_read_entry(struct pt_regs *ctx, struct kiocb *iocb)
         return 0;
 
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }""" % ext4_file_ops_addr
 
index 2243d4070638df8fd740e10416df43baec7b3ad0..a0841ac1608b88888276aeec4f56f06829859592 100755 (executable)
@@ -68,21 +68,26 @@ BPF_HISTOGRAM(dist, dist_key_t);
 // time operation
 int trace_entry(struct pt_regs *ctx)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
 static int trace_return(struct pt_regs *ctx, const char *op)
 {
     u64 *tsp;
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
 
     // fetch timestamp and calculate delta
-    tsp = start.lookup(&pid);
+    tsp = start.lookup(&tid);
     if (tsp == 0) {
         return 0;   // missed start or filtered
     }
@@ -93,7 +98,7 @@ static int trace_return(struct pt_regs *ctx, const char *op)
     __builtin_memcpy(&key.op, op, sizeof(key.op));
     dist.increment(key);
 
-    start.delete(&pid);
+    start.delete(&tid);
     return 0;
 }
 
index f409f90dbabe9cf21854e1097c2f1e49f8a98fa5..54b1687f5c6173fb7f592f214cd41cfad1ca338e 100755 (executable)
@@ -70,21 +70,26 @@ BPF_HISTOGRAM(dist, dist_key_t);
 // time operation
 int trace_entry(struct pt_regs *ctx)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
 static int trace_return(struct pt_regs *ctx, const char *op)
 {
     u64 *tsp;
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
 
     // fetch timestamp and calculate delta
-    tsp = start.lookup(&pid);
+    tsp = start.lookup(&tid);
     if (tsp == 0) {
         return 0;   // missed start or filtered
     }
@@ -95,7 +100,7 @@ static int trace_return(struct pt_regs *ctx, const char *op)
     __builtin_memcpy(&key.op, op, sizeof(key.op));
     dist.increment(key);
 
-    start.delete(&pid);
+    start.delete(&tid);
     return 0;
 }
 
index 6b29b99baddf8cee4bdc30dc993b35e34f807aa4..112b10c188bd60ffbc804567a375e7ddbb1b838b 100755 (executable)
@@ -70,21 +70,26 @@ BPF_HISTOGRAM(dist, dist_key_t);
 // time operation
 int trace_entry(struct pt_regs *ctx)
 {
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
+
     if (FILTER_PID)
         return 0;
     u64 ts = bpf_ktime_get_ns();
-    start.update(&pid, &ts);
+    start.update(&tid, &ts);
     return 0;
 }
 
 static int trace_return(struct pt_regs *ctx, const char *op)
 {
     u64 *tsp;
-    u32 pid = bpf_get_current_pid_tgid();
+    u64 pid_tgid = bpf_get_current_pid_tgid();
+    u32 pid = pid_tgid >> 32;
+    u32 tid = (u32)pid_tgid;
 
     // fetch timestamp and calculate delta
-    tsp = start.lookup(&pid);
+    tsp = start.lookup(&tid);
     if (tsp == 0) {
         return 0;   // missed start or filtered
     }
@@ -95,7 +100,7 @@ static int trace_return(struct pt_regs *ctx, const char *op)
     __builtin_memcpy(&key.op, op, sizeof(key.op));
     dist.increment(key);
 
-    start.delete(&pid);
+    start.delete(&tid);
     return 0;
 }