ProfileModel.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "ProfileModel.h"
  27. #include "Profile.h"
  28. #include <AK/StringBuilder.h>
  29. #include <ctype.h>
  30. #include <stdio.h>
  31. ProfileModel::ProfileModel(Profile& profile)
  32. : m_profile(profile)
  33. {
  34. m_user_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  35. m_kernel_frame_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object-red.png"));
  36. }
  37. ProfileModel::~ProfileModel()
  38. {
  39. }
  40. GUI::ModelIndex ProfileModel::index(int row, int column, const GUI::ModelIndex& parent) const
  41. {
  42. if (!parent.is_valid()) {
  43. if (m_profile.roots().is_empty())
  44. return {};
  45. return create_index(row, column, m_profile.roots().at(row).ptr());
  46. }
  47. auto& remote_parent = *static_cast<ProfileNode*>(parent.internal_data());
  48. return create_index(row, column, remote_parent.children().at(row).ptr());
  49. }
  50. GUI::ModelIndex ProfileModel::parent_index(const GUI::ModelIndex& index) const
  51. {
  52. if (!index.is_valid())
  53. return {};
  54. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  55. if (!node.parent())
  56. return {};
  57. // NOTE: If the parent has no parent, it's a root, so we have to look among the roots.
  58. if (!node.parent()->parent()) {
  59. for (size_t row = 0; row < m_profile.roots().size(); ++row) {
  60. if (m_profile.roots()[row].ptr() == node.parent()) {
  61. return create_index(row, index.column(), node.parent());
  62. }
  63. }
  64. ASSERT_NOT_REACHED();
  65. return {};
  66. }
  67. for (size_t row = 0; row < node.parent()->parent()->children().size(); ++row) {
  68. if (node.parent()->parent()->children()[row].ptr() == node.parent())
  69. return create_index(row, index.column(), node.parent());
  70. }
  71. ASSERT_NOT_REACHED();
  72. return {};
  73. }
  74. int ProfileModel::row_count(const GUI::ModelIndex& index) const
  75. {
  76. if (!index.is_valid())
  77. return m_profile.roots().size();
  78. auto& node = *static_cast<ProfileNode*>(index.internal_data());
  79. return node.children().size();
  80. }
  81. int ProfileModel::column_count(const GUI::ModelIndex&) const
  82. {
  83. return Column::__Count;
  84. }
  85. String ProfileModel::column_name(int column) const
  86. {
  87. switch (column) {
  88. case Column::SampleCount:
  89. return "# Samples";
  90. case Column::StackFrame:
  91. return "Stack Frame";
  92. default:
  93. ASSERT_NOT_REACHED();
  94. return {};
  95. }
  96. }
  97. GUI::Model::ColumnMetadata ProfileModel::column_metadata(int column) const
  98. {
  99. if (column == Column::SampleCount)
  100. return ColumnMetadata { 0, Gfx::TextAlignment::CenterRight };
  101. return {};
  102. }
  103. GUI::Variant ProfileModel::data(const GUI::ModelIndex& index, Role role) const
  104. {
  105. auto* node = static_cast<ProfileNode*>(index.internal_data());
  106. if (role == Role::Icon) {
  107. if (index.column() == Column::StackFrame) {
  108. if (node->address() >= 0xc0000000)
  109. return m_kernel_frame_icon;
  110. return m_user_frame_icon;
  111. }
  112. return {};
  113. }
  114. if (role == Role::Display) {
  115. if (index.column() == Column::SampleCount)
  116. return node->sample_count();
  117. if (index.column() == Column::StackFrame)
  118. return node->symbol();
  119. return {};
  120. }
  121. return {};
  122. }
  123. void ProfileModel::update()
  124. {
  125. did_update();
  126. }