Build trace-cruncher as Python pakage
cover Build trace-cruncher as Python pakage
This patch-set is an attempt to restructure the project and to make it build as a native Python package. Although it looks like a complete rewrite, this is essentially just a switching from using Cython to using directly the C API of Python. Cython is still being used but only for the implementation of the NumPy data wrapper. The new package has its own stand-alone build system (very primitive for the moment) that is completely decoupled from the existing build system used by tracecruncher. In order to build and install the new package do:
sudo python setup.py install --record files.txt
The patch-set does not remove the old implementation yet. This will happen in another successive patch-set.
Please review as careful as possible!
Yordan Karadzhov (VMware) (5):
Refactor the part of the interface that relies on libkshark Refactor the part of the interface that relies on libtraceevent Refactor NumPy based data wrapper
Add "utils"
Adapt the sched_wakeup.py example script to use the new tracecruncher
module
examples/sched_wakeup.py | 30 ++--- setup.py | 61 +++++++++ src/common.h | 20 +++ src/datawrapper.pyx | 201 ++++++++++++++++++++++++++++ src/ftracepy.c | 234 +++++++++++++++++++++++++++++++++ src/ksharkpy.c | 268 ++++++++++++++++++++++++++++++++++++++ src/trace2matrix.c | 29 +++++ tracecruncher/__init__.py | 0 tracecruncher/utils.py | 54 ++++++++ 9 files changed, 882 insertions(+), 15 deletions(-) create mode 100644 setup.py create mode 100644 src/common.h create mode 100644 src/datawrapper.pyx create mode 100644 src/ftracepy.c create mode 100644 src/ksharkpy.c create mode 100644 src/trace2matrix.c create mode 100644 tracecruncher/__init__.py create mode 100644 tracecruncher/utils.py
Signature · 1 line
2.20.1
PATCH 1/5 Refactor the part of the interface that relies on libkshark
This is the first patch from a patch-set that aims to refactor trace-cruncher completely. The goal it to be able to build the project as a native Python package, which contains several sub-packages implemented as C extensions via the Python's C API. In this patch the part of the interface that relies on libkshark gets re-implemented as an extension called "tracecruncher.ksharkpy". Note that this new extension has a stand-alone build that is completely decoupled from the existing build system used by trace-cruncher.
--- setup.py | 44 +++++++ src/common.h | 19 +++ src/ksharkpy.c | 268 ++++++++++++++++++++++++++++++++++++++ tracecruncher/__init__.py | 0 4 files changed, 331 insertions(+) create mode 100644 setup.py create mode 100644 src/common.h create mode 100644 src/ksharkpy.c create mode 100644 tracecruncher/__init__.py
setup.py new file +44 -0
@@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +""" +SPDX-License-Identifier: LGPL-2.1 + +Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <[email protected]> +""" + + +from setuptools import setup, find_packages +from distutils.core import Extension +from Cython.Build import cythonize + +def main(): + kshark_path = '/usr/local/lib/kernelshark' + + module_ks = Extension('tracecruncher.ksharkpy', + sources=['src/ksharkpy.c'], + library_dirs=[kshark_path], + runtime_library_dirs=[kshark_path], + libraries=['kshark'], + define_macros=[ + ('LIB_KSHARK_PATH', '\"' + kshark_path + '/libkshark.so\"'), + ('KS_PLUGIN_DIR', '\"' + kshark_path + '/plugins\"') + ], + ) + + setup(name='tracecruncher', + version='0.1.0', + description='NumPy based interface for accessing tracing data in Python.', + author='Yordan Karadzhov (VMware)', + author_email='[email protected]', + url='https://github.com/vmware/trace-cruncher', + license='LGPL-2.1', + packages=find_packages(), + ext_modules=[module_ks], + classifiers=[ + 'Development Status :: 3 - Alpha', + 'Programming Language :: Python :: 3', + ] + ) + +if __name__ == '__main__': + main()
src/common.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: LGPL-2.1 */ + +/* +* Copyright (C) 2017 VMware Inc, Yordan Karadzhov <[email protected]> +*/ + +#ifndef _TC_COMMON_H +#define _TC_COMMON_H + +#define TRACECRUNCHER_ERROR tracecruncher_error +#define KSHARK_ERROR kshark_error + +#define KS_INIT_ERROR \ + PyErr_SetString(KSHARK_ERROR, "libshark failed to initialize"); + +#define KS_MEM_ERROR \ + PyErr_SetString(TRACECRUNCHER_ERROR, "failed to allocate memory"); + +#endif
src/ksharkpy.c new file +268 -0
268 lines not on this page · raw message
tracecruncher/__init__.py new file +0 -0
Signature · 1 line
2.20.1
PATCH 2/5 Refactor the part of the interface that relies on libtraceevent
In reply to Yordan Karadzhov (VMware)The part of the interface that relies on libtraceevent gets re-implemented as an extension called "tracecruncher.ftracepy". The new extension gets build together with the previously implemented "tracecruncher.ksharkpy" extension.
--- setup.py | 11 ++- src/common.h | 1 + src/ftracepy.c | 234 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 245 insertions(+), 1 deletion(-) create mode 100644 src/ftracepy.c
setup.py +10 -1
@@ -13,6 +13,8 @@ from Cython.Build import cythonize def main(): kshark_path = '/usr/local/lib/kernelshark' + traceevent_path = '/usr/local/lib/traceevent/' + tracecmd_path = '/usr/local/lib/trace-cmd/' module_ks = Extension('tracecruncher.ksharkpy', sources=['src/ksharkpy.c'],
@@ -25,6 +27,13 @@ def main(): ], ) + module_ft = Extension('tracecruncher.ftracepy', + sources=['src/ftracepy.c'], + library_dirs=[kshark_path, traceevent_path, tracecmd_path], + runtime_library_dirs=[kshark_path, traceevent_path, tracecmd_path], + libraries=['kshark', 'traceevent', 'tracecmd'], + ) + setup(name='tracecruncher', version='0.1.0', description='NumPy based interface for accessing tracing data in Python.',
@@ -33,7 +42,7 @@ def main(): url='https://github.com/vmware/trace-cruncher', license='LGPL-2.1', packages=find_packages(), - ext_modules=[module_ks], + ext_modules=[module_ks, module_ft], classifiers=[ 'Development Status :: 3 - Alpha', 'Programming Language :: Python :: 3',
src/common.h +1 -0
@@ -9,6 +9,7 @@#define TRACECRUNCHER_ERROR tracecruncher_error#define KSHARK_ERROR kshark_error +#define FTRACE_ERROR ftrace_error#define KS_INIT_ERROR \ PyErr_SetString(KSHARK_ERROR, "libshark failed to initialize");
src/ftracepy.c new file +234 -0
234 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH 3/5 Refactor NumPy based data wrapper
In reply to Yordan Karadzhov (VMware)The data wrapper is the only thing that remains being built with Cython. It is now a subpackage called "tracecruncher.datawrapper".
--- setup.py | 10 ++- src/datawrapper.pyx | 201 ++++++++++++++++++++++++++++++++++++++++++++ src/trace2matrix.c | 29 +++++++ 3 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 src/datawrapper.pyx create mode 100644 src/trace2matrix.c
setup.py +9 -1
@@ -16,6 +16,14 @@ def main(): traceevent_path = '/usr/local/lib/traceevent/' tracecmd_path = '/usr/local/lib/trace-cmd/' + cythonize('src/datawrapper.pyx') + module_data = Extension('tracecruncher.datawrapper', + sources=['src/datawrapper.c'], + library_dirs=[kshark_path, traceevent_path, tracecmd_path], + runtime_library_dirs=[kshark_path, traceevent_path, tracecmd_path], + libraries=['kshark', 'traceevent', 'tracecmd'] + ) + module_ks = Extension('tracecruncher.ksharkpy', sources=['src/ksharkpy.c'], library_dirs=[kshark_path],
@@ -42,7 +50,7 @@ def main(): url='https://github.com/vmware/trace-cruncher', license='LGPL-2.1', packages=find_packages(), - ext_modules=[module_ks, module_ft], + ext_modules=[module_data, module_ks, module_ft], classifiers=[ 'Development Status :: 3 - Alpha', 'Programming Language :: Python :: 3',
src/datawrapper.pyx new file +201 -0
201 lines not on this page · raw message
src/trace2matrix.c new file +29 -0
@@ -0,0 +1,29 @@ +// SPDX-License-Identifier: LGPL-2.1 + +/* +* Copyright 2019 VMware Inc, Yordan Karadzhov <[email protected]> +*/ + +// KernelShark +#include "kernelshark/libkshark.h" + +ssize_t trace2matrix (uint64_t **offset_array, +uint16_t **cpu_array, +uint64_t **ts_array, +uint16_t **pid_array, +int **event_array) +{ +struct kshark_context *kshark_ctx =NULL ; +ssize_t total =0 ; + +if (!kshark_instance (&kshark_ctx)) +return -1 ; + + total =kshark_load_data_matrix (kshark_ctx, offset_array, + cpu_array, + ts_array, + pid_array, + event_array); + +return total; +}
Signature · 1 line
2.20.1
PATCH 4/5 Add "utils"
In reply to Yordan Karadzhov (VMware)Place the rest of the code, that is pure Python in tracecrunche/utils.py
--- tracecruncher/utils.py | 54 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 tracecruncher/utils.py
tracecruncher/utils.py new file +54 -0
@@ -0,0 +1,54 @@ +""" +SPDX-License-Identifier: LGPL-2.1 + +Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <[email protected]> +""" + +import json + +from . import datawrapper as dw +from . import ksharkpy as ks + +def size(data): + """ Get the number of trace records. + """ + for key in dw.data_column_types: + if data[key] is not None: + return data[key].size + + raise Exception('Data size is unknown.') + +def save_session(session, s): + """ Save a KernelShark session description of a JSON file. + """ + s.seek(0) + json.dump(session, s, indent=4) + s.truncate() + + +def new_gui_session(fname, sname): + """ Generate and save a default KernelShark session description + file (JSON). + """ + ks.new_session_file(fname, sname) + + with open(sname, 'r+') as s: + session = json.load(s) + + session['Filters']['filter mask'] = 7 + session['CPUPlots'] = [] + session['TaskPlots'] = [] + session['Splitter'] = [1, 1] + session['MainWindow'] = [1200, 800] + session['ViewTop'] = 0 + session['ColorScheme'] = 0.75 + session['Model']['bins'] = 1000 + + session['Markers']['markA'] = {} + session['Markers']['markA']['isSet'] = False + session['Markers']['markB'] = {} + session['Markers']['markB']['isSet'] = False + session['Markers']['Active'] = 'A' + + save_session(session, s) +
Signature · 1 line
2.20.1
PATCH 5/5 Adapt the sched_wakeup.py example script to use the new tracecruncher module
In reply to Yordan Karadzhov (VMware)--- examples/sched_wakeup.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-)
examples/sched_wakeup.py +15 -15
@@ -15,18 +15,18 @@ import matplotlib.pyplot as plt import scipy.stats as st import numpy as np -from ksharksetup import setup -# Always call setup() before importing ksharkpy!!! -setup() +import tracecruncher.datawrapper as dw +import tracecruncher.ksharkpy as ks +import tracecruncher.ftracepy as ft +import tracecruncher.utils as tc -import ksharkpy as ks # Get the name of the user program. if len(sys.argv) >= 2: fname = str(sys.argv[1]) else: fname = input('choose a trace file: ') -status = ks.open_file(fname) +status = ks.open(fname) if not status: print ("Failed to open file ", fname) sys.exit()
@@ -35,7 +35,7 @@ ks.register_plugin('reg_pid') # We do not need the Process Ids of the records. # Do not load the "pid" data. -data = ks.load_data(pid_data=False) +data = dw.load(pid_data=False) tasks = ks.get_tasks() # Get the name of the user program.
@@ -48,8 +48,8 @@ else: task_pid = tasks[prog_name][0] # Get the Event Ids of the sched_switch and sched_waking events. -ss_eid = ks.event_id('sched', 'sched_switch') -w_eid = ks.event_id('sched', 'sched_waking') +ss_eid = ft.event_id('sched', 'sched_switch') +w_eid = ft.event_id('sched', 'sched_waking') # Gey the size of the data. i = data['offset'].size
@@ -60,7 +60,7 @@ delta_max = i_ss_max = i_sw_max = 0 while i > 0: i = i - 1 if data['event'][i] == ss_eid: - next_pid = ks.read_event_field(offset=data['offset'][i], + next_pid = ft.read_event_field(offset=data['offset'][i], event_id=ss_eid, field='next_pid')
@@ -77,9 +77,9 @@ while i > 0: break if data['event'][i] == ss_eid: - next_pid = ks.read_event_field(offset=data['offset'][i], - event_id=ss_eid, - field='next_pid') + next_pid = ft.read_event_field(offset=data['offset'][i], + event_id=ss_eid, + field='next_pid') if next_pid == task_pid: # Second sched_switch for the same task. ? time_ss = data['time'][i]
@@ -89,7 +89,7 @@ while i > 0: continue if (data['event'][i] == w_eid): - waking_pid = ks.read_event_field(offset=data['offset'][i], + waking_pid = ft.read_event_field(offset=data['offset'][i], event_id=w_eid, field='pid')
@@ -120,7 +120,7 @@ ax.hist(dt, bins=(100), histtype='step') plt.show() sname = 'sched.json' -ks.new_session(fname, sname) +tc.new_gui_session(fname, sname) with open(sname, 'r+') as s: session = json.load(s)
@@ -143,6 +143,6 @@ with open(sname, 'r+') as s: session['ViewTop'] = int(i_sw_max) - 5 - ks.save_session(session, s) + tc.save_session(session, s) ks.close()
Signature · 1 line
2.20.1
Hi Yordan,
Finally got around to trying trace-cruncher. I've rediscovered how much I hated C build systems issues in general, so here is the outcome of the first day of battle :-)
kernelshark bits:
* kernelshark build system seems to ignore the standard CMAKE_INSTALL_PREFIX but uses _INSTALL_PREFIX instead, might be worth aligning with the documented standard stuff of CMake if that makes sense.
* _INSTALL_PREFIX is used for almost everything, except a polkit file that is copied to /usr/share/polkit-1 regardless of _INSTALL_PREFIX, which makes installing as non root fail.
Now trace-cruncher-specific bits:
* Building kernelshark gives shared objects with .so.X.Y.Z file names, but GCC wants to link against .so
* There are some hardcoded paths to /usr/local/ in setup.py. They don't seem to break user-given include paths but it would probably be a good idea to make that somewhat parametric.
* libkshark-input.h does not seem to exist but is used by ksharkpy.c. I'm not sure if I used the wrong version of something or if that's a genuine issue.
Here is the command I used to try to build trace-cruncher in a venv with numpy and cython installed:
$ pip install . --global-option=build_ext --global-option='-Itrace-cmd/install/include' --global-option='-Ltrace-cmd/install/lib/kernelshark:trace-cmd/install/lib/trace-cmd:trace-cmd/install/lib/traceevent'
I've pushed some WIP commits on top of your series on my fork if you want to get a look at them:
Branch: refactor
remote: https://github.com/douglas-raillard-arm/trace-cruncher.git
https://github.com/douglas-raillard-arm/trace-cruncher/commits/refactor
For the sake of reproducibility until things become stable, it might be a good idea to maintain a WIP branch with submodules/subtrees for trace-cmd in the right version in trace-cruncher repo, and the series applied, so we don't have to jugle with the ML lore to get mbox plus other patches from the repo itself etc. Once we have that, we can easily include a script that calls all the build systems involved and install all the required bits in a venv, without polluting system-wide locations, and progressively turn that into a standalone setup.py (or the nearest approximation of that we manage to make).
PS: apologies for the extra new lines and lack of reply markers, Office 365 webmail does not seem to play very nicely with plain text ...
Cheers,
Douglas
From: Yordan Karadzhov (VMware) <[email protected]>
Sent: 12 December 2019 09:02
To: [email protected] <[email protected]>
Subject: [PATCH 0/5] Build trace-cruncher as Python pakage
This patch-set is an attempt to restructure the project and to make it
build as a native Python package. Although it looks like a complete
rewrite, this is essentially just a switching from using Cython to using
directly the C API of Python. Cython is still being used but only for
the implementation of the NumPy data wrapper. The new package has its
own stand-alone build system (very primitive for the moment) that is
completely decoupled from the existing build system used by tracecruncher.
In order to build and install the new package do:
sudo python setup.py install --record files.txt
The patch-set does not remove the old implementation yet. This will
happen in another successive patch-set.
Please review as careful as possible!
Yordan Karadzhov (VMware) (5):
Refactor the part of the interface that relies on libkshark
Refactor the part of the interface that relies on libtraceevent
Refactor NumPy based data wrapper
Add "utils"
Adapt the sched_wakeup.py example script to use the new tracecruncher
module
examples/sched_wakeup.py | 30 ++---
setup.py | 61 +++++++++
src/common.h | 20 +++
src/datawrapper.pyx | 201 ++++++++++++++++++++++++++++
src/ftracepy.c | 234 +++++++++++++++++++++++++++++++++
src/ksharkpy.c | 268 ++++++++++++++++++++++++++++++++++++++
src/trace2matrix.c | 29 +++++
tracecruncher/__init__.py | 0
tracecruncher/utils.py | 54 ++++++++
9 files changed, 882 insertions(+), 15 deletions(-)
create mode 100644 setup.py
create mode 100644 src/common.h
create mode 100644 src/datawrapper.pyx
create mode 100644 src/ftracepy.c
create mode 100644 src/ksharkpy.c
create mode 100644 src/trace2matrix.c
create mode 100644 tracecruncher/__init__.py
create mode 100644 tracecruncher/utils.py
--
2.20.1
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.
Hi Douglas,
Happy new year!
Thanks a lot for trying trace-cruncher and providing us with useful feedback!
Quoted 13 lines · Douglas Raillard
Hi Yordan,
Finally got around to trying trace-cruncher. I've rediscovered how much I hated C build systems issues in general, so here is the outcome of the first day of battle :-)
kernelshark bits:
* kernelshark build system seems to ignore the standardCMAKE_INSTALL_PREFIXbut uses_INSTALL_PREFIXinstead, might be worth aligning with the documented standard stuff of CMake if that makes sense.
*_INSTALL_PREFIXis used for almost everything, except a polkit file that is copied to/usr/share/polkit-1regardless of_INSTALL_PREFIX, which makes installing as non root fail.
Quoted 9 lines
Now trace-cruncher-specific bits:
* Building kernelshark gives shared objects with .so.X.Y.Z file names, but GCC wants to link against .so
* There are some hardcoded paths to/usr/local/insetup.py. They don't seem to break user-given include paths but it would probably be a good idea to make that somewhat parametric.
*libkshark-input.hdoes not seem to exist but is used byksharkpy.c. I'm not sure if I used the wrong version of something or if that's a genuine issue.
I am going to send a second version of the patch-set that tries to address some of the problems you found. This second version of the patches goes a bit further and simplifies/automates the build process for the user. Now you only have to do:
Quoted 2 lines
make
sudo make install
and that's all. You also have "make clean" and "make uninstall". I hope this will fix the problems you reported above.
Quoted 11 lines
Here is the command I used to try to build trace-cruncher in a venv with numpy and cython installed:
$ pip install . --global-option=build_ext --global-option='-Itrace-cmd/install/include' --global-option='-Ltrace-cmd/install/lib/kernelshark:trace-cmd/install/lib/trace-cmd:trace-cmd/install/lib/traceevent'I've pushed some WIP commits on top of your series on my fork if you want to get a look at them:
Branch: refactor
remote: https://github.com/douglas-raillard-arm/trace-cruncher.git
https://github.com/douglas-raillard-arm/trace-cruncher/commits/refactor
All new patches are available here:
https://github.com/vmware/trace-cruncher/tree/refactoring_WIP (branch "refactoring_WIP")
Quoted 8 lines
For the sake of reproducibility until things become stable, it might be a good idea to maintain a WIP branch with submodules/subtrees for trace-cmd in the right version in trace-cruncher repo, and the series applied, so we don't have to jugle with the ML lore to get mbox plus other patches from the repo itself etc. Once we have that, we can easily include a script that calls all the build systems involved and install all the required bits in a venv, without polluting system-wide locations, and progressively turn that into a standalone
setup.py(or the nearest approximation of that we manage to make).
In v2 I am trying to address this, partially by automating the patching and build of trace-cmd and partially by making all those patched libraries part of the package itself (exploiting some linker magic). However, your solution to use venv is probably by fare more common. It will be great if we can have some discussion here and decide which of the two solutions is the best.
Once again, thanks for helping us!
cheers,
Yordan
Quoted 114 lines
PS: apologies for the extra new lines and lack of reply markers, Office 365 webmail does not seem to play very nicely with plain text ...
Cheers,
DouglasFrom: Yordan Karadzhov (VMware) <[email protected]>
Sent: 12 December 2019 09:02
To: [email protected] <[email protected]>
Cc [email protected] <[email protected]>; Valentin Schneider <[email protected]>; Douglas Raillard <[email protected]>; Yordan Karadzhov (VMware) <[email protected]>Subject: [PATCH 0/5] Build trace-cruncher as Python pakage
This patch-set is an attempt to restructure the project and to make it
build as a native Python package. Although it looks like a complete
rewrite, this is essentially just a switching from using Cython to using
directly the C API of Python. Cython is still being used but only for
the implementation of the NumPy data wrapper. The new package has its
own stand-alone build system (very primitive for the moment) that is
completely decoupled from the existing build system used by tracecruncher.
In order to build and install the new package do:
sudo python
setup.pyinstall --record files.txtThe patch-set does not remove the old implementation yet. This will
happen in another successive patch-set.
Please review as careful as possible!
Yordan Karadzhov (VMware) (5):
Refactor the part of the interface that relies on libkshark
Refactor the part of the interface that relies on libtraceevent
Refactor NumPy based data wrapper
Add "utils"
Adapt the
sched_wakeup.pyexample script to use the new tracecrunchermodule
examples/sched_wakeup.py | 30 ++---setup.py | 61 +++++++++src/common.h | 20 +++src/datawrapper.pyx | 201 ++++++++++++++++++++++++++++src/ftracepy.c | 234 +++++++++++++++++++++++++++++++++src/ksharkpy.c | 268 ++++++++++++++++++++++++++++++++++++++src/trace2matrix.c | 29 +++++tracecruncher/__init__.py | 0tracecruncher/utils.py | 54 ++++++++9 files changed, 882 insertions(+), 15 deletions(-)create mode 100644
setup.pycreate mode 100644
src/common.hcreate mode 100644 src/datawrapper.pyx
create mode 100644
src/ftracepy.ccreate mode 100644
src/ksharkpy.ccreate mode 100644
src/trace2matrix.ccreate mode 100644
tracecruncher/__init__.pycreate mode 100644
tracecruncher/utils.py--
2.20.1
IMPORTANT NOTICE: The contents of this email and any attachments are confidential and may also be privileged. If you are not the intended recipient, please notify the sender immediately and do not disclose the contents to any other person, use it for any purpose, or store or copy the information in any medium. Thank you.