Skip to contentSkip to conversation

To be included in KS 1.0

PATCH In review Yordan Karadzhov 2 participants 4 messages May 30, 2019 – Jun 5, 2019

lore
Share & tools
lore
#0 lore raw

cover To be included in KS 1.0

The patch-set contains modifications motivated by the attempt to implement a standalone version of the NumPy data interface. Those changes do improve the code but strictly speaking are not compulsory for KS 1.0. Nevertheless, I would prefer to get those patches upstream a.s.a.p. Versions of the two patches have been send already several times as part of different other patch-sets.

Yordan Karadzhov (2):
kernel-shark: Add new dataloading method to be used by the NumPu
interface
kernel-shark: Use full paths for non-standard library headers

 include/trace-cmd/trace-cmd.h         |   2 +-
 include/traceevent/event-parse.h      |   2 +-
 kernel-shark/CMakeLists.txt           |   3 +-
 kernel-shark/build/FindTraceCmd.cmake |  44 ++++----
 kernel-shark/src/libkshark-plugin.h   |   2 +-
 kernel-shark/src/libkshark.c          | 139 ++++++++++++++++++++++++++
 kernel-shark/src/libkshark.h          |  16 ++-
 7 files changed, 172 insertions(+), 36 deletions(-)
Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/2 kernel-shark: Add new dataloading method to be used by the NumPu interface

The new function loads the content of the trace data file into a table / matrix, made of columns / arrays of data having various integer types. Later those arrays will be wrapped as NumPy arrays.

---
 kernel-shark/src/libkshark.c | 139 +++++++++++++++++++++++++++++++++++
 kernel-shark/src/libkshark.h |   7 ++
 2 files changed, 146 insertions(+)
kernel-shark/src/libkshark.c +139 -0
index 175279c..608d570 100644
@@ -957,6 +957,145 @@ ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx,
 	return -ENOMEM;
 }
 
+static inline void free_ptr(void *ptr)
+{
+	if (ptr)
+		free(*(void **)ptr);
+}
+
+static bool data_matrix_alloc(size_t n_rows, uint64_t **offset_array,
+					     uint16_t **cpu_array,
+					     uint64_t **ts_array,
+					     uint16_t **pid_array,
+					     int **event_array)
+{
+	if (offset_array) {
+		*offset_array = calloc(n_rows, sizeof(**offset_array));
+		if (!*offset_array)
+			return false;
+	}
+
+	if (cpu_array) {
+		*cpu_array = calloc(n_rows, sizeof(**cpu_array));
+		if (!*cpu_array)
+			goto free_offset;
+	}
+
+	if (ts_array) {
+		*ts_array = calloc(n_rows, sizeof(**ts_array));
+		if (!*ts_array)
+			goto free_cpu;
+	}
+
+	if (pid_array) {
+		*pid_array = calloc(n_rows, sizeof(**pid_array));
+		if (!*pid_array)
+			goto free_ts;
+	}
+
+	if (event_array) {
+		*event_array = calloc(n_rows, sizeof(**event_array));
+		if (!*event_array)
+			goto free_pid;
+	}
+
+	return true;
+
+ free_pid:
+	free_ptr(pid_array);
+ free_ts:
+	free_ptr(ts_array);
+ free_cpu:
+	free_ptr(cpu_array);
+ free_offset:
+	free_ptr(offset_array);
+
+	fprintf(stderr, "Failed to allocate memory during data loading.\n");
+	return false;
+}
+
+/**
+ * @brief Load the content of the trace data file into a table / matrix made
+ *	  of columns / arrays of data. The user is responsible for freeing the
+ *	  elements of the outputted array
+ *
+ * @param kshark_ctx: Input location for the session context pointer.
+ * @param offset_array: Output location for the array of record offsets.
+ * @param cpu_array: Output location for the array of CPU Ids.
+ * @param ts_array: Output location for the array of timestamps.
+ * @param pid_array: Output location for the array of Process Ids.
+ * @param event_array: Output location for the array of Event Ids.
+ *
+ * @returns The size of the outputted arrays in the case of success, or a
+ *	    negative error code on failure.
+ */
+size_t kshark_load_data_matrix(struct kshark_context *kshark_ctx,
+			       uint64_t **offset_array,
+			       uint16_t **cpu_array,
+			       uint64_t **ts_array,
+			       uint16_t **pid_array,
+			       int **event_array)
+{
+	enum rec_type type = REC_ENTRY;
+	struct rec_list **rec_list;
+	size_t count, total = 0;
+	bool status;
+	int n_cpus;
+
+	total = get_records(kshark_ctx, &rec_list, type);
+	if (total < 0)
+		goto fail;
+
+	n_cpus = tracecmd_cpus(kshark_ctx->handle);
+
+	status = data_matrix_alloc(total, offset_array,
+					  cpu_array,
+					  ts_array,
+					  pid_array,
+					  event_array);
+	if (!status)
+		goto fail_free;
+
+	for (count = 0; count < total; count++) {
+		int next_cpu;
+
+		next_cpu = pick_next_cpu(rec_list, n_cpus, type);
+		if (next_cpu >= 0) {
+			struct rec_list *rec = rec_list[next_cpu];
+			struct kshark_entry *e = &rec->entry;
+
+			if (offset_array)
+				(*offset_array)[count] = e->offset;
+
+			if (cpu_array)
+				(*cpu_array)[count] = e->cpu;
+
+			if (ts_array)
+				(*ts_array)[count] = e->ts;
+
+			if (pid_array)
+				(*pid_array)[count] = e->pid;
+
+			if (event_array)
+				(*event_array)[count] = e->event_id;
+
+			rec_list[next_cpu] = rec_list[next_cpu]->next;
+			free(rec);
+		}
+	}
+
+	/* There should be no entries left in rec_list. */
+	free_rec_list(rec_list, n_cpus, type);
+	return total;
+
+ fail_free:
+	free_rec_list(rec_list, n_cpus, type);
+
+ fail:
+	fprintf(stderr, "Failed to allocate memory during data loading.\n");
+	return -ENOMEM;
+}
+
 static const char *kshark_get_latency(struct tep_handle *pe,
 				      struct tep_record *record)
 {
kernel-shark/src/libkshark.h +7 -0
index c218b61..27f7942 100644
@@ -149,6 +149,13 @@ ssize_t kshark_load_data_entries(struct kshark_context *kshark_ctx,
 ssize_t kshark_load_data_records(struct kshark_context *kshark_ctx,
 				 struct tep_record ***data_rows);
 
+size_t kshark_load_data_matrix(struct kshark_context *kshark_ctx,
+			       uint64_t **offset_array,
+			       uint16_t **cpu_array,
+			       uint64_t **ts_array,
+			       uint16_t **pid_array,
+			       int **event_array);
+
 ssize_t kshark_get_task_pids(struct kshark_context *kshark_ctx, int **pids);
 
 void kshark_close(struct kshark_context *kshark_ctx);
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/2 kernel-shark: Use full paths for non-standard library headers

In reply to Yordan Karadzhov

All header files included in the public interfaces of the trace-cmd and KernelShark libraries have to use full path when including other non-standard library headers. This will be useful if someone wants to use those public interfaces from there installation location.

You may need to clean Cmake's cache after aplaying this patch:

cd kernel-shark/build
./cmake_clean.sh
cmake ..

---
 include/trace-cmd/trace-cmd.h         |  2 +-
 include/traceevent/event-parse.h      |  2 +-
 kernel-shark/CMakeLists.txt           |  3 +-
 kernel-shark/build/FindTraceCmd.cmake | 44 +++++++++++----------------
 kernel-shark/src/libkshark-plugin.h   |  2 +-
 kernel-shark/src/libkshark.h          |  9 +++---
 6 files changed, 26 insertions(+), 36 deletions(-)
include/trace-cmd/trace-cmd.h +1 -1
index ceb03f4..9320098 100644
@@ -6,7 +6,7 @@
 #ifndef _TRACE_CMD_H
 #define _TRACE_CMD_H
 
-#include "event-parse.h"
+#include "traceevent/event-parse.h"
 
 #define ARRAY_SIZE(_a) (sizeof(_a) / sizeof((_a)[0]))
 #define __weak __attribute__((weak))
include/traceevent/event-parse.h +1 -1
index 5e0fd19..65cabd9 100644
@@ -12,7 +12,7 @@
 #include <regex.h>
 #include <string.h>
 
-#include "trace-seq.h"
+#include "traceevent/trace-seq.h"
 
 #ifndef __maybe_unused
 #define __maybe_unused __attribute__((unused))
kernel-shark/CMakeLists.txt +1 -2
index 52e4a29..145b058 100644
@@ -56,8 +56,7 @@ SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
 include_directories(${KS_DIR}/src/
                     ${KS_DIR}/build/src/
                     ${JSONC_INCLUDE_DIR}
-                    ${TRACECMD_INCLUDE_DIR}
-                    ${TRACEEVENT_INCLUDE_DIR})
+                    ${TRACECMD_INCLUDE_DIR})
 
 message("")
 message(STATUS "C flags      : " ${CMAKE_C_FLAGS})
kernel-shark/build/FindTraceCmd.cmake +18 -26

80 lines not on this page · raw message

kernel-shark/src/libkshark-plugin.h +1 -1
index 0cb677a..b3cf1c6 100644
@@ -17,7 +17,7 @@ extern "C" {
 #endif // __cplusplus
 
 // trace-cmd
-#include "event-parse.h"
+#include "traceevent/event-parse.h"
 
 /* Quiet warnings over documenting simple structures */
 //! @cond Doxygen_Suppress
kernel-shark/src/libkshark.h +4 -5
index 27f7942..fe35333 100644
@@ -18,17 +18,16 @@
 #include <errno.h>
 
 // Json-C
-#include <json.h>
+#include <json-c/json.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
 // trace-cmd
-#include "trace-cmd.h"
-#include "trace-filter-hash.h"
-#include "event-parse.h"
-#include "trace-filter-hash.h"
+#include "trace-cmd/trace-cmd.h"
+#include "trace-cmd/trace-filter-hash.h"
+#include "traceevent/event-parse.h"
 
 // KernelShark
 #include "libkshark-plugin.h"
Signature · 1 line
2.20.1
Reviewed-by on the v1 series
#3 lore raw
In reply to Yordan Karadzhov
Quoted 11 lines · On Thu, 30 May 2019 16:13:28 +0300 Yordan Karadzhov <[email protected]>…

The patch-set contains modifications motivated by the attempt to implement a standalone version of the NumPy data interface. Those changes do improve the code but strictly speaking are not compulsory for KS 1.0. Nevertheless, I would prefer to get those patches upstream a.s.a.p. Versions of the two patches have been send already several times as part of different other patch-sets.

Yordan Karadzhov (2):
kernel-shark: Add new dataloading method to be used by the NumPu
interface
kernel-shark: Use full paths for non-standard library headers

Looks good!

-- Steve

Quoted 10 lines
 include/trace-cmd/trace-cmd.h         |   2 +-
 include/traceevent/event-parse.h      |   2 +-
 kernel-shark/CMakeLists.txt           |   3 +-
 kernel-shark/build/FindTraceCmd.cmake |  44 ++++----
 kernel-shark/src/libkshark-plugin.h   |   2 +-
 kernel-shark/src/libkshark.c          | 139 ++++++++++++++++++++++++++
 kernel-shark/src/libkshark.h          |  16 ++-
 7 files changed, 172 insertions(+), 36 deletions(-)