Commit 3a45935a authored by Gary Lin's avatar Gary Lin

tools/syscount: convert syscall and process names to bytes array

subprocess.check_output() returns a bytes array in python3 while it
returns str in python2. To improve the compatibility, convert the
related fields to bytes arrays and print them with printb().
Signed-off-by: default avatarGary Lin <glin@suse.com>
parent 76bec4d5
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
# 15-Feb-2017 Sasha Goldshtein Created this. # 15-Feb-2017 Sasha Goldshtein Created this.
from bcc import BPF from bcc import BPF
from bcc.utils import printb
from time import sleep, strftime from time import sleep, strftime
import argparse import argparse
import errno import errno
...@@ -35,320 +36,320 @@ else: ...@@ -35,320 +36,320 @@ else:
# END { print "}" }' # END { print "}" }'
# #
syscalls = { syscalls = {
0: "read", 0: b"read",
1: "write", 1: b"write",
2: "open", 2: b"open",
3: "close", 3: b"close",
4: "stat", 4: b"stat",
5: "fstat", 5: b"fstat",
6: "lstat", 6: b"lstat",
7: "poll", 7: b"poll",
8: "lseek", 8: b"lseek",
9: "mmap", 9: b"mmap",
10: "mprotect", 10: b"mprotect",
11: "munmap", 11: b"munmap",
12: "brk", 12: b"brk",
13: "rt_sigaction", 13: b"rt_sigaction",
14: "rt_sigprocmask", 14: b"rt_sigprocmask",
15: "rt_sigreturn", 15: b"rt_sigreturn",
16: "ioctl", 16: b"ioctl",
17: "pread", 17: b"pread",
18: "pwrite", 18: b"pwrite",
19: "readv", 19: b"readv",
20: "writev", 20: b"writev",
21: "access", 21: b"access",
22: "pipe", 22: b"pipe",
23: "select", 23: b"select",
24: "sched_yield", 24: b"sched_yield",
25: "mremap", 25: b"mremap",
26: "msync", 26: b"msync",
27: "mincore", 27: b"mincore",
28: "madvise", 28: b"madvise",
29: "shmget", 29: b"shmget",
30: "shmat", 30: b"shmat",
31: "shmctl", 31: b"shmctl",
32: "dup", 32: b"dup",
33: "dup2", 33: b"dup2",
34: "pause", 34: b"pause",
35: "nanosleep", 35: b"nanosleep",
36: "getitimer", 36: b"getitimer",
37: "alarm", 37: b"alarm",
38: "setitimer", 38: b"setitimer",
39: "getpid", 39: b"getpid",
40: "sendfile", 40: b"sendfile",
41: "socket", 41: b"socket",
42: "connect", 42: b"connect",
43: "accept", 43: b"accept",
44: "sendto", 44: b"sendto",
45: "recvfrom", 45: b"recvfrom",
46: "sendmsg", 46: b"sendmsg",
47: "recvmsg", 47: b"recvmsg",
48: "shutdown", 48: b"shutdown",
49: "bind", 49: b"bind",
50: "listen", 50: b"listen",
51: "getsockname", 51: b"getsockname",
52: "getpeername", 52: b"getpeername",
53: "socketpair", 53: b"socketpair",
54: "setsockopt", 54: b"setsockopt",
55: "getsockopt", 55: b"getsockopt",
56: "clone", 56: b"clone",
57: "fork", 57: b"fork",
58: "vfork", 58: b"vfork",
59: "execve", 59: b"execve",
60: "_exit", 60: b"_exit",
61: "wait4", 61: b"wait4",
62: "kill", 62: b"kill",
63: "uname", 63: b"uname",
64: "semget", 64: b"semget",
65: "semop", 65: b"semop",
66: "semctl", 66: b"semctl",
67: "shmdt", 67: b"shmdt",
68: "msgget", 68: b"msgget",
69: "msgsnd", 69: b"msgsnd",
70: "msgrcv", 70: b"msgrcv",
71: "msgctl", 71: b"msgctl",
72: "fcntl", 72: b"fcntl",
73: "flock", 73: b"flock",
74: "fsync", 74: b"fsync",
75: "fdatasync", 75: b"fdatasync",
76: "truncate", 76: b"truncate",
77: "ftruncate", 77: b"ftruncate",
78: "getdents", 78: b"getdents",
79: "getcwd", 79: b"getcwd",
80: "chdir", 80: b"chdir",
81: "fchdir", 81: b"fchdir",
82: "rename", 82: b"rename",
83: "mkdir", 83: b"mkdir",
84: "rmdir", 84: b"rmdir",
85: "creat", 85: b"creat",
86: "link", 86: b"link",
87: "unlink", 87: b"unlink",
88: "symlink", 88: b"symlink",
89: "readlink", 89: b"readlink",
90: "chmod", 90: b"chmod",
91: "fchmod", 91: b"fchmod",
92: "chown", 92: b"chown",
93: "fchown", 93: b"fchown",
94: "lchown", 94: b"lchown",
95: "umask", 95: b"umask",
96: "gettimeofday", 96: b"gettimeofday",
97: "getrlimit", 97: b"getrlimit",
98: "getrusage", 98: b"getrusage",
99: "sysinfo", 99: b"sysinfo",
100: "times", 100: b"times",
101: "ptrace", 101: b"ptrace",
102: "getuid", 102: b"getuid",
103: "syslog", 103: b"syslog",
104: "getgid", 104: b"getgid",
105: "setuid", 105: b"setuid",
106: "setgid", 106: b"setgid",
107: "geteuid", 107: b"geteuid",
108: "getegid", 108: b"getegid",
109: "setpgid", 109: b"setpgid",
110: "getppid", 110: b"getppid",
111: "getpgrp", 111: b"getpgrp",
112: "setsid", 112: b"setsid",
113: "setreuid", 113: b"setreuid",
114: "setregid", 114: b"setregid",
115: "getgroups", 115: b"getgroups",
116: "setgroups", 116: b"setgroups",
117: "setresuid", 117: b"setresuid",
118: "getresuid", 118: b"getresuid",
119: "setresgid", 119: b"setresgid",
120: "getresgid", 120: b"getresgid",
121: "getpgid", 121: b"getpgid",
122: "setfsuid", 122: b"setfsuid",
123: "setfsgid", 123: b"setfsgid",
124: "getsid", 124: b"getsid",
125: "capget", 125: b"capget",
126: "capset", 126: b"capset",
127: "rt_sigpending", 127: b"rt_sigpending",
128: "rt_sigtimedwait", 128: b"rt_sigtimedwait",
129: "rt_sigqueueinfo", 129: b"rt_sigqueueinfo",
130: "rt_sigsuspend", 130: b"rt_sigsuspend",
131: "sigaltstack", 131: b"sigaltstack",
132: "utime", 132: b"utime",
133: "mknod", 133: b"mknod",
134: "uselib", 134: b"uselib",
135: "personality", 135: b"personality",
136: "ustat", 136: b"ustat",
137: "statfs", 137: b"statfs",
138: "fstatfs", 138: b"fstatfs",
139: "sysfs", 139: b"sysfs",
140: "getpriority", 140: b"getpriority",
141: "setpriority", 141: b"setpriority",
142: "sched_setparam", 142: b"sched_setparam",
143: "sched_getparam", 143: b"sched_getparam",
144: "sched_setscheduler", 144: b"sched_setscheduler",
145: "sched_getscheduler", 145: b"sched_getscheduler",
146: "sched_get_priority_max", 146: b"sched_get_priority_max",
147: "sched_get_priority_min", 147: b"sched_get_priority_min",
148: "sched_rr_get_interval", 148: b"sched_rr_get_interval",
149: "mlock", 149: b"mlock",
150: "munlock", 150: b"munlock",
151: "mlockall", 151: b"mlockall",
152: "munlockall", 152: b"munlockall",
153: "vhangup", 153: b"vhangup",
154: "modify_ldt", 154: b"modify_ldt",
155: "pivot_root", 155: b"pivot_root",
156: "_sysctl", 156: b"_sysctl",
157: "prctl", 157: b"prctl",
158: "arch_prctl", 158: b"arch_prctl",
159: "adjtimex", 159: b"adjtimex",
160: "setrlimit", 160: b"setrlimit",
161: "chroot", 161: b"chroot",
162: "sync", 162: b"sync",
163: "acct", 163: b"acct",
164: "settimeofday", 164: b"settimeofday",
165: "mount", 165: b"mount",
166: "umount", 166: b"umount",
167: "swapon", 167: b"swapon",
168: "swapoff", 168: b"swapoff",
169: "reboot", 169: b"reboot",
170: "sethostname", 170: b"sethostname",
171: "setdomainname", 171: b"setdomainname",
172: "iopl", 172: b"iopl",
173: "ioperm", 173: b"ioperm",
174: "create_module", 174: b"create_module",
175: "init_module", 175: b"init_module",
176: "delete_module", 176: b"delete_module",
177: "get_kernel_syms", 177: b"get_kernel_syms",
178: "query_module", 178: b"query_module",
179: "quotactl", 179: b"quotactl",
180: "nfsservctl", 180: b"nfsservctl",
181: "getpmsg", 181: b"getpmsg",
182: "putpmsg", 182: b"putpmsg",
183: "afs_syscall", 183: b"afs_syscall",
184: "tuxcall", 184: b"tuxcall",
185: "security", 185: b"security",
186: "gettid", 186: b"gettid",
187: "readahead", 187: b"readahead",
188: "setxattr", 188: b"setxattr",
189: "lsetxattr", 189: b"lsetxattr",
190: "fsetxattr", 190: b"fsetxattr",
191: "getxattr", 191: b"getxattr",
192: "lgetxattr", 192: b"lgetxattr",
193: "fgetxattr", 193: b"fgetxattr",
194: "listxattr", 194: b"listxattr",
195: "llistxattr", 195: b"llistxattr",
196: "flistxattr", 196: b"flistxattr",
197: "removexattr", 197: b"removexattr",
198: "lremovexattr", 198: b"lremovexattr",
199: "fremovexattr", 199: b"fremovexattr",
200: "tkill", 200: b"tkill",
201: "time", 201: b"time",
202: "futex", 202: b"futex",
203: "sched_setaffinity", 203: b"sched_setaffinity",
204: "sched_getaffinity", 204: b"sched_getaffinity",
205: "set_thread_area", 205: b"set_thread_area",
206: "io_setup", 206: b"io_setup",
207: "io_destroy", 207: b"io_destroy",
208: "io_getevents", 208: b"io_getevents",
209: "io_submit", 209: b"io_submit",
210: "io_cancel", 210: b"io_cancel",
211: "get_thread_area", 211: b"get_thread_area",
212: "lookup_dcookie", 212: b"lookup_dcookie",
213: "epoll_create", 213: b"epoll_create",
214: "epoll_ctl_old", 214: b"epoll_ctl_old",
215: "epoll_wait_old", 215: b"epoll_wait_old",
216: "remap_file_pages", 216: b"remap_file_pages",
217: "getdents64", 217: b"getdents64",
218: "set_tid_address", 218: b"set_tid_address",
219: "restart_syscall", 219: b"restart_syscall",
220: "semtimedop", 220: b"semtimedop",
221: "fadvise64", 221: b"fadvise64",
222: "timer_create", 222: b"timer_create",
223: "timer_settime", 223: b"timer_settime",
224: "timer_gettime", 224: b"timer_gettime",
225: "timer_getoverrun", 225: b"timer_getoverrun",
226: "timer_delete", 226: b"timer_delete",
227: "clock_settime", 227: b"clock_settime",
228: "clock_gettime", 228: b"clock_gettime",
229: "clock_getres", 229: b"clock_getres",
230: "clock_nanosleep", 230: b"clock_nanosleep",
231: "exit_group", 231: b"exit_group",
232: "epoll_wait", 232: b"epoll_wait",
233: "epoll_ctl", 233: b"epoll_ctl",
234: "tgkill", 234: b"tgkill",
235: "utimes", 235: b"utimes",
236: "vserver", 236: b"vserver",
237: "mbind", 237: b"mbind",
238: "set_mempolicy", 238: b"set_mempolicy",
239: "get_mempolicy", 239: b"get_mempolicy",
240: "mq_open", 240: b"mq_open",
241: "mq_unlink", 241: b"mq_unlink",
242: "mq_timedsend", 242: b"mq_timedsend",
243: "mq_timedreceive", 243: b"mq_timedreceive",
244: "mq_notify", 244: b"mq_notify",
245: "mq_getsetattr", 245: b"mq_getsetattr",
246: "kexec_load", 246: b"kexec_load",
247: "waitid", 247: b"waitid",
248: "add_key", 248: b"add_key",
249: "request_key", 249: b"request_key",
250: "keyctl", 250: b"keyctl",
251: "ioprio_set", 251: b"ioprio_set",
252: "ioprio_get", 252: b"ioprio_get",
253: "inotify_init", 253: b"inotify_init",
254: "inotify_add_watch", 254: b"inotify_add_watch",
255: "inotify_rm_watch", 255: b"inotify_rm_watch",
256: "migrate_pages", 256: b"migrate_pages",
257: "openat", 257: b"openat",
258: "mkdirat", 258: b"mkdirat",
259: "mknodat", 259: b"mknodat",
260: "fchownat", 260: b"fchownat",
261: "futimesat", 261: b"futimesat",
262: "newfstatat", 262: b"newfstatat",
263: "unlinkat", 263: b"unlinkat",
264: "renameat", 264: b"renameat",
265: "linkat", 265: b"linkat",
266: "symlinkat", 266: b"symlinkat",
267: "readlinkat", 267: b"readlinkat",
268: "fchmodat", 268: b"fchmodat",
269: "faccessat", 269: b"faccessat",
270: "pselect6", 270: b"pselect6",
271: "ppoll", 271: b"ppoll",
272: "unshare", 272: b"unshare",
273: "set_robust_list", 273: b"set_robust_list",
274: "get_robust_list", 274: b"get_robust_list",
275: "splice", 275: b"splice",
276: "tee", 276: b"tee",
277: "sync_file_range", 277: b"sync_file_range",
278: "vmsplice", 278: b"vmsplice",
279: "move_pages", 279: b"move_pages",
280: "utimensat", 280: b"utimensat",
281: "epoll_pwait", 281: b"epoll_pwait",
282: "signalfd", 282: b"signalfd",
283: "timerfd_create", 283: b"timerfd_create",
284: "eventfd", 284: b"eventfd",
285: "fallocate", 285: b"fallocate",
286: "timerfd_settime", 286: b"timerfd_settime",
287: "timerfd_gettime", 287: b"timerfd_gettime",
288: "accept4", 288: b"accept4",
289: "signalfd4", 289: b"signalfd4",
290: "eventfd2", 290: b"eventfd2",
291: "epoll_create1", 291: b"epoll_create1",
292: "dup3", 292: b"dup3",
293: "pipe2", 293: b"pipe2",
294: "inotify_init1", 294: b"inotify_init1",
295: "preadv", 295: b"preadv",
296: "pwritev", 296: b"pwritev",
297: "rt_tgsigqueueinfo", 297: b"rt_tgsigqueueinfo",
298: "perf_event_open", 298: b"perf_event_open",
299: "recvmmsg", 299: b"recvmmsg",
300: "fanotify_init", 300: b"fanotify_init",
301: "fanotify_mark", 301: b"fanotify_mark",
302: "prlimit64", 302: b"prlimit64",
303: "name_to_handle_at", 303: b"name_to_handle_at",
304: "open_by_handle_at", 304: b"open_by_handle_at",
305: "clock_adjtime", 305: b"clock_adjtime",
306: "syncfs", 306: b"syncfs",
307: "sendmmsg", 307: b"sendmmsg",
308: "setns", 308: b"setns",
309: "getcpu", 309: b"getcpu",
310: "process_vm_readv", 310: b"process_vm_readv",
311: "process_vm_writev", 311: b"process_vm_writev",
312: "kcmp", 312: b"kcmp",
313: "finit_module", 313: b"finit_module",
} }
# Try to use ausyscall if it is available, because it can give us an up-to-date # Try to use ausyscall if it is available, because it can give us an up-to-date
...@@ -362,7 +363,7 @@ try: ...@@ -362,7 +363,7 @@ try:
# Skip the first line, which is a header. The rest of the lines are simply # Skip the first line, which is a header. The rest of the lines are simply
# SYSCALL_NUM\tSYSCALL_NAME pairs. # SYSCALL_NUM\tSYSCALL_NAME pairs.
out = subprocess.check_output('ausyscall --dump | tail -n +2', shell=True) out = subprocess.check_output('ausyscall --dump | tail -n +2', shell=True)
syscalls = dict(map(parse_syscall, out.strip().split('\n'))) syscalls = dict(map(parse_syscall, out.strip().split(b'\n')))
except Exception as e: except Exception as e:
if platform.machine() == "x86_64": if platform.machine() == "x86_64":
pass pass
...@@ -507,15 +508,15 @@ time_colname = "TIME (ms)" if args.milliseconds else "TIME (us)" ...@@ -507,15 +508,15 @@ time_colname = "TIME (ms)" if args.milliseconds else "TIME (us)"
def comm_for_pid(pid): def comm_for_pid(pid):
try: try:
return open("/proc/%d/comm" % pid).read().strip() return open("/proc/%d/comm" % pid, "rb").read().strip()
except Exception: except Exception:
return "[unknown]" return b"[unknown]"
def agg_colval(key): def agg_colval(key):
if args.process: if args.process:
return "%-6d %-15s" % (key.value, comm_for_pid(key.value)) return b"%-6d %-15s" % (key.value, comm_for_pid(key.value))
else: else:
return syscalls.get(key.value, "[unknown: %d]" % key.value) return syscalls.get(key.value, b"[unknown: %d]" % key.value)
def print_count_stats(): def print_count_stats():
data = bpf["data"] data = bpf["data"]
...@@ -524,7 +525,7 @@ def print_count_stats(): ...@@ -524,7 +525,7 @@ def print_count_stats():
for k, v in sorted(data.items(), key=lambda kv: -kv[1].value)[:args.top]: for k, v in sorted(data.items(), key=lambda kv: -kv[1].value)[:args.top]:
if k.value == 0xFFFFFFFF: if k.value == 0xFFFFFFFF:
continue # happens occasionally, we don't need it continue # happens occasionally, we don't need it
print("%-22s %8d" % (agg_colval(k), v.value)) printb(b"%-22s %8d" % (agg_colval(k), v.value))
print("") print("")
data.clear() data.clear()
...@@ -536,7 +537,7 @@ def print_latency_stats(): ...@@ -536,7 +537,7 @@ def print_latency_stats():
key=lambda kv: -kv[1].total_ns)[:args.top]: key=lambda kv: -kv[1].total_ns)[:args.top]:
if k.value == 0xFFFFFFFF: if k.value == 0xFFFFFFFF:
continue # happens occasionally, we don't need it continue # happens occasionally, we don't need it
print(("%-22s %8d " + ("%16.6f" if args.milliseconds else "%16.3f")) % printb((b"%-22s %8d " + (b"%16.6f" if args.milliseconds else b"%16.3f")) %
(agg_colval(k), v.count, (agg_colval(k), v.count,
v.total_ns / (1e6 if args.milliseconds else 1e3))) v.total_ns / (1e6 if args.milliseconds else 1e3)))
print("") print("")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment