ProfileModel.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ProfileModel.h"
  7. #include "Profile.h"
  8. #include <LibGUI/FileIconProvider.h>
  9. #include <LibSymbolication/Symbolication.h>
  10. #include <stdio.h>
  11. namespace Profiler {
  12. ProfileModel::ProfileModel(Profile& profile)
  13. : m_profile(profile)
  14. {
  15. m_user_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png"));
  16. m_kernel_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object-red.png"));
  17. }
  18. ProfileModel::~ProfileModel()
  19. {
  20. }
  21. GUI::ModelIndex ProfileModel::index(int row, int column, const GUI::ModelIndex& parent) const
  22. {
  23. if (!parent.is_valid()) {
  24. if (m_profile.roots().is_empty())
  25. return {};
  26. return create_index(row, column, m_profile.roots().at(row).ptr());
  27. }
  28. auto& remote_parent = *static_cast<ProfileNode*>(parent.internal_data());
  29. return create_index(row, column, remote_parent.children().at(row).ptr());
  30. }
  31. GUI::ModelIndex ProfileModel::parent_index(const GUI::ModelIndex& index) const
  32. {
  33. if (!index.is_valid())
  34. return {};
  35. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  36. if (!node.parent())
  37. return {};
  38. // NOTE: If the parent has no parent, it's a root, so we have to look among the roots.
  39. if (!node.parent()->parent()) {
  40. for (size_t row = 0; row < m_profile.roots().size(); ++row) {
  41. if (m_profile.roots()[row].ptr() == node.parent()) {
  42. return create_index(row, index.column(), node.parent());
  43. }
  44. }
  45. VERIFY_NOT_REACHED();
  46. return {};
  47. }
  48. for (size_t row = 0; row < node.parent()->parent()->children().size(); ++row) {
  49. if (node.parent()->parent()->children()[row].ptr() == node.parent())
  50. return create_index(row, index.column(), node.parent());
  51. }
  52. VERIFY_NOT_REACHED();
  53. return {};
  54. }
  55. int ProfileModel::row_count(const GUI::ModelIndex& index) const
  56. {
  57. if (!index.is_valid())
  58. return m_profile.roots().size();
  59. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  60. return node.children().size();
  61. }
  62. int ProfileModel::column_count(const GUI::ModelIndex&) const
  63. {
  64. return Column::__Count;
  65. }
  66. String ProfileModel::column_name(int column) const
  67. {
  68. switch (column) {
  69. case Column::SampleCount:
  70. return m_profile.show_percentages() ? "% Samples" : "# Samples";
  71. case Column::SelfCount:
  72. return m_profile.show_percentages() ? "% Self" : "# Self";
  73. case Column::ObjectName:
  74. return "Object";
  75. case Column::StackFrame:
  76. return "Stack Frame";
  77. default:
  78. VERIFY_NOT_REACHED();
  79. return {};
  80. }
  81. }
  82. GUI::Variant ProfileModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  83. {
  84. auto* node = static_cast<ProfileNode*>(index.internal_data());
  85. if (role == GUI::ModelRole::TextAlignment) {
  86. if (index.column() == Column::SampleCount || index.column() == Column::SelfCount)
  87. return Gfx::TextAlignment::CenterRight;
  88. }
  89. if (role == GUI::ModelRole::Icon) {
  90. if (index.column() == Column::StackFrame) {
  91. if (node->is_root()) {
  92. return GUI::FileIconProvider::icon_for_executable(node->process().executable);
  93. }
  94. auto maybe_kernel_base = Symbolication::kernel_base();
  95. if (maybe_kernel_base.has_value() && node->address() >= maybe_kernel_base.value())
  96. return m_kernel_frame_icon;
  97. return m_user_frame_icon;
  98. }
  99. return {};
  100. }
  101. if (role == GUI::ModelRole::Display) {
  102. if (index.column() == Column::SampleCount) {
  103. if (m_profile.show_percentages())
  104. return ((float)node->event_count() / (float)m_profile.filtered_event_indices().size()) * 100.0f;
  105. return node->event_count();
  106. }
  107. if (index.column() == Column::SelfCount) {
  108. if (m_profile.show_percentages())
  109. return ((float)node->self_count() / (float)m_profile.filtered_event_indices().size()) * 100.0f;
  110. return node->self_count();
  111. }
  112. if (index.column() == Column::ObjectName)
  113. return node->object_name();
  114. if (index.column() == Column::StackFrame) {
  115. if (node->is_root()) {
  116. return String::formatted("{} ({})", node->process().basename, node->process().pid);
  117. }
  118. return node->symbol();
  119. }
  120. return {};
  121. }
  122. return {};
  123. }
  124. }