All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 00/12]  Build trace-cruncher as Python pakage
@ 2020-01-07 17:03 Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 01/12] trace-cruncher: Refactor the part of the interface that relies on libkshark Yordan Karadzhov (VMware)
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

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 building procedure
is very simple and intuitive. The appropriate versions of trace-cmd and
KernelShark libraries are automatically download, patch and build. These
third-party libraries are installed as part of trace-cruncher itself and
do not interfere with any existing system-wide installations of trace-cmd
and KernelShark.

All patches are available at:
https://github.com/vmware/trace-cruncher/tree/refactoring_WIP (branch
"refactoring_WIP")

v2 changes:
  - Trying to address the building issues reported by Douglas Raillard in 
    particular the missing header file and the problem of linking with
    libkshark.so.X.Y.Z
  - Douglas suggested to use venv in order to avoid the patched versions
    of the third-party libraries to pollut the system-wide locations.
    Here I am suggesting a different solution that allows to make those
    third-party libraries part of the package itself. Not sure which of
    the two solution is the best. It will be great if we can have a
    discussion.
  - All obsoleted source files are removed and the README file is updated
    to describe the new building procedure.



Yordan Karadzhov (VMware) (12):
  trace-cruncher: Refactor the part of the interface that relies on
    libkshark
  trace-cruncher: Refactor the part of the interface that relies on
    libtraceevent
  trace-cruncher: Refactor NumPy based data wrapper
  trace-cruncher: Add "utils"
  trace-cruncher: Adapt sched_wakeup.py to use the new module
  trace-cruncher: Add Makefile
  trace-cruncher: Adapt gpareto_fit.py to use the new module
  trace-cruncher: Adapt page_faults.py to use the new module
  trace-cruncher: Automate the third-party build
  Update README.md
  trace-cruncher: Remove all leftover files.
  trace-cruncher: Improve Makefile Provide more robust and better
    looking build process.

 ...-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch | 160 ++++++++
 0001-kernel-shark-Add-_DEVEL-build-flag.patch |  90 -----
 0002-kernel-shark-Add-reg_pid-plugin.patch    |   8 +-
 Makefile                                      |  36 ++
 README.md                                     |  50 +--
 clean.sh                                      |   6 -
 examples/gpareto_fit.py                       |  35 +-
 examples/ksharksetup.py                       |  24 --
 examples/page_faults.py                       |  64 ++--
 examples/sched_wakeup.py                      |  30 +-
 install_third_party.sh                        |  39 ++
 libkshark-py.c                                | 224 -----------
 libkshark_wrapper.pyx                         | 361 ------------------
 np_setup.py                                   |  90 -----
 setup.py                                      |  68 ++++
 src/common.h                                  |  20 +
 src/datawrapper.pyx                           | 201 ++++++++++
 src/ftracepy.c                                | 233 +++++++++++
 src/ksharkpy.c                                | 269 +++++++++++++
 src/trace2matrix.c                            |  29 ++
 tracecruncher/__init__.py                     |   4 +
 tracecruncher/utils.py                        |  54 +++
 22 files changed, 1188 insertions(+), 907 deletions(-)
 create mode 100644 0001-kernel-shark-Add-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch
 delete mode 100644 0001-kernel-shark-Add-_DEVEL-build-flag.patch
 create mode 100644 Makefile
 delete mode 100755 clean.sh
 delete mode 100644 examples/ksharksetup.py
 create mode 100755 install_third_party.sh
 delete mode 100644 libkshark-py.c
 delete mode 100644 libkshark_wrapper.pyx
 delete mode 100755 np_setup.py
 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


^ permalink raw reply	[flat|nested] 13+ messages in thread

* [PATCH v2 01/12] trace-cruncher: Refactor the part of the interface that relies on libkshark
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 02/12] trace-cruncher: Refactor the part of the interface that relies on libtraceevent Yordan Karadzhov (VMware)
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

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.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 setup.py                  |  43 ++++++
 src/common.h              |  19 +++
 src/ksharkpy.c            | 267 ++++++++++++++++++++++++++++++++++++++
 tracecruncher/__init__.py |   0
 4 files changed, 329 insertions(+)
 create mode 100644 setup.py
 create mode 100644 src/common.h
 create mode 100644 src/ksharkpy.c
 create mode 100644 tracecruncher/__init__.py

diff --git a/setup.py b/setup.py
new file mode 100644
index 0000000..1a89dc6
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python3
+
+"""
+SPDX-License-Identifier: LGPL-2.1
+
+Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+"""
+
+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='y.karadz@gmail.com',
+          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()
diff --git a/src/common.h b/src/common.h
new file mode 100644
index 0000000..d7d355a
--- /dev/null
+++ b/src/common.h
@@ -0,0 +1,19 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+
+/*
+ * Copyright (C) 2017 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
+ */
+
+#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
diff --git a/src/ksharkpy.c b/src/ksharkpy.c
new file mode 100644
index 0000000..5a15a77
--- /dev/null
+++ b/src/ksharkpy.c
@@ -0,0 +1,267 @@
+// SPDX-License-Identifier: LGPL-2.1
+
+/*
+ * Copyright (C) 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+ */
+
+/** Use GNU C Library. */
+#define _GNU_SOURCE 1
+
+// C
+#include <stdio.h>
+#include <dlfcn.h>
+
+// Python
+#include <Python.h>
+
+// KernelShark
+#include "kernelshark/libkshark.h"
+#include "kernelshark/libkshark-plugin.h"
+#include "kernelshark/libkshark-model.h"
+
+// trace-cruncher
+#include "common.h"
+
+static PyObject *KSHARK_ERROR = NULL;
+static PyObject *TRACECRUNCHER_ERROR = NULL;
+
+static PyObject *method_open(PyObject *self, PyObject *args,
+					     PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	char *fname = NULL;
+
+	static char *kwlist[] = {"fname", NULL};
+	if(!PyArg_ParseTupleAndKeywords(args,
+					kwargs,
+					"s",
+					kwlist,
+					&fname)) {
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	if (!kshark_open(kshark_ctx, fname))
+		return Py_False;
+
+	return Py_True;
+}
+
+static PyObject* method_close(PyObject* self, PyObject* noarg)
+{
+	struct kshark_context *kshark_ctx = NULL;
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	kshark_close(kshark_ctx);
+
+	Py_RETURN_NONE;
+}
+
+static int compare(const void *a, const void *b)
+{
+	int a_i, b_i;
+
+	a_i = *(const int *) a;
+	b_i = *(const int *) b;
+
+	if (a_i > b_i)
+		return +1;
+
+	if (a_i < b_i)
+		return -1;
+
+	return 0;
+}
+
+static PyObject* method_get_tasks(PyObject* self, PyObject* noarg)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	const char *comm;
+	int *pids;
+	ssize_t i, n;
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	n = kshark_get_task_pids(kshark_ctx, &pids);
+	if (n == 0) {
+		PyErr_SetString(KSHARK_ERROR,
+				"Failed to retrieve the PID-s of the tasks");
+		return NULL;
+	}
+
+	qsort(pids, n, sizeof(*pids), compare);
+
+	PyObject *tasks, *pid_list, *pid_val;
+
+	tasks = PyDict_New();
+	for (i = 0; i < n; ++i) {
+		comm = tep_data_comm_from_pid(kshark_ctx->pevent, pids[i]);
+		pid_val = PyLong_FromLong(pids[i]);
+		pid_list = PyDict_GetItemString(tasks, comm);
+		if (!pid_list) {
+			pid_list = PyList_New(1);
+			PyList_SET_ITEM(pid_list, 0, pid_val);
+			PyDict_SetItemString(tasks, comm, pid_list);
+		} else {
+			PyList_Append(pid_list, pid_val);
+		}
+	}
+
+	return tasks;
+}
+
+static PyObject *method_register_plugin(PyObject *self, PyObject *args,
+							PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	char *plugin, *lib_file;
+	int ret;
+
+	static char *kwlist[] = {"plugin", NULL};
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "s",
+					 kwlist,
+					 &plugin)) {
+		return NULL;
+	}
+
+	if (asprintf(&lib_file, "%s/plugin-%s.so", KS_PLUGIN_DIR, plugin) < 0) {
+		KS_MEM_ERROR
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	ret = kshark_register_plugin(kshark_ctx, lib_file);
+	free(lib_file);
+	if (ret < 0) {
+		PyErr_Format(KSHARK_ERROR,
+			     "libshark failed to load plugin '%s'",
+			     plugin);
+		return NULL;
+	}
+
+	if (kshark_handle_plugins(kshark_ctx, KSHARK_PLUGIN_INIT) < 0) {
+		PyErr_SetString(KSHARK_ERROR,
+				"libshark failed to handle its plugins");
+		return NULL;
+	}
+
+	Py_RETURN_NONE;
+}
+
+static PyObject *method_new_session_file(PyObject *self, PyObject *args,
+							 PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	struct kshark_config_doc *session;
+	struct kshark_config_doc *filters;
+	struct kshark_config_doc *markers;
+	struct kshark_config_doc *model;
+	struct kshark_config_doc *file;
+	struct kshark_trace_histo histo;
+	const char *session_file, *data_file;
+
+	static char *kwlist[] = {"data_file", "session_file", NULL};
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "ss",
+					 kwlist,
+					 &data_file,
+					 &session_file)) {
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	session = kshark_config_new("kshark.config.session",
+				    KS_CONFIG_JSON);
+
+	file = kshark_export_trace_file(data_file, KS_CONFIG_JSON);
+	kshark_config_doc_add(session, "Data", file);
+
+	filters = kshark_export_all_filters(kshark_ctx, KS_CONFIG_JSON);
+	kshark_config_doc_add(session, "Filters", filters);
+
+	ksmodel_init(&histo);
+	model = kshark_export_model(&histo, KS_CONFIG_JSON);
+	kshark_config_doc_add(session, "Model", model);
+
+	markers = kshark_config_new("kshark.config.markers", KS_CONFIG_JSON);
+	kshark_config_doc_add(session, "Markers", markers);
+
+	kshark_save_config_file(session_file, session);
+	kshark_free_config_doc(session);
+
+	Py_RETURN_NONE;
+}
+
+static PyMethodDef ksharkpy_methods[] = {
+	{"open",
+	 (PyCFunction) method_open,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Open trace data file"
+	},
+	{"close",
+	 (PyCFunction) method_close,
+	 METH_NOARGS,
+	 "Close trace data file"
+	},
+	{"get_tasks",
+	 (PyCFunction) method_get_tasks,
+	 METH_NOARGS,
+	 "Get all tasks recorded in a trace file"
+	},
+	{"register_plugin",
+	 (PyCFunction) method_register_plugin,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Load a plugin"
+	},
+	{"new_session_file",
+	 (PyCFunction) method_new_session_file,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Create new session description file"
+	},
+	{NULL, NULL, 0, NULL}
+};
+
+static struct PyModuleDef ksharkpy_module = {
+	PyModuleDef_HEAD_INIT,
+	"ksharkpy",
+	"",
+	-1,
+	ksharkpy_methods
+};
+
+PyMODINIT_FUNC PyInit_ksharkpy(void)
+{
+	PyObject *module = PyModule_Create(&ksharkpy_module);
+
+	KSHARK_ERROR = PyErr_NewException("tracecruncher.ksharkpy.ks_error",
+					  NULL, NULL);
+	PyModule_AddObject(module, "ks_error", KSHARK_ERROR);
+
+	TRACECRUNCHER_ERROR = PyErr_NewException("tracecruncher.tc_error",
+						 NULL, NULL);
+	PyModule_AddObject(module, "tc_error", TRACECRUNCHER_ERROR);
+
+	return module;
+}
diff --git a/tracecruncher/__init__.py b/tracecruncher/__init__.py
new file mode 100644
index 0000000..e69de29
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 02/12] trace-cruncher: Refactor the part of the interface that relies on libtraceevent
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 01/12] trace-cruncher: Refactor the part of the interface that relies on libkshark Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 03/12] trace-cruncher: Refactor NumPy based data wrapper Yordan Karadzhov (VMware)
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	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.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 setup.py       |  11 ++-
 src/common.h   |   1 +
 src/ftracepy.c | 233 +++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 244 insertions(+), 1 deletion(-)
 create mode 100644 src/ftracepy.c

diff --git a/setup.py b/setup.py
index 1a89dc6..62912e2 100644
--- a/setup.py
+++ b/setup.py
@@ -12,6 +12,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'],
@@ -24,6 +26,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.',
@@ -32,7 +41,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',
diff --git a/src/common.h b/src/common.h
index d7d355a..632e17a 100644
--- a/src/common.h
+++ b/src/common.h
@@ -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");
diff --git a/src/ftracepy.c b/src/ftracepy.c
new file mode 100644
index 0000000..fd0832b
--- /dev/null
+++ b/src/ftracepy.c
@@ -0,0 +1,233 @@
+// SPDX-License-Identifier: LGPL-2.1
+
+/*
+ * Copyright (C) 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+ */
+
+// Python
+#include <Python.h>
+
+// trace-cmd
+#include "trace-cmd/trace-cmd.h"
+
+// KernelShark
+#include "kernelshark/libkshark.h"
+
+// trace-cruncher
+#include "common.h"
+
+static PyObject *KSHARK_ERROR = NULL;
+static PyObject *FTRACE_ERROR = NULL;
+
+static PyObject *method_event_id(PyObject *self, PyObject *args,
+						 PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	struct tep_event *event;
+	const char *system, *name;
+
+	static char *kwlist[] = {"system", "event", NULL};
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "ss",
+					 kwlist,
+					 &system,
+					 &name)) {
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	event = tep_find_event_by_name(kshark_ctx->pevent, system, name);
+	if (!event) {
+		PyErr_Format(FTRACE_ERROR,
+			     "Failed to find event '%s/%s'",
+			     system, name);
+	}
+
+	return PyLong_FromLong(event->id);
+}
+
+static PyObject *method_read_event_field(PyObject *self, PyObject *args,
+							 PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	struct tep_format_field *evt_field;
+	struct tep_record *record;
+	struct tep_event *event;
+	unsigned long long val;
+	const char *field;
+	uint64_t offset;
+	int event_id, ret;
+
+	static char *kwlist[] = {"offset", "event_id", "field", NULL};
+	if(!PyArg_ParseTupleAndKeywords(args,
+					kwargs,
+					"Lis",
+					kwlist,
+					&offset,
+					&event_id,
+					&field)) {
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	event = tep_find_event(kshark_ctx->pevent, event_id);
+	if (!event) {
+		PyErr_Format(FTRACE_ERROR,
+			     "Failed to find event '%i'",
+			     event_id);
+		return NULL;
+	}
+
+	evt_field = tep_find_any_field(event, field);
+	if (!evt_field) {
+		PyErr_Format(FTRACE_ERROR,
+			     "Failed to find field '%s' of event '%i'",
+			     field, event_id);
+		return NULL;
+	}
+
+	record = tracecmd_read_at(kshark_ctx->handle, offset, NULL);
+	if (!record) {
+		PyErr_Format(FTRACE_ERROR,
+			     "Failed to read record at offset '%i'",
+			     offset);
+		return NULL;
+	}
+
+	ret = tep_read_number_field(evt_field, record->data, &val);
+	free_record(record);
+
+	if (ret != 0) {
+		PyErr_Format(FTRACE_ERROR,
+			     "Failed to read field '%s' of event '%i'",
+			     field, event_id);
+		return NULL;
+	}
+
+	return PyLong_FromLong(val);
+}
+
+static PyObject *method_get_function(PyObject *self, PyObject *args,
+						     PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	unsigned long long address;
+	const char *func;
+
+	static char *kwlist[] = {"address", NULL};
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "L",
+					 kwlist,
+					 &address)) {
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	func = tep_find_function(kshark_ctx->pevent, address);
+	if (!func)
+		Py_RETURN_NONE;
+
+	return PyUnicode_FromString(func);
+}
+
+static PyObject *method_map_instruction_address(PyObject *self, PyObject *args,
+								PyObject *kwargs)
+{
+	struct kshark_context *kshark_ctx = NULL;
+	struct tracecmd_proc_addr_map *mem_map;
+	unsigned long long proc_addr, obj_addr;
+	int pid;
+	PyObject *ret;
+
+	static char *kwlist[] = {"pid", "proc_addr", NULL};
+	if (!PyArg_ParseTupleAndKeywords(args,
+					 kwargs,
+					 "iL",
+					 kwlist,
+					 &pid,
+					 &proc_addr)) {
+		return NULL;
+	}
+
+	if (!kshark_instance(&kshark_ctx)) {
+		KS_INIT_ERROR
+		return NULL;
+	}
+
+	mem_map = tracecmd_search_task_map(kshark_ctx->handle,
+					   pid, proc_addr);
+
+	if (!mem_map)
+		Py_RETURN_NONE;
+
+	ret = PyDict_New();
+
+	PyDict_SetItemString(ret, "obj_file",
+			     PyUnicode_FromString(mem_map->lib_name));
+
+	obj_addr = proc_addr - mem_map->start;
+	PyDict_SetItemString(ret, "address", PyLong_FromLong(obj_addr));
+
+	return ret;
+}
+
+static PyMethodDef ftracepy_methods[] = {
+	{"event_id",
+	 (PyCFunction) method_event_id,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Get the Id of the event from its name"
+	},
+	{"read_event_field",
+	 (PyCFunction) method_read_event_field,
+	 METH_VARARGS | METH_KEYWORDS,
+	 "Get the value of an event field having a given name"
+	},
+	{"get_function",
+	 (PyCFunction) method_get_function,
+	 METH_VARARGS | METH_KEYWORDS,
+	 ""
+	},
+	{"map_instruction_address",
+	 (PyCFunction) method_map_instruction_address,
+	 METH_VARARGS | METH_KEYWORDS,
+	 ""
+	},
+	{NULL, NULL, 0, NULL}
+};
+
+static struct PyModuleDef ftracepy_module = {
+	PyModuleDef_HEAD_INIT,
+	"ftracepy",
+	"",
+	-1,
+	ftracepy_methods
+};
+
+PyMODINIT_FUNC PyInit_ftracepy(void)
+{
+	PyObject *module =  PyModule_Create(&ftracepy_module);
+
+	KSHARK_ERROR = PyErr_NewException("tracecruncher.ftracepy.ks_error",
+					  NULL, NULL);
+	PyModule_AddObject(module, "ks_error", KSHARK_ERROR);
+
+	FTRACE_ERROR = PyErr_NewException("tracecruncher.ftracepy.ft_error",
+					  NULL, NULL);
+	PyModule_AddObject(module, "ft_error", FTRACE_ERROR);
+
+	return module;
+}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 03/12] trace-cruncher: Refactor NumPy based data wrapper
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 01/12] trace-cruncher: Refactor the part of the interface that relies on libkshark Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 02/12] trace-cruncher: Refactor the part of the interface that relies on libtraceevent Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 04/12] trace-cruncher: Add "utils" Yordan Karadzhov (VMware)
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

The data wrapper is the only thing that remains being built with Cython.
It is now a subpackage called "tracecruncher.datawrapper".

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 setup.py            |  12 ++-
 src/datawrapper.pyx | 201 ++++++++++++++++++++++++++++++++++++++++++++
 src/trace2matrix.c  |  29 +++++++
 3 files changed, 241 insertions(+), 1 deletion(-)
 create mode 100644 src/datawrapper.pyx
 create mode 100644 src/trace2matrix.c

diff --git a/setup.py b/setup.py
index 62912e2..526e1e7 100644
--- a/setup.py
+++ b/setup.py
@@ -9,12 +9,22 @@ Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
 from setuptools import setup, find_packages
 from distutils.core import Extension
 from Cython.Build import cythonize
+import numpy as np
 
 def main():
     kshark_path = '/usr/local/lib/kernelshark'
     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'],
+                            include_dirs=[np.get_include()],
+                            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],
@@ -41,7 +51,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',
diff --git a/src/datawrapper.pyx b/src/datawrapper.pyx
new file mode 100644
index 0000000..070d4e4
--- /dev/null
+++ b/src/datawrapper.pyx
@@ -0,0 +1,201 @@
+"""
+SPDX-License-Identifier: LGPL-2.1
+
+Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+"""
+
+import ctypes
+
+# Import the Python-level symbols of numpy
+import numpy as np
+# Import the C-level symbols of numpy
+cimport numpy as np
+
+import json
+
+from libcpp cimport bool
+
+from libc.stdlib cimport free
+
+from cpython cimport PyObject, Py_INCREF
+
+from libc cimport stdint
+ctypedef stdint.int16_t int16_t
+ctypedef stdint.uint16_t uint16_t
+ctypedef stdint.int32_t int32_t
+ctypedef stdint.uint32_t uint32_t
+ctypedef stdint.int64_t int64_t
+ctypedef stdint.uint64_t uint64_t
+
+cdef extern from 'numpy/ndarraytypes.h':
+    int NPY_ARRAY_CARRAY
+    
+# Numpy must be initialized!!!
+np.import_array()
+
+cdef extern from 'trace2matrix.c':
+    ssize_t trace2matrix(uint64_t **offset_array,
+			 uint16_t **cpu_array,
+			 uint64_t **ts_array,
+			 uint16_t **pid_array,
+			 int **event_array)
+
+data_column_types = {
+    'cpu': np.NPY_UINT16,
+    'pid': np.NPY_UINT16,
+    'event': np.NPY_INT,
+    'offset': np.NPY_UINT64,
+    'time': np.NPY_UINT64
+    }
+
+cdef class KsDataWrapper:
+    cdef int item_size
+    cdef int data_size
+    cdef int data_type
+    cdef void* data_ptr
+
+    cdef init(self, int data_type,
+                    int data_size,
+                    int item_size,
+                    void* data_ptr):
+        """ This initialization cannot be done in the constructor because
+            we use C-level arguments.
+        """
+        self.item_size = item_size
+        self.data_size = data_size
+        self.data_type = data_type
+        self.data_ptr = data_ptr
+
+    def __array__(self):
+        """ Here we use the __array__ method, that is called when numpy
+            tries to get an array from the object.
+        """
+        cdef np.npy_intp shape[1]
+        shape[0] = <np.npy_intp> self.data_size
+
+        ndarray = np.PyArray_New(np.ndarray,
+                                 1, shape,
+                                 self.data_type,
+                                 NULL,
+                                 self.data_ptr,
+                                 self.item_size,
+                                 NPY_ARRAY_CARRAY,
+                                 <object>NULL)
+
+        return ndarray
+
+    def __dealloc__(self):
+        """ Free the data. This is called by Python when all the references to
+            the object are gone.
+        """
+        free(<void*>self.data_ptr)
+
+
+def load(ofst_data=True, cpu_data=True, ts_data=True,
+         pid_data=True, evt_data=True):
+    """ Python binding of the 'kshark_load_data_matrix' function that does not
+        copy the data. The input parameters can be used to avoid loading the
+        data from the unnecessary fields.
+    """
+    cdef uint64_t *ofst_c
+    cdef uint16_t *cpu_c
+    cdef uint64_t *ts_c
+    cdef uint16_t *pid_c
+    cdef int *evt_c
+
+    cdef np.ndarray ofst
+    cdef np.ndarray cpu
+    cdef np.ndarray ts
+    cdef np.ndarray pid
+    cdef np.ndarray evt
+
+    if not ofst_data:
+        ofst_c = NULL
+
+    if not cpu_data:
+        cpu_c = NULL
+
+    if not ts_data:
+        ts_c = NULL
+
+    if not pid_data:
+        pid_c = NULL
+
+    if not evt_data:
+        evt_c = NULL
+
+    data_dict = {}
+
+    cdef ssize_t size
+
+    size = trace2matrix(&ofst_c, &cpu_c, &ts_c, &pid_c, &evt_c)
+    if size <= 0:
+        raise Exception('No data has been loaded.')
+
+    if cpu_data:
+        column = 'cpu'
+        array_wrapper_cpu = KsDataWrapper()
+        array_wrapper_cpu.init(data_type=data_column_types[column],
+                               data_size=size,
+                               item_size=0,
+                               data_ptr=<void *> cpu_c)
+
+        cpu = np.array(array_wrapper_cpu, copy=False)
+        cpu.base = <PyObject *> array_wrapper_cpu
+        data_dict.update({column: cpu})
+        Py_INCREF(array_wrapper_cpu)
+
+    if pid_data:
+        column = 'pid'
+        array_wrapper_pid = KsDataWrapper()
+        array_wrapper_pid.init(data_type=data_column_types[column],
+                               data_size=size,
+                               item_size=0,
+                               data_ptr=<void *>pid_c)
+
+        pid = np.array(array_wrapper_pid, copy=False)
+        pid.base = <PyObject *> array_wrapper_pid
+        data_dict.update({column: pid})
+        Py_INCREF(array_wrapper_pid)
+
+    if evt_data:
+        column = 'event'
+        array_wrapper_evt = KsDataWrapper()
+        array_wrapper_evt.init(data_type=data_column_types[column],
+                               data_size=size,
+                               item_size=0,
+                               data_ptr=<void *>evt_c)
+
+        evt = np.array(array_wrapper_evt, copy=False)
+        evt.base = <PyObject *> array_wrapper_evt
+        data_dict.update({column: evt})
+        Py_INCREF(array_wrapper_evt)
+
+    if ofst_data:
+        column = 'offset'
+        array_wrapper_ofst = KsDataWrapper()
+        array_wrapper_ofst.init(data_type=data_column_types[column],
+                                data_size=size,
+                                item_size=0,
+                                data_ptr=<void *> ofst_c)
+
+
+        ofst = np.array(array_wrapper_ofst, copy=False)
+        ofst.base = <PyObject *> array_wrapper_ofst
+        data_dict.update({column: ofst})
+        Py_INCREF(array_wrapper_ofst)
+        
+    if ts_data:
+        column = 'time'
+        array_wrapper_ts = KsDataWrapper()
+        array_wrapper_ts.init(data_type=data_column_types[column],
+                              data_size=size,
+                              item_size=0,
+                              data_ptr=<void *> ts_c)
+
+        ts = np.array(array_wrapper_ts, copy=False)
+        ts.base = <PyObject *> array_wrapper_ts
+        data_dict.update({column: ts})
+        Py_INCREF(array_wrapper_ts)
+
+    return data_dict
diff --git a/src/trace2matrix.c b/src/trace2matrix.c
new file mode 100644
index 0000000..aaf8322
--- /dev/null
+++ b/src/trace2matrix.c
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: LGPL-2.1
+
+/*
+ * Copyright 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
+ */
+
+// 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;
+}
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 04/12] trace-cruncher: Add "utils"
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (2 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 03/12] trace-cruncher: Refactor NumPy based data wrapper Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 05/12] trace-cruncher: Adapt sched_wakeup.py to use the new module Yordan Karadzhov (VMware)
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

Place the rest of the code, that is pure Python in tracecrunche/utils.py

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 tracecruncher/utils.py | 54 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)
 create mode 100644 tracecruncher/utils.py

diff --git a/tracecruncher/utils.py b/tracecruncher/utils.py
new file mode 100644
index 0000000..f785c01
--- /dev/null
+++ b/tracecruncher/utils.py
@@ -0,0 +1,54 @@
+"""
+SPDX-License-Identifier: LGPL-2.1
+
+Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+"""
+
+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)
+
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 05/12] trace-cruncher: Adapt sched_wakeup.py to use the new module
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (3 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 04/12] trace-cruncher: Add "utils" Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 06/12] trace-cruncher: Add Makefile Yordan Karadzhov (VMware)
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 examples/sched_wakeup.py | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)

diff --git a/examples/sched_wakeup.py b/examples/sched_wakeup.py
index 52f2688..41e0c9c 100755
--- a/examples/sched_wakeup.py
+++ b/examples/sched_wakeup.py
@@ -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()
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 06/12] trace-cruncher: Add Makefile
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (4 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 05/12] trace-cruncher: Adapt sched_wakeup.py to use the new module Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 07/12] trace-cruncher: Adapt gpareto_fit.py to use the new module Yordan Karadzhov (VMware)
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

This will simplify the building procedure and will make it more intuitive.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 Makefile | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)
 create mode 100644 Makefile

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..735d43c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+#
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+#
+
+all:
+	python3 setup.py build
+
+clean:
+	rm -rf build src/datawrapper.c
+
+install:
+	python3 setup.py install --record install_manifest.txt
+
+uninstall:
+	xargs rm -v < install_manifest.txt
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 07/12] trace-cruncher: Adapt gpareto_fit.py to use the new module
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (5 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 06/12] trace-cruncher: Add Makefile Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 08/12] trace-cruncher: Adapt page_faults.py " Yordan Karadzhov (VMware)
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

The patch contains some debugging as well.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 examples/gpareto_fit.py | 35 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 18 deletions(-)

diff --git a/examples/gpareto_fit.py b/examples/gpareto_fit.py
index 4a2bb2a..eece064 100755
--- a/examples/gpareto_fit.py
+++ b/examples/gpareto_fit.py
@@ -16,11 +16,10 @@ import numpy as np
 from scipy.stats import genpareto as gpareto
 from scipy.optimize import curve_fit as cfit
 
-from ksharksetup import setup
-# Always call setup() before importing ksharkpy!!!
-setup()
-
-import ksharkpy as ks
+import tracecruncher.datawrapper as dw
+import tracecruncher.ksharkpy as ks
+import tracecruncher.ftracepy as ft
+import tracecruncher.utils as tc
 
 def chi2_test(hist, n_bins, c, loc, scale, norm):
     """ Simple Chi^2 test for the goodness of the fit.
@@ -92,7 +91,7 @@ def get_cpu_data(data, task_pid, start_id, stop_id, threshold):
         than the specified threshold.
     """
     # Get the size of the data.
-    size = ks.data_size(data)
+    size = tc.size(data)
     #print("data size:", size)
 
     time_start = -1
@@ -130,7 +129,7 @@ def make_ks_session(fname, data, start, stop):
         The sessions is zooming around the maximum observed latency.
     """
     sname = 'max_lat.json'
-    ks.new_session(fname, sname)
+    tc.new_gui_session(fname, sname)
     i_start = int(start)
     i_stop = int(stop)
 
@@ -145,28 +144,28 @@ def make_ks_session(fname, data, start, stop):
         session['Model']['range'] = [tmin, tmax]
 
         session['Markers']['markA']['isSet'] = True
-        session['Markers']['markA']['row'] = i_start)
+        session['Markers']['markA']['row'] = i_start
 
         session['Markers']['markB']['isSet'] = True
-        session['Markers']['markB']['row'] = i_stop)
+        session['Markers']['markB']['row'] = i_stop
 
-        session['ViewTop'] = i_start) - 5
+        session['ViewTop'] = i_start - 5
 
-        ks.save_session(session, s)
+        tc.save_session(session, s)
 
 
 fname = str(sys.argv[1])
-status = ks.open_file(fname)
+status = ks.open(fname)
 if not status:
     print ("Failed to open file ", fname)
     sys.exit()
 
 ks.register_plugin('reg_pid')
-data = ks.load_data()
+data = dw.load()
 
 # Get the Event Ids of the hrtimer_start and print events.
-start_id = ks.event_id('timer', 'hrtimer_start')
-stop_id = ks.event_id('ftrace', 'print')
+start_id = ft.event_id('timer', 'hrtimer_start')
+stop_id = ft.event_id('ftrace', 'print')
 print("start_id", start_id)
 print("stop_id", stop_id)
 
@@ -194,9 +193,6 @@ ks.close()
 dt_ot = np.array(data_ot)
 np.savetxt('peak_over_threshold_loaded.txt', dt_ot)
 
-make_ks_session(fname=fname, data=data, i_start=int(dt_ot[i_max_lat][1]),
-                                        i_stop=int(dt_ot[i_max_lat][2]))
-
 P = len(dt_ot) / tot
 err_P = error_P(n=len(dt_ot), N=tot)
 print('tot:', tot, ' P =', P)
@@ -208,6 +204,9 @@ print('imax:', i_max_lat, int(dt_ot[i_max_lat][1]))
 
 print('max', np.amax(dt_ot))
 
+make_ks_session(fname=fname, data=data, start=dt_ot[i_max_lat][1],
+                                        stop=dt_ot[i_max_lat][2])
+
 start = threshold
 stop = 31
 n_bins = (stop - start) * 2
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 08/12] trace-cruncher: Adapt page_faults.py to use the new module
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (6 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 07/12] trace-cruncher: Adapt gpareto_fit.py to use the new module Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 09/12] trace-cruncher: Automate the third-party build Yordan Karadzhov (VMware)
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

The patch contains some debugging as well.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 examples/page_faults.py | 64 ++++++++++++++++++-----------------------
 1 file changed, 28 insertions(+), 36 deletions(-)

diff --git a/examples/page_faults.py b/examples/page_faults.py
index 446b12d..e0ce4e2 100755
--- a/examples/page_faults.py
+++ b/examples/page_faults.py
@@ -9,20 +9,14 @@ Copyright 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
 import os
 import sys
 import subprocess as sp
-import json
-
 import pprint as pr
-import matplotlib.pyplot as plt
-import scipy.stats as st
-import numpy as np
 from collections import Counter
 from tabulate import tabulate
 
-from ksharksetup import setup
-# Always call setup() before importing ksharkpy!!!
-setup()
-
-import ksharkpy as ks
+from tracecruncher import datawrapper as dw
+from tracecruncher import ksharkpy as ks
+from tracecruncher import ftracepy as ft
+import tracecruncher.utils as tc
 
 def gdb_decode_address(obj_file, obj_address):
     """ Use gdb to examine the contents of the memory at this
@@ -33,7 +27,7 @@ def gdb_decode_address(obj_file, obj_address):
                      '-ex',
                      'x/i ' + str(obj_address),
                      obj_file],
-                    stdout=sp.PIPE)
+                     stdout=sp.PIPE)
 
     symbol = result.stdout.decode("utf-8").splitlines()
 
@@ -49,35 +43,35 @@ def gdb_decode_address(obj_file, obj_address):
 # Get the name of the tracing data file.
 fname = str(sys.argv[1])
 
-ks.open_file(fname)
+ks.open(fname)
 ks.register_plugin('reg_pid')
 
-data = ks.load_data()
+data = dw.load()
 tasks = ks.get_tasks()
 #pr.pprint(tasks)
 
 # Get the Event Ids of the page_fault_user or page_fault_kernel events.
-pf_eid = ks.event_id('exceptions', 'page_fault_user')
+pf_eid = ft.event_id('exceptions', 'page_fault_user')
 
 # Gey the size of the data.
-d_size = ks.data_size(data)
+d_size = tc.size(data)
 
 # Get the name of the user program.
 prog_name = str(sys.argv[2])
 
 table_headers = ['N p.f.', 'function', 'value', 'obj. file']
-table_list = []
 
 # Loop over all tasks associated with the user program.
 for j in range(len(tasks[prog_name])):
+    table_list = []
     count = Counter()
     task_pid = tasks[prog_name][j]
     for i in range(0, d_size):
         if data['event'][i] == pf_eid and data['pid'][i] == task_pid:
-            address = ks.read_event_field(offset=data['offset'][i],
+            address = ft.read_event_field(offset=data['offset'][i],
                                           event_id=pf_eid,
                                           field='address')
-            ip = ks.read_event_field(offset=data['offset'][i],
+            ip = ft.read_event_field(offset=data['offset'][i],
                                      event_id=pf_eid,
                                      field='ip')
             count[ip] += 1
@@ -92,29 +86,27 @@ for j in range(len(tasks[prog_name])):
     if i_max > len(pf_list):
         i_max = len(pf_list)
 
+    #print(pf_list[:25])
     for i in range(0, i_max):
-        func = ks.get_function(pf_list[i][0])
-        func_info = [func]
-        if func.startswith('0x'):
-            # The name of the function cannot be determined. We have an
-            # instruction pointer instead. Most probably this is a user-space
-            # function.
-            address = int(func, 0)
-            instruction = ks.map_instruction_address(task_pid, address)
-
-            if instruction['obj_file'] != 'UNKNOWN':
+        func_info = []
+        address = int(pf_list[i][0])
+        func = ft.get_function(address)
+        if not func :
+            # The name of the function cannot be determined. Most probably
+            # this is a user-space function.
+            instruction = ft.map_instruction_address(pid=task_pid,
+                                                     proc_addr=address)
+            if instruction:
                 func_info = gdb_decode_address(instruction['obj_file'],
                                                instruction['address'])
             else:
-                func_info += ['', instruction['obj_file']]
-
-        else:
-            func_info = [func]
+                func_info = ['addr: ' + hex(address), 'UNKNOWN']
 
         table_list.append([pf_list[i][1]] + func_info)
 
-ks.close()
+    print('\n{}-{}\n'.format(prog_name, task_pid),
+          tabulate(table_list,
+                   headers=table_headers,
+                   tablefmt='simple'))
 
-print("\n", tabulate(table_list,
-                     headers=table_headers,
-                     tablefmt='simple'))
+ks.close()
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 09/12] trace-cruncher: Automate the third-party build
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (7 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 08/12] trace-cruncher: Adapt page_faults.py " Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 10/12] trace-cruncher: Update README.md Yordan Karadzhov (VMware)
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

The goal is to provide a simple and intuitive installation procedure
and in the same time to encapsulate the used third-party libraries
inside the Python package. This way we avoid polluting the system-wide
locations with the patched versions of the third-party libraries.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 ...-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch | 160 ++++++++++++++++++
 0001-kernel-shark-Add-_DEVEL-build-flag.patch |  90 ----------
 0002-kernel-shark-Add-reg_pid-plugin.patch    |   8 +-
 Makefile                                      |   4 +-
 install_third_party.sh                        |  39 +++++
 setup.py                                      |  32 ++--
 src/ksharkpy.c                                |   4 +-
 tracecruncher/__init__.py                     |   4 +
 8 files changed, 231 insertions(+), 110 deletions(-)
 create mode 100644 0001-kernel-shark-Add-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch
 delete mode 100644 0001-kernel-shark-Add-_DEVEL-build-flag.patch
 create mode 100755 install_third_party.sh

diff --git a/0001-kernel-shark-Add-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch b/0001-kernel-shark-Add-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch
new file mode 100644
index 0000000..68fb279
--- /dev/null
+++ b/0001-kernel-shark-Add-_DEVEL-_LIBS-and-_RPATH_TO_ORIGIN-b.patch
@@ -0,0 +1,160 @@
+From 9e3a3cb07674b787f64d2f4ee9cb4467745e815b Mon Sep 17 00:00:00 2001
+From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
+Date: Fri, 20 Sep 2019 14:31:15 +0300
+Subject: [PATCH 1/2] kernel-shark: Add _DEVEL, _LIBS and _RPATH_TO_ORIGIN
+ build flags
+
+If KernelShark is built with -D_LIBS=1 as a command-line argument
+for Cmake, only libkshark and libkshapr-plot will be compiled.
+
+If KernelShark is built with -D_DEVEL=1 as a command-line argument
+for Cmake, the headers of the libraries will be installed as well
+and a symbolic link that points to the version of the library
+being installed will be created.
+
+If built with -D_RPATH_TO_ORIGIN=1 the "rpath" of all libraries is
+set to "$ORIGIN".
+
+Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
+---
+ kernel-shark/CMakeLists.txt     | 23 ++++++++++++++------
+ kernel-shark/README             |  3 +++
+ kernel-shark/src/CMakeLists.txt | 37 +++++++++++++++++++++++++++++++--
+ 3 files changed, 55 insertions(+), 8 deletions(-)
+
+diff --git a/kernel-shark/CMakeLists.txt b/kernel-shark/CMakeLists.txt
+index 20478b9..ea3133c 100644
+--- a/kernel-shark/CMakeLists.txt
++++ b/kernel-shark/CMakeLists.txt
+@@ -73,6 +73,12 @@ else (CMAKE_BUILD_TYPE MATCHES Package)
+ 
+ endif (CMAKE_BUILD_TYPE MATCHES Package)
+ 
++if (_RPATH_TO_ORIGIN)
++
++    set(CMAKE_INSTALL_RPATH $ORIGIN)
++
++endif (_RPATH_TO_ORIGIN)
++
+ include_directories(${KS_DIR}/src/
+                     ${KS_DIR}/build/src/
+                     ${JSONC_INCLUDE_DIR}
+@@ -84,7 +90,6 @@ message(STATUS "CXX flags    : " ${CMAKE_CXX_FLAGS})
+ message(STATUS "Linker flags : " ${CMAKE_EXE_LINKER_FLAGS})
+ 
+ add_subdirectory(${KS_DIR}/src)
+-add_subdirectory(${KS_DIR}/examples)
+ 
+ if (_DOXYGEN_DOC AND DOXYGEN_FOUND)
+ 
+@@ -99,12 +104,18 @@ if (_DOXYGEN_DOC AND DOXYGEN_FOUND)
+                              "${KS_DIR}/Documentation/doxygen/html"
+                              "${KS_DIR}/Documentation/doxygen/latex")
+ 
+-endif ()
++endif (_DOXYGEN_DOC AND DOXYGEN_FOUND)
++
++if (NOT _LIBS)
++
++    add_subdirectory(${KS_DIR}/examples)
++
++    configure_file(${KS_DIR}/build/ks.desktop.cmake
++                   ${KS_DIR}/${KS_APP_NAME}.desktop)
+ 
+-configure_file( ${KS_DIR}/build/ks.desktop.cmake
+-                ${KS_DIR}/${KS_APP_NAME}.desktop)
++    configure_file(${KS_DIR}/build/org.freedesktop.kshark-record.policy.cmake
++                   ${KS_DIR}/org.freedesktop.kshark-record.policy)
+ 
+-configure_file( ${KS_DIR}/build/org.freedesktop.kshark-record.policy.cmake
+-                ${KS_DIR}/org.freedesktop.kshark-record.policy)
++endif (NOT _LIBS)
+ 
+ message("")
+diff --git a/kernel-shark/README b/kernel-shark/README
+index 6c360bb..0f14212 100644
+--- a/kernel-shark/README
++++ b/kernel-shark/README
+@@ -96,6 +96,9 @@ the dialog will derive the absolut path to the trace-cmd executable from
+ 
+ If no build types is specified, the type will be "RelWithDebInfo".
+ 
++2.1.4 In order to install a development version (including headers e.t.c) add
++-D_DEVEL=1 as a CMake Command-Line option.
++
+ Examples:
+ 
+     cmake -D_DOXYGEN_DOC=1 -D_INSTALL_PREFIX=/usr ../
+diff --git a/kernel-shark/src/CMakeLists.txt b/kernel-shark/src/CMakeLists.txt
+index e20a030..f63da6a 100644
+--- a/kernel-shark/src/CMakeLists.txt
++++ b/kernel-shark/src/CMakeLists.txt
+@@ -1,5 +1,13 @@
+ message("\n src ...")
+ 
++macro(install_symlink filepath sympath)
++    install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${filepath} ${sympath})")
++    install(CODE "LIST(APPEND CMAKE_INSTALL_MANIFEST_FILES ${sympath})")
++    install(CODE "message(\"-- Created symlink: ${sympath} -> ${filepath}\")")
++endmacro(install_symlink)
++
++set(KS_INCLUDS_DESTINATION "${_INSTALL_PREFIX}/include/${KS_APP_NAME}")
++
+ message(STATUS "libkshark")
+ add_library(kshark SHARED libkshark.c
+                           libkshark-model.c
+@@ -16,6 +24,19 @@ set_target_properties(kshark  PROPERTIES SUFFIX	".so.${KS_VERSION_STRING}")
+ 
+ install(TARGETS kshark LIBRARY DESTINATION ${_INSTALL_PREFIX}/lib/${KS_APP_NAME})
+ 
++if (_DEVEL)
++
++    install_symlink("libkshark.so.${KS_VERSION_STRING}"
++                    "${_INSTALL_PREFIX}/lib/${KS_APP_NAME}/libkshark.so")
++
++    install(FILES "${KS_DIR}/src/libkshark.h"
++                  "${KS_DIR}/src/libkshark-plugin.h"
++                  "${KS_DIR}/src/libkshark-model.h"
++            DESTINATION ${KS_INCLUDS_DESTINATION}
++            COMPONENT devel)
++
++endif (_DEVEL)
++
+ if (OPENGL_FOUND AND GLUT_FOUND)
+ 
+     message(STATUS "libkshark-plot")
+@@ -30,9 +51,21 @@ if (OPENGL_FOUND AND GLUT_FOUND)
+ 
+     install(TARGETS kshark-plot LIBRARY DESTINATION ${_INSTALL_PREFIX}/lib/${KS_APP_NAME})
+ 
++    if (_DEVEL)
++
++        install_symlink("libkshark-plot.so.${KS_VERSION_STRING}"
++                        "${_INSTALL_PREFIX}/lib/${KS_APP_NAME}/libkshark-plot.so")
++
++        install(FILES "${KS_DIR}/src/KsPlotTools.hpp"
++                      "${KS_DIR}/src/libkshark-plot.h"
++                DESTINATION ${KS_INCLUDS_DESTINATION}
++                COMPONENT devel)
++
++    endif (_DEVEL)
++
+ endif (OPENGL_FOUND AND GLUT_FOUND)
+ 
+-if (Qt5Widgets_FOUND AND Qt5Network_FOUND)
++if (Qt5Widgets_FOUND AND Qt5Network_FOUND AND NOT _LIBS)
+ 
+     message(STATUS "libkshark-gui")
+     set (ks-guiLib_hdr  KsUtils.hpp
+@@ -98,7 +131,7 @@ if (Qt5Widgets_FOUND AND Qt5Network_FOUND)
+     install(PROGRAMS "${KS_DIR}/bin/kshark-su-record"
+             DESTINATION ${_INSTALL_PREFIX}/bin/)
+ 
+-endif (Qt5Widgets_FOUND AND Qt5Network_FOUND)
++endif (Qt5Widgets_FOUND AND Qt5Network_FOUND AND NOT _LIBS)
+ 
+ add_subdirectory(plugins)
+ 
+-- 
+2.20.1
+
diff --git a/0001-kernel-shark-Add-_DEVEL-build-flag.patch b/0001-kernel-shark-Add-_DEVEL-build-flag.patch
deleted file mode 100644
index ddd3fd4..0000000
--- a/0001-kernel-shark-Add-_DEVEL-build-flag.patch
+++ /dev/null
@@ -1,90 +0,0 @@
-From 6c9e3b3f29c8af4780bb46313c3af73fb5d852c7 Mon Sep 17 00:00:00 2001
-From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
-Date: Fri, 20 Sep 2019 14:31:15 +0300
-Subject: [PATCH 1/2] kernel-shark: Add _DEVEL build flag
-
-KernelShark can be built with -D_DEVEL=1 as a command-line argument
-for Cmake. In this case the headers of the libraries will be installed
-as well and a symbolic link that points to the version of the library
-being installed will be created.
-
-Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
----
- kernel-shark/README             |  3 +++
- kernel-shark/src/CMakeLists.txt | 33 +++++++++++++++++++++++++++++++++
- 2 files changed, 36 insertions(+)
-
-diff --git a/kernel-shark/README b/kernel-shark/README
-index 6c360bb..0f14212 100644
---- a/kernel-shark/README
-+++ b/kernel-shark/README
-@@ -96,6 +96,9 @@ the dialog will derive the absolut path to the trace-cmd executable from
- 
- If no build types is specified, the type will be "RelWithDebInfo".
- 
-+2.1.4 In order to install a development version (including headers e.t.c) add
-+-D_DEVEL=1 as a CMake Command-Line option.
-+
- Examples:
- 
-     cmake -D_DOXYGEN_DOC=1 -D_INSTALL_PREFIX=/usr ../
-diff --git a/kernel-shark/src/CMakeLists.txt b/kernel-shark/src/CMakeLists.txt
-index e20a030..305840b 100644
---- a/kernel-shark/src/CMakeLists.txt
-+++ b/kernel-shark/src/CMakeLists.txt
-@@ -1,5 +1,13 @@
- message("\n src ...")
- 
-+macro(install_symlink filepath sympath)
-+    install(CODE "execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${filepath} ${sympath})")
-+    install(CODE "LIST(APPEND CMAKE_INSTALL_MANIFEST_FILES ${sympath})")
-+    install(CODE "message(\"-- Created symlink: ${sympath} -> ${filepath}\")")
-+endmacro(install_symlink)
-+
-+set(KS_INCLUDS_DESTINATION "${_INSTALL_PREFIX}/include/${KS_APP_NAME}")
-+
- message(STATUS "libkshark")
- add_library(kshark SHARED libkshark.c
-                           libkshark-model.c
-@@ -16,6 +24,19 @@ set_target_properties(kshark  PROPERTIES SUFFIX	".so.${KS_VERSION_STRING}")
- 
- install(TARGETS kshark LIBRARY DESTINATION ${_INSTALL_PREFIX}/lib/${KS_APP_NAME})
- 
-+if (_DEVEL)
-+
-+    install_symlink("libkshark.so.${KS_VERSION_STRING}"
-+                    "${_INSTALL_PREFIX}/lib/${KS_APP_NAME}/libkshark.so")
-+
-+    install(FILES "${KS_DIR}/src/libkshark.h"
-+                  "${KS_DIR}/src/libkshark-plugin.h"
-+                  "${KS_DIR}/src/libkshark-model.h"
-+            DESTINATION ${KS_INCLUDS_DESTINATION}
-+            COMPONENT devel)
-+
-+endif (_DEVEL)
-+
- if (OPENGL_FOUND AND GLUT_FOUND)
- 
-     message(STATUS "libkshark-plot")
-@@ -30,6 +51,18 @@ if (OPENGL_FOUND AND GLUT_FOUND)
- 
-     install(TARGETS kshark-plot LIBRARY DESTINATION ${_INSTALL_PREFIX}/lib/${KS_APP_NAME})
- 
-+    if (_DEVEL)
-+
-+        install_symlink("libkshark-plot.so.${KS_VERSION_STRING}"
-+                        "${_INSTALL_PREFIX}/lib/${KS_APP_NAME}/libkshark-plot.so")
-+
-+        install(FILES "${KS_DIR}/src/KsPlotTools.hpp"
-+                      "${KS_DIR}/src/libkshark-plot.h"
-+                DESTINATION ${KS_INCLUDS_DESTINATION}
-+                COMPONENT devel)
-+
-+    endif (_DEVEL)
-+
- endif (OPENGL_FOUND AND GLUT_FOUND)
- 
- if (Qt5Widgets_FOUND AND Qt5Network_FOUND)
--- 
-2.20.1
-
diff --git a/0002-kernel-shark-Add-reg_pid-plugin.patch b/0002-kernel-shark-Add-reg_pid-plugin.patch
index 146e3e6..8edc91f 100644
--- a/0002-kernel-shark-Add-reg_pid-plugin.patch
+++ b/0002-kernel-shark-Add-reg_pid-plugin.patch
@@ -1,4 +1,4 @@
-From b3efcb6368bc7f70a23e156dce6c58d09953889a Mon Sep 17 00:00:00 2001
+From dbfb97e61e9907df195970cf378740a1ee927de9 Mon Sep 17 00:00:00 2001
 From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
 Date: Wed, 9 Oct 2019 16:57:27 +0300
 Subject: [PATCH 2/2] kernel-shark: Add "reg_pid" plugin
@@ -33,7 +33,7 @@ index 6c77179..bf69945 100644
  set(PLUGINS ${PLUGIN_LIST} PARENT_SCOPE)
 diff --git a/kernel-shark/src/plugins/reg_pid.c b/kernel-shark/src/plugins/reg_pid.c
 new file mode 100644
-index 0000000..4116dd8
+index 0000000..df2e406
 --- /dev/null
 +++ b/kernel-shark/src/plugins/reg_pid.c
 @@ -0,0 +1,189 @@
@@ -216,14 +216,14 @@ index 0000000..4116dd8
 +/** Load this plugin. */
 +int KSHARK_PLUGIN_INITIALIZER(struct kshark_context *kshark_ctx)
 +{
-+// 	printf("--> pid_reg init\n");
++	printf("--> pid_reg init\n");
 +	return plugin_pid_reg_init(kshark_ctx);
 +}
 +
 +/** Unload this plugin. */
 +int KSHARK_PLUGIN_DEINITIALIZER(struct kshark_context *kshark_ctx)
 +{
-+// 	printf("<-- pid reg close\n");
++	printf("<-- pid reg close\n");
 +	return plugin_pid_reg_close(kshark_ctx);
 +}
 -- 
diff --git a/Makefile b/Makefile
index 735d43c..9f49184 100644
--- a/Makefile
+++ b/Makefile
@@ -5,10 +5,12 @@
 #
 
 all:
+	./install_third_party.sh
 	python3 setup.py build
 
 clean:
-	rm -rf build src/datawrapper.c
+	rm -rf third_party build tracecruncher/lib
+	rm -f src/datawrapper.c
 
 install:
 	python3 setup.py install --record install_manifest.txt
diff --git a/install_third_party.sh b/install_third_party.sh
new file mode 100755
index 0000000..412885e
--- /dev/null
+++ b/install_third_party.sh
@@ -0,0 +1,39 @@
+#!/bin/bash
+
+TRUNK=${PWD}
+THIRD_PARTY_LIB=${TRUNK}/tracecruncher/lib/libtracecmd.so
+
+if [ -f ${THIRD_PARTY_LIB} ]; then
+    exit 0
+fi
+
+TAG=kernelshark-v1.1
+THIRD_PARTY_DIR=${TRUNK}/third_party
+mkdir ${THIRD_PARTY_DIR}
+cd ${THIRD_PARTY_DIR}
+
+echo 'Installing: ' ${TAG}
+
+git clone git://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git --branch=${TAG}
+
+cd trace-cmd
+git am ${TRUNK}/0001-kernel-shark-*
+git am ${TRUNK}/0002-kernel-shark-*
+
+make prefix=${THIRD_PARTY_DIR} install_libs
+
+cd kernel-shark/build/
+cmake -D_DEVEL=1 -D_LIBS=1 -D_INSTALL_PREFIX=${THIRD_PARTY_DIR} -D_RPATH_TO_ORIGIN=1 ..
+make install
+
+cd ${TRUNK}
+
+LIB_DIR=${TRUNK}/tracecruncher/lib/
+if [ ! -d "${LIB_DIR}" ]; then
+    mkdir ${LIB_DIR}
+fi
+
+cp -v ${THIRD_PARTY_DIR}/lib/kernelshark/libkshark.so.* ${TRUNK}/tracecruncher/lib/
+cp -v ${THIRD_PARTY_DIR}/lib/kernelshark/plugins/*.so ${TRUNK}/tracecruncher/lib/
+cp -v ${THIRD_PARTY_DIR}/lib/traceevent/*.so ${TRUNK}/tracecruncher/lib/
+cp -v ${THIRD_PARTY_DIR}/lib/trace-cmd/*.so ${TRUNK}/tracecruncher/lib/
diff --git a/setup.py b/setup.py
index 526e1e7..81f6e8e 100644
--- a/setup.py
+++ b/setup.py
@@ -12,34 +12,37 @@ from Cython.Build import cythonize
 import numpy as np
 
 def main():
-    kshark_path = '/usr/local/lib/kernelshark'
-    traceevent_path = '/usr/local/lib/traceevent/'
-    tracecmd_path = '/usr/local/lib/trace-cmd/'
+    third_party = './third_party'
 
     cythonize('src/datawrapper.pyx')
+
+    third_party_libdirs = [third_party+'/lib/kernelshark',
+                           third_party+'/lib/traceevent',
+                           third_party+'/lib/trace-cmd']
+
+    runtime_library_dirs=['$ORIGIN/lib']
+
     module_data = Extension('tracecruncher.datawrapper',
                             sources=['src/datawrapper.c'],
-                            include_dirs=[np.get_include()],
-                            library_dirs=[kshark_path, traceevent_path, tracecmd_path],
-                            runtime_library_dirs=[kshark_path, traceevent_path, tracecmd_path],
+                            include_dirs=[np.get_include(), third_party+'/include'],
+                            library_dirs=third_party_libdirs,
+                            runtime_library_dirs=runtime_library_dirs,
                             libraries=['kshark', 'traceevent', 'tracecmd']
                             )
 
     module_ks = Extension('tracecruncher.ksharkpy',
                           sources=['src/ksharkpy.c'],
-                          library_dirs=[kshark_path],
-                          runtime_library_dirs=[kshark_path],
+                          include_dirs=[third_party+'/include'],
+                          library_dirs=third_party_libdirs,
+                          runtime_library_dirs=runtime_library_dirs,
                           libraries=['kshark'],
-                          define_macros=[
-                              ('LIB_KSHARK_PATH', '\"' + kshark_path + '/libkshark.so\"'),
-                              ('KS_PLUGIN_DIR',   '\"' + kshark_path + '/plugins\"')
-                              ],
                           )
 
     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],
+                          include_dirs=[third_party+'/include'],
+                          library_dirs=third_party_libdirs,
+                          runtime_library_dirs=runtime_library_dirs,
                           libraries=['kshark', 'traceevent', 'tracecmd'],
                           )
 
@@ -52,6 +55,7 @@ def main():
           license='LGPL-2.1',
           packages=find_packages(),
           ext_modules=[module_data, module_ks, module_ft],
+          package_data={'tracecruncher': ['lib/*.so*']},
           classifiers=[
               'Development Status :: 3 - Alpha',
               'Programming Language :: Python :: 3',
diff --git a/src/ksharkpy.c b/src/ksharkpy.c
index 5a15a77..553c70a 100644
--- a/src/ksharkpy.c
+++ b/src/ksharkpy.c
@@ -137,7 +137,9 @@ static PyObject *method_register_plugin(PyObject *self, PyObject *args,
 		return NULL;
 	}
 
-	if (asprintf(&lib_file, "%s/plugin-%s.so", KS_PLUGIN_DIR, plugin) < 0) {
+	if (asprintf(&lib_file, "%s/lib/plugin-%s.so",
+			        getenv("TRACE_CRUNCHER_PATH"),
+				plugin) < 0) {
 		KS_MEM_ERROR
 		return NULL;
 	}
diff --git a/tracecruncher/__init__.py b/tracecruncher/__init__.py
index e69de29..1b34d53 100644
--- a/tracecruncher/__init__.py
+++ b/tracecruncher/__init__.py
@@ -0,0 +1,4 @@
+import os
+
+pwd = os.path.dirname(os.path.abspath(__file__))
+os.environ['TRACE_CRUNCHER_PATH'] = pwd
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 10/12] trace-cruncher: Update README.md
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (8 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 09/12] trace-cruncher: Automate the third-party build Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 11/12] trace-cruncher: Remove all leftover files Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 12/12] trace-cruncher: Improve Makefile Provide more robust and better looking build process Yordan Karadzhov (VMware)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 README.md | 50 +++++++++++---------------------------------------
 1 file changed, 11 insertions(+), 39 deletions(-)

diff --git a/README.md b/README.md
index c5121ab..2f7d533 100644
--- a/README.md
+++ b/README.md
@@ -4,9 +4,9 @@
 
 ## Overview
 
-The Trace-Cruncher project aims to provide an interface between the existing instrumentation for collection and visualization of tracing data from the Linux kernel and the broad and very well developed ecosystem of instruments for data analysis available in Python. The interface will be based on NumPy.
+The Trace-Cruncher project aims to provide an interface between the existing instrumentation for collection and visualization of tracing data from the Linux kernel and the broad and very well developed ecosystem of instruments for data analysis available in Python. The interface is based on NumPy.
 
-NumPy implements an efficient multi-dimensional container of generic data and uses strong typing in order to provide fast data processing in Python. The  Trace-Cruncher will allow for sophisticated analysis of kernel tracing data via scripts, but it will also opens the door for exposing the kernel tracing data to the instruments provided by the scientific toolkit of Python like MatPlotLib, Stats, Scikit-Learn and even to the nowadays most popular frameworks for Machine Learning like TensorFlow and PyTorch. The Trace-Cruncher is strongly coupled to the KernelShark project and is build on top of the C API of libkshark.
+NumPy implements an efficient multi-dimensional container of generic data and uses strong typing in order to provide fast data processing in Python. The  Trace-Cruncher allows for sophisticated analysis of kernel tracing data via scripts, but it also opens the door for exposing the kernel tracing data to the instruments provided by the scientific toolkit of Python like MatPlotLib, Stats, Scikit-Learn and even to the nowadays most popular frameworks for Machine Learning like TensorFlow and PyTorch. The Trace-Cruncher is strongly coupled to the KernelShark project and is build on top of the C API of libkshark.
 
 ## Try it out
 
@@ -17,55 +17,27 @@ Trace-Cruncher has the following external dependencies:
 
 1.1 In order to install the packages on Ubuntu do the following:
 
-    sudo apt-get install libjson-c-dev libpython3-dev cython3 -y
+    > sudo apt-get install libjson-c-dev libpython3-dev cython3 -y
 
-    sudo apt-get install python3-numpy python3-matplotlib -y
+    > sudo apt-get install python3-numpy python3-matplotlib -y
 
 1.2 In order to install the packages on Fedora, as root do the following:
 
-    dnf install json-c-devel python3-devel python3-Cython -y
+    > dnf install json-c-devel python3-devel python3-Cython -y
 
-    dnf install python3-numpy python3-matplotlib -y
-
-2. In order to get the proper version of KernelShark / trace-cmd do the
-following:
-
-    git clone git://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git --branch=kernelshark-v1.1
-
-or download a tarball from here:
-https://git.kernel.org/pub/scm/utils/trace-cmd/trace-cmd.git/snapshot/trace-cmd-kernelshark-v1.1.tar.gz
+    > dnf install python3-numpy python3-matplotlib -y
 
 ### Build & Run
 
-1. Patch trace-cmd / KernelShark:
-
-    cd path/to/trace-cmd/
-
-    git am ../path/to/trace-cruncher/0001-kernel-shark-Add-_DEVEL-build-flag.patch
-
-    git am ../path/to/trace-cruncher/0002-kernel-shark-Add-reg_pid-plugin.patch
-
-2. Install trace-cmd:
-
-    make
-
-    sudo make install_libs
-
-3. Install KernelShark:
-
-    cd kernel-shark/build
-
-    cmake -D_DEVEL=1 ../
-
-    make
+Installing trace-cruncher is very simple. After downloading the source code, you just have to run:
 
-    sudo make install
+     > cd trace-cruncher
 
-4. Build the NumPy API itself:
+     > make
 
-    cd path/to/trace-cruncher
+     > sudo make install
 
-    ./np_setup.py build_ext -i
+Note that this will automatically download, patch and build the appropriate versions of "trace-cmd / KernelShark" libraries from kernel.org. These third-party libraries will be installed as part of trace-cruncher itself and will not interfere with any existing system-wide installations of trace-cmd and KernelShark.
 
 ## Documentation
 
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 11/12] trace-cruncher: Remove all leftover files.
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (9 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 10/12] trace-cruncher: Update README.md Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  2020-01-07 17:03 ` [PATCH v2 12/12] trace-cruncher: Improve Makefile Provide more robust and better looking build process Yordan Karadzhov (VMware)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

This patch completes the refactoring of trace-cruncher into a Python
module. All obsoleted source files are removed.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 clean.sh                |   6 -
 examples/ksharksetup.py |  24 ---
 libkshark-py.c          | 224 -------------------------
 libkshark_wrapper.pyx   | 361 ----------------------------------------
 np_setup.py             |  90 ----------
 5 files changed, 705 deletions(-)
 delete mode 100755 clean.sh
 delete mode 100644 examples/ksharksetup.py
 delete mode 100644 libkshark-py.c
 delete mode 100644 libkshark_wrapper.pyx
 delete mode 100755 np_setup.py

diff --git a/clean.sh b/clean.sh
deleted file mode 100755
index a739b88..0000000
--- a/clean.sh
+++ /dev/null
@@ -1,6 +0,0 @@
-#!/bin/bash
-
-rm libkshark_wrapper.c
-rm ksharkpy.cpython-3*.so
-rm -rf build/
-rm -rf examples/__pycache__/
diff --git a/examples/ksharksetup.py b/examples/ksharksetup.py
deleted file mode 100644
index 86729e3..0000000
--- a/examples/ksharksetup.py
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/usr/bin/env python3
-
-"""
-SPDX-License-Identifier: LGPL-2.1
-
-Copyright 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
-"""
-
-import os
-import sys
-
-def setup():
-    os.chdir(os.path.dirname(__file__))
-    path = os.getcwd() + '/..'
-    sys.path.append(path)
-
-    if 'LD_LIBRARY_PATH' not in os.environ:
-        os.environ['LD_LIBRARY_PATH'] = '/usr/local/lib/kernelshark:/usr/local/lib/traceevent:/usr/local/lib/trace-cmd'
-        try:
-            os.execv(sys.argv[0], sys.argv)
-        except e:
-            print('Failed re-exec:', e)
-            sys.exit(1)
-
diff --git a/libkshark-py.c b/libkshark-py.c
deleted file mode 100644
index 8b39bae..0000000
--- a/libkshark-py.c
+++ /dev/null
@@ -1,224 +0,0 @@
-// SPDX-License-Identifier: LGPL-2.1
-
-/*
- * Copyright 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
- */
-
- /**
-  *  @file    libkshark-py.c
-  *  @brief   Python API for processing of FTRACE (trace-cmd) data.
-  */
-
-// KernelShark
-#include "kernelshark/libkshark.h"
-#include "kernelshark/libkshark-model.h"
-
-bool kspy_open(const char *fname)
-{
-	struct kshark_context *kshark_ctx = NULL;
-
-	if (!kshark_instance(&kshark_ctx))
-		return false;
-
-	return kshark_open(kshark_ctx, fname);
-}
-
-void kspy_close(void)
-{
-	struct kshark_context *kshark_ctx = NULL;
-
-	if (!kshark_instance(&kshark_ctx))
-		return;
-
-	kshark_close(kshark_ctx);
-	kshark_free(kshark_ctx);
-}
-
-static int compare(const void * a, const void * b)
-{
-	return *(int*)a - *(int*)b;
-}
-
-size_t kspy_get_tasks(int **pids, char ***names)
-{
-	struct kshark_context *kshark_ctx = NULL;
-	const char *comm;
-	ssize_t i, n;
-	int ret;
-
-	if (!kshark_instance(&kshark_ctx))
-		return 0;
-
-	n = kshark_get_task_pids(kshark_ctx, pids);
-	if (n == 0)
-		return 0;
-
-	qsort(*pids, n, sizeof(**pids), compare);
-
-	*names = calloc(n, sizeof(char*));
-	if (!(*names))
-		goto fail;
-
-	for (i = 0; i < n; ++i) {
-		comm = tep_data_comm_from_pid(kshark_ctx->pevent, (*pids)[i]);
-		ret = asprintf(&(*names)[i], "%s", comm);
-		if (ret < 1)
-			goto fail;
-	}
-
-	return n;
-
-  fail:
-	free(*pids);
-	free(*names);
-	return 0;
-}
-
-size_t kspy_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;
-	size_t total = 0;
-
-	if (!kshark_instance(&kshark_ctx))
-		return false;
-
-	total = kshark_load_data_matrix(kshark_ctx, offset_array,
-					cpu_array,
-					ts_array,
-					pid_array,
-					event_array);
-
-	return total;
-}
-
-int kspy_get_event_id(const char *sys, const char *evt)
-{
-	struct kshark_context *kshark_ctx = NULL;
-	struct tep_event *event;
-
-	if (!kshark_instance(&kshark_ctx))
-		return -1;
-
-	event = tep_find_event_by_name(kshark_ctx->pevent, sys, evt);
-
-	return event->id;
-}
-
-unsigned long long kspy_read_event_field(uint64_t offset,
-					 int id, const char *field)
-{
-	struct kshark_context *kshark_ctx = NULL;
-	struct tep_format_field *evt_field;
-	struct tep_record *record;
-	struct tep_event *event;
-	unsigned long long val;
-	int ret;
-
-	if (!kshark_instance(&kshark_ctx))
-		return 0;
-
-	event = tep_find_event(kshark_ctx->pevent, id);
-	if (!event)
-		return 0;
-
-	evt_field = tep_find_any_field(event, field);
-	if (!evt_field)
-		return 0;
-
-	record = tracecmd_read_at(kshark_ctx->handle, offset, NULL);
-	if (!record)
-		return 0;
-
-	ret = tep_read_number_field(evt_field, record->data, &val);
-	free_record(record);
-
-	if (ret != 0)
-		return 0;
-
-	return val;
-}
-
-const char *kspy_get_function(unsigned long long addr)
-{
-	struct kshark_context *kshark_ctx = NULL;
-
-	if (!kshark_instance(&kshark_ctx))
-		return "";
-
-	return tep_find_function(kshark_ctx->pevent, addr);
-}
-
-void kspy_register_plugin(const char *plugin)
-{
-	struct kshark_context *kshark_ctx = NULL;
-	char *lib_file;
-	int n;
-
-	if (!kshark_instance(&kshark_ctx))
-		return;
-
-	n = asprintf(&lib_file, "%s/plugin-%s.so", KS_PLUGIN_DIR, plugin);
-	if (n > 0) {
-		kshark_register_plugin(kshark_ctx, lib_file);
-		kshark_handle_plugins(kshark_ctx, KSHARK_PLUGIN_INIT);
-		free(lib_file);
-	}
-}
-
-const char *kspy_map_instruction_address(int pid, unsigned long long proc_addr,
-					 unsigned long long *obj_addr)
-{
-	struct kshark_context *kshark_ctx = NULL;
-	struct tracecmd_proc_addr_map *mem_map;
-
-	*obj_addr = 0;
-	if (!kshark_instance(&kshark_ctx))
-		return "UNKNOWN";
-
-	mem_map = tracecmd_search_task_map(kshark_ctx->handle,
-					   pid, proc_addr);
-
-	if (!mem_map)
-		return "UNKNOWN";
-
-	*obj_addr = proc_addr - mem_map->start;
-
-	return mem_map->lib_name;
-}
-
-void kspy_new_session_file(const char *data_file, const char *session_file)
-{
-	struct kshark_context *kshark_ctx = NULL;
-	struct kshark_trace_histo histo;
-	struct kshark_config_doc *session;
-	struct kshark_config_doc *filters;
-	struct kshark_config_doc *markers;
-	struct kshark_config_doc *model;
-	struct kshark_config_doc *file;
-
-	if (!kshark_instance(&kshark_ctx))
-		return;
-
-	session = kshark_config_new("kshark.config.session",
-				    KS_CONFIG_JSON);
-
-	file = kshark_export_trace_file(data_file, KS_CONFIG_JSON);
-	kshark_config_doc_add(session, "Data", file);
-
-	filters = kshark_export_all_filters(kshark_ctx, KS_CONFIG_JSON);
-	kshark_config_doc_add(session, "Filters", filters);
-
-	ksmodel_init(&histo);
-	model = kshark_export_model(&histo, KS_CONFIG_JSON);
-	kshark_config_doc_add(session, "Model", model);
-
-	markers = kshark_config_new("kshark.config.markers", KS_CONFIG_JSON);
-	kshark_config_doc_add(session, "Markers", markers);
-
-	kshark_save_config_file(session_file, session);
-	kshark_free_config_doc(session);
-}
diff --git a/libkshark_wrapper.pyx b/libkshark_wrapper.pyx
deleted file mode 100644
index 1b75685..0000000
--- a/libkshark_wrapper.pyx
+++ /dev/null
@@ -1,361 +0,0 @@
-"""
-SPDX-License-Identifier: LGPL-2.1
-
-Copyright 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
-"""
-
-import ctypes
-
-# Import the Python-level symbols of numpy
-import numpy as np
-# Import the C-level symbols of numpy
-cimport numpy as np
-
-import json
-
-from libcpp cimport bool
-
-from libc.stdlib cimport free
-
-from cpython cimport PyObject, Py_INCREF
-
-
-cdef extern from 'stdint.h':
-    ctypedef unsigned short uint8_t
-    ctypedef unsigned short uint16_t
-    ctypedef unsigned long long uint64_t
-
-cdef extern from 'numpy/ndarraytypes.h':
-    int NPY_ARRAY_CARRAY
-
-# Declare all C functions we are going to call
-cdef extern from 'libkshark-py.c':
-    bool kspy_open(const char *fname)
-
-cdef extern from 'libkshark-py.c':
-    bool kspy_close()
-
-cdef extern from 'libkshark-py.c':
-    size_t kspy_trace2matrix(uint64_t **offset_array,
-                             uint8_t **cpu_array,
-                             uint64_t **ts_array,
-                             uint16_t **pid_array,
-                             int **event_array)
-
-cdef extern from 'libkshark-py.c':
-    int kspy_get_event_id(const char *sys, const char *evt)
-
-cdef extern from 'libkshark-py.c':
-    uint64_t kspy_read_event_field(uint64_t offset,
-                                   int event_id,
-                                   const char *field)
-
-cdef extern from 'libkshark-py.c':
-    ssize_t kspy_get_tasks(int **pids, char ***names)
-
-cdef extern from 'libkshark-py.c':
-    const char *kspy_get_function(unsigned long long addr)
-
-cdef extern from 'libkshark-py.c':
-    void kspy_register_plugin(const char *file)
-
-cdef extern from 'libkshark-py.c':
-    const char *kspy_map_instruction_address(int pid,
-					     unsigned long long proc_addr,
-					     unsigned long long *obj_addr)
-
-cdef extern from 'kernelshark/libkshark.h':
-    int KS_EVENT_OVERFLOW
-
-cdef extern from 'libkshark-py.c':
-    void kspy_new_session_file(const char *data_file,
-                               const char *session_file)
-
-EVENT_OVERFLOW = KS_EVENT_OVERFLOW
-
-# Numpy must be initialized!!!
-np.import_array()
-
-
-cdef class KsDataWrapper:
-    cdef int item_size
-    cdef int data_size
-    cdef int data_type
-    cdef void* data_ptr
-
-    cdef init(self,
-              int data_type,
-              int data_size,
-              int item_size,
-              void* data_ptr):
-        """ This initialization cannot be done in the constructor because
-            we use C-level arguments.
-        """
-        self.item_size = item_size
-        self.data_size = data_size
-        self.data_type = data_type
-        self.data_ptr = data_ptr
-
-    def __array__(self):
-        """ Here we use the __array__ method, that is called when numpy
-            tries to get an array from the object.
-        """
-        cdef np.npy_intp shape[1]
-        shape[0] = <np.npy_intp> self.data_size
-
-        ndarray = np.PyArray_New(np.ndarray,
-                                 1, shape,
-                                 self.data_type,
-                                 NULL,
-                                 self.data_ptr,
-                                 self.item_size,
-                                 NPY_ARRAY_CARRAY,
-                                 <object>NULL)
-
-        return ndarray
-
-    def __dealloc__(self):
-        """ Free the data. This is called by Python when all the references to
-            the object are gone.
-        """
-        free(<void*>self.data_ptr)
-
-
-def c_str2py(char *c_str):
-    """ String convertion C -> Python
-    """
-    return ctypes.c_char_p(c_str).value.decode('utf-8')
-
-
-def py_str2c(py_str):
-    """ String convertion Python -> C
-    """
-    return py_str.encode('utf-8')
-
-
-def open_file(fname):
-    """ Open a tracing data file.
-    """
-    return kspy_open(py_str2c(fname))
-
-
-def close():
-    """ Open the session file.
-    """
-    kspy_close()
-
-
-def read_event_field(offset, event_id, field):
-    """ Read the value of a specific field of the trace event.
-    """
-    cdef uint64_t v
-
-    v = kspy_read_event_field(offset, event_id, py_str2c(field))
-    return v
-
-
-def event_id(system, event):
-    """ Get the unique Id of the event
-    """
-    return kspy_get_event_id(py_str2c(system), py_str2c(event))
-
-
-def get_tasks():
-    """ Get a dictionary of all task's PIDs
-    """
-    cdef int *pids
-    cdef char **names
-    cdef int size = kspy_get_tasks(&pids, &names)
-
-    task_dict = {}
-
-    for i in range(0, size):
-        name = c_str2py(names[i])
-        pid_list = task_dict.get(name)
-
-        if pid_list is None:
-            pid_list = []
-
-        pid_list.append(pids[i])
-        task_dict.update({name : pid_list})
-
-    return task_dict
-
-def get_function(ip):
-    """ Get the name of the function from its ip
-    """
-    func = kspy_get_function(ip)
-    if func:
-        return c_str2py(kspy_get_function(ip))
-
-    return str("0x%x" %ip)
-
-def register_plugin(plugin):
-    """
-    """
-    kspy_register_plugin(py_str2c(plugin))
-
-def map_instruction_address(pid, address):
-    """
-    """
-    cdef unsigned long long obj_addr;
-    cdef const char* obj_file;
-    obj_file = kspy_map_instruction_address(pid, address, &obj_addr)
-
-    return {'obj_file' : c_str2py(obj_file), 'address' : obj_addr}
-
-def load_data(ofst_data=True, cpu_data=True,
-	      ts_data=True, pid_data=True,
-	      evt_data=True):
-    """ Python binding of the 'kshark_load_data_matrix' function that does not
-        copy the data. The input parameters can be used to avoid loading the
-        data from the unnecessary fields.
-    """
-    cdef uint64_t *ofst_c
-    cdef uint16_t *cpu_c
-    cdef uint64_t *ts_c
-    cdef uint16_t *pid_c
-    cdef int *evt_c
-
-    cdef np.ndarray ofst
-    cdef np.ndarray cpu
-    cdef np.ndarray ts
-    cdef np.ndarray pid
-    cdef np.ndarray evt
-
-    if not ofst_data:
-        ofst_c = NULL
-
-    if not cpu_data:
-        cpu_c = NULL
-
-    if not ts_data:
-        ts_c = NULL
-
-    if not pid_data:
-        pid_c = NULL
-
-    if not evt_data:
-        evt_c = NULL
-
-    data_dict = {}
-
-    # Call the C function
-    size = kspy_trace2matrix(&ofst_c, &cpu_c, &ts_c, &pid_c, &evt_c)
-
-    if ofst_data:
-        array_wrapper_ofst = KsDataWrapper()
-        array_wrapper_ofst.init(data_type=np.NPY_UINT64,
-                                item_size=0,
-                                data_size=size,
-                                data_ptr=<void *> ofst_c)
-
-
-        ofst = np.array(array_wrapper_ofst, copy=False)
-        ofst.base = <PyObject *> array_wrapper_ofst
-        data_dict.update({'offset': ofst})
-        Py_INCREF(array_wrapper_ofst)
-
-    if cpu_data:
-        array_wrapper_cpu = KsDataWrapper()
-        array_wrapper_cpu.init(data_type=np.NPY_UINT16,
-                               data_size=size,
-                               item_size=0,
-                               data_ptr=<void *> cpu_c)
-
-        cpu = np.array(array_wrapper_cpu, copy=False)
-        cpu.base = <PyObject *> array_wrapper_cpu
-        data_dict.update({'cpu': cpu})
-        Py_INCREF(array_wrapper_cpu)
-
-    if ts_data:
-        array_wrapper_ts = KsDataWrapper()
-        array_wrapper_ts.init(data_type=np.NPY_UINT64,
-                              data_size=size,
-                              item_size=0,
-                              data_ptr=<void *> ts_c)
-
-        ts = np.array(array_wrapper_ts, copy=False)
-        ts.base = <PyObject *> array_wrapper_ts
-        data_dict.update({'time': ts})
-        Py_INCREF(array_wrapper_ts)
-
-    if pid_data:
-        array_wrapper_pid = KsDataWrapper()
-        array_wrapper_pid.init(data_type=np.NPY_UINT16,
-                               data_size=size,
-                               item_size=0,
-                               data_ptr=<void *>pid_c)
-
-        pid = np.array(array_wrapper_pid, copy=False)
-        pid.base = <PyObject *> array_wrapper_pid
-        data_dict.update({'pid': pid})
-        Py_INCREF(array_wrapper_pid)
-
-    if evt_data:
-        array_wrapper_evt = KsDataWrapper()
-        array_wrapper_evt.init(data_type=np.NPY_INT,
-                               data_size=size,
-                               item_size=0,
-                               data_ptr=<void *>evt_c)
-
-        evt = np.array(array_wrapper_evt, copy=False)
-        evt.base = <PyObject *> array_wrapper_evt
-        data_dict.update({'event': evt})
-        Py_INCREF(array_wrapper_evt)
-
-    return data_dict
-
-def data_size(data):
-    """ Get the number of trace records.
-    """
-    if data['offset'] is not None:
-        return data['offset'].size
-
-    if data['cpu'] is not None:
-        return data['cpu'].size
-
-    if data['time'] is not None:
-        return data['time'].size
-
-    if data['pid'] is not None:
-        return data['pid'].size
-
-    if data['event'] is not None:
-        return data['event'].size
-
-    return 0
-
-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_session(fname, sname):
-    """ Generate and save a default KernelShark session description
-        file (JSON).
-    """
-    kspy_new_session_file(py_str2c(fname), py_str2c(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)
diff --git a/np_setup.py b/np_setup.py
deleted file mode 100755
index 40bb6fc..0000000
--- a/np_setup.py
+++ /dev/null
@@ -1,90 +0,0 @@
-#!/usr/bin/env python3
-
-"""
-SPDX-License-Identifier: LGPL-2.1
-
-Copyright 2019 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
-"""
-
-import sys
-import getopt
-
-from Cython.Distutils import build_ext
-from numpy.distutils.misc_util import Configuration
-from numpy.distutils.core import setup
-
-def lib_dirs(argv):
-    """ Function used to retrieve the library paths.
-    """
-    kslibdir = ''
-    evlibdir = ''
-    trlibdir = ''
-
-    try:
-        opts, args = getopt.getopt(
-            argv, 'k:t:e:', ['kslibdir=',
-                             'trlibdir=',
-                             'evlibdir='])
-
-    except getopt.GetoptError:
-        sys.exit(2)
-
-    for opt, arg in opts:
-        if opt in ('-k', '--kslibdir'):
-            kslibdir = arg
-        elif opt in ('-t', '--trlibdir'):
-            trlibdir = arg
-        elif opt in ('-e', '--evlibdir'):
-            evlibdir = arg
-
-    cmd1 = 1
-    for i in range(len(sys.argv)):
-        if sys.argv[i] == 'build_ext':
-            cmd1 = i
-
-    sys.argv = sys.argv[:1] + sys.argv[cmd1:]
-
-    if kslibdir == '':
-        kslibdir = '/usr/local/lib/kernelshark'
-
-    if evlibdir == '':
-        evlibdir = '/usr/local/lib/traceevent'
-
-    if trlibdir == '':
-        trlibdir = '/usr/local/lib/trace-cmd/'
-
-    return [kslibdir, evlibdir, trlibdir]
-
-
-def configuration(parent_package='',
-                  top_path=None,
-                  libs=['kshark', 'tracecmd', 'traceevent', 'json-c'],
-                  libdirs=['.']):
-    """ Function used to build configuration.
-    """
-    config = Configuration('', parent_package, top_path)
-    config.add_extension('ksharkpy',
-                         sources=['libkshark_wrapper.pyx'],
-                         libraries=libs,
-                         define_macros=[('KS_PLUGIN_DIR','\"' + libdirs[0] + '/plugins\"')],
-                         library_dirs=libdirs,
-                         depends=['libkshark-py.c'],
-                         include_dirs=libdirs)
-
-    return config
-
-
-def main(argv):
-    # Retrieve third-party libraries.
-    libdirs = lib_dirs(sys.argv[1:])
-
-    # Retrieve the parameters of the configuration.
-    params = configuration(libdirs=libdirs).todict()
-    params['cmdclass'] = dict(build_ext=build_ext)
-
-    ## Building the extension.
-    setup(**params)
-
-
-if __name__ == '__main__':
-    main(sys.argv[1:])
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [PATCH v2 12/12] trace-cruncher: Improve Makefile Provide more robust and better looking build process.
  2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
                   ` (10 preceding siblings ...)
  2020-01-07 17:03 ` [PATCH v2 11/12] trace-cruncher: Remove all leftover files Yordan Karadzhov (VMware)
@ 2020-01-07 17:03 ` Yordan Karadzhov (VMware)
  11 siblings, 0 replies; 13+ messages in thread
From: Yordan Karadzhov (VMware) @ 2020-01-07 17:03 UTC (permalink / raw)
  To: linux-trace-devel
  Cc: rostedt, Douglas.Raillard, Valentin.Schneider, nd,
	Yordan Karadzhov (VMware)

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 Makefile | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index 9f49184..0acd655 100644
--- a/Makefile
+++ b/Makefile
@@ -4,8 +4,17 @@
 # Copyright 2019 VMware Inc, Yordan Karadzhov (VMware) <y.karadz@gmail.com>
 #
 
+UID := $(shell id -u)
+
+CYAN	:= '\e[36m'
+PURPLE	:= '\e[35m'
+NC	:= '\e[0m'
+
 all:
+	@ echo ${CYAN}Installing third party:${NC};
 	./install_third_party.sh
+	@ echo
+	@ echo ${CYAN}Buildinging trace-cruncher:${NC};
 	python3 setup.py build
 
 clean:
@@ -13,7 +22,15 @@ clean:
 	rm -f src/datawrapper.c
 
 install:
+	@ echo ${CYAN}Installing trace-cruncher:${NC};
 	python3 setup.py install --record install_manifest.txt
 
 uninstall:
-	xargs rm -v < install_manifest.txt
+	@ if [ $(UID) -ne 0 ]; then \
+	echo ${PURPLE}Permission denied${NC} 1>&2; \
+	else \
+	echo ${CYAN}Uninstalling trace-cruncher:${NC}; \
+	xargs rm -v < install_manifest.txt; \
+	rm -rfv dist tracecruncher.egg-info; \
+	rm -fv install_manifest.txt; \
+	fi
-- 
2.20.1


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-01-07 17:04 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-07 17:03 [PATCH v2 00/12] Build trace-cruncher as Python pakage Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 01/12] trace-cruncher: Refactor the part of the interface that relies on libkshark Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 02/12] trace-cruncher: Refactor the part of the interface that relies on libtraceevent Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 03/12] trace-cruncher: Refactor NumPy based data wrapper Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 04/12] trace-cruncher: Add "utils" Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 05/12] trace-cruncher: Adapt sched_wakeup.py to use the new module Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 06/12] trace-cruncher: Add Makefile Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 07/12] trace-cruncher: Adapt gpareto_fit.py to use the new module Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 08/12] trace-cruncher: Adapt page_faults.py " Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 09/12] trace-cruncher: Automate the third-party build Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 10/12] trace-cruncher: Update README.md Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 11/12] trace-cruncher: Remove all leftover files Yordan Karadzhov (VMware)
2020-01-07 17:03 ` [PATCH v2 12/12] trace-cruncher: Improve Makefile Provide more robust and better looking build process Yordan Karadzhov (VMware)

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.