Skip to contentSkip to conversation

kernel-shark: Multi-thread the computaion of stream/combo plots

PATCH RESEND Superseded Libo Chen 2 participants 4 messages Mar 14, 2025 – Mar 26, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Libo Chen

author
#0 lore raw

PATCH RESEND v2 1/1 kernel-shark: Multi-thread the computaion of stream/combo plots

Parallelize _newCPUGraph() and _newTaskGraph() calls to dramatically speed up graph rendering particularly for traces from very large systems.

OpenMP technically is a new dependency here, but it's part of GCC, so long as your GCC >= v4.9, the libgomp library will make the code compiled.

---
 CMakeLists.txt     |  6 ++++++
 src/KsGLWidget.cpp | 25 +++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)
CMakeLists.txt +6 -0
index 988bfd6..7847177 100644
@@ -84,6 +84,12 @@ set(EXECUTABLE_OUTPUT_PATH "${KS_DIR}/bin")
 set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
 
+find_package(OpenMP 3.2.5)
+if (OPENMP_FOUND)
+    set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${OpenMP_C_FLAGS}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+endif(OPENMP_FOUND)
+
 set(CMAKE_CXX_STANDARD 17)
 set(CMAKE_CXX_STANDARD_REQUIRED ON)
 set(CMAKE_CXX_EXTENSIONS OFF)
src/KsGLWidget.cpp +23 -2
index 9311d98..004d64b 100644
@@ -13,6 +13,9 @@
 #include <GL/glut.h>
 #include <GL/gl.h>
 
+// OpenMP
+#include <omp.h>
+
 // KernelShark
 #include "libkshark-plugin.h"
 #include "KsGLWidget.hpp"
@@ -688,25 +691,43 @@ void KsGLWidget::_makeGraphs()
 		return graph;
 	};
 
+	omp_set_num_threads(omp_get_num_procs());
 	for (auto it = _streamPlots.begin(); it != _streamPlots.end(); ++it) {
 		sd = it.key();
+		QVector<KsPlot::Graph *> cpuGraphs(it.value()._cpuList.count());
+		QVector<KsPlot::Graph *> taskGraphs(it.value()._taskList.count());
+
 		/* Create CPU graphs according to the cpuList. */
 		it.value()._cpuGraphs = {};
+		#pragma omp parallel for
 		for (auto const &cpu: it.value()._cpuList) {
-			g = lamAddGraph(sd, _newCPUGraph(sd, cpu), _vSpacing);
+			int idx = it.value()._cpuList.indexOf(cpu);
+			cpuGraphs[idx] = _newCPUGraph(sd, cpu);
+		}
+		QVectorIterator<KsPlot::Graph *> itCpuGraphs(cpuGraphs);
+		while (itCpuGraphs.hasNext()) {
+			g = lamAddGraph(sd, itCpuGraphs.next(), _vSpacing);
 			it.value()._cpuGraphs.append(g);
 		}
 
 		/* Create Task graphs according to the taskList. */
 		it.value()._taskGraphs = {};
+		#pragma omp parallel for
 		for (auto const &pid: it.value()._taskList) {
-			g = lamAddGraph(sd, _newTaskGraph(sd, pid), _vSpacing);
+			int idx = it.value()._taskList.indexOf(pid);
+			taskGraphs[idx] = _newTaskGraph(sd, pid);
+		}
+		QVectorIterator<KsPlot::Graph *> itTaskGraphs(taskGraphs);
+		while (itTaskGraphs.hasNext()) {
+			g = lamAddGraph(sd, itTaskGraphs.next(), _vSpacing);
 			it.value()._taskGraphs.append(g);
 		}
+
 	}
 
 	for (auto &c: _comboPlots) {
 		int n = c.count();
+		#pragma omp parallel for
 		for (int i = 0; i < n; ++i) {
 			sd = c[i]._streamId;
 			if (c[i]._type & KSHARK_TASK_DRAW) {
Signature · 1 line
2.43.5
#1 lore raw

Hi Libo,
Please see my comments below.

Quoted 44 lines · Libo Chen

Parallelize _newCPUGraph() and _newTaskGraph() calls to dramatically speed up graph rendering particularly for traces from very large systems.

OpenMP technically is a new dependency here, but it's part of GCC, so long as your GCC >= v4.9, the libgomp library will make the code compiled.

---
  CMakeLists.txt     |  6 ++++++
  src/KsGLWidget.cpp | 25 +++++++++++++++++++++++--
  2 files changed, 29 insertions(+), 2 deletions(-)
CMakeLists.txt +6 -0
index 988bfd6..7847177 100644
@@ -84,6 +84,12 @@ set(EXECUTABLE_OUTPUT_PATH "${KS_DIR}/bin")
  set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
 
+find_package(OpenMP 3.2.5)
+if (OPENMP_FOUND)
+    set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${OpenMP_C_FLAGS}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+endif(OPENMP_FOUND)
+
  set(CMAKE_CXX_STANDARD 17)
  set(CMAKE_CXX_STANDARD_REQUIRED ON)
  set(CMAKE_CXX_EXTENSIONS OFF)
src/KsGLWidget.cpp +3 -0
index 9311d98..004d64b 100644
@@ -13,6 +13,9 @@
  #include <GL/glut.h>
  #include <GL/gl.h>
 
+// OpenMP
+#include <omp.h>
+
  // KernelShark
  #include "libkshark-plugin.h"
  #include "KsGLWidget.hpp"

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 694

Quoted 4 lines
src/KsGLWidget.cpp +1 -0
@@ -688,25 +691,43 @@ void KsGLWidget::_makeGraphs()
  		return graph;
  	};
 
+	omp_set_num_threads(omp_get_num_procs());

I think I already asked you to check if it is possible to move this to the constructor of the widget so that it is called just once. If there is some reason why this is not possible, at least provide some explanation.

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 704

Quoted 11 lines
src/KsGLWidget.cpp +5 -1
  	for (auto it = _streamPlots.begin(); it != _streamPlots.end(); ++it) {
  		sd = it.key();
+		QVector<KsPlot::Graph *> cpuGraphs(it.value()._cpuList.count());
+		QVector<KsPlot::Graph *> taskGraphs(it.value()._taskList.count());
+
  		/* Create CPU graphs according to the cpuList. */
  		it.value()._cpuGraphs = {};
+		#pragma omp parallel for
  		for (auto const &cpu: it.value()._cpuList) {
-			g = lamAddGraph(sd, _newCPUGraph(sd, cpu), _vSpacing);
+			int idx = it.value()._cpuList.indexOf(cpu);

Maybe I do not understand what you want to do here, but this looks over-complicated to me. Isn't it equivalent to having simply

		for (size_t idx = 0; idx < nCpus; ++idx) {
	 		int cpu = it.value()._cpuList[idx];

The same comment applies for the other loop below.

Quoted 12 lines
src/KsGLWidget.cpp +6 -0
+			cpuGraphs[idx] = _newCPUGraph(sd, cpu);
+		}
+		QVectorIterator<KsPlot::Graph *> itCpuGraphs(cpuGraphs);
+		while (itCpuGraphs.hasNext()) {
+			g = lamAddGraph(sd, itCpuGraphs.next(), _vSpacing);
  			it.value()._cpuGraphs.append(g);
  		}
 
  		/* Create Task graphs according to the taskList. */
  		it.value()._taskGraphs = {};
+		#pragma omp parallel for
  		for (auto const &pid: it.value()._taskList) {

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 725

Quoted 10 lines
src/KsGLWidget.cpp +7 -1
-			g = lamAddGraph(sd, _newTaskGraph(sd, pid), _vSpacing);
+			int idx = it.value()._taskList.indexOf(pid);
+			taskGraphs[idx] = _newTaskGraph(sd, pid);
+		}
+		QVectorIterator<KsPlot::Graph *> itTaskGraphs(taskGraphs);
+		while (itTaskGraphs.hasNext()) {
+			g = lamAddGraph(sd, itTaskGraphs.next(), _vSpacing);
  			it.value()._taskGraphs.append(g);
  		}
+

Please remove this empty line.

Beside those minor things, the patch looks good to me. Please address the comments and I will be happy to apply your patch.

Thanks for helping us improve KerrnelShark!

Cheers,
Yordan

Quoted 8 lines
src/KsGLWidget.cpp +1 -0
  	}
 
  	for (auto &c: _comboPlots) {
  		int n = c.count();
+		#pragma omp parallel for
  		for (int i = 0; i < n; ++i) {
  			sd = c[i]._streamId;
  			if (c[i]._type & KSHARK_TASK_DRAW) {
author
#2 lore raw
Quoted 42 lines · Yordan Karadzhov

Hi Libo,
Please see my comments below.

Quoted 38 lines · Libo Chen

Parallelize _newCPUGraph() and _newTaskGraph() calls to dramatically speed up graph rendering particularly for traces from very large systems.

OpenMP technically is a new dependency here, but it's part of GCC, so long as your GCC >= v4.9, the libgomp library will make the code compiled.

---
  CMakeLists.txt     |  6 ++++++ src/KsGLWidget.cpp | 25 +++++++++++++++++++++++--

  2 files changed, 29 insertions(+), 2 deletions(-)
CMakeLists.txt +5 -0
index 988bfd6..7847177 100644
@@ -84,6 +84,12 @@ set(EXECUTABLE_OUTPUT_PATH "${KS_DIR}/bin")
   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
   +find_package(OpenMP 3.2.5)
+if (OPENMP_FOUND)
+    set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${OpenMP_C_FLAGS}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+endif(OPENMP_FOUND)
+
   set(CMAKE_CXX_STANDARD 17)
   set(CMAKE_CXX_STANDARD_REQUIRED ON)
   set(CMAKE_CXX_EXTENSIONS OFF)
src/KsGLWidget.cpp +0 -0
index 9311d98..004d64b 100644
@@ -13,6 +13,9 @@
   #include <GL/glut.h>
   #include <GL/gl.h>

  +// OpenMP

src/KsGLWidget.cpp +2 -0
+#include <omp.h>
+
Quoted 8 lines

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 693

Quoted 7 lines

  // KernelShark
  #include "libkshark-plugin.h"
  #include "KsGLWidget.hpp"

src/KsGLWidget.cpp +0 -0
@@ -688,25 +691,43 @@ void KsGLWidget::_makeGraphs()
           return graph;
       };
   +    omp_set_num_threads(omp_get_num_procs());

I think I already asked you to check if it is possible to move this to the constructor of the widget so that it is called just once. If there is some reason why this is not possible, at least provide some explanation.

Hi Yordan, thanks for your review.

oops I missed that, will move it over.

Quoted 13 lines

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 704

Quoted 12 lines
src/KsGLWidget.cpp +5 -1
       for (auto it = _streamPlots.begin(); it != _streamPlots.end(); ++it) {
           sd = it.key();
+        QVector<KsPlot::Graph *> cpuGraphs(it.value()._cpuList.count());
+        QVector<KsPlot::Graph *> taskGraphs(it.value()._taskList.count());
+
           /* Create CPU graphs according to the cpuList. */
           it.value()._cpuGraphs = {};
+        #pragma omp parallel for
           for (auto const &cpu: it.value()._cpuList) {
-            g = lamAddGraph(sd, _newCPUGraph(sd, cpu), _vSpacing);
+            int idx = it.value()._cpuList.indexOf(cpu);
Quoted 5 lines

Maybe I do not understand what you want to do here, but this looks over-complicated to me. Isn't it equivalent to having simply

        for (size_t idx = 0; idx < nCpus; ++idx) { int cpu = it.value()._cpuList[idx];

I think _cpuList isn't exactly the same as [0..nCpus) here. In a default plot, some idle cpus may not be appended to the vector

	/* Do not add plots for idle CPUs. */
	if (!kshark_hash_id_find(stream->idle_cpus, cpu))
		plotVec.append(cpu);

Also you can set a subset of CPUs to show, so _cpuList could be quite random.

Best,
Libo

Quoted 38 lines

The same comment applies for the other loop below.

Quoted 11 lines
src/KsGLWidget.cpp +6 -0
+            cpuGraphs[idx] = _newCPUGraph(sd, cpu);
+        }
+        QVectorIterator<KsPlot::Graph *> itCpuGraphs(cpuGraphs);
+        while (itCpuGraphs.hasNext()) {
+            g = lamAddGraph(sd, itCpuGraphs.next(), _vSpacing);
               it.value()._cpuGraphs.append(g);
           }
             /* Create Task graphs according to the taskList. */
           it.value()._taskGraphs = {};
+        #pragma omp parallel for
           for (auto const &pid: it.value()._taskList) {

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 725

Quoted 10 lines
src/KsGLWidget.cpp +7 -1
-            g = lamAddGraph(sd, _newTaskGraph(sd, pid), _vSpacing);
+            int idx = it.value()._taskList.indexOf(pid);
+            taskGraphs[idx] = _newTaskGraph(sd, pid);
+        }
+        QVectorIterator<KsPlot::Graph *> itTaskGraphs(taskGraphs);
+        while (itTaskGraphs.hasNext()) {
+            g = lamAddGraph(sd, itTaskGraphs.next(), _vSpacing);
               it.value()._taskGraphs.append(g);
           }
+

Please remove this empty line.

Beside those minor things, the patch looks good to me. Please address the comments and I will be happy to apply your patch.

Thanks for helping us improve KerrnelShark!

Cheers,
Yordan

Quoted 7 lines
src/KsGLWidget.cpp +1 -0
       }
         for (auto &c: _comboPlots) {
           int n = c.count();
+        #pragma omp parallel for
           for (int i = 0; i < n; ++i) {
               sd = c[i]._streamId;
               if (c[i]._type & KSHARK_TASK_DRAW) {
author
#3 lore raw
Quoted 74 lines · Libo Chen
Quoted 42 lines · Yordan Karadzhov

Hi Libo,
Please see my comments below.

Quoted 38 lines · Libo Chen

Parallelize _newCPUGraph() and _newTaskGraph() calls to dramatically speed up graph rendering particularly for traces from very large systems.

OpenMP technically is a new dependency here, but it's part of GCC, so long as your GCC >= v4.9, the libgomp library will make the code compiled.

---
  CMakeLists.txt     |  6 ++++++ src/KsGLWidget.cpp | 25 +++++++++++++++++++++++--

  2 files changed, 29 insertions(+), 2 deletions(-)
CMakeLists.txt +5 -0
index 988bfd6..7847177 100644
@@ -84,6 +84,12 @@ set(EXECUTABLE_OUTPUT_PATH "${KS_DIR}/bin")
   set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pthread -fPIC -fno-common")
   +find_package(OpenMP 3.2.5)
+if (OPENMP_FOUND)
+    set(CMAKE_C_FLAGS   "${CMAKE_C_FLAGS}   ${OpenMP_C_FLAGS}")
+    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
+endif(OPENMP_FOUND)
+
   set(CMAKE_CXX_STANDARD 17)
   set(CMAKE_CXX_STANDARD_REQUIRED ON)
   set(CMAKE_CXX_EXTENSIONS OFF)
src/KsGLWidget.cpp +0 -0
index 9311d98..004d64b 100644
@@ -13,6 +13,9 @@
   #include <GL/glut.h>
   #include <GL/gl.h>

  +// OpenMP

src/KsGLWidget.cpp +2 -0
+#include <omp.h>
+
Quoted 8 lines

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 693

Quoted 7 lines

  // KernelShark
  #include "libkshark-plugin.h"
  #include "KsGLWidget.hpp"

src/KsGLWidget.cpp +0 -0
@@ -688,25 +691,43 @@ void KsGLWidget::_makeGraphs()
           return graph;
       };
   +    omp_set_num_threads(omp_get_num_procs());

I think I already asked you to check if it is possible to move this to the constructor of the widget so that it is called just once. If there is some reason why this is not possible, at least provide some explanation.

Hi Yordan, thanks for your review.

oops I missed that, will move it over.

Quoted 13 lines

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 704

Quoted 12 lines
src/KsGLWidget.cpp +5 -1
       for (auto it = _streamPlots.begin(); it != _streamPlots.end(); ++it) {
           sd = it.key();
+        QVector<KsPlot::Graph *> cpuGraphs(it.value()._cpuList.count());
+        QVector<KsPlot::Graph *> taskGraphs(it.value()._taskList.count());
+
           /* Create CPU graphs according to the cpuList. */
           it.value()._cpuGraphs = {};
+        #pragma omp parallel for
           for (auto const &cpu: it.value()._cpuList) {
-            g = lamAddGraph(sd, _newCPUGraph(sd, cpu), _vSpacing);
+            int idx = it.value()._cpuList.indexOf(cpu);
Quoted 5 lines

Maybe I do not understand what you want to do here, but this looks over-complicated to me. Isn't it equivalent to having simply

        for (size_t idx = 0; idx < nCpus; ++idx) { int cpu = it.value()._cpuList[idx];

Quoted 8 lines

I think _cpuList isn't exactly the same as [0..nCpus) here. In a default plot, some idle cpus may not be appended to the vector

	/* Do not add plots for idle CPUs. */
	if (!kshark_hash_id_find(stream->idle_cpus, cpu))
		plotVec.append(cpu);

Also you can set a subset of CPUs to show, so _cpuList could be quite random.

Never mind, you're right. I got myself confused here. Will send you v3 soon. Thanks

Quoted 42 lines

Best,
Libo

Quoted 38 lines

The same comment applies for the other loop below.

Quoted 11 lines
src/KsGLWidget.cpp +6 -0
+            cpuGraphs[idx] = _newCPUGraph(sd, cpu);
+        }
+        QVectorIterator<KsPlot::Graph *> itCpuGraphs(cpuGraphs);
+        while (itCpuGraphs.hasNext()) {
+            g = lamAddGraph(sd, itCpuGraphs.next(), _vSpacing);
               it.value()._cpuGraphs.append(g);
           }
             /* Create Task graphs according to the taskList. */
           it.value()._taskGraphs = {};
+        #pragma omp parallel for
           for (auto const &pid: it.value()._taskList) {

src/KsGLWidget.cpp void KsGLWidget::_makeGraphs() · line 725

Quoted 10 lines
src/KsGLWidget.cpp +7 -1
-            g = lamAddGraph(sd, _newTaskGraph(sd, pid), _vSpacing);
+            int idx = it.value()._taskList.indexOf(pid);
+            taskGraphs[idx] = _newTaskGraph(sd, pid);
+        }
+        QVectorIterator<KsPlot::Graph *> itTaskGraphs(taskGraphs);
+        while (itTaskGraphs.hasNext()) {
+            g = lamAddGraph(sd, itTaskGraphs.next(), _vSpacing);
               it.value()._taskGraphs.append(g);
           }
+

Please remove this empty line.

Beside those minor things, the patch looks good to me. Please address the comments and I will be happy to apply your patch.

Thanks for helping us improve KerrnelShark!

Cheers,
Yordan

Quoted 7 lines
src/KsGLWidget.cpp +1 -0
       }
         for (auto &c: _comboPlots) {
           int n = c.count();
+        #pragma omp parallel for
           for (int i = 0; i < n; ++i) {
               sd = c[i]._streamId;
               if (c[i]._type & KSHARK_TASK_DRAW) {