diff --git a/tools/memleak.py b/tools/memleak.py index 80cef0818687..e9856ea3853d 100755 --- a/tools/memleak.py +++ b/tools/memleak.py @@ -16,34 +16,8 @@ from datetime import datetime import argparse import subprocess -import ctypes import os -class Time(object): - # BPF timestamps come from the monotonic clock. To be able to filter - # and compare them from Python, we need to invoke clock_gettime. - # Adapted from http://stackoverflow.com/a/1205762 - CLOCK_MONOTONIC_RAW = 4 # see - - class timespec(ctypes.Structure): - _fields_ = [ - ('tv_sec', ctypes.c_long), - ('tv_nsec', ctypes.c_long) - ] - - librt = ctypes.CDLL('librt.so.1', use_errno=True) - clock_gettime = librt.clock_gettime - clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] - - @staticmethod - def monotonic_time(): - t = Time.timespec() - if Time.clock_gettime( - Time.CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0: - errno_ = ctypes.get_errno() - raise OSError(errno_, os.strerror(errno_)) - return t.tv_sec * 1e9 + t.tv_nsec - class KStackDecoder(object): def refresh(self): pass @@ -275,7 +249,7 @@ def print_outstanding(): allocs = bpf_program["allocs"] stack_traces = bpf_program["stack_traces"] for address, info in sorted(allocs.items(), key=lambda a: a[1].size): - if Time.monotonic_time() - min_age_ns < info.timestamp_ns: + if BPF.monotonic_time() - min_age_ns < info.timestamp_ns: continue if info.stack_id < 0: continue diff --git a/tools/old/memleak.py b/tools/old/memleak.py index 566281575e5f..bff7cfd25106 100755 --- a/tools/old/memleak.py +++ b/tools/old/memleak.py @@ -16,34 +16,8 @@ from datetime import datetime import argparse import subprocess -import ctypes import os -class Time(object): - # BPF timestamps come from the monotonic clock. To be able to filter - # and compare them from Python, we need to invoke clock_gettime. - # Adapted from http://stackoverflow.com/a/1205762 - CLOCK_MONOTONIC_RAW = 4 # see - - class timespec(ctypes.Structure): - _fields_ = [ - ('tv_sec', ctypes.c_long), - ('tv_nsec', ctypes.c_long) - ] - - librt = ctypes.CDLL('librt.so.1', use_errno=True) - clock_gettime = librt.clock_gettime - clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)] - - @staticmethod - def monotonic_time(): - t = Time.timespec() - if Time.clock_gettime( - Time.CLOCK_MONOTONIC_RAW, ctypes.pointer(t)) != 0: - errno_ = ctypes.get_errno() - raise OSError(errno_, os.strerror(errno_)) - return t.tv_sec * 1e9 + t.tv_nsec - class StackDecoder(object): def __init__(self, pid): self.pid = pid @@ -289,7 +263,7 @@ def print_outstanding(): (datetime.now().strftime("%H:%M:%S"), top_stacks)) allocs = bpf_program.get_table("allocs") for address, info in sorted(allocs.items(), key=lambda a: a[1].size): - if Time.monotonic_time() - min_age_ns < info.timestamp_ns: + if BPF.monotonic_time() - min_age_ns < info.timestamp_ns: continue stack = decoder.decode_stack(info, kernel_trace) if stack in stacks: diff --git a/tools/trace.py b/tools/trace.py index aa2c62aeeb87..3a957e9b304b 100755 --- a/tools/trace.py +++ b/tools/trace.py @@ -20,31 +20,6 @@ import traceback import sys -class Time(object): - # BPF timestamps come from the monotonic clock. To be able to filter - # and compare them from Python, we need to invoke clock_gettime. - # Adapted from http://stackoverflow.com/a/1205762 - CLOCK_MONOTONIC_RAW = 4 # see - - class timespec(ct.Structure): - _fields_ = [ - ('tv_sec', ct.c_long), - ('tv_nsec', ct.c_long) - ] - - librt = ct.CDLL('librt.so.1', use_errno=True) - clock_gettime = librt.clock_gettime - clock_gettime.argtypes = [ct.c_int, ct.POINTER(timespec)] - - @staticmethod - def monotonic_time(): - t = Time.timespec() - if Time.clock_gettime( - Time.CLOCK_MONOTONIC_RAW, ct.pointer(t)) != 0: - errno_ = ct.get_errno() - raise OSError(errno_, os.strerror(errno_)) - return t.tv_sec * 1e9 + t.tv_nsec - class Probe(object): probe_count = 0 streq_index = 0 @@ -60,7 +35,7 @@ def configure(cls, args): cls.max_events = args.max_events cls.print_time = args.timestamp or args.time cls.use_localtime = not args.timestamp - cls.first_ts = Time.monotonic_time() + cls.first_ts = BPF.monotonic_time() cls.tgid = args.tgid or -1 cls.pid = args.pid or -1