Skip to contentSkip to conversation

kernel-shark-qt: Add Cmake build system for the Qt based KernelShark

PATCH In review Yordan Karadzhov (VMware) 2 participants 11 messages Jul 2, 2018 – Jul 9, 2018

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/9 kernel-shark-qt: Add Cmake build system for the Qt based KernelShark

This patch adds in /kernel-shark-qt a simple CMake building infrastructure, to be used by the new, Qt-based version of KernelShark. Currently, this does not build any objects. It only generates a header file (src/KsDeff.h) containing some user-specific definitions. The building infrastructure is necessary for the coming changes.

---
 kernel-shark-qt/CMakeLists.txt           | 35 ++++++++++++
 kernel-shark-qt/build/FindTraceCmd.cmake | 70 ++++++++++++++++++++++++
 kernel-shark-qt/build/cmake_clean.sh     |  8 +++
 kernel-shark-qt/build/deff.h.cmake       | 20 +++++++
 kernel-shark-qt/src/CMakeLists.txt       |  4 ++
 5 files changed, 137 insertions(+)
 create mode 100644 kernel-shark-qt/CMakeLists.txt
 create mode 100644 kernel-shark-qt/build/FindTraceCmd.cmake
 create mode 100755 kernel-shark-qt/build/cmake_clean.sh
 create mode 100644 kernel-shark-qt/build/deff.h.cmake
 create mode 100644 kernel-shark-qt/src/CMakeLists.txt
kernel-shark-qt/CMakeLists.txt new file +35 -0
new file mode 100644
index 0000000..56fca44
@@ -0,0 +1,35 @@
+# Check if cmake has the required version
+cmake_minimum_required(VERSION 2.8.11 FATAL_ERROR)
+
+# Set the name and version of the project
+project(kernel-shark-qt)
+
+set(KS_VERSION_MAJOR 0)
+set(KS_VERSION_MINOR 7)
+set(KS_VERSION_PATCH 0)
+set(KS_VERSION_STRING ${KS_VERSION_MAJOR}.${KS_VERSION_MINOR}.${KS_VERSION_PATCH})
+message("\n project: Kernel Shark: (version: ${KS_VERSION_STRING})\n")
+
+set(KS_DIR ${CMAKE_SOURCE_DIR})
+
+include(${KS_DIR}/build/FindTraceCmd.cmake)
+
+set(LIBRARY_OUTPUT_PATH    "${KS_DIR}/lib")
+set(EXECUTABLE_OUTPUT_PATH "${KS_DIR}/bin")
+
+set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -Wall -pthread")
+set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -pthread")
+
+include_directories(${KS_DIR}/src/
+                    ${KS_DIR}/build/src/
+                    ${TRACECMD_INCLUDE_DIR}
+                    ${TRACEEVENT_INCLUDE_DIR})
+
+message("")
+message(STATUS "C flags      : " ${CMAKE_C_FLAGS})
+message(STATUS "CXX flags    : " ${CMAKE_CXX_FLAGS})
+message(STATUS "Linker flags : " ${CMAKE_EXE_LINKER_FLAGS})
+
+add_subdirectory(${KS_DIR}/src)
+
+message("")
kernel-shark-qt/build/FindTraceCmd.cmake new file +70 -0

70 lines not on this page · raw message

kernel-shark-qt/build/cmake_clean.sh new file +8 -0
new file mode 100755
index 0000000..ee0abbb
@@ -0,0 +1,8 @@
+make clean
+rm CMakeCache.txt
+rm cmake_install.cmake
+rm Makefile
+rm -rf CMakeFiles/
+rm -rf src/
+rm -f ../lib/*
+rm -f ../src/KsDeff.h
kernel-shark-qt/build/deff.h.cmake new file +20 -0
new file mode 100644
index 0000000..111fcdd
@@ -0,0 +1,20 @@
+ /**
+ *  \file    KsDeff.h
+ *  \brief   This File is generated by CMAKE
+ */
+
+/* ! -- Do Not Hand Edit - This File is generated by CMAKE -- ! */
+
+#ifndef _KS_CONFIG_H
+#define _KS_CONFIG_H
+
+/** KernelShark Version number. */
+#cmakedefine KS_VERSION_STRING "@KS_VERSION_STRING@"
+
+/** KernelShark source code path. */
+#cmakedefine KS_DIR "@KS_DIR@"
+
+/** Location of the trace-cmd executable. */
+#cmakedefine TRACECMD_BIN_DIR "@TRACECMD_BIN_DIR@"
+
+#endif // _KS_CONFIG_H
kernel-shark-qt/src/CMakeLists.txt new file +4 -0
new file mode 100644
index 0000000..8c66424
@@ -0,0 +1,4 @@
+message("\n src ...")
+
+configure_file( ${KS_DIR}/build/deff.h.cmake
+                ${KS_DIR}/src/KsDeff.h)
Signature · 1 line
2.17.1
#1 lore raw

PATCH v4 2/9 kernel-shark-qt: Automatic generation of doxygen documentation

A possibility for automatic generation of doxygen documentation of the KernelShark code is added to the CMake build system. In order to generate such a documentation use the "_DOXYGEN_DOC" CMake Command-Line option.

example:
cd build/
cmake -D_DOXYGEN_DOC=1 ../
make

---
 kernel-shark-qt/CMakeLists.txt       | 17 +++++++++++++++++
 kernel-shark-qt/build/cmake_clean.sh |  2 ++
 kernel-shark-qt/doc/dox_config       | 15 +++++++++++++++
 3 files changed, 34 insertions(+)
 create mode 100644 kernel-shark-qt/doc/dox_config
kernel-shark-qt/CMakeLists.txt +17 -0
index 56fca44..9929937 100644
@@ -14,6 +14,8 @@ set(KS_DIR ${CMAKE_SOURCE_DIR})
 
 include(${KS_DIR}/build/FindTraceCmd.cmake)
 
+find_package(Doxygen)
+
 set(LIBRARY_OUTPUT_PATH    "${KS_DIR}/lib")
 set(EXECUTABLE_OUTPUT_PATH "${KS_DIR}/bin")
 
@@ -32,4 +34,19 @@ message(STATUS "Linker flags : " ${CMAKE_EXE_LINKER_FLAGS})
 
 add_subdirectory(${KS_DIR}/src)
 
+if (_DOXYGEN_DOC AND DOXYGEN_FOUND)
+
+    message("\n doxygen documentation ...")
+    add_custom_target(doc ALL)
+    add_custom_command(TARGET doc
+                       COMMAND doxygen dox_config > dox_build.log
+                       WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/doc)
+
+    set_property(DIRECTORY APPEND PROPERTY ADDITIONAL_MAKE_CLEAN_FILES
+                                          "${KS_DIR}/doc/dox_build.log"
+                                          "${KS_DIR}/doc/html"
+                                          "${KS_DIR}/doc/latex")
+
+endif ()
+
 message("")
kernel-shark-qt/build/cmake_clean.sh +2 -0
index ee0abbb..57c40a4 100755
@@ -6,3 +6,5 @@ rm -rf CMakeFiles/
 rm -rf src/
 rm -f ../lib/*
 rm -f ../src/KsDeff.h
+rm -f CMakeDoxyfile.in
+rm -f CMakeDoxygenDefaults.cmake
kernel-shark-qt/doc/dox_config new file +15 -0
new file mode 100644
index 0000000..b982baa
@@ -0,0 +1,15 @@
+# For detailed description of all Doxygen Configuration Parameters see:
+# http://www.doxygen.org/manual/config.html
+
+DOXYFILE_ENCODING      = UTF-8
+PROJECT_NAME           = "kernel-shark-qt"
+PROJECT_BRIEF = "Kernel Shark is a front-end reader of the Linux kernel tracing data."
+INPUT                  = "../src/"
+SOURCE_BROWSER         = YES
+QT_AUTOBRIEF           = YES
+TAB_SIZE               = 8
+OPTIMIZE_OUTPUT_FOR_C  = YES
+CASE_SENSE_NAMES       = YES
+SORT_MEMBER_DOCS       = NO
+STRICT_PROTO_MATCHING  = YES
+DOT_MULTI_TARGETS      = YES
Signature · 1 line
2.17.1
#2 lore raw

PATCH v4 3/9 kernel-shark-qt: Add API for loading trace.dat files

In reply to Yordan Karadzhov (VMware)

This patch introduces the first component of the C API used by the new Qt-based version of KernelShark. The patch includes only the part of the API responsible for loading data files generated by trace-cmd. The following patch will introduce an example, demonstrating the usage of this part of the API.

This version of the patch contains a number of improvements suggested by Steven Rostedt. Thanks Steven!

---
 kernel-shark-qt/src/CMakeLists.txt |   9 +
 kernel-shark-qt/src/libkshark.c    | 602 +++++++++++++++++++++++++++++
 kernel-shark-qt/src/libkshark.h    | 114 ++++++
 3 files changed, 725 insertions(+)
 create mode 100644 kernel-shark-qt/src/libkshark.c
 create mode 100644 kernel-shark-qt/src/libkshark.h
kernel-shark-qt/src/CMakeLists.txt +9 -0
index 8c66424..ed3c60e 100644
@@ -1,4 +1,13 @@
 message("\n src ...")
 
+message(STATUS "libkshark")
+add_library(kshark SHARED libkshark.c)
+
+target_link_libraries(kshark ${CMAKE_DL_LIBS}
+                             ${TRACEEVENT_LIBRARY}
+                             ${TRACECMD_LIBRARY})
+
+set_target_properties(kshark  PROPERTIES SUFFIX	".so.${KS_VERSION_STRING}")
+
 configure_file( ${KS_DIR}/build/deff.h.cmake
                 ${KS_DIR}/src/KsDeff.h)
kernel-shark-qt/src/libkshark.c new file +602 -0

602 lines not on this page · raw message

kernel-shark-qt/src/libkshark.h new file +114 -0

114 lines not on this page · raw message

Signature · 1 line
2.17.1
#3 lore raw

Re: [PATCH v4 3/9] kernel-shark-qt: Add API for loading trace.dat files

Quoted 28 lines · On Mon, 2 Jul 2018 17:04:18 +0300 "Yordan Karadzhov (VMware)"…
kernel-shark-qt/src/libkshark.c new file +28 -0
+/**
+ * @brief Get an array containing the Process Ids of all tasks presented in
+ *	  the loaded trace data file.
+ * @param kshark_ctx: Input location for context pointer.
+ * @param pids: Output location for the Pids of the tasks. The user is
+ *		responsible for freeing the elements of the outputted array.
+ * @returns The size of the outputted array of Pids in the case of success,
+ *	    or a negative error code on failure.
+ */
+ssize_t kshark_get_task_pids(struct kshark_context *kshark_ctx, int **pids)
+{
+	size_t i, pid_count = 0, pid_size = KS_TASK_HASH_SIZE;
+	struct kshark_task_list *list;
+	int *temp_pids;
+
+	*pids = calloc(pid_size, sizeof(int));
+	if(!*pids)
+		goto fail;
+
+	for (i = 0; i < KS_TASK_HASH_SIZE; ++i) {
+		list = kshark_ctx->tasks[i];
+		while (list) {
+			(*pids)[pid_count] = list->pid;
+			list = list->next;
+			if (++pid_count >= pid_size) {
+				pid_size *= 2;
+				temp_pids = realloc(*pids, pid_size * sizeof(int));
+				if (!temp_pids) {

kernel-shark-qt/src/libkshark.c line 276

Quoted 10 lines
kernel-shark-qt/src/libkshark.c new file +10 -0
+					goto fail;
+				}
+				*pids = temp_pids;
+			}
+		}
+	}
+
+	temp_pids = realloc(*pids, pid_count * sizeof(int));
+	if (!temp_pids)
+		goto fail;

Almost, but we need:

	*pids = temp_pids;

Otherwise, if the unlikely case of *pids gets reallocated (it shouldn't because we are shrinking *pids), it gets assigned properly. Yes, I know I'm being paranoid here, but it's better safe than sorry.

No need to send another patch, I'll add this myself.

Thanks!

-- Steve

Quoted 10 lines
kernel-shark-qt/src/libkshark.c new file +10 -0
+
+	return pid_count;
+
+fail:
+	fprintf(stderr, "Failed to allocate memory for Task Pids.\n");
+	free(*pids);
+	*pids = NULL;
+	return -ENOMEM;
+}
+
#4 lore raw

Re: [PATCH v4 3/9] kernel-shark-qt: Add API for loading trace.dat files

In reply to Yordan Karadzhov (VMware)
Quoted 23 lines · On Mon, 2 Jul 2018 17:04:18 +0300 "Yordan Karadzhov (VMware)"…
kernel-shark-qt/src/libkshark.h new file +17 -0
new file mode 100644
index 0000000..b59c94c
@@ -0,0 +1,114 @@
+/* SPDX-License-Identifier: LGPL-2.1 */
+
+/*
+ * Copyright (C) 2017 VMware Inc, Yordan Karadzhov <[email protected]>
+ */
+
+ /**
+ *  @file    libkshark.h
+ *  @brief   API for processing of FTRACE (trace-cmd) data.
+ */
+
+#ifndef _LIB_KSHARK_H
+#define _LIB_KSHARK_H
+
+// C
+#include <stdint.h>
+#include <pthread.h>

kernel-shark-qt/src/libkshark.h line 27

Quoted 10 lines
kernel-shark-qt/src/libkshark.h new file +10 -0
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// trace-cmd
+#include "trace-cmd.h"
+#include "event-parse.h"
+#include "trace-filter-hash.h"
+

Making updates, I notice some issues here.

Quoted 5 lines
kernel-shark-qt/src/libkshark.h new file +5 -0
+/**
+ * Kernel Shark entry contains all information from one trace record needed
+ * in order to  visualize the time-series of trace records. The part of the
+ * data which is not directly required for the visualization (latency, record
+ * info etc.) is available on-demand via the offset into the trace file.

kernel-shark-qt/src/libkshark.h line 42

Quoted 10 lines
kernel-shark-qt/src/libkshark.h new file +10 -0
+ */
+struct kshark_entry {
+	/**
+	 * A bit mask controlling the visibility of the entry. A value of OxFF
+	 * would mean that the entry is visible everywhere.
+	 */
+	uint8_t		visible;
+
+	/** The CPU core of the record. */
+	uint8_t		cpu;

One byte is too small. We already have boxes that I trace with over 1000 CPUs.

kernel-shark-qt/src/libkshark.h line 45

Quoted 3 lines
kernel-shark-qt/src/libkshark.h new file +3 -0
+
+	/** The PID of the task the record was generated. */
+	int16_t		pid;

16bits is too small for pid. There's work to have it grow to 32 bits.

I know it's going to take up more memory, but both of these will need to grow to at 32bits. Perhaps we can combine the cpu and visible into one 32 bits, where we reserve the MSBs for flags for visible. But to do that, one needs to be concerned about updates to them.

-- Steve

Quoted 19 lines
kernel-shark-qt/src/libkshark.h new file +19 -0
+
+	/** Unique Id ot the trace event type. */
+	int		event_id;
+
+	/** The offset into the trace file, used to find the record. */
+	uint64_t	offset;
+
+	/**
+	 * The time of the record in nano seconds. The value is taken from
+	 * the timestamps within the trace data file, which are architecture
+	 * dependent. The time usually is the timestamp from when the system
+	 * started.
+	 */
+	uint64_t	ts;
+
+	/** Pointer to the next (in time) kshark_entry on the same CPU core. */
+	struct kshark_entry *next;
+};
+
#5 lore raw

PATCH v4 4/9 kernel-shark-qt: Add an example showing how to load trace data

In reply to Yordan Karadzhov (VMware)

This patch introduces a basic example, showing how to use the C API of KernelShark and open a new session, load a trace data file, produced by trace-cmd, print some data to the screen, cleanup and then exit.

---
 kernel-shark-qt/CMakeLists.txt          |  1 +
 kernel-shark-qt/build/cmake_clean.sh    |  1 +
 kernel-shark-qt/examples/CMakeLists.txt |  5 ++
 kernel-shark-qt/examples/dataload.c     | 87 +++++++++++++++++++++++++
 4 files changed, 94 insertions(+)
 create mode 100644 kernel-shark-qt/examples/CMakeLists.txt
 create mode 100644 kernel-shark-qt/examples/dataload.c
kernel-shark-qt/CMakeLists.txt +1 -0
index 9929937..9ff12a1 100644
@@ -33,6 +33,7 @@ message(STATUS "CXX flags    : " ${CMAKE_CXX_FLAGS})
 message(STATUS "Linker flags : " ${CMAKE_EXE_LINKER_FLAGS})
 
 add_subdirectory(${KS_DIR}/src)
+add_subdirectory(${KS_DIR}/examples)
 
 if (_DOXYGEN_DOC AND DOXYGEN_FOUND)
 
kernel-shark-qt/build/cmake_clean.sh +1 -0
index 57c40a4..acdbb43 100755
@@ -4,6 +4,7 @@ rm cmake_install.cmake
 rm Makefile
 rm -rf CMakeFiles/
 rm -rf src/
+rm -rf examples/
 rm -f ../lib/*
 rm -f ../src/KsDeff.h
 rm -f CMakeDoxyfile.in
kernel-shark-qt/examples/CMakeLists.txt new file +5 -0
new file mode 100644
index 0000000..98df9d8
@@ -0,0 +1,5 @@
+message("\n examples ...")
+
+message(STATUS "dataload")
+add_executable(dload          dataload.c)
+target_link_libraries(dload   kshark)
kernel-shark-qt/examples/dataload.c new file +87 -0

87 lines not on this page · raw message

Signature · 1 line
2.17.1
#6 lore raw

PATCH v4 5/9 kernel-shark-qt: Add a README file to trace-cmd/kernel-shark-qt

In reply to Yordan Karadzhov (VMware)

The README file contains instructions for building and using the Qt-based version of KernelShark.

---
 kernel-shark-qt/README | 44 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 44 insertions(+)
 create mode 100644 kernel-shark-qt/README
kernel-shark-qt/README new file +44 -0
new file mode 100644
index 0000000..6ff77a5
@@ -0,0 +1,44 @@
+
+This directory contains the new Qt-based version of the KernelShark GUI.
+
+
+Third Party Software:
+------------------------------------------------------------
+The external dependencies:
+1. In order to install the packages on Ubuntu do the following:
+    sudo apt-get install build-essential git cmake -y
+
+1.1 I you want to be able to generate Doxygen documentation:
+    sudo apt-get install graphviz doxygen-gui -y
+
+
+2. In order to install the packages on Fedora, as root do the following:
+    dnf install gcc gcc-c++ git cmake -y
+
+2.1 I you want to be able to generate Doxygen documentation:
+    dnf install graphviz doxygen -y
+
+
+Building:
+------------------------------------------------------------
+1. Follow the instructions given in trace-cmd/README and build
+the original trace-cmd end traceevent libraries.
+
+2. Do:
+    cd kernel-shark-qt/build
+    cmake ../
+    make
+
+2.1 In order to create a Doxygen documentation add -D_DOXYGEN_DOC=1
+as a CMake Command-Line option.
+    cd kernel-shark-qt/build
+    cmake -D_DOXYGEN_DOC=1 ../
+    make
+
+2.2.1 Use "make clean" if you want to delete all already compiled object.
+
+2.2.2 Use the script "cmake_clean.sh" if you want to delete all already
+compiled object and all files generated by CMake.
+
+3. After building the code "kernel-shark-qt/lib" will contain all libraries
+and "kernel-shark-qt/bin" will contain all executables.
Signature · 1 line
2.17.1
#7 lore raw

PATCH v4 6/9 kernel-shark-qt: Add filtering to the C API of KernelShark

In reply to Yordan Karadzhov (VMware)

Instruments for trace data filtering are added to the C API of the Qt-based version of KernelShark. The following patch will introduces an example, demonstrating the usage of this instruments.

This version of the patch contains a number of improvements suggested by Steven Rostedt. Thanks Steven!

---
 kernel-shark-qt/src/libkshark.c | 178 ++++++++++++++++++++++++++++++++
 kernel-shark-qt/src/libkshark.h |  79 +++++++++++++-
 2 files changed, 256 insertions(+), 1 deletion(-)
kernel-shark-qt/src/libkshark.c +178 -0
index a5349ed..4d86f9c 100644
@@ -33,6 +33,14 @@ static bool kshark_default_context(struct kshark_context **context)
 	if (!kshark_ctx)
 		return false;
 
+	kshark_ctx->show_task_filter = tracecmd_filter_id_hash_alloc();
+	kshark_ctx->hide_task_filter = tracecmd_filter_id_hash_alloc();
+
+	kshark_ctx->show_event_filter = tracecmd_filter_id_hash_alloc();
+	kshark_ctx->hide_event_filter = tracecmd_filter_id_hash_alloc();
+
+	kshark_ctx->filter_mask = 0x0;
+
 	/* Will free kshark_context_handler. */
 	kshark_free(NULL);
 
@@ -154,6 +162,15 @@ void kshark_close(struct kshark_context *kshark_ctx)
 	if (!kshark_ctx || !kshark_ctx->handle)
 		return;
 
+	/*
+	 * All Id filters are file specific. Make sure that the Pids and Event Ids
+	 * from this file are not going to be used with another file.
+	 */
+	tracecmd_filter_id_clear(kshark_ctx->show_task_filter);
+	tracecmd_filter_id_clear(kshark_ctx->hide_task_filter);
+	tracecmd_filter_id_clear(kshark_ctx->show_event_filter);
+	tracecmd_filter_id_clear(kshark_ctx->hide_event_filter);
+
 	tracecmd_close(kshark_ctx->handle);
 	kshark_ctx->handle = NULL;
 	kshark_ctx->pevent = NULL;
@@ -179,6 +196,12 @@ void kshark_free(struct kshark_context *kshark_ctx)
 		/* kshark_ctx_handler will be set to NULL below. */
 	}
 
+	tracecmd_filter_id_hash_free(kshark_ctx->show_task_filter);
+	tracecmd_filter_id_hash_free(kshark_ctx->hide_task_filter);
+
+	tracecmd_filter_id_hash_free(kshark_ctx->show_event_filter);
+	tracecmd_filter_id_hash_free(kshark_ctx->hide_event_filter);
+
 	kshark_free_task_list(kshark_ctx);
 
 	if (seq.buffer)
@@ -284,6 +307,148 @@ fail:
 	return -ENOMEM;
 }
 
+static bool filter_find(struct tracecmd_filter_id *filter, int pid,
+			bool test)
+{
+	return !filter || !filter->count ||
+		!!(unsigned long)tracecmd_filter_id_find(filter, pid) == test;
+}
+
+static bool kshark_show_task(struct kshark_context *kshark_ctx, int pid)
+{
+	return filter_find(kshark_ctx->show_task_filter, pid, true) &&
+	       filter_find(kshark_ctx->hide_task_filter, pid, false);
+}
+
+static bool kshark_show_event(struct kshark_context *kshark_ctx, int pid)
+{
+	return filter_find(kshark_ctx->show_event_filter, pid, true) &&
+	       filter_find(kshark_ctx->hide_event_filter, pid, false);
+}
+
+/**
+ * @brief Add an Id value to the filster specified by "filter_id".
+ * @param kshark_ctx: Input location for the session context pointer.
+ * @param filter_id: Identifier of the filter.
+ * @param id: Id value to be added to the filter.
+ */
+void kshark_filter_add_id(struct kshark_context *kshark_ctx,
+			  int filter_id, int id)
+{
+	struct tracecmd_filter_id *filter;
+
+	switch (filter_id) {
+		case KS_SHOW_EVENT_FILTER:
+			filter = kshark_ctx->show_event_filter;
+			break;
+		case KS_HIDE_EVENT_FILTER:
+			filter = kshark_ctx->hide_event_filter;
+			break;
+		case KS_SHOW_TASK_FILTER:
+			filter = kshark_ctx->show_task_filter;
+			break;
+		case KS_HIDE_TASK_FILTER:
+			filter = kshark_ctx->hide_task_filter;
+			break;
+		default:
+			return;
+	}
+
+	tracecmd_filter_id_add(filter, id);
+}
+
+/**
+ * @brief Clear (reset) the filster specified by "filter_id".
+ * @param kshark_ctx: Input location for the session context pointer.
+ * @param filter_id: Identifier of the filter.
+ */
+void kshark_filter_clear(struct kshark_context *kshark_ctx, int filter_id)
+{
+	struct tracecmd_filter_id *filter;
+
+	switch (filter_id) {
+		case KS_SHOW_EVENT_FILTER:
+			filter = kshark_ctx->show_event_filter;
+			break;
+		case KS_HIDE_EVENT_FILTER:
+			filter = kshark_ctx->hide_event_filter;
+			break;
+		case KS_SHOW_TASK_FILTER:
+			filter = kshark_ctx->show_task_filter;
+			break;
+		case KS_HIDE_TASK_FILTER:
+			filter = kshark_ctx->hide_task_filter;
+			break;
+		default:
+			return;
+	}
+
+	tracecmd_filter_id_clear(filter);
+}
+
+static bool filter_is_set(struct tracecmd_filter_id *filter)
+{
+	return filter && filter->count;
+}
+
+static bool kshark_filter_is_set(struct kshark_context *kshark_ctx)
+{
+	return filter_is_set(kshark_ctx->show_task_filter) ||
+	       filter_is_set(kshark_ctx->hide_task_filter) ||
+	       filter_is_set(kshark_ctx->show_event_filter) ||
+	       filter_is_set(kshark_ctx->hide_event_filter);
+}
+
+static void unset_event_filter_flag(struct kshark_context *kshark_ctx,
+				    struct kshark_entry *e)
+{
+	/*
+	 * All entries, filtered-out by the event filters, will be treated
+	 * differently, when visualized. Because of this, ignore the value
+	 * of the GRAPH_VIEW flag provided by the user via
+	 * kshark_ctx->filter_mask and unset the EVENT_VIEW flag.
+	 */
+	int event_mask = kshark_ctx->filter_mask;
+
+	event_mask &= ~KS_GRAPH_VIEW_FILTER_MASK;
+	event_mask |= KS_EVENT_VIEW_FILTER_MASK;
+	e->visible &= ~event_mask;
+}
+
+/**
+ * @brief This function loops over the array of entries specified by "data"
+ *	  and "n_entries" and sets the "visible" fields of each entry
+ *	  according to the criteria provided by the filters of the session's
+ *	  context. The field "filter_mask" of the session's context is used to
+ *	  control the level of visibility/invisibility of the entries which
+ *	  are filtered-out.
+ * @param kshark_ctx: Input location for the session context pointer.
+ * @param data: Input location for the trace data to be filtered.
+ * @param n_entries: The size of the inputted data.
+ */
+void kshark_filter_entries(struct kshark_context *kshark_ctx,
+			   struct kshark_entry **data,
+			   size_t n_entries)
+{
+	int i;
+
+	if (!kshark_filter_is_set(kshark_ctx))
+		return;
+
+	for (i = 0; i < n_entries; ++i) {
+		/* Start with and entry which is visible everywhere. */
+		data[i]->visible = 0xFF;
+
+		/* Apply event filtering. */
+		if (!kshark_show_event(kshark_ctx, data[i]->event_id))
+			unset_event_filter_flag(kshark_ctx, data[i]);
+
+		/* Apply task filtering. */
+		if (!kshark_show_task(kshark_ctx, data[i]->pid))
+			data[i]->visible &= ~kshark_ctx->filter_mask;
+	}
+}
+
 static void kshark_set_entry_values(struct kshark_context *kshark_ctx,
 				    struct pevent_record *record,
 				    struct kshark_entry *entry)
@@ -316,6 +481,10 @@ static void kshark_set_entry_values(struct kshark_context *kshark_ctx,
  *	  "latency" and the "info" fields can be accessed only via the offset
  *	  into the file. This makes the access to these two fields much
  *	  slower.
+ *	  If one or more filters are set, the "visible" fields of each entry
+ *	  is updated according to the criteria provided by the filters. The
+ *	  field "filter_mask" of the session's context is used to control the
+ *	  level of visibility/invisibility of the filtered entries.
  * @param kshark_ctx: Input location for context pointer.
  * @param data_rows: Output location for the trace data. The user is
  *		     responsible for freeing the elements of the outputted
@@ -356,6 +525,15 @@ ssize_t kshark_load_data_entries(struct kshark_context *kshark_ctx,
 			if (!task)
 				goto fail;
 
+			/* Apply event filtering. */
+			if (!kshark_show_event(kshark_ctx, entry->event_id))
+				unset_event_filter_flag(kshark_ctx, entry);
+
+			/* Apply task filtering. */
+			if (!kshark_show_task(kshark_ctx, entry->pid)) {
+				entry->visible &= ~kshark_ctx->filter_mask;
+			}
+
 			entry->next = NULL;
 			next = &entry->next;
 			free_record(rec);
kernel-shark-qt/src/libkshark.h +78 -1

103 lines not on this page · raw message

Signature · 1 line
2.17.1
#8 lore raw

PATCH v4 7/9 kernel-shark-qt: Add an example showing how to filter trace data

In reply to Yordan Karadzhov (VMware)

This patch introduces a basic example, showing how to use the C API of KernelShark in order to filter trace data.

---
 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
kernel-shark-qt/examples/CMakeLists.txt +4 -0
index 98df9d8..009fd1e 100644
@@ -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)
kernel-shark-qt/examples/datafilter.c new file +124 -0

124 lines not on this page · raw message

Signature · 1 line
2.17.1
#9 lore raw

PATCH v4 8/9 kernel-shark-qt: Add Advanced filter to the session context.

In reply to Yordan Karadzhov (VMware)

This patch adds to the KernelShark session's context instrumentation for sophisticated filtering based on the content of the trace event.

---
 kernel-shark-qt/src/libkshark.c | 34 +++++++++++++++++++++++++++++++--
 kernel-shark-qt/src/libkshark.h |  6 ++++++
 2 files changed, 38 insertions(+), 2 deletions(-)
kernel-shark-qt/src/libkshark.c +32 -2
index 4d86f9c..13d7e6d 100644
@@ -143,6 +143,9 @@ bool kshark_open(struct kshark_context *kshark_ctx, const char *file)
 	kshark_ctx->handle = handle;
 	kshark_ctx->pevent = tracecmd_get_pevent(handle);
 
+	kshark_ctx->advanced_event_filter =
+		pevent_filter_alloc(kshark_ctx->pevent);
+
 	/*
 	 * Turn off function trace indent and turn on show parent
 	 * if possible.
@@ -163,7 +166,7 @@ void kshark_close(struct kshark_context *kshark_ctx)
 		return;
 
 	/*
-	 * All Id filters are file specific. Make sure that the Pids and Event Ids
+	 * All filters are file specific. Make sure that the Pids and Event Ids
 	 * from this file are not going to be used with another file.
 	 */
 	tracecmd_filter_id_clear(kshark_ctx->show_task_filter);
@@ -171,6 +174,12 @@ void kshark_close(struct kshark_context *kshark_ctx)
 	tracecmd_filter_id_clear(kshark_ctx->show_event_filter);
 	tracecmd_filter_id_clear(kshark_ctx->hide_event_filter);
 
+	if (kshark_ctx->advanced_event_filter) {
+		pevent_filter_reset(kshark_ctx->advanced_event_filter);
+		pevent_filter_free(kshark_ctx->advanced_event_filter);
+		kshark_ctx->advanced_event_filter = NULL;
+	}
+
 	tracecmd_close(kshark_ctx->handle);
 	kshark_ctx->handle = NULL;
 	kshark_ctx->pevent = NULL;
@@ -422,6 +431,9 @@ static void unset_event_filter_flag(struct kshark_context *kshark_ctx,
  *	  context. The field "filter_mask" of the session's context is used to
  *	  control the level of visibility/invisibility of the entries which
  *	  are filtered-out.
+ *	  WARNING: Do not use this function if the advanced filter is set.
+ *	  Applying the advanced filter requires access to prevent_record,
+ *	  hence the data has to be reloaded using kshark_load_data_entries().
  * @param kshark_ctx: Input location for the session context pointer.
  * @param data: Input location for the trace data to be filtered.
  * @param n_entries: The size of the inputted data.
@@ -432,9 +444,19 @@ void kshark_filter_entries(struct kshark_context *kshark_ctx,
 {
 	int i;
 
+	if (kshark_ctx->advanced_event_filter->filters) {
+		/* The advanced filter is set. */
+		fprintf(stderr,
+			"Failed to filter!\n");
+		fprintf(stderr,
+			"Reset the Advanced filter or reload the data.\n");
+		return;
+	}
+
 	if (!kshark_filter_is_set(kshark_ctx))
 		return;
 
+	/* Apply only the Id filters. */
 	for (i = 0; i < n_entries; ++i) {
 		/* Start with and entry which is visible everywhere. */
 		data[i]->visible = 0xFF;
@@ -495,6 +517,7 @@ static void kshark_set_entry_values(struct kshark_context *kshark_ctx,
 ssize_t kshark_load_data_entries(struct kshark_context *kshark_ctx,
 				struct kshark_entry ***data_rows)
 {
+	struct event_filter *adv_filter = kshark_ctx->advanced_event_filter;
 	struct kshark_entry **cpu_list, **rows;
 	struct kshark_entry *entry, **next;
 	struct kshark_task_list *task;
@@ -502,6 +525,7 @@ ssize_t kshark_load_data_entries(struct kshark_context *kshark_ctx,
 	int cpu, n_cpus, next_cpu;
 	size_t count, total = 0;
 	uint64_t ts;
+	int ret;
 
 	if (*data_rows)
 		free(*data_rows);
@@ -526,8 +550,14 @@ ssize_t kshark_load_data_entries(struct kshark_context *kshark_ctx,
 				goto fail;
 
 			/* Apply event filtering. */
-			if (!kshark_show_event(kshark_ctx, entry->event_id))
+			ret = FILTER_NONE;
+			if (adv_filter->filters)
+				ret = pevent_filter_match(adv_filter, rec);
+
+			if (!kshark_show_event(kshark_ctx, entry->event_id) ||
+			    ret != FILTER_MATCH) {
 				unset_event_filter_flag(kshark_ctx, entry);
+			}
 
 			/* Apply task filtering. */
 			if (!kshark_show_task(kshark_ctx, entry->pid)) {
kernel-shark-qt/src/libkshark.h +6 -0
index dfb1be5..6ed2a1e 100644
@@ -109,6 +109,12 @@ struct kshark_context {
 	 * have this bit unset in their "visible" fields.
 	 */
 	uint8_t				filter_mask;
+
+	/**
+	 * Filter allowing sophisticated filtering based on the content of
+	 * the event.
+	 */
+	struct event_filter		*advanced_event_filter;
 };
 
 bool kshark_instance(struct kshark_context **kshark_ctx);
Signature · 1 line
2.17.1
#10 lore raw

PATCH v4 9/9 kernel-shark-qt: Add example of advanded filtering

In reply to Yordan Karadzhov (VMware)

The filtering example is extended and now demonstrates filtering based on the content of the event.

---
 kernel-shark-qt/examples/datafilter.c | 29 +++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
kernel-shark-qt/examples/datafilter.c +29 -0
index e2039ca..f8048bf 100644
@@ -108,6 +108,35 @@ int main(int argc, char **argv)
 		++i;
 	}
 
+	puts("\n\n");
+
+	/* Clear all filters. */
+	kshark_filter_clear(kshark_ctx, KS_HIDE_TASK_FILTER);
+	kshark_filter_clear(kshark_ctx, KS_SHOW_EVENT_FILTER);
+
+	/* Use the Advanced filter to do event content based filtering. */
+	struct event_filter *adv_filter = kshark_ctx->advanced_event_filter;
+	pevent_filter_add_filter_str(adv_filter,
+				     "sched/sched_wakeup:target_cpu==1");
+
+	/* The Advanced filter requires reloading the data. */
+	for (i = 0; i < n_rows; ++i)
+		free(data[i]);
+
+	n_rows = kshark_load_data_entries(kshark_ctx, &data);
+
+	count = 0;
+	for (i = 0; i < n_rows; ++i) {
+		if (data[i]->visible & KS_EVENT_VIEW_FILTER_MASK) {
+			entry_str = kshark_dump_entry(data[i]);
+			puts(entry_str);
+			free(entry_str);
+
+			if(++count > 10)
+				break;
+		}
+	}
+
 	/* Free the memory. */
 	for (i = 0; i < n_rows; ++i)
 		free(data[i]);
Signature · 1 line
2.17.1