From: "Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
To: rostedt@goodmis.org
Cc: linux-trace-devel@vger.kernel.org,
"Yordan Karadzhov (VMware)" <y.karadz@gmail.com>
Subject: [PATCH v4 7/9] kernel-shark-qt: Add an example showing how to filter trace data
Date: Mon, 2 Jul 2018 17:04:22 +0300 [thread overview]
Message-ID: <20180702140424.23221-7-y.karadz@gmail.com> (raw)
In-Reply-To: <20180702140424.23221-1-y.karadz@gmail.com>
This patch introduces a basic example, showing how to use the
C API of KernelShark in order to filter trace data.
Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
kernel-shark-qt/examples/CMakeLists.txt | 4 +
kernel-shark-qt/examples/datafilter.c | 124 ++++++++++++++++++++++++
2 files changed, 128 insertions(+)
create mode 100644 kernel-shark-qt/examples/datafilter.c
diff --git a/kernel-shark-qt/examples/CMakeLists.txt b/kernel-shark-qt/examples/CMakeLists.txt
index 98df9d8..009fd1e 100644
--- a/kernel-shark-qt/examples/CMakeLists.txt
+++ b/kernel-shark-qt/examples/CMakeLists.txt
@@ -3,3 +3,7 @@ message("\n examples ...")
message(STATUS "dataload")
add_executable(dload dataload.c)
target_link_libraries(dload kshark)
+
+message(STATUS "datafilter")
+add_executable(dfilter datafilter.c)
+target_link_libraries(dfilter kshark)
diff --git a/kernel-shark-qt/examples/datafilter.c b/kernel-shark-qt/examples/datafilter.c
new file mode 100644
index 0000000..e2039ca
--- /dev/null
+++ b/kernel-shark-qt/examples/datafilter.c
@@ -0,0 +1,124 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * Copyright (C) 2018 VMware Inc, Yordan Karadzhov <y.karadz@gmail.com>
+ */
+
+// C
+#include <stdio.h>
+#include <stdlib.h>
+
+// KernelShark
+#include "libkshark.h"
+
+const char *default_file = "trace.dat";
+
+int main(int argc, char **argv)
+{
+ size_t i, n_rows, n_tasks, n_evts, count;
+ struct kshark_context *kshark_ctx;
+ struct kshark_entry **data = NULL;
+ char *entry_str;
+ bool status;
+ int *pids;
+
+ /* 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);
+
+ /* Filter the trace data coming from trace-cmd. */
+ n_tasks = kshark_get_task_pids(kshark_ctx, &pids);
+ for (i = 0; i < n_tasks; ++i) {
+ const char *task_str =
+ pevent_data_comm_from_pid(kshark_ctx->pevent,
+ pids[i]);
+
+ if (strcmp(task_str, "trace-cmd") == 0)
+ kshark_filter_add_id(kshark_ctx, KS_HIDE_TASK_FILTER,
+ pids[i]);
+ }
+
+ free(pids);
+
+ /*
+ * Set the Filter Mask. In this case we want to avoid showing the
+ * filterd entris in text format.
+ */
+ kshark_ctx->filter_mask = KS_TEXT_VIEW_FILTER_MASK;
+ kshark_filter_entries(kshark_ctx, data, n_rows);
+
+ /* Print to the screen the first 10 visible entries. */
+ count = 0;
+ i = 0;
+ for (i = 0; i < n_rows; ++i) {
+ if (data[i]->visible & KS_TEXT_VIEW_FILTER_MASK) {
+ entry_str = kshark_dump_entry(data[i]);
+ puts(entry_str);
+ free(entry_str);
+
+ if (++count > 10)
+ break;
+ }
+
+ ++i;
+ }
+
+ puts("\n\n");
+
+ /* Show only "sched" events. */
+ struct event_format *event;
+ n_evts = kshark_ctx->pevent->nr_events;
+ for (i = 0; i < n_evts; ++i) {
+ event = kshark_ctx->pevent->events[i];
+ if (strcmp(event->system, "sched") == 0)
+ kshark_filter_add_id(kshark_ctx, KS_SHOW_EVENT_FILTER,
+ event->id);
+ }
+
+ kshark_filter_entries(kshark_ctx, data, n_rows);
+
+ /* Print to the screen the first 10 visible entries. */
+ count = 0;
+ i = 0;
+ for (i = 0; i < n_rows; ++i) {
+ if (data[i]->visible & KS_TEXT_VIEW_FILTER_MASK) {
+ entry_str = kshark_dump_entry(data[i]);
+ puts(entry_str);
+ free(entry_str);
+
+ if (++count > 10)
+ break;
+ }
+
+ ++i;
+ }
+
+ /* Free the memory. */
+ for (i = 0; i < n_rows; ++i)
+ free(data[i]);
+
+ free(data);
+
+ /* Close the file. */
+ kshark_close(kshark_ctx);
+
+ /* Close the session. */
+ kshark_free(kshark_ctx);
+
+ return 0;
+}
--
2.17.1
next prev parent reply other threads:[~2018-07-02 14:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-07-02 14:04 [PATCH v4 1/9] kernel-shark-qt: Add Cmake build system for the Qt based KernelShark Yordan Karadzhov (VMware)
2018-07-02 14:04 ` [PATCH v4 2/9] kernel-shark-qt: Automatic generation of doxygen documentation Yordan Karadzhov (VMware)
2018-07-02 14:04 ` [PATCH v4 3/9] kernel-shark-qt: Add API for loading trace.dat files Yordan Karadzhov (VMware)
2018-07-02 19:03 ` Steven Rostedt
2018-07-09 15:57 ` Steven Rostedt
2018-07-02 14:04 ` [PATCH v4 4/9] kernel-shark-qt: Add an example showing how to load trace data Yordan Karadzhov (VMware)
2018-07-02 14:04 ` [PATCH v4 5/9] kernel-shark-qt: Add a README file to trace-cmd/kernel-shark-qt Yordan Karadzhov (VMware)
2018-07-02 14:04 ` [PATCH v4 6/9] kernel-shark-qt: Add filtering to the C API of KernelShark Yordan Karadzhov (VMware)
2018-07-02 14:04 ` Yordan Karadzhov (VMware) [this message]
2018-07-02 14:04 ` [PATCH v4 8/9] kernel-shark-qt: Add Advanced filter to the session context Yordan Karadzhov (VMware)
2018-07-02 14:04 ` [PATCH v4 9/9] kernel-shark-qt: Add example of advanded filtering 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=20180702140424.23221-7-y.karadz@gmail.com \
--to=y.karadz@gmail.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).