diff mbox series

[21/24] kernel-shark: Update MissedEvents plugin

Message ID 20210201172358.175407-22-y.karadz@gmail.com (mailing list archive)
State Superseded
Headers show
Series Complete the KernelShark v2 transformation | expand

Commit Message

Yordan Karadzhov Feb. 1, 2021, 5:23 p.m. UTC
General revision of the MissedEvents plugin that uses
the new generic methods for visualization of trace event.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/plugins/CMakeLists.txt    |   6 +-
 src/plugins/MissedEvents.cpp  | 105 ++++++++++++++--------------------
 src/plugins/missed_events.c   |  25 ++++----
 src/plugins/missed_events.h   |   4 +-
 tests/libkshark-gui-tests.cpp |   4 +-
 5 files changed, 62 insertions(+), 82 deletions(-)
diff mbox series

Patch

diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt
index 108bc5f..dc0c320 100644
--- a/src/plugins/CMakeLists.txt
+++ b/src/plugins/CMakeLists.txt
@@ -22,9 +22,9 @@  BUILD_PLUGIN(NAME sched_events
              SOURCE sched_events.c SchedEvents.cpp)
 list(APPEND PLUGIN_LIST "sched_events")
 
-# BUILD_PLUGIN(NAME missed_events
-#              SOURCE missed_events.c MissedEvents.cpp)
-# list(APPEND PLUGIN_LIST "missed_events")
+BUILD_PLUGIN(NAME missed_events
+             SOURCE missed_events.c MissedEvents.cpp)
+list(APPEND PLUGIN_LIST "missed_events")
 
 install(TARGETS ${PLUGIN_LIST}
         LIBRARY DESTINATION ${KS_PLUGIN_INSTALL_PREFIX}
diff --git a/src/plugins/MissedEvents.cpp b/src/plugins/MissedEvents.cpp
index 05dfcb5..cf0ed34 100644
--- a/src/plugins/MissedEvents.cpp
+++ b/src/plugins/MissedEvents.cpp
@@ -26,12 +26,8 @@  using namespace KsPlot;
  */
 class MissedEventsMark : public PlotObject {
 public:
-	/** Create a default Missed events marker. */
-	MissedEventsMark() : _base(0, 0), _height(0)
-	{
-		_color = {0, 0, 255};
-		_size = 2;
-	}
+	/** No default constructor. */
+	MissedEventsMark() = delete;
 
 	/**
 	 * @brief Create and position a Missed events marker.
@@ -40,17 +36,7 @@  public:
 	 * @param h: vertical size (height) of the marker.
 	 */
 	MissedEventsMark(const Point &p, int h)
-	: _base(p.x(), p.y()), _height(h)
-	{
-		_color = {0, 0, 255};
-		_size = 2;
-	}
-
-	/** Set the Base point of the marker. */
-	void setBase(const Point &p) {_base.set(p.x(), p.y());}
-
-	/** Set the vertical size (height) point of the marker. */
-	void setHeight(int h) {_height = h;}
+	: _base(p.x(), p.y()), _height(h) {}
 
 private:
 	/** Base point of the Mark's line. */
@@ -76,61 +62,38 @@  void MissedEventsMark::_draw(const Color &col, float size) const
 	rec.draw();
 }
 
+static PlotObject *makeShape(std::vector<const Graph *> graph,
+			     std::vector<int> bin,
+			     std::vector<kshark_data_field_int64 *>,
+			     Color col, float size)
+{
+	MissedEventsMark *mark = new MissedEventsMark(graph[0]->bin(bin[0])._base,
+						      graph[0]->height());
+	mark->_size = size;
+	mark->_color = col;
+
+	return mark;
+}
+
 //! @cond Doxygen_Suppress
 
 #define PLUGIN_MAX_ENTRIES		10000
 
-#define KS_TASK_COLLECTION_MARGIN	25
-
 //! @endcond
 
-static void pluginDraw(kshark_context *kshark_ctx,
-		       KsCppArgV *argvCpp,
-		       int val, int draw_action)
-{
-	int height = argvCpp->_graph->getHeight();
-	const kshark_entry *entry(nullptr);
-	MissedEventsMark *mark;
-	ssize_t index;
-
-	int nBins = argvCpp->_graph->size();
-	for (int bin = 0; bin < nBins; ++bin) {
-		if (draw_action == KSHARK_PLUGIN_TASK_DRAW)
-			entry = ksmodel_get_task_missed_events(argvCpp->_histo,
-							       bin, val,
-							       nullptr,
-							       &index);
-		if (draw_action == KSHARK_PLUGIN_CPU_DRAW)
-			entry = ksmodel_get_cpu_missed_events(argvCpp->_histo,
-							      bin, val,
-							      nullptr,
-							      &index);
-
-		if (entry) {
-			mark = new MissedEventsMark(argvCpp->_graph->getBin(bin)._base,
-						    height);
-
-			argvCpp->_shapes->push_front(mark);
-		}
-	}
-}
-
 /**
  * @brief Plugin's draw function.
  *
  * @param argv_c: A C pointer to be converted to KsCppArgV (C++ struct).
+ * @param sd: Data stream identifier.
  * @param val: Process or CPU Id value.
  * @param draw_action: Draw action identifier.
  */
 void draw_missed_events(kshark_cpp_argv *argv_c,
-			int val, int draw_action)
+			int sd, int val, int draw_action)
 {
-	kshark_context *kshark_ctx(NULL);
-
-	if (!kshark_instance(&kshark_ctx))
-		return;
-
 	KsCppArgV *argvCpp = KS_ARGV_TO_CPP(argv_c);
+	IsApplicableFunc checkEntry;
 
 	/*
 	 * Plotting the "Missed events" makes sense only in the case of a deep
@@ -141,9 +104,29 @@  void draw_missed_events(kshark_cpp_argv *argv_c,
 	if (argvCpp->_histo->tot_count > PLUGIN_MAX_ENTRIES)
 		return;
 
-	try {
-		pluginDraw(kshark_ctx, argvCpp, val, draw_action);
-	} catch (const std::exception &exc) {
-		std::cerr << "Exception in MissedEvents\n" << exc.what();
-	}
+	if (!(draw_action & KSHARK_CPU_DRAW) &&
+	    !(draw_action & KSHARK_TASK_DRAW))
+		return;
+
+	if (draw_action & KSHARK_CPU_DRAW)
+		checkEntry = [=] (kshark_data_container *, ssize_t bin) {
+			return !!ksmodel_get_cpu_missed_events(argvCpp->_histo,
+							       bin, sd, val,
+							       nullptr,
+							       nullptr);
+		};
+
+	else if (draw_action & KSHARK_TASK_DRAW)
+		checkEntry = [=] (kshark_data_container *, ssize_t bin) {
+			return !!ksmodel_get_task_missed_events(argvCpp->_histo,
+								bin, sd, val,
+								nullptr,
+								nullptr);
+		};
+
+	eventPlot(argvCpp,
+		  checkEntry,
+		  makeShape,
+		  {0, 0, 255}, // Blue
+		  -1);         // Default size
 }
diff --git a/src/plugins/missed_events.c b/src/plugins/missed_events.c
index cf652bf..8fe9780 100644
--- a/src/plugins/missed_events.c
+++ b/src/plugins/missed_events.c
@@ -10,32 +10,27 @@ 
  *	     ring buffer.
  */
 
+// C
+#include <stdio.h>
+
 // KernelShark
+#include "libkshark.h"
 #include "plugins/missed_events.h"
 
-static void nop_action(struct kshark_context *kshark_ctx,
-				struct tep_record *record,
-				struct kshark_entry *entry)
-{}
-
 /** Load this plugin. */
-int KSHARK_PLUGIN_INITIALIZER(struct kshark_context *kshark_ctx)
+int KSHARK_PLOT_PLUGIN_INITIALIZER(struct kshark_data_stream *stream)
 {
-	kshark_register_event_handler(&kshark_ctx->event_handlers,
-				      KS_EVENT_OVERFLOW,
-				      nop_action,
-				      draw_missed_events);
+	printf("--> missed_events init %i\n", stream->stream_id);
+	kshark_register_draw_handler(stream, draw_missed_events);
 
 	return 1;
 }
 
 /** Unload this plugin. */
-int KSHARK_PLUGIN_DEINITIALIZER(struct kshark_context *kshark_ctx)
+int KSHARK_PLOT_PLUGIN_DEINITIALIZER(struct kshark_data_stream *stream)
 {
-	kshark_unregister_event_handler(&kshark_ctx->event_handlers,
-					KS_EVENT_OVERFLOW,
-					nop_action,
-					draw_missed_events);
+	printf("<-- missed_events close %i\n", stream->stream_id);
+	kshark_unregister_draw_handler(stream, draw_missed_events);
 
 	return 1;
 }
diff --git a/src/plugins/missed_events.h b/src/plugins/missed_events.h
index e05d79a..10bc549 100644
--- a/src/plugins/missed_events.h
+++ b/src/plugins/missed_events.h
@@ -14,14 +14,14 @@ 
 #define _KS_PLUGIN_M_EVTS_H
 
 // KernelShark
-#include "libkshark.h"
+#include "libkshark-plugin.h"
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 void draw_missed_events(struct kshark_cpp_argv *argv,
-			int pid, int draw_action);
+			int sd, int pid, int draw_action);
 
 #ifdef __cplusplus
 }
diff --git a/tests/libkshark-gui-tests.cpp b/tests/libkshark-gui-tests.cpp
index dedd69f..5a0ca01 100644
--- a/tests/libkshark-gui-tests.cpp
+++ b/tests/libkshark-gui-tests.cpp
@@ -146,7 +146,9 @@  BOOST_AUTO_TEST_CASE(KsUtils_KsDataStore)
 
 BOOST_AUTO_TEST_CASE(KsUtils_getPluginList)
 {
-	QStringList plugins{"sched_events"};
+	QStringList plugins{"sched_events",
+			    "missed_events"
+	};
 
 	BOOST_CHECK(getPluginList() == plugins);
 }