From 9d31d7f18fed4d81dcbfa5fecb0c64fb0ba93bfe Mon Sep 17 00:00:00 2001 From: Ameya Vikram Singh Date: Wed, 1 Nov 2017 23:29:38 +0530 Subject: [PATCH] Added initial Python Call tracer Addedd a simple python function tracer. To-Do: - Improve the performance for tracing application. - Create a generic script for plugging in any Python module. --- PythonTracer/README.md | 8 +++++ PythonTracer/testcov/__init__.py | 0 PythonTracer/testcov/testfact.py | 16 +++++++++ PythonTracer/tracer.py | 56 ++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 PythonTracer/README.md create mode 100644 PythonTracer/testcov/__init__.py create mode 100644 PythonTracer/testcov/testfact.py create mode 100644 PythonTracer/tracer.py diff --git a/PythonTracer/README.md b/PythonTracer/README.md new file mode 100644 index 0000000..14331a4 --- /dev/null +++ b/PythonTracer/README.md @@ -0,0 +1,8 @@ +# PythonTracer + +This directory consist of _prototype_ Python Call tracer. + +**Note:** +1. This is a poor mans implementation not ready for everyday use!!! +2. This implementation significantly slows down the execution speed. + diff --git a/PythonTracer/testcov/__init__.py b/PythonTracer/testcov/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/PythonTracer/testcov/testfact.py b/PythonTracer/testcov/testfact.py new file mode 100644 index 0000000..239b01b --- /dev/null +++ b/PythonTracer/testcov/testfact.py @@ -0,0 +1,16 @@ +#!/usr/bin/python + +from __future__ import print_function + +def fact(n): + if n == 0: + return 1 + else: + return n*fact(n - 1) + +def main(): + i = 20 + print("fact(%d): %d" %(i, fact(i))) + +if __name__ == '__main__': + main() diff --git a/PythonTracer/tracer.py b/PythonTracer/tracer.py new file mode 100644 index 0000000..72fccbc --- /dev/null +++ b/PythonTracer/tracer.py @@ -0,0 +1,56 @@ +#!/usr/bin/python + +# This file is looking into the python tracing support. + +#from __future__ import print_function + +import sys +import threading +import logging +from testcov import testfact + +logger = None +# Enable Logger +logging.basicConfig(level = logging.DEBUG) +logger = logging.getLogger(__name__) +logger.propagate = False + +# Create file handler +handler = logging.FileHandler(str(__file__) + '_tracing.log') +handler.setLevel(logging.DEBUG) + +# Create a logging format +formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') +handler.setFormatter(formatter) + +# Add the handlers to the logger +logger.addHandler(handler) + +def profilefunc(frame, event, arg): + code = frame.f_code + #print("%s:%d:%s:%s" %(code.co_filename, frame.f_lineno, code.co_name, event)) + # Logger doesn't work with multithreaded Python 2.x environment. + # It seems to be an issue with python internals. + # Logger seems to work under Python 3.6.1 + if 'threading.py' not in code.co_filename.lower(): + logger.debug("%s:%d:%s:%s" %(code.co_filename, frame.f_lineno, code.co_name, event)) + +def fibo(n): + if n == 0: + return 0 + elif n == 1: + return 1 + else: + return fibo(n - 2) + fibo(n - 1) + +def main(): + for i in range(0, 20): + print("fibo(%d): %d, fact(%d): %d" %(i, fibo(i), i, testfact.fact(i))) + +if __name__ == '__main__': + #threading.setprofile(profilefunc) + sys.setprofile(profilefunc) + logger.info('Start Logging') + main() + logger.info('Closing Logging') +