linux-trace-devel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
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 v2 14/27] kernel-shark: Update the plotting example
Date: Thu, 11 Feb 2021 12:31:52 +0200	[thread overview]
Message-ID: <20210211103205.418588-15-y.karadz@gmail.com> (raw)
In-Reply-To: <20210211103205.418588-1-y.karadz@gmail.com>

The compilation of the plotting example is re-enabled and it is
made compatible with the new version of the C API of libkshark
(KernelShark 2.0).

Signed-off-by: Yordan Karadzhov (VMware) <y.karadz@gmail.com>
---
 examples/CMakeLists.txt |  6 +--
 examples/dataplot.cpp   | 88 ++++++++++++++++++++++++++++-------------
 2 files changed, 63 insertions(+), 31 deletions(-)

diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index 8360841..e6af5f2 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -20,9 +20,9 @@ message(STATUS "confogio")
 add_executable(confio          configio.c)
 target_link_libraries(confio   kshark)
 
-# message(STATUS "dataplot")
-# add_executable(dplot          dataplot.cpp)
-# target_link_libraries(dplot   kshark-plot)
+message(STATUS "dataplot")
+add_executable(dplot          dataplot.cpp)
+target_link_libraries(dplot   kshark-plot)
 
 if (Qt5Widgets_FOUND)
 
diff --git a/examples/dataplot.cpp b/examples/dataplot.cpp
index 94841e7..b3ff29f 100644
--- a/examples/dataplot.cpp
+++ b/examples/dataplot.cpp
@@ -10,6 +10,7 @@
 // C++
 #include <vector>
 #include <iostream>
+#include <sstream>
 
 // OpenGL
 #include <GL/freeglut.h>
@@ -20,25 +21,29 @@
 
 using namespace std;
 
-#define GRAPH_HEIGHT	40   // width of the graph in pixels
-#define GRAPH_H_MARGIN	50   // size of the white space surrounding the graph
-#define WINDOW_WIDTH	800  // width of the screen window in pixels
-#define WINDOW_HEIGHT	480  // height of the scrren window in pixels
+#define GRAPH_HEIGHT		40   // width of the graph in pixels
+#define GRAPH_H_MARGIN		15   // size of the white space surrounding
+				     // the graph
+#define GRAPH_LABEL_WIDTH	80   // width of the graph's label in pixels
+#define WINDOW_WIDTH		800  // width of the screen window in pixels
+#define WINDOW_HEIGHT		480  // height of the scrren window in pixels
 
 #define default_file (char*)"trace.dat"
 
 struct kshark_trace_histo	histo;
 vector<KsPlot::Graph *>		graphs;
+struct ksplot_font		font;
+int stream_id;
 
 void usage(const char *prog)
 {
 	cout << "Usage: " << prog << endl;
-	cout << "  -h	Display this help message\n";
+	cout << "  -h	Display this help message.\n";
 	cout << "  -s	Draw shapes. This demonstrates how to draw simple "
 	     << "geom. shapes.\n";
 	cout << "  -i	<file>	Input file and draw animated graphs.\n";
 	cout << "  No args.	Import " << default_file
-	     << "and draw animated graphs.\n";
+	     << " and draw animated graphs.\n";
 }
 
 /* An example function drawing something. */
@@ -57,6 +62,11 @@ void drawShapes()
 	t._color = {100, 200, 50};
 	t.draw();
 
+	/* Print/draw "Hello Kernel!". */
+	KsPlot::Color col = {50, 150, 255};
+	KsPlot::TextBox tb(&font, "Hello Kernel!", col, {250, 70}, 250);
+	tb.draw();
+
 	KsPlot::Rectangle r;
 	KsPlot::Point d(400, 200), e(400, 300), f(500, 300), g(500, 200);
 	r.setPoint(0, d);
@@ -78,13 +88,14 @@ void drawShapes()
 /* An example function demonstrating Zoom In and Zoom Out. */
 void play()
 {
-	KsPlot::ColorTable taskColors = KsPlot::getTaskColorTable();
-	KsPlot::ColorTable cpuColors = KsPlot::getCPUColorTable();
+	KsPlot::ColorTable taskColors = KsPlot::taskColorTable();
+	KsPlot::ColorTable cpuColors = KsPlot::CPUColorTable();
 	vector<KsPlot::Graph *>::iterator it;
+	KsPlot::Graph *graph;
 	vector<int> CPUs, Tasks;
 	bool zoomIn(true);
 	int base;
-	size_t i;
+	size_t i(1);
 
 	CPUs = {3, 4, 6};
 	Tasks = {}; // Add valid pids here, if you want task plots.
@@ -92,35 +103,47 @@ void play()
 	auto lamAddGraph = [&] (KsPlot::Graph *g) {
 		/* Set the dimensions of the Graph. */
 		g->setHeight(GRAPH_HEIGHT);
-		g->setHMargin(GRAPH_H_MARGIN);
 
 		/*
 		 * Set the Y coordinate of the Graph's base.
 		 * Remember that the "Y" coordinate is inverted.
 		 */
-		base = 1.7 * GRAPH_HEIGHT * (i + 1);
+		base = 1.7 * GRAPH_HEIGHT * (i++);
 		g->setBase(base);
 
+		g->setLabelAppearance(&font, {160, 255, 255}, GRAPH_LABEL_WIDTH,
+							      GRAPH_H_MARGIN);
+
 		/* Add the Graph. */
 		graphs.push_back(g);
 	};
 
-	for (i = 0; i < CPUs.size(); ++i)
-		lamAddGraph(new KsPlot::Graph(&histo, &taskColors,
-						      &taskColors));
+	for (auto const &cpu: CPUs) {
+		std::stringstream ss;
+		ss << "CPU " << cpu;
+
+		graph = new KsPlot::Graph(&histo, &taskColors, &taskColors);
+		graph->setLabelText(ss.str());
+		lamAddGraph(graph);
+	}
+
+	for (auto const &pid: Tasks) {
+		std::stringstream ss;
+		ss << "PID " << pid;
 
-	for (;i < CPUs.size() + Tasks.size(); ++i)
-		lamAddGraph(new KsPlot::Graph(&histo, &taskColors,
-						      &cpuColors));
+		graph = new KsPlot::Graph(&histo, &taskColors, &cpuColors);
+		graph->setLabelText(ss.str());
+		lamAddGraph(graph);
+	}
 
 	for (i = 1; i < 1000; ++i) {
 		it = graphs.begin();
 
 		for (int const &cpu: CPUs)
-			(*it++)->fillCPUGraph(cpu);
+			(*it++)->fillCPUGraph(stream_id, cpu);
 
 		for (int const &pid: Tasks)
-			(*it++)->fillTaskGraph(pid);
+			(*it++)->fillTaskGraph(stream_id, pid);
 
 		/* Clear the screen. */
 		glClear(GL_COLOR_BUFFER_BIT);
@@ -146,7 +169,8 @@ int main(int argc, char **argv)
 	struct kshark_context *kshark_ctx(nullptr);
 	struct kshark_entry **data(nullptr);
 	static char *input_file(nullptr);
-	bool status, shapes(false);
+	bool shapes(false);
+	char *font_file;
 	size_t r, nRows;
 	int c, nBins;
 
@@ -166,11 +190,20 @@ int main(int argc, char **argv)
 		}
 	}
 
+	font_file = ksplot_find_font_file("FreeMono", "FreeMonoBold");
+	if (!font_file)
+		return 1;
+
 	auto lamDraw = [&] (void (*func)(void)) {
-		/* Initialize OpenGL/Glut. */
+		/* Initialize Glut. */
 		glutInit(&argc, argv);
 		ksplot_make_scene(WINDOW_WIDTH, WINDOW_HEIGHT);
+
+		/* Initialize OpenGL. */
 		ksplot_init_opengl(1);
+		ksplot_resize_opengl(WINDOW_WIDTH, WINDOW_HEIGHT);
+
+		ksplot_init_font(&font, 18, font_file);
 
 		/* Display something. */
 		glutDisplayFunc(func);
@@ -191,8 +224,8 @@ int main(int argc, char **argv)
 	if (!input_file)
 		input_file = default_file;
 
-	status = kshark_open(kshark_ctx, input_file);
-	if (!status) {
+	stream_id = kshark_open(kshark_ctx, input_file);
+	if (stream_id < 0) {
 		kshark_free(kshark_ctx);
 		usage(argv[0]);
 		cerr << "\nFailed to open file " << input_file << endl;
@@ -201,12 +234,12 @@ int main(int argc, char **argv)
 	}
 
 	/* Load the content of the file into an array of entries. */
-	nRows = kshark_load_data_entries(kshark_ctx, &data);
+	nRows = kshark_load_entries(kshark_ctx, stream_id, &data);
 
 	/* Initialize the Visualization Model. */
 	ksmodel_init(&histo);
 
-	nBins = WINDOW_WIDTH - 2 * GRAPH_HEIGHT;
+	nBins = WINDOW_WIDTH - GRAPH_LABEL_WIDTH - 3 * GRAPH_H_MARGIN;
 	ksmodel_set_bining(&histo, nBins, data[0]->ts,
 					  data[nRows - 1]->ts);
 
@@ -216,6 +249,8 @@ int main(int argc, char **argv)
 	/* Play animated Graph. */
 	lamDraw(play);
 
+	free(font_file);
+
 	/* Free the memory. */
 	for (auto &g: graphs)
 		delete g;
@@ -227,9 +262,6 @@ int main(int argc, char **argv)
 	/* Reset (clear) the model. */
 	ksmodel_clear(&histo);
 
-	/* Close the file. */
-	kshark_close(kshark_ctx);
-
 	/* Close the session. */
 	kshark_free(kshark_ctx);
 
-- 
2.25.1


  parent reply	other threads:[~2021-02-11 10:38 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-11 10:31 [PATCH v2 00/27] Complete the KernelShark v2 transformation Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 01/27] kernel-shark: Add get_stream_object() Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 02/27] kernel-shark: Do proper reset in kshark_close_all() Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 03/27] kernel-shark: Restore the counting of event handlers Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 04/27] kernel-shark: Fix a misleading comment Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 05/27] kernel-shark: Count the number of readout interfaces Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 06/27] kernel-shark: Update KsUtils Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 07/27] kernel-shark: Update KsModels and KsSearchFSM Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 08/27] kernel-shark: Add trace data files for CI testing Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 09/27] kernel-shark: Add plugin tests Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 10/27] kernel-shark: Add model tests Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 11/27] kernel-shark: Update KsWidgetsLib Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 12/27] kernel-shark: Add combo point to Mark Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 13/27] kernel-shark: Add new methods to KsPlot::Mark Yordan Karadzhov (VMware)
2021-02-11 10:31 ` Yordan Karadzhov (VMware) [this message]
2021-02-11 10:31 ` [PATCH v2 15/27] kernel-shark: Search for font with Cmake at pre-build Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 16/27] kernel-shark: Update KsDualMarker and KsGLWidget Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 17/27] kernel-shark: Update KsTraceGraph and KsQuickContextMenu Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 18/27] kernel-shark: Update KsTraceViewer Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 19/27] kernel-shark: Update KsAdvFilteringDialog Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 20/27] kernel-shark: Update KsCaptureDialog Yordan Karadzhov (VMware)
2021-02-11 10:31 ` [PATCH v2 21/27] kernel-shark: Update KsSession Yordan Karadzhov (VMware)
2021-02-11 10:32 ` [PATCH v2 22/27] kernel-shark: Update MissedEvents plugin Yordan Karadzhov (VMware)
2021-02-11 10:32 ` [PATCH v2 23/27] kernel-shark: Update KsMainWindow and kernelshark.cpp Yordan Karadzhov (VMware)
2021-02-11 10:32 ` [PATCH v2 24/27] kernel-shark: Clickable sched_event plugin shapes Yordan Karadzhov (VMware)
2021-02-11 10:32 ` [PATCH v2 25/27] kernel-shark: Show Task plots from command lime Yordan Karadzhov (VMware)
2021-02-11 10:32 ` [PATCH v2 26/27] kernel-shark: Add pkg-config configuration for libkshark Yordan Karadzhov (VMware)
2021-02-11 10:32 ` [PATCH v2 27/27] kernel-shark: Install libkshark-tepdata.h Yordan Karadzhov (VMware)
2021-02-11 15:02 ` [PATCH v2 00/27] Complete the KernelShark v2 transformation 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=20210211103205.418588-15-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).