From patchwork Thu Sep 20 16:55:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yordan Karadzhov X-Patchwork-Id: 10759349 Return-Path: Received: from mail-wr1-f66.google.com ([209.85.221.66]:36683 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727029AbeITWkh (ORCPT ); Thu, 20 Sep 2018 18:40:37 -0400 Received: by mail-wr1-f66.google.com with SMTP id e1-v6so10147779wrt.3 for ; Thu, 20 Sep 2018 09:56:13 -0700 (PDT) From: "Yordan Karadzhov (VMware)" To: rostedt@goodmis.org Cc: linux-trace-devel@vger.kernel.org, "Yordan Karadzhov (VMware)" Subject: [PATCH v5 1/8] kernel-shark-qt: Add plugin infrastructure to be used by the Qt-baset KS. Date: Thu, 20 Sep 2018 19:55:37 +0300 Message-Id: <20180920165544.17579-2-y.karadz@gmail.com> In-Reply-To: <20180920165544.17579-1-y.karadz@gmail.com> References: <20180920165544.17579-1-y.karadz@gmail.com> Sender: linux-trace-devel-owner@vger.kernel.org List-ID: Content-Length: 14856 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. This version of the patch contains number of improvements suggested by Steven Rostedt. Thanks Steven! Signed-off-by: Yordan Karadzhov (VMware) --- kernel-shark-qt/src/CMakeLists.txt | 1 + kernel-shark-qt/src/libkshark-plugin.c | 290 +++++++++++++++++++++++++ kernel-shark-qt/src/libkshark-plugin.h | 179 +++++++++++++++ kernel-shark-qt/src/libkshark.h | 3 + 4 files changed, 473 insertions(+) create mode 100644 kernel-shark-qt/src/libkshark-plugin.c create mode 100644 kernel-shark-qt/src/libkshark-plugin.h diff --git a/kernel-shark-qt/src/CMakeLists.txt b/kernel-shark-qt/src/CMakeLists.txt index ac2847a..cdc28c4 100644 --- a/kernel-shark-qt/src/CMakeLists.txt +++ b/kernel-shark-qt/src/CMakeLists.txt @@ -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) diff --git a/kernel-shark-qt/src/libkshark-plugin.c b/kernel-shark-qt/src/libkshark-plugin.c new file mode 100644 index 0000000..10e4647 --- /dev/null +++ b/kernel-shark-qt/src/libkshark-plugin.c @@ -0,0 +1,290 @@ +// SPDX-License-Identifier: LGPL-2.1 + +/* + * Copyright (C) 2017 VMware Inc, Yordan Karadzhov + */ + + /** + * @file libkshark-plugin.c + * @brief KernelShark plugins. + */ + +// C +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include + +// KernelShark +#include "libkshark-plugin.h" +#include "libkshark.h" + +static struct kshark_event_handler * +gui_event_handler_alloc(int event_id, + kshark_plugin_event_handler_func evt_func, + kshark_plugin_draw_handler_func dw_func) +{ + struct kshark_event_handler *handler = malloc(sizeof(*handler)); + + if (!handler) { + fprintf(stderr, + "failed to allocate memory for gui eventhandler"); + return NULL; + } + + handler->next = NULL; + handler->id = event_id; + handler->event_func = evt_func; + handler->draw_func = dw_func; + + return handler; +} + +/** + * @brief Search the list of event handlers for a handle associated with a + * given event type. + * + * @param handlers: Input location for the Event handler list. + * @param event_id: Event Id to search for. + */ +struct kshark_event_handler * +kshark_find_event_handler(struct kshark_event_handler *handlers, int event_id) +{ + for (; handlers; handlers = handlers->next) + if (handlers->id == event_id) + return handlers; + + return NULL; +} + +/** + * @brief Add new event handler to an existing list of handlers. + * + * @param handlers: Input location for the Event handler list. + * @param event_id: Event Id. + * @param evt_func: Input location for an Event action provided by the plugin. + * @param dw_func: Input location for a Draw action provided by the plugin. + * + * @returns Zero on success, or a negative error code on failure. + */ +int kshark_register_event_handler(struct kshark_event_handler **handlers, + int event_id, + kshark_plugin_event_handler_func evt_func, + kshark_plugin_draw_handler_func dw_func) +{ + struct kshark_event_handler *handler = + gui_event_handler_alloc(event_id, evt_func, dw_func); + + if(!handler) + return -ENOMEM; + + handler->next = *handlers; + *handlers = handler; + return 0; +} + +/** + * @brief Search the list for a specific plugin handle. If such a plugin handle + * exists, unregister (remove and free) this handle from the list. + * + * @param handlers: Input location for the Event handler list. + * @param event_id: Event Id of the plugin handler to be unregistered. + * @param evt_func: Event action function of the handler to be unregistered. + * @param dw_func: Draw action function of the handler to be unregistered. + */ +void kshark_unregister_event_handler(struct kshark_event_handler **handlers, + int event_id, + kshark_plugin_event_handler_func evt_func, + kshark_plugin_draw_handler_func dw_func) +{ + struct kshark_event_handler **last; + + for (last = handlers; *last; last = &(*last)->next) { + if ((*last)->id == event_id && + (*last)->event_func == evt_func && + (*last)->draw_func == dw_func) { + struct kshark_event_handler *this_handler; + this_handler = *last; + *last = this_handler->next; + free(this_handler); + + return; + } + } +} + +/** + * @brief Free all Event handlers in a given list. + * + * @param handlers: Input location for the Event handler list. + */ +void kshark_free_event_handler_list(struct kshark_event_handler *handlers) +{ + struct kshark_event_handler *last; + + while (handlers) { + last = handlers; + handlers = handlers->next; + free(last); + } +} + +/** + * @brief Allocate memory for a new plugin. Add this plugin to the list of + * plugins used by the session. + * + * @param kshark_ctx: Input location for the session context pointer. + * @param file: The plugin object file to load. + * + * @returns Zero on success, or a negative error code on failure. + */ +int kshark_register_plugin(struct kshark_context *kshark_ctx, + const char *file) +{ + struct kshark_plugin_list *plugin = kshark_ctx->plugins; + struct stat st; + int ret; + + while (plugin) { + if (strcmp(plugin->file, file) == 0) + return -EEXIST; + + plugin = plugin->next; + } + + ret = stat(file, &st); + if (ret < 0) { + fprintf(stderr, "plugin %s not found\n", file); + return -ENODEV; + } + + plugin = calloc(sizeof(struct kshark_plugin_list), 1); + if (!plugin) { + fprintf(stderr, "failed to allocate memory for plugin\n"); + return -ENOMEM; + } + + if (asprintf(&plugin->file, "%s", file) <= 0) { + fprintf(stderr, + "failed to allocate memory for plugin file name"); + return -ENOMEM; + } + + plugin->handle = dlopen(plugin->file, RTLD_NOW | RTLD_GLOBAL); + if (!plugin->handle) + goto fail; + + plugin->init = dlsym(plugin->handle, + KSHARK_PLUGIN_INITIALIZER_NAME); + + plugin->close = dlsym(plugin->handle, + KSHARK_PLUGIN_DEINITIALIZER_NAME); + + if (!plugin->init || !plugin->close) + goto fail; + + plugin->next = kshark_ctx->plugins; + kshark_ctx->plugins = plugin; + + return 0; + + fail: + fprintf(stderr, "cannot load plugin '%s'\n%s\n", + plugin->file, dlerror()); + + if (plugin->handle) { + dlclose(plugin->handle); + plugin->handle = NULL; + } + + free(plugin); + + return EFAULT; +} + +/** + * @brief Unrgister a plugin. + * + * @param kshark_ctx: Input location for context pointer. + * @param file: The plugin object file to unregister. + */ +void kshark_unregister_plugin(struct kshark_context *kshark_ctx, + const char *file) +{ + struct kshark_plugin_list **last; + + for (last = &kshark_ctx->plugins; *last; last = &(*last)->next) { + if (strcmp((*last)->file, file) == 0) { + struct kshark_plugin_list *this_plugin; + this_plugin = *last; + *last = this_plugin->next; + + dlclose(this_plugin->handle); + free(this_plugin); + + return; + } + } +} + +/** + * @brief Free all plugins in a given list. + * + * @param plugins: Input location for the plugins list. + */ +void kshark_free_plugin_list(struct kshark_plugin_list *plugins) +{ + struct kshark_plugin_list *last; + + while (plugins) { + last = plugins; + plugins = plugins->next; + + free(last->file); + dlclose(last->handle); + + free(last); + } +} + +/** + * @brief Use this function to initialize/update/deinitialize all registered + * plugins. + * + * @param kshark_ctx: Input location for context pointer. + * @param task_id: Action identifier specifying the action to be executed. + * + * @returns The number of successful added/removed plugin handlers on success, + * or a negative error code on failure. + */ +int kshark_handle_plugins(struct kshark_context *kshark_ctx, + enum kshark_plugin_actions task_id) +{ + struct kshark_plugin_list *plugin; + int handler_count = 0; + + for (plugin = kshark_ctx->plugins; plugin; plugin = plugin->next) { + switch (task_id) { + case KSHARK_PLUGIN_INIT: + handler_count += plugin->init(kshark_ctx); + break; + + case KSHARK_PLUGIN_UPDATE: + plugin->close(kshark_ctx); + handler_count += plugin->init(kshark_ctx); + break; + + case KSHARK_PLUGIN_CLOSE: + handler_count += plugin->close(kshark_ctx); + break; + + default: + return -EINVAL; + } + } + + return handler_count; +} diff --git a/kernel-shark-qt/src/libkshark-plugin.h b/kernel-shark-qt/src/libkshark-plugin.h new file mode 100644 index 0000000..0cb677a --- /dev/null +++ b/kernel-shark-qt/src/libkshark-plugin.h @@ -0,0 +1,179 @@ +/* SPDX-License-Identifier: LGPL-2.1 */ + +/* + * Copyright (C) 2016 Red Hat Inc, Steven Rostedt + */ + + /** + * @file libkshark-plugin.h + * @brief KernelShark plugins. + */ + +#ifndef _KSHARK_PLUGIN_H +#define _KSHARK_PLUGIN_H + +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + +// trace-cmd +#include "event-parse.h" + +/* Quiet warnings over documenting simple structures */ +//! @cond Doxygen_Suppress + +#define KSHARK_PLUGIN_INITIALIZER kshark_plugin_initializer + +#define KSHARK_PLUGIN_DEINITIALIZER kshark_plugin_deinitializer + +#define _MAKE_STR(x) #x +#define MAKE_STR(x) _MAKE_STR(x) + +#define KSHARK_PLUGIN_INITIALIZER_NAME MAKE_STR(KSHARK_PLUGIN_INITIALIZER) + +#define KSHARK_PLUGIN_DEINITIALIZER_NAME MAKE_STR(KSHARK_PLUGIN_DEINITIALIZER) + +struct kshark_context; + +struct kshark_entry; + +//! @endcond + +/** + * A function type to be used when defining load/reload/unload plugin + * functions. + */ +typedef int (*kshark_plugin_load_func)(struct kshark_context *); + +struct kshark_trace_histo; + +/** + * Structure representing the C arguments of the drawing function of + * a plugin. + */ +struct kshark_cpp_argv { + /** Pointer to the model descriptor object. */ + struct kshark_trace_histo *histo; +}; + +/** A function type to be used when defining plugin functions for drawing. */ +typedef void +(*kshark_plugin_draw_handler_func)(struct kshark_cpp_argv *argv, + int val, int draw_action); + +/** + * A function type to be used when defining plugin functions for data + * manipulation. + */ +typedef void +(*kshark_plugin_event_handler_func)(struct kshark_context *kshark_ctx, + struct tep_record *rec, + struct kshark_entry *e); + +/** Plugin action identifier. */ +enum kshark_plugin_actions { + /** + * Load plugins action. This action identifier is used when handling + * plugins. + */ + KSHARK_PLUGIN_INIT, + + /** + * Reload plugins action. This action identifier is used when handling + * plugins. + */ + KSHARK_PLUGIN_UPDATE, + + /** + * Unload plugins action. This action identifier is used when handling + * plugins. + */ + KSHARK_PLUGIN_CLOSE, + + /** + * Task draw action. This action identifier is used by the plugin draw + * function. + */ + KSHARK_PLUGIN_TASK_DRAW, + + /** + * CPU draw action. This action identifier is used by the plugin draw + * function. + */ + KSHARK_PLUGIN_CPU_DRAW, +}; + +/** + * Plugin Event handler structure, defining the properties of the required + * kshark_entry. + */ +struct kshark_event_handler { + /** Pointer to the next Plugin Event handler. */ + struct kshark_event_handler *next; + + /** Unique Id ot the trace event type. */ + int id; + + /** + * Event action function. This action can be used to modify the content + * of all kshark_entries having Event Ids equal to "id". + */ + kshark_plugin_event_handler_func event_func; + + /** + * Draw action function. This action can be used to draw additional + * graphical elements (shapes) for all kshark_entries having Event Ids + * equal to "id". + */ + kshark_plugin_draw_handler_func draw_func; +}; + +struct kshark_event_handler * +kshark_find_event_handler(struct kshark_event_handler *handlers, int event_id); + +int kshark_register_event_handler(struct kshark_event_handler **handlers, + int event_id, + kshark_plugin_event_handler_func evt_func, + kshark_plugin_draw_handler_func dw_func); + +void kshark_unregister_event_handler(struct kshark_event_handler **handlers, + int event_id, + kshark_plugin_event_handler_func evt_func, + kshark_plugin_draw_handler_func dw_func); + +void kshark_free_event_handler_list(struct kshark_event_handler *handlers); + +/** Linked list of plugins. */ +struct kshark_plugin_list { + /** Pointer to the next Plugin. */ + struct kshark_plugin_list *next; + + /** The plugin object file to load. */ + char *file; + + /** Plugin Event handler. */ + void *handle; + + /** Callback function for initialization of the plugin. */ + kshark_plugin_load_func init; + + /** Callback function for deinitialization of the plugin. */ + kshark_plugin_load_func close; +}; + +int kshark_register_plugin(struct kshark_context *kshark_ctx, + const char *file); + +void kshark_unregister_plugin(struct kshark_context *kshark_ctx, + const char *file); + +void kshark_free_plugin_list(struct kshark_plugin_list *plugins); + +int kshark_handle_plugins(struct kshark_context *kshark_ctx, + enum kshark_plugin_actions task_id); + +#ifdef __cplusplus +} +#endif // __cplusplus + +#endif // _KSHARK_PLUGIN_H diff --git a/kernel-shark-qt/src/libkshark.h b/kernel-shark-qt/src/libkshark.h index 2580449..fda133c 100644 --- a/kernel-shark-qt/src/libkshark.h +++ b/kernel-shark-qt/src/libkshark.h @@ -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);