Skip to content

Commit

Permalink
Update tools to use new BPF.monotonic_time method
Browse files Browse the repository at this point in the history
  • Loading branch information
goldshtn committed Feb 9, 2017
1 parent d5e24d9 commit 60c4192
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 80 deletions.
28 changes: 1 addition & 27 deletions tools/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://stackoverflow.com/a/1205762
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>

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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 1 addition & 27 deletions tools/old/memleak.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://stackoverflow.com/a/1205762
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>

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
Expand Down Expand Up @@ -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:
Expand Down
27 changes: 1 addition & 26 deletions tools/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:https://stackoverflow.com/a/1205762
CLOCK_MONOTONIC_RAW = 4 # see <linux/time.h>

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
Expand All @@ -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

Expand Down

0 comments on commit 60c4192

Please sign in to comment.