ProfileModel.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 <AK/StringBuilder.h>
  9. #include <LibGUI/FileIconProvider.h>
  10. #include <ctype.h>
  11. #include <stdio.h>
  12. namespace Profiler {
  13. ProfileModel::ProfileModel(Profile& profile)
  14. : m_profile(profile)
  15. {
  16. m_user_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  17. m_kernel_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object-red.png"));
  18. }
  19. ProfileModel::~ProfileModel()
  20. {
  21. }
  22. GUI::ModelIndex ProfileModel::index(int row, int column, const GUI::ModelIndex& parent) const
  23. {
  24. if (!parent.is_valid()) {
  25. if (m_profile.roots().is_empty())
  26. return {};
  27. return create_index(row, column, m_profile.roots().at(row).ptr());
  28. }
  29. auto& remote_parent = *static_cast<ProfileNode*>(parent.internal_data());
  30. return create_index(row, column, remote_parent.children().at(row).ptr());
  31. }
  32. GUI::ModelIndex ProfileModel::parent_index(const GUI::ModelIndex& index) const
  33. {
  34. if (!index.is_valid())
  35. return {};
  36. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  37. if (!node.parent())
  38. return {};
  39. // NOTE: If the parent has no parent, it's a root, so we have to look among the roots.
  40. if (!node.parent()->parent()) {
  41. for (size_t row = 0; row < m_profile.roots().size(); ++row) {
  42. if (m_profile.roots()[row].ptr() == node.parent()) {
  43. return create_index(row, index.column(), node.parent());
  44. }
  45. }
  46. VERIFY_NOT_REACHED();
  47. return {};
  48. }
  49. for (size_t row = 0; row < node.parent()->parent()->children().size(); ++row) {
  50. if (node.parent()->parent()->children()[row].ptr() == node.parent())
  51. return create_index(row, index.column(), node.parent());
  52. }
  53. VERIFY_NOT_REACHED();
  54. return {};
  55. }
  56. int ProfileModel::row_count(const GUI::ModelIndex& index) const
  57. {
  58. if (!index.is_valid())
  59. return m_profile.roots().size();
  60. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  61. return node.children().size();
  62. }
  63. int ProfileModel::column_count(const GUI::ModelIndex&) const
  64. {
  65. return Column::__Count;
  66. }
  67. String ProfileModel::column_name(int column) const
  68. {
  69. switch (column) {
  70. case Column::SampleCount:
  71. return m_profile.show_percentages() ? "% Samples" : "# Samples";
  72. case Column::SelfCount:
  73. return m_profile.show_percentages() ? "% Self" : "# Self";
  74. case Column::ObjectName:
  75. return "Object";
  76. case Column::StackFrame:
  77. return "Stack Frame";
  78. default:
  79. VERIFY_NOT_REACHED();
  80. return {};
  81. }
  82. }
  83. GUI::Variant ProfileModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  84. {
  85. auto* node = static_cast<ProfileNode*>(index.internal_data());
  86. if (role == GUI::ModelRole::TextAlignment) {
  87. if (index.column() == Column::SampleCount || index.column() == Column::SelfCount)
  88. return Gfx::TextAlignment::CenterRight;
  89. }
  90. if (role == GUI::ModelRole::Icon) {
  91. if (index.column() == Column::StackFrame) {
  92. if (node->is_root()) {
  93. return GUI::FileIconProvider::icon_for_executable(node->process().executable);
  94. }
  95. if (node->address() >= 0xc0000000)
  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. void ProfileModel::update()
  125. {
  126. did_update(Model::InvalidateAllIndices);
  127. }
  128. }