Skip to contentSkip to conversation

Add infrastructure for plugins.

PATCH Superseded Yordan Karadzhov (VMware) 1 participant 8 messages Sep 17, 2018

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add infrastructure for plugins.

The infrastructure for plugins used by the Qt-based version of KernelShark is introduced in this series of patches. This is the last major component of the C API. The last patch adds a plugin for dealing with "sched" events.

This is the version 3 of this series of patches. The major change from v2 is the correction of the return type of the LOADER/RELOADER/UNLOADER functions in src/plugins/sched_events.c [PATCH v3 7/7].

Yordan Karadzhov (VMware) (7):
kernel-shark-qt: Add plugin infrastructure to be used by the Qt-baset

    KS.

kernel-shark-qt: Add Plugin event handlers to session.
kernel-shark-qt: Add C++/C conversion for args of a plugin draw
function.
kernel-shark-qt: Make kshark_read_at() non-static.
kernel-shark-qt: Add src/plugins dir. to hold the source code of the
plugins
kernel-shark-qt: Tell Doxygen to enter ../src/plugins/
kernel-shark-qt: Add a plugin for sched events.

 kernel-shark-qt/doc/dox_config              |   2 +-
 kernel-shark-qt/src/CMakeLists.txt          |   3 +
 kernel-shark-qt/src/KsPlugins.hpp           |  51 ++++
 kernel-shark-qt/src/libkshark-plugin.c      | 297 +++++++++++++++++++
 kernel-shark-qt/src/libkshark-plugin.h      | 171 +++++++++++
 kernel-shark-qt/src/libkshark.c             |  33 ++-
 kernel-shark-qt/src/libkshark.h             |  18 ++
 kernel-shark-qt/src/plugins/CMakeLists.txt  |  27 ++
 kernel-shark-qt/src/plugins/SchedEvents.cpp | 263 +++++++++++++++++
 kernel-shark-qt/src/plugins/sched_events.c  | 306 ++++++++++++++++++++
 kernel-shark-qt/src/plugins/sched_events.h  |  76 +++++
 11 files changed, 1244 insertions(+), 3 deletions(-)
 create mode 100644 kernel-shark-qt/src/KsPlugins.hpp
 create mode 100644 kernel-shark-qt/src/libkshark-plugin.c
 create mode 100644 kernel-shark-qt/src/libkshark-plugin.h
 create mode 100644 kernel-shark-qt/src/plugins/CMakeLists.txt
 create mode 100644 kernel-shark-qt/src/plugins/SchedEvents.cpp
 create mode 100644 kernel-shark-qt/src/plugins/sched_events.c
 create mode 100644 kernel-shark-qt/src/plugins/sched_events.h
Signature · 1 line
2.17.1
#1 lore raw

PATCH v3 1/7 kernel-shark-qt: Add plugin infrastructure to be used by the Qt-baset KS.

This patch adds infrastructure for loading/unloading of plugins. Each plugin is coupled to a specific type of trace event and is allowed to perform two types of actions.

The first action is executed once for each kshark_entry only at the time when the data is loaded. This action can modify (even completely rewrite) the content of the kshark_entrys generated from the trace events having the type of the plugin.

The second action can add graphical elements on top of the existing graphs generated by the Visualization model. This will become clear in the following patches. This action is executed once for each graph and this is happening every time when the Visualization model changes its state. It is very powerful and can be used not only for drawing, but also for performing arbitrary modifications on the data. The pointer to the model descriptor object can access the entire array of kshark_entries, which, in turn, could be used to modify those entries.

The plugin infrastructure of the Qt-baset KernelShark reuses the system of macros for loading/unloading, implemented for the original GTK version.

---
 kernel-shark-qt/src/CMakeLists.txt     |   1 +
 kernel-shark-qt/src/libkshark-plugin.c | 297 +++++++++++++++++++++++++
 kernel-shark-qt/src/libkshark-plugin.h | 171 ++++++++++++++
 kernel-shark-qt/src/libkshark.h        |   3 +
 4 files changed, 472 insertions(+)
 create mode 100644 kernel-shark-qt/src/libkshark-plugin.c
 create mode 100644 kernel-shark-qt/src/libkshark-plugin.h
kernel-shark-qt/src/CMakeLists.txt +1 -0
index ac2847a..cdc28c4 100644
@@ -3,6 +3,7 @@ message("\n src ...")
 message(STATUS "libkshark")
 add_library(kshark SHARED libkshark.c
                           libkshark-model.c
+                          libkshark-plugin.c
                           libkshark-configio.c
                           libkshark-collection.c)
 
kernel-shark-qt/src/libkshark-plugin.c new file +297 -0

297 lines not on this page · raw message

kernel-shark-qt/src/libkshark-plugin.h new file +171 -0

171 lines not on this page · raw message

kernel-shark-qt/src/libkshark.h +3 -0
index 2580449..fda133c 100644
@@ -121,6 +121,9 @@ struct kshark_context {
 
 	/** List of Data collections. */
 	struct kshark_entry_collection *collections;
+
+	/** List of Plugins. */
+	struct kshark_plugin_list	*plugins;
 };
 
 bool kshark_instance(struct kshark_context **kshark_ctx);
Signature · 1 line
2.17.1
#2 lore raw

PATCH v3 2/7 kernel-shark-qt: Add Plugin event handlers to session.

In reply to Yordan Karadzhov (VMware)

Plugin event handlers are added to the Session context descriptor. The handlers are used to execute plugin-specific action (callback function) during the processing of the trace data.

---
 kernel-shark-qt/src/libkshark.c | 21 +++++++++++++++++++++
 kernel-shark-qt/src/libkshark.h | 12 ++++++++++++
 2 files changed, 33 insertions(+)
kernel-shark-qt/src/libkshark.c +21 -0
index b4a76ae..eef0236 100644
@@ -33,6 +33,9 @@ static bool kshark_default_context(struct kshark_context **context)
 	if (!kshark_ctx)
 		return false;
 
+	kshark_ctx->event_handlers = NULL;
+	kshark_ctx->plugins = NULL;
+
 	kshark_ctx->show_task_filter = tracecmd_filter_id_hash_alloc();
 	kshark_ctx->hide_task_filter = tracecmd_filter_id_hash_alloc();
 
@@ -217,6 +220,12 @@ void kshark_free(struct kshark_context *kshark_ctx)
 	tracecmd_filter_id_hash_free(kshark_ctx->show_event_filter);
 	tracecmd_filter_id_hash_free(kshark_ctx->hide_event_filter);
 
+	if(kshark_ctx->plugins) {
+		kshark_handle_plugins(kshark_ctx, KSHARK_PLUGIN_UNLOAD);
+		kshark_free_plugin_list(kshark_ctx->plugins);
+		kshark_free_event_handler_list(kshark_ctx->event_handlers);
+	}
+
 	kshark_free_task_list(kshark_ctx);
 
 	if (seq.buffer)
@@ -564,6 +573,7 @@ static void free_rec_list(struct rec_list **rec_list, int n_cpus,
 static size_t get_records(struct kshark_context *kshark_ctx,
 			  struct rec_list ***rec_list, enum rec_type type)
 {
+	struct kshark_event_handler *evt_handler;
 	struct event_filter *adv_filter;
 	struct kshark_task_list *task;
 	struct tep_record *rec;
@@ -608,6 +618,17 @@ static size_t get_records(struct kshark_context *kshark_ctx,
 
 				entry = &temp_rec->entry;
 				kshark_set_entry_values(kshark_ctx, rec, entry);
+
+				/* Execute all plugin-provided actions (if any). */
+				evt_handler = kshark_ctx->event_handlers;
+				while ((evt_handler = find_event_handler(evt_handler,
+									 entry->event_id))) {
+					evt_handler->event_func(kshark_ctx, rec, entry);
+
+					if ((evt_handler = evt_handler->next))
+						entry->visible &= ~KS_PLUGIN_UNTOUCHED_MASK;
+				}
+
 				pid = entry->pid;
 				/* Apply event filtering. */
 				ret = FILTER_MATCH;
kernel-shark-qt/src/libkshark.h +12 -0
index fda133c..203c812 100644
@@ -29,6 +29,9 @@ extern "C" {
 #include "event-parse.h"
 #include "trace-filter-hash.h"
 
+// KernelShark
+#include "libkshark-plugin.h"
+
 /**
  * Kernel Shark entry contains all information from one trace record needed
  * in order to  visualize the time-series of trace records. The part of the
@@ -124,6 +127,9 @@ struct kshark_context {
 
 	/** List of Plugins. */
 	struct kshark_plugin_list	*plugins;
+
+	/** List of Plugin Event handlers. */
+	struct kshark_event_handler	*event_handlers;
 };
 
 bool kshark_instance(struct kshark_context **kshark_ctx);
@@ -160,6 +166,12 @@ enum kshark_filter_masks {
 
 	/** Special mask used whene filtering events. */
 	KS_EVENT_VIEW_FILTER_MASK	= 1 << 2,
+
+	/**
+	 * Use this mask to check if the content of the entry has been accessed
+	 * by a plugin-defined function.
+	 */
+	KS_PLUGIN_UNTOUCHED_MASK	= 1 << 7
 };
 
 /** Filter type identifier. */
Signature · 1 line
2.17.1
#3 lore raw

PATCH v3 3/7 kernel-shark-qt: Add C++/C conversion for args of a plugin draw function.

In reply to Yordan Karadzhov (VMware)

The draw function of a plugin has a type declared in C. However, the function needs to access some high level objects, defined in the C++ libraries. This patch adds instruments for converting the arguments of the plugin's draw function from C++ to C and from C to C++.

---
 kernel-shark-qt/src/KsPlugins.hpp | 51 +++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)
 create mode 100644 kernel-shark-qt/src/KsPlugins.hpp
kernel-shark-qt/src/KsPlugins.hpp new file +51 -0
new file mode 100644
index 0000000..3955cdf
@@ -0,0 +1,51 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+
+/*
+ * Copyright (C) 2018 VMware Inc, Yordan Karadzhov <[email protected]>
+ */
+
+/**
+  *  @file    KsPlugins.hpp
+  *  @brief   KernelShark C++ plugin declarations.
+  */
+
+#ifndef _KS_PLUGINS_H
+#define _KS_PLUGINS_H
+
+// KernelShark
+#include "libkshark-model.h"
+#include "KsPlotTools.hpp"
+
+/**
+ * Structure representing the vector of C++ arguments of the drawing function
+ * of a plugin.
+ */
+struct KsCppArgV {
+	/** Pointer to the model descriptor object. */
+	kshark_trace_histo	*_histo;
+
+	/** Pointer to the graph object. */
+	KsPlot::Graph		*_graph;
+
+	/**
+	 * Pointer to the list of shapes. All shapes created by the plugin
+	 * will be added to this list.
+	 */
+	KsPlot::PlotObjList	*_shapes;
+
+	/**
+	 * Convert the "this" pointer of the C++ argument vector into a
+	 * C pointer.
+	 */
+	kshark_cpp_argv *toC()
+	{
+		return reinterpret_cast<kshark_cpp_argv *>(this);
+	}
+};
+
+/**
+ * Macro used to convert a C pointer into a pointer to KsCppArgV (C++ struct).
+ */
+#define KS_ARGV_TO_CPP(a) (reinterpret_cast<KsCppArgV *>(a))
+
+#endif
Signature · 1 line
2.17.1
#4 lore raw

PATCH v3 4/7 kernel-shark-qt: Make kshark_read_at() non-static.

In reply to Yordan Karadzhov (VMware)

kshark_read_at() function provides a thread-safe read of a record from a specific offset inside the trace.dat file. So far this function was used only in libkshark.c and was defined static. This patch makes the function non-static in order to make possible to use it from a plugin.

---
 kernel-shark-qt/src/libkshark.c | 12 ++++++++++--
 kernel-shark-qt/src/libkshark.h |  3 +++
 2 files changed, 13 insertions(+), 2 deletions(-)
kernel-shark-qt/src/libkshark.c +10 -2
index eef0236..9113852 100644
@@ -830,8 +830,16 @@ ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx,
 	return -ENOMEM;
 }
 
-static struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
-					 uint64_t offset)
+/**
+ * @brief A thread-safe read of a record from a specific offset.
+ *
+ * @param kshark_ctx: Input location for the session context pointer.
+ * @param offset: the offset into the file to find the record.
+ *
+ * @returns The returned pevent_record must be freed.
+ */
+struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
+				  uint64_t offset)
 {
 	/*
 	 * It turns that tracecmd_read_at() is not thread-safe.
kernel-shark-qt/src/libkshark.h +3 -0
index 203c812..ba5ea6c 100644
@@ -150,6 +150,9 @@ void kshark_free(struct kshark_context *kshark_ctx);
 
 char* kshark_dump_entry(const struct kshark_entry *entry);
 
+struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
+				  uint64_t offset);
+
 /** Bit masks used to control the visibility of the entry after filtering. */
 enum kshark_filter_masks {
 	/**
Signature · 1 line
2.17.1
#5 lore raw

PATCH v3 5/7 kernel-shark-qt: Add src/plugins dir. to hold the source code of the plugins

In reply to Yordan Karadzhov (VMware)

Tell Cmake to enter src/plugins. Add a Cmake function for building plugin libraries.

---
 kernel-shark-qt/src/CMakeLists.txt         |  2 ++
 kernel-shark-qt/src/plugins/CMakeLists.txt | 22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+)
 create mode 100644 kernel-shark-qt/src/plugins/CMakeLists.txt
kernel-shark-qt/src/CMakeLists.txt +2 -0
index cdc28c4..3365413 100644
@@ -28,5 +28,7 @@ if (OPENGL_FOUND AND GLUT_FOUND)
 
 endif (OPENGL_FOUND AND GLUT_FOUND)
 
+add_subdirectory(plugins)
+
 configure_file( ${KS_DIR}/build/deff.h.cmake
                 ${KS_DIR}/src/KsDeff.h)
kernel-shark-qt/src/plugins/CMakeLists.txt new file +22 -0
new file mode 100644
index 0000000..565f1cb
@@ -0,0 +1,22 @@
+message("\n src/plugins ...")
+
+function(BUILD_PLUGIN)
+    set(options )
+    set(oneValueArgs NAME)
+    set(multiValueArgs SOURCE)
+    cmake_parse_arguments(ADD_PLUGIN "${options}"
+                                     ${oneValueArgs}
+                                     ${multiValueArgs}
+                                     ${ARGN})
+
+    message(STATUS ${ADD_PLUGIN_NAME})
+
+    add_library(${ADD_PLUGIN_NAME} SHARED ${ADD_PLUGIN_SOURCE})
+    set_target_properties(${ADD_PLUGIN_NAME} PROPERTIES PREFIX "plugin-")
+    target_link_libraries(${ADD_PLUGIN_NAME} kshark)
+
+endfunction()
+
+set(PLUGIN_LIST "")
+
+set(PLUGINS ${PLUGIN_LIST} PARENT_SCOPE)
Signature · 1 line
2.17.1
#6 lore raw

PATCH v3 6/7 kernel-shark-qt: Tell Doxygen to enter ../src/plugins/

In reply to Yordan Karadzhov (VMware)

This Patch adds ../src/plugins/ to the list of input locations used by Doxygen to search for documented source files.

---
 kernel-shark-qt/doc/dox_config | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel-shark-qt/doc/dox_config +1 -1
index b982baa..acc9083 100644
@@ -4,7 +4,7 @@
 DOXYFILE_ENCODING      = UTF-8
 PROJECT_NAME           = "kernel-shark-qt"
 PROJECT_BRIEF = "Kernel Shark is a front-end reader of the Linux kernel tracing data."
-INPUT                  = "../src/"
+INPUT                  = ../src/ ../src/plugins/
 SOURCE_BROWSER         = YES
 QT_AUTOBRIEF           = YES
 TAB_SIZE               = 8
Signature · 1 line
2.17.1
#7 lore raw

PATCH v3 7/7 kernel-shark-qt: Add a plugin for sched events.

In reply to Yordan Karadzhov (VMware)

The plugin is responsible for the following actions, specific for "sched" events:

1. Changes the value of the "pid" field of the "sched_switch" entries. This alows the sched_switch entry to be plotted as part of the "next" task.

2. On "sched_switch" event, the plugin registers the "next" task (if not registered already).

3. Plots in green the wake up latency of the task and in red the time the task was preempted by another task.

---
 kernel-shark-qt/src/plugins/CMakeLists.txt  |   5 +
 kernel-shark-qt/src/plugins/SchedEvents.cpp | 263 +++++++++++++++++
 kernel-shark-qt/src/plugins/sched_events.c  | 306 ++++++++++++++++++++
 kernel-shark-qt/src/plugins/sched_events.h  |  76 +++++
 4 files changed, 650 insertions(+)
 create mode 100644 kernel-shark-qt/src/plugins/SchedEvents.cpp
 create mode 100644 kernel-shark-qt/src/plugins/sched_events.c
 create mode 100644 kernel-shark-qt/src/plugins/sched_events.h
kernel-shark-qt/src/plugins/CMakeLists.txt +5 -0
index 565f1cb..88fd93c 100644
@@ -19,4 +19,9 @@ endfunction()
 
 set(PLUGIN_LIST "")
 
+BUILD_PLUGIN(NAME sched_events
+             SOURCE sched_events.c SchedEvents.cpp)
+list(APPEND PLUGIN_LIST "sched_events default") # This plugin will be loaded by default
+# list(APPEND PLUGIN_LIST "sched_events") # This plugin isn't loaded by default
+
 set(PLUGINS ${PLUGIN_LIST} PARENT_SCOPE)
kernel-shark-qt/src/plugins/SchedEvents.cpp new file +263 -0

263 lines not on this page · raw message

kernel-shark-qt/src/plugins/sched_events.c new file +306 -0

306 lines not on this page · raw message

kernel-shark-qt/src/plugins/sched_events.h new file +76 -0

76 lines not on this page · raw message

Signature · 1 line
2.17.1