2022-08-19 15:59:36 +02:00
|
|
|
// Copyright (C) 2019 Klarälvdalens Datakonsult AB, a KDAB Group company,
|
2022-12-21 10:12:09 +01:00
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
2019-08-29 11:45:45 +02:00
|
|
|
#include "ctfvisualizertool.h"
|
|
|
|
|
|
|
|
|
|
#include "ctftracemanager.h"
|
|
|
|
|
|
|
|
|
|
#include "ctfstatisticsmodel.h"
|
|
|
|
|
#include "ctfstatisticsview.h"
|
2019-10-17 13:36:37 +02:00
|
|
|
#include "ctftimelinemodel.h"
|
2023-02-08 11:15:14 +01:00
|
|
|
#include "ctfvisualizertr.h"
|
2019-08-29 11:45:45 +02:00
|
|
|
#include "ctfvisualizertraceview.h"
|
|
|
|
|
|
|
|
|
|
#include <coreplugin/actionmanager/actioncontainer.h>
|
|
|
|
|
#include <coreplugin/actionmanager/actionmanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
|
|
|
|
#include <debugger/analyzer/analyzerconstants.h>
|
2023-05-09 13:20:04 +02:00
|
|
|
|
|
|
|
|
#include <utils/stylehelper.h>
|
2019-10-17 13:36:37 +02:00
|
|
|
#include <utils/utilsicons.h>
|
2019-08-29 11:45:45 +02:00
|
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
|
#include <QFutureInterface>
|
|
|
|
|
#include <QMenu>
|
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
|
|
using namespace Core;
|
|
|
|
|
using namespace CtfVisualizer::Constants;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace CtfVisualizer {
|
|
|
|
|
namespace Internal {
|
|
|
|
|
|
|
|
|
|
CtfVisualizerTool::CtfVisualizerTool()
|
|
|
|
|
: QObject (nullptr)
|
|
|
|
|
, m_isLoading(false)
|
|
|
|
|
, m_loadJson(nullptr)
|
|
|
|
|
, m_traceView(nullptr)
|
|
|
|
|
, m_modelAggregator(new Timeline::TimelineModelAggregator(this))
|
|
|
|
|
, m_zoomControl(new Timeline::TimelineZoomControl(this))
|
|
|
|
|
, m_statisticsModel(new CtfStatisticsModel(this))
|
|
|
|
|
, m_statisticsView(nullptr)
|
|
|
|
|
, m_traceManager(new CtfTraceManager(this, m_modelAggregator.get(), m_statisticsModel.get()))
|
2019-10-17 13:36:37 +02:00
|
|
|
, m_restrictToThreadsButton(new QToolButton)
|
|
|
|
|
, m_restrictToThreadsMenu(new QMenu(m_restrictToThreadsButton))
|
2019-08-29 11:45:45 +02:00
|
|
|
{
|
|
|
|
|
ActionContainer *menu = ActionManager::actionContainer(Debugger::Constants::M_DEBUG_ANALYZER);
|
|
|
|
|
ActionContainer *options = ActionManager::createMenu(Constants::CtfVisualizerMenuId);
|
2022-09-01 12:13:45 +02:00
|
|
|
options->menu()->setTitle(Tr::tr("Chrome Trace Format Viewer"));
|
2019-08-29 11:45:45 +02:00
|
|
|
menu->addMenu(options, Debugger::Constants::G_ANALYZER_REMOTE_TOOLS);
|
|
|
|
|
options->menu()->setEnabled(true);
|
|
|
|
|
|
|
|
|
|
const Core::Context globalContext(Core::Constants::C_GLOBAL);
|
|
|
|
|
|
2022-09-01 12:13:45 +02:00
|
|
|
m_loadJson.reset(new QAction(Tr::tr("Load JSON File"), options));
|
2019-08-29 11:45:45 +02:00
|
|
|
Core::Command *command = Core::ActionManager::registerAction(m_loadJson.get(), Constants::CtfVisualizerTaskLoadJson,
|
|
|
|
|
globalContext);
|
2023-09-21 19:11:01 +02:00
|
|
|
connect(m_loadJson.get(), &QAction::triggered, this, [this] {
|
|
|
|
|
QString filename = m_loadJson->data().toString();
|
|
|
|
|
if (filename.isEmpty())
|
|
|
|
|
filename = QFileDialog::getOpenFileName(ICore::dialogParent(),
|
|
|
|
|
Tr::tr("Load Chrome Trace Format File"),
|
|
|
|
|
"",
|
|
|
|
|
Tr::tr("JSON File (*.json)"));
|
|
|
|
|
loadJson(filename);
|
|
|
|
|
});
|
2019-08-29 11:45:45 +02:00
|
|
|
options->addAction(command);
|
|
|
|
|
|
|
|
|
|
m_perspective.setAboutToActivateCallback([this]() { createViews(); });
|
2019-10-17 13:36:37 +02:00
|
|
|
|
|
|
|
|
m_restrictToThreadsButton->setIcon(Utils::Icons::FILTER.icon());
|
2022-09-01 12:13:45 +02:00
|
|
|
m_restrictToThreadsButton->setToolTip(Tr::tr("Restrict to Threads"));
|
2019-10-17 13:36:37 +02:00
|
|
|
m_restrictToThreadsButton->setPopupMode(QToolButton::InstantPopup);
|
2023-05-09 13:20:04 +02:00
|
|
|
m_restrictToThreadsButton->setProperty(Utils::StyleHelper::C_NO_ARROW, true);
|
2019-10-17 13:36:37 +02:00
|
|
|
m_restrictToThreadsButton->setMenu(m_restrictToThreadsMenu);
|
|
|
|
|
connect(m_restrictToThreadsMenu, &QMenu::triggered,
|
|
|
|
|
this, &CtfVisualizerTool::toggleThreadRestriction);
|
|
|
|
|
|
|
|
|
|
m_perspective.addToolBarWidget(m_restrictToThreadsButton);
|
2019-08-29 11:45:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CtfVisualizerTool::~CtfVisualizerTool() = default;
|
|
|
|
|
|
|
|
|
|
void CtfVisualizerTool::createViews()
|
|
|
|
|
{
|
|
|
|
|
m_traceView = new CtfVisualizerTraceView(nullptr, this);
|
2022-09-01 12:13:45 +02:00
|
|
|
m_traceView->setWindowTitle(Tr::tr("Timeline"));
|
2019-08-29 11:45:45 +02:00
|
|
|
|
|
|
|
|
QMenu *contextMenu = new QMenu(m_traceView);
|
|
|
|
|
contextMenu->addAction(m_loadJson.get());
|
2022-09-01 12:13:45 +02:00
|
|
|
connect(contextMenu->addAction(Tr::tr("Reset Zoom")), &QAction::triggered, this, [this](){
|
2019-08-29 11:45:45 +02:00
|
|
|
m_zoomControl->setRange(m_zoomControl->traceStart(), m_zoomControl->traceEnd());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_traceView->setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
|
connect(m_traceView, &QWidget::customContextMenuRequested,
|
|
|
|
|
contextMenu, [contextMenu, this](const QPoint &pos) {
|
|
|
|
|
contextMenu->exec(m_traceView->mapToGlobal(pos));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
m_perspective.addWindow(m_traceView, Utils::Perspective::OperationType::SplitVertical, nullptr);
|
|
|
|
|
|
|
|
|
|
m_statisticsView = new CtfStatisticsView(m_statisticsModel.get());
|
2022-09-01 12:13:45 +02:00
|
|
|
m_statisticsView->setWindowTitle(Tr::tr("Statistics"));
|
2023-08-01 19:59:54 +02:00
|
|
|
connect(m_statisticsView, &CtfStatisticsView::eventTypeSelected, this, [this](QString title) {
|
2019-08-29 11:45:45 +02:00
|
|
|
int typeId = m_traceManager->getSelectionId(title.toStdString());
|
|
|
|
|
m_traceView->selectByTypeId(typeId);
|
|
|
|
|
});
|
|
|
|
|
connect(m_traceManager.get(), &CtfTraceManager::detailsRequested, m_statisticsView,
|
|
|
|
|
&CtfStatisticsView::selectByTitle);
|
|
|
|
|
|
|
|
|
|
m_perspective.addWindow(m_statisticsView, Utils::Perspective::AddToTab, m_traceView);
|
|
|
|
|
|
|
|
|
|
m_perspective.setAboutToActivateCallback(Utils::Perspective::Callback());
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-17 13:36:37 +02:00
|
|
|
void CtfVisualizerTool::setAvailableThreads(const QList<CtfTimelineModel *> &threads)
|
|
|
|
|
{
|
|
|
|
|
m_restrictToThreadsMenu->clear();
|
|
|
|
|
|
|
|
|
|
for (auto timelineModel : threads) {
|
|
|
|
|
QAction *action = m_restrictToThreadsMenu->addAction(timelineModel->displayName());
|
|
|
|
|
action->setCheckable(true);
|
|
|
|
|
action->setData(timelineModel->tid());
|
|
|
|
|
action->setChecked(m_traceManager->isRestrictedTo(timelineModel->tid()));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CtfVisualizerTool::toggleThreadRestriction(QAction *action)
|
|
|
|
|
{
|
2023-09-08 10:17:29 +02:00
|
|
|
const QString tid = action->data().toString();
|
2019-10-17 13:36:37 +02:00
|
|
|
m_traceManager->setThreadRestriction(tid, action->isChecked());
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-29 11:45:45 +02:00
|
|
|
Timeline::TimelineModelAggregator *CtfVisualizerTool::modelAggregator() const
|
|
|
|
|
{
|
|
|
|
|
return m_modelAggregator.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CtfTraceManager *CtfVisualizerTool::traceManager() const
|
|
|
|
|
{
|
|
|
|
|
return m_traceManager.get();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Timeline::TimelineZoomControl *CtfVisualizerTool::zoomControl() const
|
|
|
|
|
{
|
|
|
|
|
return m_zoomControl.get();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 19:11:01 +02:00
|
|
|
void CtfVisualizerTool::loadJson(const QString &filename)
|
2019-08-29 11:45:45 +02:00
|
|
|
{
|
|
|
|
|
if (m_isLoading)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (filename.isEmpty()) {
|
|
|
|
|
m_isLoading = false;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-21 19:11:01 +02:00
|
|
|
m_isLoading = true;
|
|
|
|
|
|
2019-08-29 11:45:45 +02:00
|
|
|
auto *futureInterface = new QFutureInterface<void>();
|
|
|
|
|
auto *task = new QFuture<void>(futureInterface);
|
|
|
|
|
|
|
|
|
|
QThread *thread = QThread::create([this, filename, futureInterface]() {
|
2023-09-07 14:51:01 +02:00
|
|
|
try {
|
|
|
|
|
m_traceManager->load(filename);
|
|
|
|
|
} catch (...) {
|
|
|
|
|
// nlohmann::json can throw exceptions when requesting type that is wrong
|
|
|
|
|
}
|
2019-08-29 11:45:45 +02:00
|
|
|
m_modelAggregator->moveToThread(QApplication::instance()->thread());
|
|
|
|
|
m_modelAggregator->setParent(this);
|
|
|
|
|
futureInterface->reportFinished();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
connect(thread, &QThread::finished, this, [this, thread, task, futureInterface]() {
|
|
|
|
|
// in main thread:
|
|
|
|
|
if (m_traceManager->isEmpty()) {
|
2020-06-02 09:10:40 +02:00
|
|
|
QMessageBox::warning(Core::ICore::dialogParent(),
|
2022-09-01 12:13:45 +02:00
|
|
|
Tr::tr("CTF Visualizer"),
|
|
|
|
|
Tr::tr("The file does not contain any trace data."));
|
2019-08-29 11:45:45 +02:00
|
|
|
} else {
|
|
|
|
|
m_traceManager->finalize();
|
|
|
|
|
m_perspective.select();
|
|
|
|
|
zoomControl()->setTrace(m_traceManager->traceBegin(), m_traceManager->traceEnd() + m_traceManager->traceDuration() / 20);
|
|
|
|
|
zoomControl()->setRange(m_traceManager->traceBegin(), m_traceManager->traceEnd() + m_traceManager->traceDuration() / 20);
|
|
|
|
|
}
|
2019-10-17 13:36:37 +02:00
|
|
|
setAvailableThreads(m_traceManager->getSortedThreads());
|
2019-08-29 11:45:45 +02:00
|
|
|
thread->deleteLater();
|
|
|
|
|
delete task;
|
|
|
|
|
delete futureInterface;
|
|
|
|
|
m_isLoading = false;
|
|
|
|
|
}, Qt::QueuedConnection);
|
|
|
|
|
|
|
|
|
|
m_modelAggregator->setParent(nullptr);
|
|
|
|
|
m_modelAggregator->moveToThread(thread);
|
|
|
|
|
|
|
|
|
|
thread->start();
|
2022-09-01 12:13:45 +02:00
|
|
|
Core::ProgressManager::addTask(*task, Tr::tr("Loading CTF File"), CtfVisualizerTaskLoadJson);
|
2019-08-29 11:45:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace CtfVisualizer
|