Skip to content

Commit

Permalink
tools: remove redundant Python event data structure definitions (iovi…
Browse files Browse the repository at this point in the history
…sor#2204)

Simplify code following iovisor#2198 (iovisor#2198).

Some tools are not touched: mountsnoop.py, trace.py, lib/*.py, old/*.py.
  • Loading branch information
boat0 authored and yonghong-song committed Feb 15, 2019
1 parent 75a1f3d commit 51d62d3
Show file tree
Hide file tree
Showing 38 changed files with 46 additions and 628 deletions.
10 changes: 1 addition & 9 deletions tools/bashreadline.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from __future__ import print_function
from bcc import BPF
from time import strftime
import ctypes as ct

# load BPF program
bpf_text = """
Expand All @@ -40,13 +39,6 @@
return 0;
};
"""
STR_DATA = 80

class Data(ct.Structure):
_fields_ = [
("pid", ct.c_ulonglong),
("str", ct.c_char * STR_DATA)
]

b = BPF(text=bpf_text)
b.attach_uretprobe(name="/bin/bash", sym="readline", fn_name="printret")
Expand All @@ -55,7 +47,7 @@ class Data(ct.Structure):
print("%-9s %-6s %s" % ("TIME", "PID", "COMMAND"))

def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)
print("%-9s %-6d %s" % (strftime("%H:%M:%S"), event.pid,
event.str.decode('utf-8', 'replace')))

Expand Down
18 changes: 1 addition & 17 deletions tools/biosnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

from __future__ import print_function
from bcc import BPF
import ctypes as ct
import re

# load BPF program
Expand Down Expand Up @@ -128,21 +127,6 @@
b.attach_kprobe(event="blk_account_io_completion",
fn_name="trace_req_completion")

TASK_COMM_LEN = 16 # linux/sched.h
DISK_NAME_LEN = 32 # linux/genhd.h

class Data(ct.Structure):
_fields_ = [
("pid", ct.c_ulonglong),
("rwflag", ct.c_ulonglong),
("delta", ct.c_ulonglong),
("sector", ct.c_ulonglong),
("len", ct.c_ulonglong),
("ts", ct.c_ulonglong),
("disk_name", ct.c_char * DISK_NAME_LEN),
("name", ct.c_char * TASK_COMM_LEN)
]

# header
print("%-14s %-14s %-6s %-7s %-2s %-9s %-7s %7s" % ("TIME(s)", "COMM", "PID",
"DISK", "T", "SECTOR", "BYTES", "LAT(ms)"))
Expand All @@ -154,7 +138,7 @@ class Data(ct.Structure):

# process event
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)

val = -1
global start_ts
Expand Down
18 changes: 1 addition & 17 deletions tools/btrfsslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
from bcc import BPF
import argparse
from time import strftime
import ctypes as ct

# symbols
kallsyms = "/proc/kallsyms"
Expand Down Expand Up @@ -287,24 +286,9 @@
if args.ebpf:
exit()

# kernel->user event data: struct data_t
DNAME_INLINE_LEN = 32 # linux/dcache.h
TASK_COMM_LEN = 16 # linux/sched.h
class Data(ct.Structure):
_fields_ = [
("ts_us", ct.c_ulonglong),
("type", ct.c_ulonglong),
("size", ct.c_ulonglong),
("offset", ct.c_ulonglong),
("delta_us", ct.c_ulonglong),
("pid", ct.c_ulonglong),
("task", ct.c_char * TASK_COMM_LEN),
("file", ct.c_char * DNAME_INLINE_LEN)
]

# process event
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)

type = 'R'
if event.type == 1:
Expand Down
16 changes: 1 addition & 15 deletions tools/capable.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import errno
import argparse
from time import strftime
import ctypes as ct

# arguments
examples = """examples:
Expand Down Expand Up @@ -165,19 +164,6 @@ def __getattr__(self, name):
# initialize BPF
b = BPF(text=bpf_text)

TASK_COMM_LEN = 16 # linux/sched.h

class Data(ct.Structure):
_fields_ = [
("tgid", ct.c_uint32),
("pid", ct.c_uint32),
("uid", ct.c_uint32),
("cap", ct.c_int),
("audit", ct.c_int),
("comm", ct.c_char * TASK_COMM_LEN),
] + ([("kernel_stack_id", ct.c_int)] if args.kernel_stack else []) \
+ ([("user_stack_id", ct.c_int)] if args.user_stack else [])

# header
print("%-9s %-6s %-6s %-6s %-16s %-4s %-20s %s" % (
"TIME", "UID", "PID", "TID", "COMM", "CAP", "NAME", "AUDIT"))
Expand All @@ -198,7 +184,7 @@ def print_stack(bpf, stack_id, stack_type, tgid):

# process event
def print_event(bpf, cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)

if event.cap in capabilities:
name = capabilities[event.cap]
Expand Down
10 changes: 1 addition & 9 deletions tools/cpuunclaimed.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,9 @@
from __future__ import print_function
from bcc import BPF, PerfType, PerfSWConfig
from time import sleep, strftime
from ctypes import c_int
import argparse
import multiprocessing
from os import getpid, system, open, close, dup, unlink, O_WRONLY
import ctypes as ct
from tempfile import NamedTemporaryFile

# arguments
Expand Down Expand Up @@ -248,20 +246,14 @@ def check_runnable_weight_field():
else:
print(("Sampling run queues... Output every %s seconds. " +
"Hit Ctrl-C to end.") % args.interval)
class Data(ct.Structure):
_fields_ = [
("ts", ct.c_ulonglong),
("cpu", ct.c_ulonglong),
("len", ct.c_ulonglong)
]

samples = {}
group = {}
last = 0

# process event
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)
samples[event.ts] = {}
samples[event.ts]['cpu'] = event.cpu
samples[event.ts]['len'] = event.len
Expand Down
15 changes: 1 addition & 14 deletions tools/criticalstat.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
from __future__ import print_function
from bcc import BPF
import argparse
import ctypes as ct
import sys
import subprocess
import os.path
Expand Down Expand Up @@ -271,18 +270,6 @@

b = BPF(text=bpf_text)

TASK_COMM_LEN = 16 # linux/sched.h

class Data(ct.Structure):
_fields_ = [
("time", ct.c_ulonglong),
("stack_id", ct.c_longlong),
("cpu", ct.c_int),
("id", ct.c_ulonglong),
("addrs", ct.c_int * 4),
("comm", ct.c_char * TASK_COMM_LEN),
]

def get_syms(kstack):
syms = []

Expand All @@ -296,7 +283,7 @@ def get_syms(kstack):
def print_event(cpu, data, size):
try:
global b
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)
stack_traces = b['stack_traces']
stext = b.ksymname('_stext')

Expand Down
11 changes: 1 addition & 10 deletions tools/dbslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from bcc import BPF, USDT
import argparse
import re
import ctypes as ct
import subprocess

examples = """examples:
Expand Down Expand Up @@ -203,18 +202,10 @@
if args.ebpf:
exit()

class Data(ct.Structure):
_fields_ = [
("pid", ct.c_ulonglong),
("timestamp", ct.c_ulonglong),
("delta", ct.c_ulonglong),
("query", ct.c_char * 256)
]

start = BPF.monotonic_time()

def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = bpf["events"].event(data)
print("%-14.6f %-6d %8.3f %s" % (
float(event.timestamp - start) / 1000000000,
event.pid, float(event.delta) / 1000000, event.query))
Expand Down
17 changes: 1 addition & 16 deletions tools/execsnoop.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from bcc.utils import ArgString, printb
import bcc.utils as utils
import argparse
import ctypes as ct
import re
import time
from collections import defaultdict
Expand Down Expand Up @@ -173,19 +172,6 @@
print("%-8s" % ("TIME(s)"), end="")
print("%-16s %-6s %-6s %3s %s" % ("PCOMM", "PID", "PPID", "RET", "ARGS"))

TASK_COMM_LEN = 16 # linux/sched.h
ARGSIZE = 128 # should match #define in C above

class Data(ct.Structure):
_fields_ = [
("pid", ct.c_uint),
("ppid", ct.c_uint),
("comm", ct.c_char * TASK_COMM_LEN),
("type", ct.c_int),
("argv", ct.c_char * ARGSIZE),
("retval", ct.c_int),
]

class EventType(object):
EVENT_ARG = 0
EVENT_RET = 1
Expand All @@ -209,8 +195,7 @@ def get_ppid(pid):

# process event
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents

event = b["events"].event(data)
skip = False

if event.type == EventType.EVENT_ARG:
Expand Down
18 changes: 1 addition & 17 deletions tools/ext4slower.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
from bcc import BPF
import argparse
from time import strftime
import ctypes as ct

# symbols
kallsyms = "/proc/kallsyms"
Expand Down Expand Up @@ -285,24 +284,9 @@
if args.ebpf:
exit()

# kernel->user event data: struct data_t
DNAME_INLINE_LEN = 32 # linux/dcache.h
TASK_COMM_LEN = 16 # linux/sched.h
class Data(ct.Structure):
_fields_ = [
("ts_us", ct.c_ulonglong),
("type", ct.c_ulonglong),
("size", ct.c_ulonglong),
("offset", ct.c_ulonglong),
("delta_us", ct.c_ulonglong),
("pid", ct.c_ulonglong),
("task", ct.c_char * TASK_COMM_LEN),
("file", ct.c_char * DNAME_INLINE_LEN)
]

# process event
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)

type = 'R'
if event.type == 1:
Expand Down
14 changes: 1 addition & 13 deletions tools/filelife.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from bcc import BPF
import argparse
from time import strftime
import ctypes as ct

# arguments
examples = """examples:
Expand Down Expand Up @@ -100,17 +99,6 @@
}
"""

TASK_COMM_LEN = 16 # linux/sched.h
DNAME_INLINE_LEN = 255 # linux/dcache.h

class Data(ct.Structure):
_fields_ = [
("pid", ct.c_uint),
("delta", ct.c_ulonglong),
("comm", ct.c_char * TASK_COMM_LEN),
("fname", ct.c_char * DNAME_INLINE_LEN)
]

if args.pid:
bpf_text = bpf_text.replace('FILTER',
'if (pid != %s) { return 0; }' % args.pid)
Expand All @@ -134,7 +122,7 @@ class Data(ct.Structure):

# process event
def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)
print("%-8s %-6d %-16s %-7.2f %s" % (strftime("%H:%M:%S"), event.pid,
event.comm.decode('utf-8', 'replace'), float(event.delta) / 1000,
event.fname.decode('utf-8', 'replace')))
Expand Down
17 changes: 1 addition & 16 deletions tools/fileslower.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
from __future__ import print_function
from bcc import BPF
import argparse
import ctypes as ct
import time

# arguments
Expand Down Expand Up @@ -210,20 +209,6 @@
b.attach_kprobe(event="vfs_write", fn_name="trace_write_entry")
b.attach_kretprobe(event="vfs_write", fn_name="trace_write_return")

TASK_COMM_LEN = 16 # linux/sched.h
DNAME_INLINE_LEN = 32 # linux/dcache.h

class Data(ct.Structure):
_fields_ = [
("mode", ct.c_int),
("pid", ct.c_uint),
("sz", ct.c_uint),
("delta_us", ct.c_ulonglong),
("name_len", ct.c_uint),
("name", ct.c_char * DNAME_INLINE_LEN),
("comm", ct.c_char * TASK_COMM_LEN),
]

mode_s = {
0: 'R',
1: 'W',
Expand All @@ -237,7 +222,7 @@ class Data(ct.Structure):
start_ts = time.time()

def print_event(cpu, data, size):
event = ct.cast(data, ct.POINTER(Data)).contents
event = b["events"].event(data)

ms = float(event.delta_us) / 1000
name = event.name.decode('utf-8', 'replace')
Expand Down
Loading

0 comments on commit 51d62d3

Please sign in to comment.