ProfileModel.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "ProfileModel.h"
  2. #include "Profile.h"
  3. #include <AK/StringBuilder.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. ProfileModel::ProfileModel(Profile& profile)
  7. : m_profile(profile)
  8. {
  9. m_user_frame_icon.set_bitmap_for_size(16, GraphicsBitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  10. m_kernel_frame_icon.set_bitmap_for_size(16, GraphicsBitmap::load_from_file("/res/icons/16x16/inspector-object-red.png"));
  11. }
  12. ProfileModel::~ProfileModel()
  13. {
  14. }
  15. GModelIndex ProfileModel::index(int row, int column, const GModelIndex& parent) const
  16. {
  17. if (!parent.is_valid()) {
  18. if (m_profile.roots().is_empty())
  19. return {};
  20. return create_index(row, column, m_profile.roots().at(row).ptr());
  21. }
  22. auto& remote_parent = *static_cast<ProfileNode*>(parent.internal_data());
  23. return create_index(row, column, remote_parent.children().at(row).ptr());
  24. }
  25. GModelIndex ProfileModel::parent_index(const GModelIndex& index) const
  26. {
  27. if (!index.is_valid())
  28. return {};
  29. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  30. if (!node.parent())
  31. return {};
  32. // NOTE: If the parent has no parent, it's a root, so we have to look among the roots.
  33. if (!node.parent()->parent()) {
  34. for (int row = 0; row < m_profile.roots().size(); ++row) {
  35. if (m_profile.roots()[row].ptr() == node.parent()) {
  36. return create_index(row, 0, node.parent());
  37. }
  38. }
  39. ASSERT_NOT_REACHED();
  40. return {};
  41. }
  42. for (int row = 0; row < node.parent()->parent()->children().size(); ++row) {
  43. if (node.parent()->parent()->children()[row].ptr() == node.parent())
  44. return create_index(row, 0, node.parent());
  45. }
  46. ASSERT_NOT_REACHED();
  47. return {};
  48. }
  49. int ProfileModel::row_count(const GModelIndex& index) const
  50. {
  51. if (!index.is_valid())
  52. return m_profile.roots().size();
  53. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  54. return node.children().size();
  55. }
  56. int ProfileModel::column_count(const GModelIndex&) const
  57. {
  58. return Column::__Count;
  59. }
  60. String ProfileModel::column_name(int column) const
  61. {
  62. switch (column) {
  63. case Column::SampleCount:
  64. return "# Samples";
  65. case Column::StackFrame:
  66. return "Stack Frame";
  67. default:
  68. ASSERT_NOT_REACHED();
  69. return {};
  70. }
  71. }
  72. GModel::ColumnMetadata ProfileModel::column_metadata(int column) const
  73. {
  74. if (column == Column::SampleCount)
  75. return ColumnMetadata { 0, TextAlignment::CenterRight };
  76. return {};
  77. }
  78. GVariant ProfileModel::data(const GModelIndex& index, Role role) const
  79. {
  80. auto* node = static_cast<ProfileNode*>(index.internal_data());
  81. if (role == Role::Icon) {
  82. if (index.column() == Column::StackFrame) {
  83. if (node->address() < (8 * MB) || node->address() >= 0xc0000000)
  84. return m_kernel_frame_icon;
  85. return m_user_frame_icon;
  86. }
  87. return {};
  88. }
  89. if (role == Role::Display) {
  90. if (index.column() == Column::SampleCount)
  91. return node->sample_count();
  92. if (index.column() == Column::StackFrame)
  93. return node->symbol();
  94. return {};
  95. }
  96. return {};
  97. }
  98. void ProfileModel::update()
  99. {
  100. did_update();
  101. }