| 1 | // Copyright 2024 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/JitBlockTableModel.h" |
| 5 | |
| 6 | #include <array> |
| 7 | #include <span> |
| 8 | #include <utility> |
| 9 | |
| 10 | #include "Common/Assert.h" |
| 11 | #include "Core/Core.h" |
| 12 | #include "Core/PowerPC/JitInterface.h" |
| 13 | #include "Core/PowerPC/PPCSymbolDB.h" |
| 14 | #include "DolphinQt/Host.h" |
| 15 | #include "DolphinQt/Settings.h" |
| 16 | |
| 17 | const JitBlock& JitBlockTableModel::GetJitBlock(const QModelIndex& index) const |
| 18 | { |
| 19 | ASSERT(index.isValid()); |
| 20 | return m_jit_blocks[index.row()]; |
| 21 | } |
| 22 | |
| 23 | void JitBlockTableModel::SumOverallCosts() |
| 24 | { |
| 25 | m_overall_cycles_spent = 0; |
| 26 | m_overall_time_spent = {}; |
| 27 | for (const JitBlock& block : m_jit_blocks) |
| 28 | { |
| 29 | if (block.profile_data == nullptr) |
| 30 | continue; |
| 31 | m_overall_cycles_spent += block.profile_data->cycles_spent; |
| 32 | m_overall_time_spent += block.profile_data->time_spent; |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | static QVariant GetSymbolNameQVariant(const Common::Symbol* symbol) |
| 37 | { |
| 38 | return symbol ? QString::fromStdString(symbol->name) : QVariant{}; |
| 39 | } |
| 40 | |
| 41 | void JitBlockTableModel::PrefetchSymbols() |
| 42 | { |
| 43 | m_symbol_list.clear(); |
| 44 | m_symbol_list.reserve(m_jit_blocks.size()); |
| 45 | // If the table viewing this model will be accessing every element, |
| 46 | // it would be a waste of effort to lazy-initialize the symbol list. |
| 47 | if (m_sorting_by_symbols || m_filtering_by_symbols) |
| 48 | { |
| 49 | for (const JitBlock& block : m_jit_blocks) |
| 50 | { |
| 51 | m_symbol_list.emplace_back( |
| 52 | GetSymbolNameQVariant(m_ppc_symbol_db.GetSymbolFromAddr(block.effectiveAddress))); |
| 53 | } |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | for (const JitBlock& block : m_jit_blocks) |
| 58 | { |
| 59 | m_symbol_list.emplace_back([this, &block] { |
| 60 | return GetSymbolNameQVariant(m_ppc_symbol_db.GetSymbolFromAddr(block.effectiveAddress)); |
| 61 | }); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void JitBlockTableModel::Clear() |
| 67 | { |
| 68 | emit layoutAboutToBeChanged(); |
| 69 | m_jit_blocks.clear(); |
| 70 | m_symbol_list.clear(); |
| 71 | emit layoutChanged(); |
| 72 | } |
| 73 | |
| 74 | void JitBlockTableModel::Update(Core::State state) |
| 75 | { |
| 76 | emit layoutAboutToBeChanged(); |
| 77 | m_jit_blocks.clear(); |
| 78 | if (state == Core::State::Paused) |
| 79 | { |
| 80 | m_jit_blocks.reserve(m_jit_interface.GetBlockCount()); |
| 81 | m_jit_interface.RunOnBlocks(Core::CPUThreadGuard{m_system}, [this](const JitBlock& block) { |
| 82 | m_jit_blocks.emplace_back(block); |
| 83 | }); |
| 84 | SumOverallCosts(); |
| 85 | } |
| 86 | PrefetchSymbols(); |
| 87 | emit layoutChanged(); |
| 88 | } |
| 89 | |
| 90 | void JitBlockTableModel::UpdateProfileData() |
| 91 | { |
| 92 | const int row_count = rowCount(); |
| 93 | if (row_count <= 0) |
| 94 | return; |
| 95 | SumOverallCosts(); |
| 96 | static const QList<int> roles = {Qt::DisplayRole}; |
| 97 | const int last = row_count - 1; |
| 98 | emit dataChanged(createIndex(0, Column::RunCount), createIndex(last, Column::TimePercent), roles); |
| 99 | } |
| 100 | |
| 101 | void JitBlockTableModel::UpdateSymbols() |
| 102 | { |
| 103 | const int row_count = rowCount(); |
| 104 | if (row_count <= 0) |
| 105 | return; |
| 106 | PrefetchSymbols(); |
| 107 | static const QList<int> roles = {Qt::DisplayRole}; |
| 108 | const int last = row_count - 1; |
| 109 | emit dataChanged(createIndex(0, Column::Symbol), createIndex(last, Column::Symbol), roles); |
| 110 | } |
| 111 | |
| 112 | void JitBlockTableModel::ConnectSlots() |
| 113 | { |
| 114 | auto* const host = Host::GetInstance(); |
| 115 | connect(host, &Host::JitCacheInvalidation, this, &JitBlockTableModel::OnJitCacheInvalidation); |
| 116 | connect(host, &Host::JitProfileDataWiped, this, &JitBlockTableModel::OnJitProfileDataWiped); |
| 117 | connect(host, &Host::UpdateDisasmDialog, this, &JitBlockTableModel::OnUpdateDisasmDialog); |
| 118 | connect(host, &Host::PPCSymbolsChanged, this, &JitBlockTableModel::OnPPCSymbolsUpdated); |
| 119 | connect(host, &Host::PPCBreakpointsChanged, this, &JitBlockTableModel::OnPPCBreakpointsChanged); |
| 120 | auto* const settings = &Settings::Instance(); |
| 121 | connect(settings, &Settings::EmulationStateChanged, this, |
| 122 | &JitBlockTableModel::OnEmulationStateChanged); |
| 123 | } |
| 124 | |
| 125 | void JitBlockTableModel::DisconnectSlots() |
| 126 | { |
| 127 | auto* const host = Host::GetInstance(); |
| 128 | disconnect(host, &Host::JitCacheInvalidation, this, &JitBlockTableModel::OnJitCacheInvalidation); |
| 129 | disconnect(host, &Host::JitProfileDataWiped, this, &JitBlockTableModel::OnJitProfileDataWiped); |
| 130 | disconnect(host, &Host::UpdateDisasmDialog, this, &JitBlockTableModel::OnUpdateDisasmDialog); |
| 131 | disconnect(host, &Host::PPCSymbolsChanged, this, &JitBlockTableModel::OnPPCSymbolsUpdated); |
| 132 | disconnect(host, &Host::PPCBreakpointsChanged, this, |
| 133 | &JitBlockTableModel::OnPPCBreakpointsChanged); |
| 134 | auto* const settings = &Settings::Instance(); |
| 135 | disconnect(settings, &Settings::EmulationStateChanged, this, |
| 136 | &JitBlockTableModel::OnEmulationStateChanged); |
| 137 | } |
| 138 | |
| 139 | void JitBlockTableModel::Show() |
| 140 | { |
| 141 | ConnectSlots(); |
| 142 | // Every slot that may have missed a signal while this model was hidden can be handled by: |
| 143 | Update(Core::GetState(m_system)); |
| 144 | } |
| 145 | |
| 146 | void JitBlockTableModel::Hide() |
| 147 | { |
| 148 | DisconnectSlots(); |
| 149 | Clear(); |
| 150 | } |
| 151 | |
| 152 | void JitBlockTableModel::OnShowSignal() |
| 153 | { |
| 154 | Show(); |
| 155 | } |
| 156 | |
| 157 | void JitBlockTableModel::OnHideSignal() |
| 158 | { |
| 159 | Hide(); |
| 160 | } |
| 161 | |
| 162 | void JitBlockTableModel::OnSortIndicatorChanged(int logicalIndex, Qt::SortOrder) |
| 163 | { |
| 164 | m_sorting_by_symbols = logicalIndex == Column::Symbol; |
| 165 | } |
| 166 | |
| 167 | void JitBlockTableModel::OnFilterSymbolTextChanged(const QString& string) |
| 168 | { |
| 169 | m_filtering_by_symbols = !string.isEmpty(); |
| 170 | } |
| 171 | |
| 172 | void JitBlockTableModel::OnJitCacheInvalidation() |
| 173 | { |
| 174 | Update(Core::GetState(m_system)); |
| 175 | } |
| 176 | |
| 177 | void JitBlockTableModel::OnJitProfileDataWiped() |
| 178 | { |
| 179 | UpdateProfileData(); |
| 180 | } |
| 181 | |
| 182 | void JitBlockTableModel::OnUpdateDisasmDialog() |
| 183 | { |
| 184 | // This should hopefully catch all the little things that lead to stale JitBlock references. |
| 185 | Update(Core::GetState(m_system)); |
| 186 | } |
| 187 | |
| 188 | void JitBlockTableModel::OnPPCSymbolsUpdated() |
| 189 | { |
| 190 | // Previously, this was only a call to `UpdateSymbols`, but HLE patch engine code can |
| 191 | // invalidate JIT blocks when specific symbols are loaded. What can be done about it? |
| 192 | Update(Core::GetState(m_system)); |
| 193 | } |
| 194 | |
| 195 | void JitBlockTableModel::OnPPCBreakpointsChanged() |
| 196 | { |
| 197 | Update(Core::GetState(m_system)); |
| 198 | } |
| 199 | |
| 200 | void JitBlockTableModel::OnEmulationStateChanged(Core::State state) |
| 201 | { |
| 202 | Update(state); |
| 203 | } |
| 204 | |
| 205 | static QString GetQStringDescription(const CPUEmuFeatureFlags flags) |
| 206 | { |
| 207 | static const std::array<QString, (FEATURE_FLAG_END_OF_ENUMERATION - 1) << 1> descriptions = { |
| 208 | QStringLiteral(""), QStringLiteral("DR"), |
| 209 | QStringLiteral("IR"), QStringLiteral("DR|IR"), |
| 210 | QStringLiteral("PERFMON"), QStringLiteral("DR|PERFMON"), |
| 211 | QStringLiteral("IR|PERFMON"), QStringLiteral("DR|IR|PERFMON"), |
| 212 | }; |
| 213 | return descriptions[flags]; |
| 214 | } |
| 215 | |
| 216 | static QVariant GetValidSymbolStringVariant(const QVariant& symbol_name_v) |
| 217 | { |
| 218 | if (symbol_name_v.isValid()) |
| 219 | return symbol_name_v; |
| 220 | return QStringLiteral(" --- "); |
| 221 | } |
| 222 | |
| 223 | QVariant JitBlockTableModel::DisplayRoleData(const QModelIndex& index) const |
| 224 | { |
| 225 | const int column = index.column(); |
| 226 | if (column == Column::Symbol) |
| 227 | return GetValidSymbolStringVariant(*m_symbol_list[index.row()]); |
| 228 | |
| 229 | const JitBlock& jit_block = m_jit_blocks[index.row()]; |
| 230 | switch (column) |
| 231 | { |
| 232 | case Column::PPCFeatureFlags: |
| 233 | return GetQStringDescription(jit_block.feature_flags); |
| 234 | case Column::EffectiveAddress: |
| 235 | return QString::number(jit_block.effectiveAddress, 16); |
| 236 | case Column::CodeBufferSize: |
| 237 | return QString::number(jit_block.originalSize * sizeof(UGeckoInstruction)); |
| 238 | case Column::RepeatInstructions: |
| 239 | return QString::number(jit_block.originalSize - jit_block.physical_addresses.get_stats().first / |
| 240 | sizeof(UGeckoInstruction)); |
| 241 | case Column::HostNearCodeSize: |
| 242 | return QString::number(jit_block.near_end - jit_block.near_begin); |
| 243 | case Column::HostFarCodeSize: |
| 244 | return QString::number(jit_block.far_end - jit_block.far_begin); |
| 245 | } |
| 246 | const JitBlock::ProfileData* const profile_data = jit_block.profile_data.get(); |
| 247 | if (profile_data == nullptr) |
| 248 | return QStringLiteral(" --- "); |
| 249 | switch (column) |
| 250 | { |
| 251 | case Column::RunCount: |
| 252 | return QString::number(profile_data->run_count); |
| 253 | case Column::CyclesSpent: |
| 254 | return QString::number(profile_data->cycles_spent); |
| 255 | case Column::CyclesAverage: |
| 256 | if (profile_data->run_count == 0) |
| 257 | return QStringLiteral(" --- "); |
| 258 | return QString::number( |
| 259 | static_cast<double>(profile_data->cycles_spent) / profile_data->run_count, 'f', 6); |
| 260 | case Column::CyclesPercent: |
| 261 | if (m_overall_cycles_spent == 0) |
| 262 | return QStringLiteral(" --- "); |
| 263 | return QStringLiteral("%1%").arg(100.0 * profile_data->cycles_spent / m_overall_cycles_spent, |
| 264 | 10, 'f', 6); |
| 265 | case Column::TimeSpent: |
| 266 | { |
| 267 | const auto cast_time = |
| 268 | std::chrono::duration_cast<std::chrono::nanoseconds>(profile_data->time_spent); |
| 269 | return QString::number(cast_time.count()); |
| 270 | } |
| 271 | case Column::TimeAverage: |
| 272 | { |
| 273 | if (profile_data->run_count == 0) |
| 274 | return QStringLiteral(" --- "); |
| 275 | const auto cast_time = std::chrono::duration_cast<std::chrono::duration<double, std::nano>>( |
| 276 | profile_data->time_spent); |
| 277 | return QString::number(cast_time.count() / profile_data->run_count, 'f', 6); |
| 278 | } |
| 279 | case Column::TimePercent: |
| 280 | { |
| 281 | if (m_overall_time_spent == JitBlock::ProfileData::Clock::duration{}) |
| 282 | return QStringLiteral(" --- "); |
| 283 | return QStringLiteral("%1%").arg( |
| 284 | 100.0 * profile_data->time_spent.count() / m_overall_time_spent.count(), 10, 'f', 6); |
| 285 | } |
| 286 | } |
| 287 | static_assert(Column::NumberOfColumns == 14); |
| 288 | std::unreachable(); |
| 289 | } |
| 290 | |
| 291 | QVariant JitBlockTableModel::TextAlignmentRoleData(const QModelIndex& index) const |
| 292 | { |
| 293 | switch (index.column()) |
| 294 | { |
| 295 | case Column::PPCFeatureFlags: |
| 296 | case Column::EffectiveAddress: |
| 297 | return Qt::AlignCenter; |
| 298 | case Column::CodeBufferSize: |
| 299 | case Column::RepeatInstructions: |
| 300 | case Column::HostNearCodeSize: |
| 301 | case Column::HostFarCodeSize: |
| 302 | case Column::RunCount: |
| 303 | case Column::CyclesSpent: |
| 304 | case Column::CyclesAverage: |
| 305 | case Column::CyclesPercent: |
| 306 | case Column::TimeSpent: |
| 307 | case Column::TimeAverage: |
| 308 | case Column::TimePercent: |
| 309 | return QVariant::fromValue(Qt::AlignRight | Qt::AlignVCenter); |
| 310 | case Column::Symbol: |
| 311 | return QVariant::fromValue(Qt::AlignLeft | Qt::AlignVCenter); |
| 312 | } |
| 313 | static_assert(Column::NumberOfColumns == 14); |
| 314 | std::unreachable(); |
| 315 | } |
| 316 | |
| 317 | QVariant JitBlockTableModel::SortRoleData(const QModelIndex& index) const |
| 318 | { |
| 319 | const int column = index.column(); |
| 320 | if (column == Column::Symbol) |
| 321 | return *m_symbol_list[index.row()]; |
| 322 | |
| 323 | const JitBlock& jit_block = m_jit_blocks[index.row()]; |
| 324 | switch (column) |
| 325 | { |
| 326 | case Column::PPCFeatureFlags: |
| 327 | return jit_block.feature_flags; |
| 328 | case Column::EffectiveAddress: |
| 329 | return jit_block.effectiveAddress; |
| 330 | case Column::CodeBufferSize: |
| 331 | return static_cast<qulonglong>(jit_block.originalSize); |
| 332 | case Column::RepeatInstructions: |
| 333 | return static_cast<qulonglong>(jit_block.originalSize - |
| 334 | jit_block.physical_addresses.get_stats().first / |
| 335 | sizeof(UGeckoInstruction)); |
| 336 | case Column::HostNearCodeSize: |
| 337 | return static_cast<qulonglong>(jit_block.near_end - jit_block.near_begin); |
| 338 | case Column::HostFarCodeSize: |
| 339 | return static_cast<qulonglong>(jit_block.far_end - jit_block.far_begin); |
| 340 | } |
| 341 | const JitBlock::ProfileData* const profile_data = jit_block.profile_data.get(); |
| 342 | if (profile_data == nullptr) |
| 343 | return QVariant(); |
| 344 | switch (column) |
| 345 | { |
| 346 | case Column::RunCount: |
| 347 | return static_cast<qulonglong>(profile_data->run_count); |
| 348 | case Column::CyclesSpent: |
| 349 | case Column::CyclesPercent: |
| 350 | return static_cast<qulonglong>(profile_data->cycles_spent); |
| 351 | case Column::CyclesAverage: |
| 352 | if (profile_data->run_count == 0) |
| 353 | return QVariant(); |
| 354 | return static_cast<double>(profile_data->cycles_spent) / profile_data->run_count; |
| 355 | case Column::TimeSpent: |
| 356 | case Column::TimePercent: |
| 357 | return static_cast<qulonglong>(profile_data->time_spent.count()); |
| 358 | case Column::TimeAverage: |
| 359 | if (profile_data->run_count == 0) |
| 360 | return QVariant(); |
| 361 | return static_cast<double>(profile_data->time_spent.count()) / profile_data->run_count; |
| 362 | } |
| 363 | static_assert(Column::NumberOfColumns == 14); |
| 364 | std::unreachable(); |
| 365 | } |
| 366 | |
| 367 | QVariant JitBlockTableModel::data(const QModelIndex& index, int role) const |
| 368 | { |
| 369 | switch (role) |
| 370 | { |
| 371 | case Qt::DisplayRole: |
| 372 | return DisplayRoleData(index); |
| 373 | case Qt::TextAlignmentRole: |
| 374 | return TextAlignmentRoleData(index); |
| 375 | case UserRole::SortRole: |
| 376 | return SortRoleData(index); |
| 377 | } |
| 378 | return QVariant(); |
| 379 | } |
| 380 | |
| 381 | QVariant JitBlockTableModel::headerData(int section, Qt::Orientation orientation, int role) const |
| 382 | { |
| 383 | if (orientation != Qt::Horizontal || role != Qt::DisplayRole) |
| 384 | return QVariant(); |
| 385 | |
| 386 | // These abbreviated table header display names have unabbreviated counterparts in JITWidget.cpp. |
| 387 | static constexpr std::array<const char*, Column::NumberOfColumns> headers = { |
| 388 | // i18n: PPC Feature Flags |
| 389 | QT_TR_NOOP("PPC Feat. Flags"), |
| 390 | // i18n: Effective Address |
| 391 | QT_TR_NOOP("Eff. Address"), |
| 392 | // i18n: Code Buffer Size |
| 393 | QT_TR_NOOP("Code Buff. Size"), |
| 394 | // i18n: Repeat Instructions |
| 395 | QT_TR_NOOP("Repeat Instr."), |
| 396 | // i18n: Host Near Code Size |
| 397 | QT_TR_NOOP("Host N. Size"), |
| 398 | // i18n: Host Far Code Size |
| 399 | QT_TR_NOOP("Host F. Size"), |
| 400 | QT_TR_NOOP("Run Count"), |
| 401 | QT_TR_NOOP("Cycles Spent"), |
| 402 | // i18n: Cycles Average |
| 403 | QT_TR_NOOP("Cycles Avg."), |
| 404 | // i18n: Cycles Percent |
| 405 | QT_TR_NOOP("Cycles %"), |
| 406 | QT_TR_NOOP("Time Spent (ns)"), |
| 407 | // i18n: Time Average (ns) |
| 408 | QT_TR_NOOP("Time Avg. (ns)"), |
| 409 | // i18n: Time Percent |
| 410 | QT_TR_NOOP("Time %"), |
| 411 | QT_TR_NOOP("Symbol"), |
| 412 | }; |
| 413 | |
| 414 | return tr(headers[section]); |
| 415 | } |
| 416 | |
| 417 | int JitBlockTableModel::rowCount(const QModelIndex& parent) const |
| 418 | { |
| 419 | if (parent.isValid()) [[unlikely]] |
| 420 | return 0; |
| 421 | return m_jit_blocks.size(); |
| 422 | } |
| 423 | |
| 424 | int JitBlockTableModel::columnCount(const QModelIndex& parent) const |
| 425 | { |
| 426 | if (parent.isValid()) [[unlikely]] |
| 427 | return 0; |
| 428 | return Column::NumberOfColumns; |
| 429 | } |
| 430 | |
| 431 | bool JitBlockTableModel::removeRows(int row, int count, const QModelIndex& parent) |
| 432 | { |
| 433 | if (parent.isValid() || row < 0) [[unlikely]] |
| 434 | return false; |
| 435 | if (count <= 0) [[unlikely]] |
| 436 | return true; |
| 437 | |
| 438 | beginRemoveRows(parent, row, row + count - 1); // Last is inclusive in Qt! |
| 439 | for (const JitBlock& block : |
| 440 | std::span{m_jit_blocks.data() + row, static_cast<std::size_t>(count)}) |
| 441 | { |
| 442 | m_jit_interface.EraseSingleBlock(block); |
| 443 | } |
| 444 | m_jit_blocks.remove(row, count); |
| 445 | m_symbol_list.remove(row, count); |
| 446 | endRemoveRows(); |
| 447 | return true; |
| 448 | } |
| 449 | |
| 450 | JitBlockTableModel::JitBlockTableModel(Core::System& system, JitInterface& jit_interface, |
| 451 | PPCSymbolDB& ppc_symbol_db, QObject* parent) |
| 452 | : QAbstractTableModel(parent), m_system(system), m_jit_interface(jit_interface), |
| 453 | m_ppc_symbol_db(ppc_symbol_db) |
| 454 | { |
| 455 | } |
| 456 | |
| 457 | JitBlockTableModel::~JitBlockTableModel() = default; |