linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: linux-trace-devel@vger.kernel.org
Cc: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH 4/7] kernel-shark: Add cleanup of all plugin contexts
Date: Tue, 27 Apr 2021 19:24:05 +0300	[thread overview]
Message-ID: <20210427162408.134001-5-y.karadz@gmail.com> (raw)
In-Reply-To: <20210427162408.134001-1-y.karadz@gmail.com>

The when the macro KS_DEFINE_PLUGIN_CONTEXT is used by the plugin,
the final free of the memory is done by calling __close() with a
negative stream id. Althow, this was provisioned in the definition
of the macro, it was never implemented in the GUI.

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 src/libkshark-plugin.c     | 10 ++++++++++
 src/libkshark-plugin.h     |  5 ++++-
 src/libkshark.h            |  2 +-
 src/plugins/sched_events.c |  4 ++++
 4 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/libkshark-plugin.c b/src/libkshark-plugin.c
index ebd2579..09886ce 100644
--- a/src/libkshark-plugin.c
+++ b/src/libkshark-plugin.c
@@ -117,6 +117,9 @@ int kshark_unregister_event_handler(struct kshark_data_stream *stream,
 {
 	struct kshark_event_proc_handler **last;
 
+	if (stream->stream_id < 0)
+		return 0;
+
 	for (last = &stream->event_handlers; *last; last = &(*last)->next) {
 		if ((*last)->id == event_id &&
 		    (*last)->event_func == evt_func) {
@@ -182,6 +185,9 @@ void kshark_unregister_draw_handler(struct kshark_data_stream *stream,
 {
 	struct kshark_draw_handler **last;
 
+	if (stream->stream_id < 0)
+		return;
+
 	for (last = &stream->draw_handlers; *last; last = &(*last)->next) {
 		if ((*last)->draw_func == draw_func) {
 			struct kshark_draw_handler *this_handler;
@@ -410,12 +416,16 @@ void kshark_unregister_plugin(struct kshark_context *kshark_ctx,
  */
 void kshark_free_plugin_list(struct kshark_plugin_list *plugins)
 {
+	struct kshark_data_stream stream;
 	struct kshark_plugin_list *last;
 
+	stream.stream_id = KS_PLUGIN_CONTEXT_FREE;
 	while (plugins) {
 		last = plugins;
 		plugins = plugins->next;
 
+		if (last->process_interface)
+			last->process_interface->close(&stream);
 		free_plugin(last);
 	}
 }
diff --git a/src/libkshark-plugin.h b/src/libkshark-plugin.h
index 752dbeb..85a0a8c 100644
--- a/src/libkshark-plugin.h
+++ b/src/libkshark-plugin.h
@@ -366,6 +366,9 @@ int kshark_handle_all_dpis(struct kshark_data_stream *stream,
 	__ok;								\
 })									\
 
+/** Identifier used to free the plugin context. */
+#define KS_PLUGIN_CONTEXT_FREE	-1
+
 /**
  * General purpose macro defining methods for adding plugin context.
  * Do not use this macro in header files.
@@ -395,7 +398,7 @@ __hidden type *__init(int sd)						\
 }									\
 __hidden void __close(int sd)						\
 {									\
-	if (sd < 0) {							\
+	if (sd == KS_PLUGIN_CONTEXT_FREE) {				\
 		free(__context_handler);				\
 		__n_streams = -1;					\
 		return;							\
diff --git a/src/libkshark.h b/src/libkshark.h
index aa4b3ca..ee3a1d3 100644
--- a/src/libkshark.h
+++ b/src/libkshark.h
@@ -281,7 +281,7 @@ struct kshark_generic_stream_interface {
 /** Structure representing a stream of trace data. */
 struct kshark_data_stream {
 	/** Data stream identifier. */
-	uint16_t		stream_id;
+	int16_t			stream_id;
 
 	/** The number of CPUs presented in this data stream. */
 	int			n_cpus;
diff --git a/src/plugins/sched_events.c b/src/plugins/sched_events.c
index 5798322..5c82803 100644
--- a/src/plugins/sched_events.c
+++ b/src/plugins/sched_events.c
@@ -201,6 +201,9 @@ int KSHARK_PLOT_PLUGIN_DEINITIALIZER(struct kshark_data_stream *stream)
 	struct plugin_sched_context *plugin_ctx;
 	int sd = stream->stream_id;
 
+	if (sd == KS_PLUGIN_CONTEXT_FREE)
+		goto close;
+
 	plugin_ctx = __get_context(sd);
 	if (!plugin_ctx)
 		return 0;
@@ -215,6 +218,7 @@ int KSHARK_PLOT_PLUGIN_DEINITIALIZER(struct kshark_data_stream *stream)
 
 	kshark_unregister_draw_handler(stream, plugin_draw);
 
+ close:
 	__close(sd);
 
 	return 1;
-- 
2.27.0


  parent reply	other threads:[~2021-04-27 16:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27 16:24 [PATCH 0/7] More minor modifications and fixes toward KS 2.0 Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 1/7] kernel-shark: Fix the build for 32b systems Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 2/7] kernel-shark: Add "cron" job to workflows Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 3/7] kernel-shark: Fix KS_DEFINE_PLUGIN_CONTEXT macro Yordan Karadzhov (VMware)
2021-04-27 16:24 ` Yordan Karadzhov (VMware) [this message]
2021-04-27 16:24 ` [PATCH 5/7] kernel-shark: Fix memory leak in "sched events" plugin Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 6/7] kernel-shark: Disable the pop-up offset dialog Yordan Karadzhov (VMware)
2021-04-27 16:24 ` [PATCH 7/7] kernel-shark: Remove kvm_combo from the list of default plugins Yordan Karadzhov (VMware)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210427162408.134001-5-y.karadz@gmail.com \
    --to=y.karadz@gmail.com \
    --cc=linux-trace-devel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).