File: widgetdemo.cpp

package info (click to toggle)
kernelshark 2.2.1-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,744 kB
  • sloc: cpp: 12,517; ansic: 11,521; makefile: 89; sh: 88
file content (155 lines) | stat: -rw-r--r-- 3,312 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// SPDX-License-Identifier: GPL-2.0

/*
 * Copyright (C) 2017 VMware Inc, Yordan Karadzhov <ykaradzhov@vmware.com>
 */

// C
#include <sys/stat.h>
#include <getopt.h>
#include <unistd.h>

// C++
#include <iostream>

// Qt
#include <QtWidgets>

// KernelShark
#include "KsUtils.hpp"
#include "KsWidgetsLib.hpp"

#define default_input_file (char*)"trace.dat"

static char *input_file = nullptr;

using namespace std;
using namespace KsWidgetsLib;

void usage(const char *prog)
{
	cout << "Usage: " << prog << endl
	     << "  -h	Display this help message\n"
	     << "  -v	Display version and exit\n"
	     << "  -i	input_file, default is " << default_input_file << endl
	     << "  -p	register plugin, use plugin name, absolute or relative path\n"
	     << "  -u	unregister plugin, use plugin name or absolute path\n";
}

struct TaskPrint : public QObject
{
	void print(int sd, QVector<int> pids)
	{
		for (auto const &pid: pids)
			cout << "task: "
			     << kshark_comm_from_pid(sd, pid)
			     << "  pid: " << pid << endl;
	}
};

int main(int argc, char **argv)
{
	kshark_context *kshark_ctx(nullptr);
	kshark_data_stream *stream;
	QApplication a(argc, argv);
	KsPluginManager plugins;
	int c, i(0), sd(-1);
	KsDataStore data;
	size_t nRows(0);

	if (!kshark_instance(&kshark_ctx))
		return 1;

	while ((c = getopt(argc, argv, "hvi:p:u:")) != -1) {
		switch(c) {
		case 'v':
			printf("kshark-gui %s\n", KS_VERSION_STRING);
			return 0;

		case 'i':
			input_file = optarg;
			break;

		case 'p':
			plugins.registerPlugins(QString(optarg));
			break;

		case 'u':
			plugins.unregisterPlugins(QString(optarg));
			break;

		case 'h':
			usage(argv[0]);
			return 0;
		}
	}

	if (!input_file) {
			struct stat st;
			if (stat(default_input_file, &st) == 0)
				input_file = default_input_file;
	}

	if (input_file) {
		sd = data.loadDataFile(input_file, {});
		nRows = data.size();
	} else {
		cerr << "No input file is provided.\n";
	}

	cout << nRows << " entries loaded\n";

	if (!nRows)
		return 1;

	stream = kshark_get_data_stream(kshark_ctx, sd);
	if (!stream)
		return 1;

	cout << "\n\n";
	for (kshark_plugin_list *pl = kshark_ctx->plugins; pl; pl = pl->next)
			cout << pl->file << endl;
	sleep(1);

	QStringList pluginsList;
	QVector<int> streamIds, enabledPlugins, failedPlugins;

	pluginsList = plugins.getStreamPluginList(sd);
	enabledPlugins = plugins.getActivePlugins(sd);
	failedPlugins = plugins.getPluginsByStatus(sd, KSHARK_PLUGIN_FAILED);

	KsCheckBoxWidget *pluginCBD
		= new KsPluginCheckBoxWidget(sd, pluginsList);
	pluginCBD->set(enabledPlugins);

	KsCheckBoxDialog *dialog1 = new KsCheckBoxDialog({pluginCBD});
	dialog1->applyStatus();
	QObject::connect(dialog1,	&KsCheckBoxDialog::apply,
			 &plugins,	&KsPluginManager::updatePlugins);

	dialog1->show();
	a.exec();

	cout << "\n\nYou selected\n";
	enabledPlugins = plugins.getActivePlugins(sd);
	for (auto const &p: pluginsList)
		qInfo() << p << (bool) enabledPlugins[i++];

	sleep(1);

	KsCheckBoxWidget *tasks_cbd =
		new KsTasksCheckBoxWidget(stream, true);

	tasks_cbd->setDefault(false);

	TaskPrint p;
	KsCheckBoxDialog *dialog2 = new KsCheckBoxDialog({tasks_cbd});
	QObject::connect(dialog2,	&KsCheckBoxDialog::apply,
			 &p,		&TaskPrint::print);

	cout << "\n\nYou selected\n";
	dialog2->show();
	a.exec();

	return 0;
}