ProfileModel.cpp 4.1 KB

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