From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: Tzvetomir Stoyanov <tstoyanov@vmware.com>,
Steven Rostedt <rostedt@goodmis.org>
Cc: Linux Trace Devel <linux-trace-devel@vger.kernel.org>
Subject: multi-thread read bug
Date: Thu, 3 Jan 2019 14:08:37 +0200 [thread overview]
Message-ID: <e85a0404-85e7-b8f7-2c47-0c90ba42e7e0@gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 283 bytes --]
Hi Ceco,
I have a problem when trying to call tracecmd_read_at() from different
threads. Although the call of tracecmd_read_at() is protected by a
mutex, I am still getting a segmentation fault.
A simple program that reproduces the problem is attached as a patch.
Thanks!
Yordan
[-- Attachment #2: mt_read_bug.patch --]
[-- Type: text/x-patch, Size: 2498 bytes --]
diff --git a/kernel-shark-qt/examples/CMakeLists.txt b/kernel-shark-qt/examples/CMakeLists.txt
index e16216e..ae1331c 100644
--- a/kernel-shark-qt/examples/CMakeLists.txt
+++ b/kernel-shark-qt/examples/CMakeLists.txt
@@ -23,3 +23,7 @@ target_link_libraries(dplot kshark-plot)
message(STATUS "widgetdemo")
add_executable(widgetdemo widgetdemo.cpp)
target_link_libraries(widgetdemo kshark-gui)
+
+message(STATUS "mtread")
+add_executable(mtread mt_read.cpp)
+target_link_libraries(mtread kshark-gui)
diff --git a/kernel-shark-qt/examples/mt_read.cpp b/kernel-shark-qt/examples/mt_read.cpp
index e69de29..0419454 100644
--- a/kernel-shark-qt/examples/mt_read.cpp
+++ b/kernel-shark-qt/examples/mt_read.cpp
@@ -0,0 +1,82 @@
+// C
+#include <stdio.h>
+#include <stdlib.h>
+
+// C++
+#include <vector>
+#include <thread>
+
+// KernelShark
+#include "libkshark.h"
+
+using namespace std;
+
+const char *default_file = "trace.dat";
+
+static void read_job(struct kshark_context *kshark_ctx,
+ struct kshark_entry **data,
+ size_t first, size_t last)
+{
+ struct tep_record *rec;
+
+ for (size_t r = first; r < last; ++r) {
+ rec = kshark_read_at(kshark_ctx, data[r]->offset);
+ free(rec);
+ }
+}
+
+int main(int argc, char **argv)
+{
+ int nThreads = std::thread::hardware_concurrency();
+ struct kshark_context *kshark_ctx;
+ struct kshark_entry **data = NULL;
+ size_t r, n_rows;
+ int first, last, delta;
+ bool status;
+
+ /* Create a new kshark session. */
+ kshark_ctx = NULL;
+ if (!kshark_instance(&kshark_ctx))
+ return 1;
+
+ /* Open a trace data file produced by trace-cmd. */
+ if (argc > 1)
+ status = kshark_open(kshark_ctx, argv[1]);
+ else
+ status = kshark_open(kshark_ctx, default_file);
+
+ if (!status) {
+ kshark_free(kshark_ctx);
+ return 1;
+ }
+
+ /* Load the content of the file into an array of entries. */
+ n_rows = kshark_load_data_entries(kshark_ctx, &data);
+
+// nThreads = 1;
+ delta = n_rows / nThreads;
+ vector<thread> threads;
+ for (int i = 0; i < nThreads; ++i) {
+ first = i * delta;
+ last = first + delta - 1;
+ threads.push_back(thread(read_job, kshark_ctx, data, first, last));
+// threads.push_back(thread(read_job, kshark_ctx, data, 0, n_rows));
+ }
+
+ for (auto &t:threads)
+ t.join();
+
+ /* Free the memory. */
+ for (r = 0; r < n_rows; ++r)
+ free(data[r]);
+
+ free(data);
+
+ /* Close the file. */
+ kshark_close(kshark_ctx);
+
+ /* Close the session. */
+ kshark_free(kshark_ctx);
+
+ return 0;
+}
next reply other threads:[~2019-01-03 12:08 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-01-03 12:08 Yordan Karadzhov (VMware) [this message]
2019-01-03 14:32 ` multi-thread read bug Steven Rostedt
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=e85a0404-85e7-b8f7-2c47-0c90ba42e7e0@gmail.com \
--to=y.karadz@gmail.com \
--cc=linux-trace-devel@vger.kernel.org \
--cc=rostedt@goodmis.org \
--cc=tstoyanov@vmware.com \
/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).