Skip to contentSkip to conversation

kernel-shark-2.alpha: Force trace-cmd.h to be used as plain C

PATCH New Tzvetomir Stoyanov (VMware) 1 participant 5 messages Apr 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kernel-shark-2.alpha: Force trace-cmd.h to be used as plain C

From: Tzvetomir (VMware) Stoyanov <[email protected]>

C++ compiler uses name mangling to handle function overloading. As there is no function overloading in C, function names are not mangled. This breaks the linking of C library into C++ binary. Declare functions from trace-cmd.h as pure C, so the loader will not mangle the names when resolving them.

---
 src/plugins/KVMCombo.cpp | 2 ++
 1 file changed, 2 insertions(+)
src/plugins/KVMCombo.cpp +2 -0
index 3ff9ca5..1ae03aa 100644
@@ -13,7 +13,9 @@
 #include<iostream>
 
 // trace-cmd
+extern "C" {
 #include "trace-cmd/trace-cmd.h"
+}
 
 // KernelShark
 #include "libkshark.h"
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 kernel-shark-2.alpha: Restructure KVMCombo plugin to use CPU mapping information from the trace files

From: Tzvetomir (VMware) Stoyanov <[email protected]>

In the upcoming trace-cmd 2.9, extra information about host and guest tracing will be stored in the trace.dat files. Information about host CPU - guest vCPU mapping will be saved in host trace file. This mapping is mandatory for KVMCombo plugin. Currently, the mapping is extracted for the tracing data. Getting the information from the trace files is more reliable and solves the use case with more than one guest.

---
 src/plugins/KVMCombo.cpp | 203 ++++++++++++++++++++++-----------------
 src/plugins/KVMCombo.hpp |  14 +--
 2 files changed, 120 insertions(+), 97 deletions(-)
src/plugins/KVMCombo.cpp +113 -90
index 1ae03aa..25c2331 100644
@@ -69,10 +69,19 @@ KsVCPUCheckBoxWidget::KsVCPUCheckBoxWidget(QWidget *parent)
 	_initTree();
 }
 
-void KsVCPUCheckBoxWidget::update(int sdHost, VCPUVector vcpus)
+void KsVCPUCheckBoxWidget::update(int GuestId, GuestMap *gMap, GuestName *gName)
 {
-	int nVCPUs = vcpus.count();
 	KsPlot::ColorTable colors;
+	VCPUVector VCPUs;
+	HostPair Host;
+	int nVCPUs;
+
+	if (!gMap->contains(GuestId) || !gName->contains(GuestId))
+		return;
+
+	Host = gMap->value(GuestId);
+	VCPUs = Host.second;
+	nVCPUs = VCPUs.count();
 
 	_tree.clear();
 	_id.resize(nVCPUs);
@@ -80,9 +89,12 @@ void KsVCPUCheckBoxWidget::update(int sdHost, VCPUVector vcpus)
 	colors = KsPlot::getCPUColorTable();
 
 	for (int i = 0; i < nVCPUs; ++i) {
+		QString strCPU = QLatin1String("vCPU ") + QString::number(VCPUs.at(i).second);
+		strCPU += (QLatin1String("\t<") + gName->value(GuestId) + QLatin1Char('>'));
+
 		QTreeWidgetItem *cpuItem = new QTreeWidgetItem;
 		cpuItem->setText(0, "  ");
-		cpuItem->setText(1, QString("vCPU %1").arg(vcpus.at(i).second));
+		cpuItem->setText(1, strCPU);
 		cpuItem->setCheckState(0, Qt::Checked);
 		cpuItem->setBackgroundColor(0, QColor(colors[i].r(),
 						      colors[i].g(),
@@ -96,6 +108,65 @@ void KsVCPUCheckBoxWidget::update(int sdHost, VCPUVector vcpus)
 	setDefault(false);
 }
 
+static void getGuestMap(GuestMap *gMap, GuestName *gName)
+{
+	struct tracecmd_input *peer_handle = NULL;
+	struct kshark_data_stream *peer_stream;
+	struct tracecmd_input *guest_handle = NULL;
+	struct kshark_data_stream *guest_stream;
+	kshark_context *kshark_ctx(nullptr);
+	unsigned long long trace_id;
+	int ret;
+	const char *name;
+	int vcpu_count;
+	const int *cpu_pid;
+	int *streamIds;
+	int i, j, k;
+
+	if (!kshark_instance(&kshark_ctx))
+		return;
+
+	gMap->clear();
+	gName->clear();
+
+	streamIds = kshark_all_streams(kshark_ctx);
+	for (i = 0; i < kshark_ctx->n_streams; i++) {
+		guest_stream = kshark_get_data_stream(kshark_ctx, streamIds[i]);
+		if (!guest_stream || guest_stream->format != KS_TEP_DATA)
+			continue;
+		guest_handle = kshark_get_tep_input(guest_stream);
+		if (!guest_handle)
+			continue;
+		trace_id = tracecmd_get_traceid(guest_handle);
+		if (!trace_id)
+			continue;
+		for (j = 0; j < kshark_ctx->n_streams; j++) {
+			if (streamIds[i] == streamIds[j])
+				continue;
+			peer_stream = kshark_get_data_stream(kshark_ctx, streamIds[j]);
+			if (!peer_stream || peer_stream->format != KS_TEP_DATA)
+				continue;
+			peer_handle = kshark_get_tep_input(peer_stream);
+			if (!peer_handle)
+				continue;
+			ret = tracecmd_get_guest_cpumap(peer_handle, trace_id,
+							&name, &vcpu_count, &cpu_pid);
+			if (!ret) {
+				VCPUVector vcpus(vcpu_count);
+				for (k = 0; k < vcpu_count; k++) {
+					vcpus[k].first = cpu_pid[k];
+					vcpus[k].second = k;
+				}
+				gMap->insert(streamIds[i], QPair<int, VCPUVector>(streamIds[j], vcpus));
+				gName->insert(streamIds[i], QString(name));
+				break;
+			}
+		}
+	}
+
+	free(streamIds);
+}
+
 #define DIALOG_NAME	"KVM Combo plots"
 
 #define LABEL_WIDTH	(FONT_WIDTH * 50)
@@ -170,24 +241,29 @@ KsComboPlotDialog::KsComboPlotDialog(QWidget *parent)
 	setLayout(&_topLayout);
 }
 
-void KsComboPlotDialog::update(int sdHost, VCPUVector vcpus)
+void KsComboPlotDialog::update()
 {
 	kshark_context *kshark_ctx(nullptr);
-	int sd, *streamIds;
+	GuestMap::iterator i;
+	VCPUVector Vect;
+	HostPair hPair;
+	int sd;
+	int j;
 
 	if (!kshark_instance(&kshark_ctx))
 		return;
 
-	_sdHost = sdHost;
-	_vcpus = vcpus;
+	getGuestMap(&_guestMap, &_guestName);
+
+	hPair = _guestMap.begin().value();
 	KsUtils::setElidedText(&_hostFileLabel,
-			       kshark_ctx->stream[sdHost]->file,
+			       kshark_ctx->stream[hPair.first]->file,
 			       Qt::ElideLeft, LABEL_WIDTH);
 
-	streamIds = kshark_all_streams(kshark_ctx);
-	for (int i = 0; i < kshark_ctx->n_streams; ++i) {
-		sd = streamIds[i];
-		if (sd == sdHost)
+	_guestStreamComboBox.clear();
+	for (i = _guestMap.begin(); i != _guestMap.end(); ++i) {
+		sd = i.key();
+		if (sd >= kshark_ctx->n_streams)
 			continue;
 
 		_guestStreamComboBox.addItem(kshark_ctx->stream[sd]->file,
@@ -200,8 +276,8 @@ void KsComboPlotDialog::update(int sdHost, VCPUVector vcpus)
 				this,		&KsComboPlotDialog::_applyPress);
 	}
 
-	_vcpuTree.update(sdHost, vcpus);
-	free(streamIds);
+	sd = _guestStreamComboBox.currentData().toInt();
+	_vcpuTree.update(sd, &_guestMap, &_guestName);
 }
 
 void KsComboPlotDialog::_applyPress()
@@ -210,6 +286,18 @@ void KsComboPlotDialog::_applyPress()
 	QVector<int> allCombosVec;
 	KsComboPlot combo(2);
 	int nPlots(0);
+	VCPUVector VCPUs;
+	HostPair Host;
+	int GuestId;
+	int nVCPUs;
+
+	GuestId = _guestStreamComboBox.currentData().toInt();
+	if (!_guestMap.contains(GuestId))
+		return;
+
+	Host = _guestMap.value(GuestId);
+	VCPUs = Host.second;
+	nVCPUs = VCPUs.count();
 
 	/*
 	 * Disconnect _applyButton. This is done in order to protect
@@ -218,17 +306,20 @@ void KsComboPlotDialog::_applyPress()
 	disconnect(_applyButtonConnection);
 
 	for (auto const &i: cbVec) {
+		if (i >= nVCPUs)
+			continue;
+
 		allCombosVec.append(2);
 
-		combo[0]._streamId = _guestStreamComboBox.currentData().toInt();
-		combo[0]._id = _vcpus.at(i).second;
+		combo[0]._streamId = GuestId;
+		combo[0]._id = VCPUs.at(i).second;
 		combo[0]._type = KsPlot::KSHARK_CPU_DRAW |
 				 KsPlot::KSHARK_GUEST_DRAW;
 
 		combo[0] >> allCombosVec;
 
-		combo[1]._streamId = _sdHost;
-		combo[1]._id = _vcpus.at(i).first;
+		combo[1]._streamId = Host.first;
+		combo[1]._id = VCPUs.at(i).first;
 		combo[1]._type = KsPlot::KSHARK_TASK_DRAW |
 				 KsPlot::KSHARK_HOST_DRAW;
 
@@ -241,66 +332,8 @@ void KsComboPlotDialog::_applyPress()
 
 void KsComboPlotDialog::_guestStreamChanged(const QString &sdStr)
 {
-
-}
-
-static int getVCPU(plugin_kvm_context *plugin_ctx,
-		   kshark_trace_histo *histo,
-		   int sdHost, int pid)
-{
-	int values[2] = {plugin_ctx->vm_entry_id, pid};
-	const kshark_entry *entry;
-	unsigned long long vcpu;
-
-	for (int b = 0; b < histo->n_bins; ++b) {
-		entry = ksmodel_get_entry_front(histo,
-						b, false,
-						kshark_match_event_and_pid,
-						sdHost, values,
-						nullptr,
-						nullptr);
-		if (!entry)
-			continue;
-
-		if (kshark_read_event_field(entry, "vcpu_id", &vcpu) >= 0)
-			return vcpu;
-	}
-
-	return -1;
-}
-
-HostMap getVCPUPids(kshark_context *kshark_ctx, kshark_trace_histo *histo)
-{
-	int sd, n_vcpus, *streamIds, *pids;
-	plugin_kvm_context *plugin_ctx;
-	HostMap hMap;
-
-	streamIds = kshark_all_streams(kshark_ctx);
-	for (int i = 0; i < kshark_ctx->n_streams; ++i) {
-		sd = streamIds[i];
-		plugin_ctx = get_kvm_context(sd);
-		if (!plugin_ctx)
-			continue;
-
-		/* This stream contains KVM events. */
-		n_vcpus = plugin_ctx->vcpu_pids->count;
-		if (n_vcpus) {
-			VCPUVector vcpus(n_vcpus);
-			pids = kshark_hash_ids(plugin_ctx->vcpu_pids);
-			for (int j = 0; j < n_vcpus; ++j) {
-				vcpus[j].first = pids[j];
-				vcpus[j].second = getVCPU(plugin_ctx,
-							  histo,
-							  sd, pids[j]);
-			}
-
-			free(pids);
-			hMap[sd] = vcpus;
-		}
-	}
-
-	free(streamIds);
-	return hMap;
+	int GuestId = _guestStreamComboBox.currentData().toInt();
+	_vcpuTree.update(GuestId, &_guestMap, &_guestName);
 }
 
 KsComboPlotDialog dialog;
@@ -309,18 +342,11 @@ QMetaObject::Connection dialogConnection;
 static void showDialog(KsMainWindow *ks)
 {
 	kshark_context *kshark_ctx(nullptr);
-	kshark_trace_histo *histo;
-	VCPUVector vcpus;
-	HostMap hMap;
-	int sdHost;
 
 	if (!kshark_instance(&kshark_ctx))
 		return;
 
-	histo = ks->graphPtr()->glPtr()->model()->histo();
-	hMap = getVCPUPids(kshark_ctx, histo);
-
-	if (kshark_ctx->n_streams < 2 || hMap.count() != 1) {
+	if (kshark_ctx->n_streams < 2) {
 		QString err("Data from one Host and at least one Guest is required.");
 		QMessageBox msgBox;
 		msgBox.critical(nullptr, "Error", err);
@@ -328,10 +354,7 @@ static void showDialog(KsMainWindow *ks)
 		return;
 	}
 
-	sdHost = hMap.begin().key();
-	vcpus = hMap.begin().value();
-
-	dialog.update(sdHost, vcpus);
+	dialog.update();
 
 	if (!dialogConnection) {
 		dialogConnection =
src/plugins/KVMCombo.hpp +7 -7
index b47f557..a343141 100644
@@ -16,8 +16,9 @@
 #include "KsWidgetsLib.hpp"
 
 typedef QVector<QPair<int, int>> VCPUVector;
-
-typedef QMap<int, VCPUVector> HostMap;
+typedef QPair<int, VCPUVector> HostPair;
+typedef QMap<int, HostPair> GuestMap;
+typedef QMap<int, QString> GuestName;
 
 /**
  * The KsVCPUCheckBoxWidget class provides a widget for selecting CPU plots to
@@ -27,7 +28,7 @@ struct KsVCPUCheckBoxWidget : public KsWidgetsLib::KsCheckBoxTreeWidget
 {
 	explicit KsVCPUCheckBoxWidget(QWidget *parent = nullptr);
 
-	void update(int sdHost, VCPUVector vcpus);
+	void update(int GuestId, GuestMap *gMap, GuestName *gName);
 };
 
 /**
@@ -40,16 +41,15 @@ class KsComboPlotDialog : public QDialog
 public:
 	explicit KsComboPlotDialog(QWidget *parent = nullptr);
 
-	void update(int sdHost, VCPUVector vcpus);
+	void update();
 
 signals:
 	/** Signal emitted when the "Apply" button is pressed. */
 	void apply(int sd, QVector<int>);
 
 private:
-	int				_sdHost;
-
-	VCPUVector			_vcpus;
+	GuestMap			_guestMap;
+	GuestName			_guestName;
 
 	KsVCPUCheckBoxWidget		_vcpuTree;
 
Signature · 1 line
2.25.1
#2 lore raw

PATCH 1/1 kernel-shark-2.alpha: Print the plugin's file name in case of loading error

In reply to Tzvetomir Stoyanov (VMware)

From: Tzvetomir (VMware) Stoyanov <[email protected]>

When dlopen() fails to load the plugin, "plugin->file" is still not initialized. It is useful to print the file name of the problematic plugin, "file", instead of not yet initialized "plugin->file".

---
 src/libkshark-plugin.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
src/libkshark-plugin.c +1 -2
index f9a721f..1cea87b 100644
@@ -331,8 +331,7 @@ kshark_register_plugin(struct kshark_context *kshark_ctx,
 	return plugin;
 
  fail:
-	fprintf(stderr, "cannot load plugin '%s'\n",
-		plugin->file);
+	fprintf(stderr, "cannot load plugin '%s'\n", file);
 
 	if (plugin->handle)
 		dlclose(plugin->handle);
Signature · 1 line
2.25.1
#3 lore raw

PATCH 1/1 KernelShark: Adjust the width of marker buttons

In reply to Tzvetomir Stoyanov (VMware)

From: Tzvetomir (VMware) Stoyanov <[email protected]>

Both "Marker A" and "Marker B" buttons were too tight to fit the button's label on them. Add some extra space.

---
 src/KsDualMarker.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
src/KsDualMarker.cpp +2 -2
index 3e0b462..e2b8ac1 100644
@@ -145,8 +145,8 @@ KsDualMarkerSM::KsDualMarkerSM(QWidget *parent)
 {
 	QString styleSheetA, styleSheetB;
 
-	_buttonA.setFixedWidth(STRING_WIDTH(" Marker A "));
-	_buttonB.setFixedWidth(STRING_WIDTH(" Marker B "));
+	_buttonA.setFixedWidth(STRING_WIDTH(" Marker A ") + FONT_WIDTH);
+	_buttonB.setFixedWidth(STRING_WIDTH(" Marker B ") + FONT_WIDTH);
 
 	for (auto const &l: {&_labelMA, &_labelMB, &_labelDelta}) {
 		l->setFrameStyle(QFrame::Panel | QFrame::Sunken);
Signature · 1 line
2.21.0
#4 lore raw

PATCH v2 1/1 kernel-shark-2.alpha: Use new tracecmd APIs to open guest tracing file

In reply to Tzvetomir Stoyanov (VMware)

From: Tzvetomir (VMware) Stoyanov <[email protected]>

The new tracecmd API tracecmd_open_head() allows opening a trace.dat file on stages. First, only the headers from the file are read and parsed, without reading the tracing data.
The tracecmd_set_merge_peer() API is used to bind a tracing peer to this file, before parsing the trace data. This affects events timestamps correction when the tracing data is loaded.
The tracecmd_get_guest_cpumap() API is used to find dependencies between all files and find a possible tracing peers.

This change depends on these trace-cmd patch sets:
"Useful APIs for merging tracing files"
https://patchwork.kernel.org/project/linux-trace-devel/list/?series=268745
"Split reading the trace.dat options from trace data"
https://patchwork.kernel.org/project/linux-trace-devel/list/?series=268743

---
 src/libkshark-tepdata.c | 59 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 58 insertions(+), 1 deletion(-)
src/libkshark-tepdata.c +58 -1
index 8678e12..c68b52a 100644
@@ -985,6 +985,45 @@ const char *tep_plugin_names[] = {"sched_events",
 
 #define LINUX_IDLE_TASK_PID	0
 
+/** Find a host stream from the same tracing session, that has guest information */
+struct tracecmd_input *kshark_tep_find_merge_peer(struct kshark_context *kshark_ctx,
+						  struct tracecmd_input *handle)
+{
+	struct tracecmd_input *peer_handle = NULL;
+	struct kshark_data_stream *peer_stream;
+	unsigned long long trace_id;
+	int *streamIds = NULL;
+	int ret;
+	int i;
+
+	trace_id = tracecmd_get_traceid(handle);
+	if (!trace_id)
+		goto out;
+
+	streamIds = kshark_all_streams(kshark_ctx);
+	if (!streamIds)
+		goto out;
+	for (i = 0; i < kshark_ctx->n_streams; i++) {
+		peer_stream = kshark_get_data_stream(kshark_ctx, streamIds[i]);
+		if (!peer_stream || peer_stream->format != KS_TEP_DATA)
+			continue;
+		peer_handle = kshark_get_tep_input(peer_stream);
+		if (!peer_handle)
+			continue;
+		ret = tracecmd_get_guest_cpumap(peer_handle, trace_id,
+						NULL, NULL, NULL);
+		if (!ret)
+			break;
+	}
+
+	if (i == kshark_ctx->n_streams)
+		peer_handle = NULL;
+
+out:
+	free(streamIds);
+	return peer_handle;
+}
+
 /** Initialize the FTRACE data input (from file). */
 int kshark_tep_init_input(struct kshark_data_stream *stream,
 			  const char *file)
@@ -992,8 +1031,10 @@ int kshark_tep_init_input(struct kshark_data_stream *stream,
 	struct kshark_context *kshark_ctx = NULL;
 	struct tepdata_handle *tep_handle;
 	struct kshark_plugin_list *plugin;
+	struct tracecmd_input *merge_peer;
 	struct tep_event *event;
 	int i, n_tep_plugins;
+	int ret;
 
 	if (!kshark_instance(&kshark_ctx) || !init_thread_seq())
 		return -EEXIST;
@@ -1009,13 +1050,29 @@ int kshark_tep_init_input(struct kshark_data_stream *stream,
 	if (!tep_handle)
 		return -EFAULT;
 
-	tep_handle->input = tracecmd_open(file);
+	/** Open the tracing file, parse headers and create trace input context */
+	tep_handle->input = tracecmd_open_head(file);
 	if (!tep_handle->input) {
 		free(tep_handle);
 		stream->interface.handle = NULL;
 		return -EEXIST;
 	}
 
+	/** Find a merge peer from the same tracing session */
+	merge_peer = kshark_tep_find_merge_peer(kshark_ctx, tep_handle->input);
+	if (merge_peer)
+		tracecmd_set_merge_peer(tep_handle->input, merge_peer);
+
+	/** Read the racing data from the file */
+	ret = tracecmd_init_data(tep_handle->input);
+
+	if (ret < 0) {
+		tracecmd_close(tep_handle->input);
+		free(tep_handle);
+		stream->interface.handle = NULL;
+		return -EEXIST;
+	}
+
 	tep_handle->tep = tracecmd_get_pevent(tep_handle->input);
 
 	tep_handle->sched_switch_event_id = -EINVAL;
Signature · 1 line
2.25.1