Skip to contentSkip to conversation

Improvements and fixes for sched_switch plugin

PATCH In review Yordan Karadzhov 3 participants 12 messages Mar 5, 2019

lore
Share & tools
lore
#0 lore raw

cover Improvements and fixes for sched_switch plugin

Yordan Karadzhov (3):
kernel-shark: Define free_contex function for the sched_events plugin
kernel-shark: Fix a memory leak in the sched_events plugin
kernel-shark: sched_events plugin init with sched_switch events only

 kernel-shark/src/plugins/sched_events.c | 30 +++++++++++++++----------
 1 file changed, 18 insertions(+), 12 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH 1/3 kernel-shark: Define free_contex function for the sched_events plugin

This static helper function makes the code of the plugin cleaner and easier to read.

---
 kernel-shark/src/plugins/sched_events.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
kernel-shark/src/plugins/sched_events.c +12 -4
index 68734d4..0d6de2d 100644
@@ -39,6 +39,17 @@ static bool define_wakeup_event(struct tep_handle *tep, const char *wakeup_name,
 	return true;
 }
 
+static void plugin_free_context(struct plugin_sched_context *plugin_ctx)
+{
+	if (!plugin_ctx)
+		return;
+
+	tracecmd_filter_id_hash_free(plugin_ctx->second_pass_hash);
+	kshark_free_collection_list(plugin_ctx->collections);
+
+	free(plugin_ctx);
+}
+
 static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 {
 	struct plugin_sched_context *plugin_ctx;
@@ -339,10 +350,7 @@ static int plugin_sched_close(struct kshark_context *kshark_ctx)
 					plugin_sched_action,
 					plugin_draw);
 
-	tracecmd_filter_id_hash_free(plugin_ctx->second_pass_hash);
-
-	kshark_free_collection_list(plugin_ctx->collections);
-	free(plugin_ctx);
+	plugin_free_context(plugin_ctx);
 	plugin_sched_context_handler = NULL;
 
 	return 1;
Signature · 1 line
2.19.1
Reviewed-by on v1 1/3
#2 lore raw

Re: [PATCH 1/3] kernel-shark: Define free_contex function for the sched_events plugin

Quoted 32 lines · Yordan Karadzhov

This static helper function makes the code of the plugin cleaner and easier to read.

---
 kernel-shark/src/plugins/sched_events.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
kernel-shark/src/plugins/sched_events.c +11 -0
index 68734d4..0d6de2d 100644
@@ -39,6 +39,17 @@ static bool define_wakeup_event(struct tep_handle *tep, const char *wakeup_name,
 	return true;
 }
 
+static void plugin_free_context(struct plugin_sched_context *plugin_ctx)
+{
+	if (!plugin_ctx)
+		return;
+
+	tracecmd_filter_id_hash_free(plugin_ctx->second_pass_hash);
+	kshark_free_collection_list(plugin_ctx->collections);
+
+	free(plugin_ctx);
+}
+
 static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 {
 	struct plugin_sched_context *plugin_ctx;
@@ -339,10 +350,7 @@ static int plugin_sched_close(struct kshark_context *kshark_ctx)
 					plugin_sched_action,

kernel-shark/src/plugins/sched_events.c static int plugin_sched_close(struct kshark_context *kshark_ctx) · line 356

Quoted 10 lines
kernel-shark/src/plugins/sched_events.c +1 -4
 					plugin_draw);
 
-	tracecmd_filter_id_hash_free(plugin_ctx->second_pass_hash);
-
-	kshark_free_collection_list(plugin_ctx->collections);
-	free(plugin_ctx);
+	plugin_free_context(plugin_ctx);
 	plugin_sched_context_handler = NULL;
 
 	return 1;

Nit: there's a typo in the commit message

Quoted 1 line

kernel-shark: Define free_contex function for the sched_events plugin

                                     ^

:s/free_contex/plugin_free_context/g ?

Other than that it looks good to me.

Cheers,

-- Slavi

#3 lore raw

Re: [PATCH 1/3] kernel-shark: Define free_contex function for the sched_events plugin

Quoted 5 lines · On Tue, 5 Mar 2019 17:54:26 +0200 Slavomir Kaslev <[email protected]> wrote:

Nit: there's a typo in the commit message

Quoted 1 line

kernel-shark: Define free_contex function for the sched_events plugin

                                     ^

:s/free_contex/plugin_free_context/g ?

I caught that too, and fixed it.

Quoted 4 lines

Other than that it looks good to me.

Thanks Slavomir,

I'll add your review tag.

-- Steve

#4 lore raw

PATCH 2/3 kernel-shark: Fix a memory leak in the sched_events plugin

In reply to Yordan Karadzhov

When the sched_events plugin fails to initialize not all the memory allocated for the context of the plugin is freed properly. The problem gets fixed by using the free_context helper function defined in the previous patch.

Fixes 4f392730e ("kernel-shark-qt: Make Sched event plugin use ...")
---
 kernel-shark/src/plugins/sched_events.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel-shark/src/plugins/sched_events.c +6 -5
index 0d6de2d..fe13e6a 100644
@@ -74,8 +74,12 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 
 	event = tep_find_event_by_name(plugin_ctx->pevent,
 				       "sched", "sched_switch");
-	if (!event)
+	if (!event) {
+		plugin_free_context(plugin_ctx);
+		plugin_sched_context_handler = NULL;
+
 		return false;
+	}
 
 	plugin_ctx->sched_switch_event = event;
 	plugin_ctx->sched_switch_next_field =
@@ -320,11 +324,8 @@ static int plugin_sched_init(struct kshark_context *kshark_ctx)
 {
 	struct plugin_sched_context *plugin_ctx;
 
-	if (!plugin_sched_init_context(kshark_ctx)) {
-		free(plugin_sched_context_handler);
-		plugin_sched_context_handler = NULL;
+	if (!plugin_sched_init_context(kshark_ctx))
 		return 0;
-	}
 
 	plugin_ctx = plugin_sched_context_handler;
 
Signature · 1 line
2.19.1
Reviewed-by on v1 2/3
#5 lore raw

Re: [PATCH 2/3] kernel-shark: Fix a memory leak in the sched_events plugin

Quoted 16 lines · Yordan Karadzhov

When the sched_events plugin fails to initialize not all the memory allocated for the context of the plugin is freed properly. The problem gets fixed by using the free_context helper function defined in the previous patch.

Fixes 4f392730e ("kernel-shark-qt: Make Sched event plugin use ...")
---
 kernel-shark/src/plugins/sched_events.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel-shark/src/plugins/sched_events.c +0 -0
index 0d6de2d..fe13e6a 100644

kernel-shark/src/plugins/sched_events.c static bool plugin_sched_init_context(struct kshark_context *kshark_ctx) · line 80

Quoted 8 lines
kernel-shark/src/plugins/sched_events.c +4 -1
@@ -74,8 +74,12 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 
 	event = tep_find_event_by_name(plugin_ctx->pevent,
 				       "sched", "sched_switch");
-	if (!event)
+	if (!event) {
+		plugin_free_context(plugin_ctx);
+		plugin_sched_context_handler = NULL;
+

Nit: drop the above empty line?

Quoted 7 lines
kernel-shark/src/plugins/sched_events.c +1 -0
 		return false;
+	}
 
 	plugin_ctx->sched_switch_event = event;
 	plugin_ctx->sched_switch_next_field =
@@ -320,11 +324,8 @@ static int plugin_sched_init(struct kshark_context *kshark_ctx)
 {

kernel-shark/src/plugins/sched_events.c static int plugin_sched_init(struct kshark_context *kshark_ctx) · line 330

Quoted 10 lines
kernel-shark/src/plugins/sched_events.c +1 -4
 	struct plugin_sched_context *plugin_ctx;
 
-	if (!plugin_sched_init_context(kshark_ctx)) {
-		free(plugin_sched_context_handler);
-		plugin_sched_context_handler = NULL;
+	if (!plugin_sched_init_context(kshark_ctx))
 		return 0;
-	}
 
 	plugin_ctx = plugin_sched_context_handler;

Looks good to me.

-- Slavi

#6 lore raw

Re: [PATCH 2/3] kernel-shark: Fix a memory leak in the sched_events plugin

Quoted 18 lines · On Tue, 5 Mar 2019 17:56:28 +0200 Slavomir Kaslev <[email protected]> wrote:
Quoted 17 lines · Yordan Karadzhov

When the sched_events plugin fails to initialize not all the memory allocated for the context of the plugin is freed properly. The problem gets fixed by using the free_context helper function defined in the previous patch.

Fixes 4f392730e ("kernel-shark-qt: Make Sched event plugin use ...")
---
 kernel-shark/src/plugins/sched_events.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel-shark/src/plugins/sched_events.c +0 -0
index 0d6de2d..fe13e6a 100644
@@ -74,8 +74,12 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 
Quoted 9 lines

kernel-shark/src/plugins/sched_events.c static bool plugin_sched_init_context(struct kshark_context *kshark_ctx) · line 80

Quoted 7 lines
kernel-shark/src/plugins/sched_events.c +4 -1
 	event = tep_find_event_by_name(plugin_ctx->pevent,
 				       "sched", "sched_switch");
-	if (!event)
+	if (!event) {
+		plugin_free_context(plugin_ctx);
+		plugin_sched_context_handler = NULL;
+  

Nit: drop the above empty line?

Yeah, it would be better to do that. But for now, I'll take the patch anyway. Perhaps clean it up later.

Quoted 17 lines
Quoted 17 lines
kernel-shark/src/plugins/sched_events.c +2 -4
 		return false;
+	}
 
 	plugin_ctx->sched_switch_event = event;
 	plugin_ctx->sched_switch_next_field =
@@ -320,11 +324,8 @@ static int plugin_sched_init(struct kshark_context *kshark_ctx)
 {
 	struct plugin_sched_context *plugin_ctx;
 
-	if (!plugin_sched_init_context(kshark_ctx)) {
-		free(plugin_sched_context_handler);
-		plugin_sched_context_handler = NULL;
+	if (!plugin_sched_init_context(kshark_ctx))
 		return 0;
-	}
 
Quoted 5 lines

kernel-shark/src/plugins/sched_events.c static int plugin_sched_init(struct kshark_context *kshark_ctx) · line 330

Quoted 1 line
kernel-shark/src/plugins/sched_events.c +0 -0
 	plugin_ctx = plugin_sched_context_handler;  

Looks good to me.

Thanks Slavomir!

-- Steve

#7 lore raw

Re: [PATCH 2/3] kernel-shark: Fix a memory leak in the sched_events plugin

In reply to Yordan Karadzhov
Quoted 6 lines · On Tue, 5 Mar 2019 16:39:23 +0200 Yordan Karadzhov <[email protected]>…

When the sched_events plugin fails to initialize not all the memory allocated for the context of the plugin is freed properly. The problem gets fixed by using the free_context helper function defined in the previous patch.

Fixes 4f392730e ("kernel-shark-qt: Make Sched event plugin use ...")

I took the patch, but don't truncate the fixes line. It's fine to go over 80 characters:

Fixes 4f392730e ("kernel-shark-qt: Make Sched event plugin use its own data collections")

-- Steve

Quoted 36 lines
---
 kernel-shark/src/plugins/sched_events.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
kernel-shark/src/plugins/sched_events.c +6 -5
index 0d6de2d..fe13e6a 100644
@@ -74,8 +74,12 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 
 	event = tep_find_event_by_name(plugin_ctx->pevent,
 				       "sched", "sched_switch");
-	if (!event)
+	if (!event) {
+		plugin_free_context(plugin_ctx);
+		plugin_sched_context_handler = NULL;
+
 		return false;
+	}
 
 	plugin_ctx->sched_switch_event = event;
 	plugin_ctx->sched_switch_next_field =
@@ -320,11 +324,8 @@ static int plugin_sched_init(struct kshark_context *kshark_ctx)
 {
 	struct plugin_sched_context *plugin_ctx;
 
-	if (!plugin_sched_init_context(kshark_ctx)) {
-		free(plugin_sched_context_handler);
-		plugin_sched_context_handler = NULL;
+	if (!plugin_sched_init_context(kshark_ctx))
 		return 0;
-	}
 
 	plugin_ctx = plugin_sched_context_handler;
 
#8 lore raw

PATCH 3/3 kernel-shark: sched_events plugin init with sched_switch events only

In reply to Yordan Karadzhov

The sched_events plugin should not be totally disabled when the wakeup events are missing. The initialization of the plugin must fail only if the trace.dat file contains no sched_switch events.

---
 kernel-shark/src/plugins/sched_events.c | 3 ---
 1 file changed, 3 deletions(-)
kernel-shark/src/plugins/sched_events.c +0 -3
index fe13e6a..c52fb29 100644
@@ -104,9 +104,6 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 					   &plugin_ctx->sched_waking_event,
 					   &plugin_ctx->sched_waking_pid_field);
 
-	if (!wakeup_found)
-		return false;
-
 	plugin_ctx->second_pass_hash = tracecmd_filter_id_hash_alloc();
 
 	return true;
Signature · 1 line
2.19.1
Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] kernel-shark: sched_events plugin init with sched_switch events only

Quoted 14 lines · Yordan Karadzhov

The sched_events plugin should not be totally disabled when the wakeup events are missing. The initialization of the plugin must fail only if the trace.dat file contains no sched_switch events.

---
 kernel-shark/src/plugins/sched_events.c | 3 ---
 1 file changed, 3 deletions(-)
kernel-shark/src/plugins/sched_events.c +0 -0
index fe13e6a..c52fb29 100644

kernel-shark/src/plugins/sched_events.c static bool plugin_sched_init_context(struct kshark_context *kshark_ctx) · line 109

Quoted 9 lines
kernel-shark/src/plugins/sched_events.c +0 -3
@@ -104,9 +104,6 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 					   &plugin_ctx->sched_waking_event,
 					   &plugin_ctx->sched_waking_pid_field);
 
-	if (!wakeup_found)
-		return false;
-
 	plugin_ctx->second_pass_hash = tracecmd_filter_id_hash_alloc();
 
 	return true;

Looks good to me.

Thank you,

-- Slavi

#11 lore raw

Re: [PATCH 3/3] kernel-shark: sched_events plugin init with sched_switch events only

In reply to Yordan Karadzhov
Quoted 4 lines · On Tue, 5 Mar 2019 16:39:24 +0200 Yordan Karadzhov <[email protected]>…

The sched_events plugin should not be totally disabled when the wakeup events are missing. The initialization of the plugin must fail only if the trace.dat file contains no sched_switch events.

I added:

-- Steve

Quoted 19 lines
---
 kernel-shark/src/plugins/sched_events.c | 3 ---
 1 file changed, 3 deletions(-)
kernel-shark/src/plugins/sched_events.c +0 -3
index fe13e6a..c52fb29 100644
@@ -104,9 +104,6 @@ static bool plugin_sched_init_context(struct kshark_context *kshark_ctx)
 					   &plugin_ctx->sched_waking_event,
 					   &plugin_ctx->sched_waking_pid_field);
 
-	if (!wakeup_found)
-		return false;
-
 	plugin_ctx->second_pass_hash = tracecmd_filter_id_hash_alloc();
 
 	return true;