linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Yordan Karadzhov <ykaradzhov@vmware.com>
To: "rostedt@goodmis.org" <rostedt@goodmis.org>
Cc: "linux-trace-devel@vger.kernel.org" <linux-trace-devel@vger.kernel.org>
Subject: [PATCH 1/2] kernel-shark-qt: Avoid race condition when reading data
Date: Fri, 4 Jan 2019 20:06:20 +0000	[thread overview]
Message-ID: <20190104200559.24471-2-ykaradzhov@vmware.com> (raw)
In-Reply-To: <20190104200559.24471-1-ykaradzhov@vmware.com>

We know that the data reading operations are not thread-safe.
A permanent solution for the problem is being worked on. For
the time being, this patch provides a naive temporary fix by
slapping mutexes all over the code, which will slow things down
a bit.

Signed-off-by: Yordan Karadzhov <ykaradzhov@vmware.com>
---
 kernel-shark-qt/src/libkshark.c            | 74 +++++++++++++---------
 kernel-shark-qt/src/libkshark.h            |  3 -
 kernel-shark-qt/src/plugins/sched_events.c |  6 +-
 3 files changed, 47 insertions(+), 36 deletions(-)

diff --git a/kernel-shark-qt/src/libkshark.c b/kernel-shark-qt/src/libkshark.c
index 598ea52..23dc813 100644
--- a/kernel-shark-qt/src/libkshark.c
+++ b/kernel-shark-qt/src/libkshark.c
@@ -960,31 +960,6 @@ ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx,
 	return -ENOMEM;
 }
 
-/**
- * @brief A thread-safe read of a record from a specific offset.
- *
- * @param kshark_ctx: Input location for the session context pointer.
- * @param offset: the offset into the file to find the record.
- *
- * @returns The returned pevent_record must be freed.
- */
-struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
-				  uint64_t offset)
-{
-	/*
-	 * Calling tracecmd_read_at() is not thread-safe. Use a mutex to
-	 * protect the access.
-	 */
-	pthread_mutex_lock(&kshark_ctx->input_mutex);
-
-	struct tep_record *data = tracecmd_read_at(kshark_ctx->handle,
-						      offset, NULL);
-
-	pthread_mutex_unlock(&kshark_ctx->input_mutex);
-
-	return data;
-}
-
 static const char *kshark_get_latency(struct tep_handle *pe,
 				      struct tep_record *record)
 {
@@ -1046,10 +1021,18 @@ int kshark_get_pid_easy(struct kshark_entry *entry)
 		/*
 		 * The entry has been touched by a plugin callback function.
 		 * Because of this we do not trust the value of "entry->pid".
+		 *
+		 * Currently the data reading operations are not thread-safe.
+		 * Use a mutex to protect the access.
 		 */
-		data = kshark_read_at(kshark_ctx, entry->offset);
+		pthread_mutex_lock(&kshark_ctx->input_mutex);
+
+		data = tracecmd_read_at(kshark_ctx->handle, entry->offset,
+					NULL);
 		pid = tep_data_pid(kshark_ctx->pevent, data);
 		free_record(data);
+
+		pthread_mutex_unlock(&kshark_ctx->input_mutex);
 	}
 
 	return pid;
@@ -1106,10 +1089,18 @@ const char *kshark_get_latency_easy(struct kshark_entry *entry)
 	if (entry->event_id < 0)
 		return NULL;
 
-	data = kshark_read_at(kshark_ctx, entry->offset);
+	/*
+	 * Currently the data reading operations are not thread-safe.
+	 * Use a mutex to protect the access.
+	 */
+	pthread_mutex_lock(&kshark_ctx->input_mutex);
+
+	data = tracecmd_read_at(kshark_ctx->handle, entry->offset, NULL);
 	lat = kshark_get_latency(kshark_ctx->pevent, data);
 	free_record(data);
 
+	pthread_mutex_unlock(&kshark_ctx->input_mutex);
+
 	return lat;
 }
 
@@ -1142,10 +1133,18 @@ int kshark_get_event_id_easy(struct kshark_entry *entry)
 		 * The entry has been touched by a plugin callback function.
 		 * Because of this we do not trust the value of
 		 * "entry->event_id".
+		 *
+		 * Currently the data reading operations are not thread-safe.
+		 * Use a mutex to protect the access.
 		 */
-		data = kshark_read_at(kshark_ctx, entry->offset);
+		pthread_mutex_lock(&kshark_ctx->input_mutex);
+
+		data = tracecmd_read_at(kshark_ctx->handle, entry->offset,
+					NULL);
 		event_id = tep_data_type(kshark_ctx->pevent, data);
 		free_record(data);
+
+		pthread_mutex_unlock(&kshark_ctx->input_mutex);
 	}
 
 	return (event_id == -1)? -EFAULT : event_id;
@@ -1184,7 +1183,14 @@ const char *kshark_get_event_name_easy(struct kshark_entry *entry)
 		}
 	}
 
+	/*
+	 * Currently the data reading operations are not thread-safe.
+	 * Use a mutex to protect the access.
+	 */
+	pthread_mutex_lock(&kshark_ctx->input_mutex);
 	event = tep_data_event_from_type(kshark_ctx->pevent, event_id);
+	pthread_mutex_unlock(&kshark_ctx->input_mutex);
+
 	if (event)
 		return event->name;
 
@@ -1223,8 +1229,13 @@ const char *kshark_get_info_easy(struct kshark_entry *entry)
 		}
 	}
 
-	data = kshark_read_at(kshark_ctx, entry->offset);
+	/*
+	 * Currently the data reading operations are not thread-safe.
+	 * Use a mutex to protect the access.
+	 */
+	pthread_mutex_lock(&kshark_ctx->input_mutex);
 
+	data = tracecmd_read_at(kshark_ctx->handle, entry->offset, NULL);
 	event_id = tep_data_type(kshark_ctx->pevent, data);
 	event = tep_data_event_from_type(kshark_ctx->pevent, event_id);
 	if (event)
@@ -1232,6 +1243,8 @@ const char *kshark_get_info_easy(struct kshark_entry *entry)
 
 	free_record(data);
 
+	pthread_mutex_unlock(&kshark_ctx->input_mutex);
+
 	return info;
 }
 
@@ -1315,7 +1328,8 @@ char* kshark_dump_entry(const struct kshark_entry *entry)
 		struct tep_event_format *event;
 		struct tep_record *data;
 
-		data = kshark_read_at(kshark_ctx, entry->offset);
+		data = tracecmd_read_at(kshark_ctx->handle, entry->offset,
+					NULL);
 
 		event = tep_data_event_from_type(kshark_ctx->pevent,
 						 entry->event_id);
diff --git a/kernel-shark-qt/src/libkshark.h b/kernel-shark-qt/src/libkshark.h
index 7d1edfc..a1b1f91 100644
--- a/kernel-shark-qt/src/libkshark.h
+++ b/kernel-shark-qt/src/libkshark.h
@@ -183,9 +183,6 @@ char* kshark_dump_custom_entry(struct kshark_context *kshark_ctx,
 			       const struct kshark_entry *entry,
 			       kshark_custom_info_func info_func);
 
-struct tep_record *kshark_read_at(struct kshark_context *kshark_ctx,
-				  uint64_t offset);
-
 /** Bit masks used to control the visibility of the entry after filtering. */
 enum kshark_filter_masks {
 	/**
diff --git a/kernel-shark-qt/src/plugins/sched_events.c b/kernel-shark-qt/src/plugins/sched_events.c
index 1500110..5409bc6 100644
--- a/kernel-shark-qt/src/plugins/sched_events.c
+++ b/kernel-shark-qt/src/plugins/sched_events.c
@@ -184,7 +184,7 @@ bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,
 
 	if (plugin_ctx->sched_wakeup_event &&
 	    e->event_id == plugin_ctx->sched_wakeup_event->id) {
-		record = kshark_read_at(kshark_ctx, e->offset);
+		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
 
 		/* We only want those that actually woke up the task. */
 		ret = tep_read_number_field(plugin_ctx->sched_wakeup_success_field,
@@ -196,7 +196,7 @@ bool plugin_wakeup_match_rec_pid(struct kshark_context *kshark_ctx,
 
 	if (plugin_ctx->sched_wakeup_new_event &&
 	    e->event_id == plugin_ctx->sched_wakeup_new_event->id) {
-		record = kshark_read_at(kshark_ctx, e->offset);
+		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
 
 		/* We only want those that actually woke up the task. */
 		ret = tep_read_number_field(plugin_ctx->sched_wakeup_new_success_field,
@@ -238,7 +238,7 @@ bool plugin_switch_match_rec_pid(struct kshark_context *kshark_ctx,
 	    e->event_id == plugin_ctx->sched_switch_event->id) {
 		struct tep_record *record;
 
-		record = kshark_read_at(kshark_ctx, e->offset);
+		record = tracecmd_read_at(kshark_ctx->handle, e->offset, NULL);
 		ret = tep_read_number_field(plugin_ctx->sched_switch_prev_state_field,
 					    record->data, &val);
 
-- 
2.17.1

  reply	other threads:[~2019-01-04 20:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-04 20:06 [PATCH 0/2] Improve/debug the searching logic Yordan Karadzhov
2019-01-04 20:06 ` Yordan Karadzhov [this message]
2019-01-04 20:06 ` [PATCH 2/2] kernel-shark-qt: Implement State machine for searching in the data Yordan Karadzhov

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=20190104200559.24471-2-ykaradzhov@vmware.com \
    --to=ykaradzhov@vmware.com \
    --cc=linux-trace-devel@vger.kernel.org \
    --cc=rostedt@goodmis.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).