/usr/share/pyshared/cobe/instatrace.py is in python-cobe 2.1.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | # Copyright (C) 2010 Peter Teichman
import datetime
import os
import time
from contextlib import contextmanager
_instatrace = None
def init_trace(filename):
global _instatrace
if _instatrace is not None:
_instatrace.close()
_instatrace = Instatrace(filename)
class Instatrace:
def __init__(self, filename):
# rotate logs if present
if os.path.exists(filename):
now = datetime.datetime.now()
stamp = now.strftime("%Y-%m-%d.%H%M%S")
os.rename(filename, "%s.%s" % (filename, stamp))
self._fd = open(filename, "w")
def now(self):
"""Microsecond resolution, integer now"""
return int(time.time() * 100000)
def now_ms(self):
"""Millisecond resolution, integer now"""
return int(time.time() * 1000)
def trace(self, stat, value, data=None):
extra = ""
if data is not None:
extra = " " + repr(data)
self._fd.write("%s %d%s\n" % (stat, value, extra))
def trace(stat, value, user_data=None):
if _instatrace is not None:
_instatrace.trace(stat, value, user_data)
@contextmanager
def trace_us(statName):
if _instatrace is None:
yield
return
# Microsecond resolution, integer now
now = _instatrace.now()
yield
_instatrace.trace(statName, _instatrace.now() - now)
@contextmanager
def trace_ms(statName):
if _instatrace is None:
yield
return
# Millisecond resolution, integer now
now = _instatrace.now_ms()
yield
_instatrace.trace(statName, _instatrace.now_ms() - now)
|