For Xenomai-cobalt enabled system, cobalt_switch_context means that there
is context switch in companion core(realtime core), which we may need
to do special treatment and take correct action as main kernel sched_switch.
We need to update cpu bar regarding cobalt_switch_context event to correct
color as switching-in task.

Signed-off-by: Hongzhan Chen <[email protected]>
---
 src/libkshark-tepdata.c                    |   1 +
 src/plugins/CMakeLists.txt                 |   4 +
 src/plugins/xenomai_cobalt_switch_events.c | 116 +++++++++++++++++++++
 src/plugins/xenomai_cobalt_switch_events.h |  45 ++++++++
 4 files changed, 166 insertions(+)
 create mode 100644 src/plugins/xenomai_cobalt_switch_events.c
 create mode 100644 src/plugins/xenomai_cobalt_switch_events.h

diff --git a/src/libkshark-tepdata.c b/src/libkshark-tepdata.c
index 9740ed9..e933b39 100644
--- a/src/libkshark-tepdata.c
+++ b/src/libkshark-tepdata.c
@@ -1208,6 +1208,7 @@ static void kshark_tep_init_methods(struct 
kshark_generic_stream_interface *inte
 /** A list of built in default plugins for FTRACE (trace-cmd) data. */
 const char *tep_plugin_names[] = {
        "sched_events",
+       "xenomai_cobalt_switch_events",
        "missed_events",
        "kvm_combo",
 };
diff --git a/src/plugins/CMakeLists.txt b/src/plugins/CMakeLists.txt
index 3e170fa..276eaaa 100644
--- a/src/plugins/CMakeLists.txt
+++ b/src/plugins/CMakeLists.txt
@@ -44,6 +44,10 @@ if (Qt5Widgets_FOUND AND TT_FONT_FILE)
                      SOURCE sched_events.c SchedEvents.cpp)
     list(APPEND PLUGIN_LIST "sched_events")
 
+    BUILD_GUI_PLUGIN(NAME xenomai_cobalt_switch_events
+                     SOURCE xenomai_cobalt_switch_events.c)
+    list(APPEND PLUGIN_LIST "xenomai_cobalt_switch_events")
+
     BUILD_GUI_PLUGIN(NAME event_field_plot
                      MOC EventFieldDialog.hpp
                      SOURCE event_field_plot.c EventFieldDialog.cpp 
EventFieldPlot.cpp)
diff --git a/src/plugins/xenomai_cobalt_switch_events.c 
b/src/plugins/xenomai_cobalt_switch_events.c
new file mode 100644
index 0000000..4639d24
--- /dev/null
+++ b/src/plugins/xenomai_cobalt_switch_events.c
@@ -0,0 +1,116 @@
+// SPDX-License-Identifier: LGPL-2.1
+
+/*
+ * Copyright (C) 2021 Intel Inc, Hongzhan Chen <[email protected]>
+ */
+
+/**
+ *  @file    xenomai_cobalt_switch_events.c
+ *  @brief   handle xenomai cobalt switch context event
+ */
+
+// C
+#include <stdlib.h>
+#include <stdio.h>
+
+// trace-cmd
+#include <trace-cmd.h>
+
+// KernelShark
+#include "plugins/xenomai_cobalt_switch_events.h"
+#include "libkshark-tepdata.h"
+
+/** Plugin context instance. */
+
+static void cobalt_free_context(struct plugin_cobalt_context *plugin_ctx)
+{
+       return;
+}
+
+/** A general purpose macro is used to define plugin context. */
+KS_DEFINE_PLUGIN_CONTEXT(struct plugin_cobalt_context, cobalt_free_context);
+
+static bool plugin_cobalt_init_context(struct kshark_data_stream *stream,
+                                     struct plugin_cobalt_context *plugin_ctx)
+{
+       struct tep_event *event;
+
+       if (!kshark_is_tep(stream))
+               return false;
+
+       plugin_ctx->tep = kshark_get_tep(stream);
+
+       event = tep_find_event_by_name(plugin_ctx->tep,
+                                       "cobalt_core", "cobalt_switch_context");
+       if (!event)
+               return false;
+
+       plugin_ctx->cobalt_switch_event = event;
+       plugin_ctx->cobalt_switch_next_field =
+               tep_find_any_field(event, "next_pid");
+
+       plugin_ctx->cobalt_switch_prev_state_field =
+               tep_find_field(event, "prev_state");
+
+       return true;
+}
+
+static void plugin_cobalt_switch_action(struct kshark_data_stream *stream,
+                                     void *rec, struct kshark_entry *entry)
+{
+       struct tep_record *record = (struct tep_record *) rec;
+       struct plugin_cobalt_context *plugin_ctx;
+       unsigned long long next_pid;
+       int ret;
+
+       plugin_ctx = __get_context(stream->stream_id);
+       if (!plugin_ctx)
+               return;
+
+       ret = tep_read_number_field(plugin_ctx->cobalt_switch_next_field,
+                                   record->data, &next_pid);
+
+       if (ret == 0 && next_pid >= 0)
+               entry->pid = next_pid;
+}
+
+/** Load this plugin. */
+int KSHARK_PLOT_PLUGIN_INITIALIZER(struct kshark_data_stream *stream)
+{
+       struct plugin_cobalt_context *plugin_ctx;
+
+       plugin_ctx = __init(stream->stream_id);
+       if (!plugin_ctx || !plugin_cobalt_init_context(stream, plugin_ctx)) {
+               __close(stream->stream_id);
+               return 0;
+       }
+
+       if (plugin_ctx->cobalt_switch_event) {
+               kshark_register_event_handler(stream,
+                                               
plugin_ctx->cobalt_switch_event->id,
+                                               plugin_cobalt_switch_action);
+       }
+
+       return 1;
+}
+
+/** Unload this plugin. */
+int KSHARK_PLOT_PLUGIN_DEINITIALIZER(struct kshark_data_stream *stream)
+{
+       struct plugin_cobalt_context *plugin_ctx = 
__get_context(stream->stream_id);
+       int ret = 0;
+
+       if (plugin_ctx) {
+               if (plugin_ctx->cobalt_switch_event) {
+                       kshark_unregister_event_handler(stream,
+                                                       
plugin_ctx->cobalt_switch_event->id,
+                                                       
plugin_cobalt_switch_action);
+               }
+
+               ret = 1;
+       }
+
+       __close(stream->stream_id);
+
+       return ret;
+}
diff --git a/src/plugins/xenomai_cobalt_switch_events.h 
b/src/plugins/xenomai_cobalt_switch_events.h
new file mode 100644
index 0000000..4a3bc66
--- /dev/null
+++ b/src/plugins/xenomai_cobalt_switch_events.h
@@ -0,0 +1,45 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+
+/*
+ * Copyright (C) 2021 Intel Inc, Hongzhan Chen<[email protected]>
+ */
+
+/**
+ *  @file    xenomai_cobalt_switch_events.h
+ *  @brief   Plugin for xenomai cobalt switch context event
+ */
+
+#ifndef _KS_PLUGIN_SHED_H
+#define _KS_PLUGIN_SHED_H
+
+// KernelShark
+#include "libkshark.h"
+#include "libkshark-plugin.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/** Structure representing a plugin-specific context. */
+struct plugin_cobalt_context {
+       /** Page event used to parse the page. */
+       struct tep_handle       *tep;
+
+       /** Pointer to the cobalt_switch_event object. */
+       struct tep_event        *cobalt_switch_event;
+
+        /** Pointer to the cobalt_switch_next_field format descriptor. */
+       struct tep_format_field *cobalt_switch_next_field;
+
+       /** Pointer to the cobalt_switch_prev_state_field format descriptor. */
+       struct tep_format_field *cobalt_switch_prev_state_field;
+
+};
+
+KS_DECLARE_PLUGIN_CONTEXT_METHODS(struct plugin_cobalt_context)
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
-- 
2.17.1


Reply via email to