SamplesModel.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SamplesModel.h"
  7. #include "Profile.h"
  8. #include <AK/StringBuilder.h>
  9. #include <stdio.h>
  10. namespace Profiler {
  11. SamplesModel::SamplesModel(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. SamplesModel::~SamplesModel()
  18. {
  19. }
  20. int SamplesModel::row_count(const GUI::ModelIndex&) const
  21. {
  22. return m_profile.filtered_event_indices().size();
  23. }
  24. int SamplesModel::column_count(const GUI::ModelIndex&) const
  25. {
  26. return Column::__Count;
  27. }
  28. String SamplesModel::column_name(int column) const
  29. {
  30. switch (column) {
  31. case Column::SampleIndex:
  32. return "#";
  33. case Column::Timestamp:
  34. return "Timestamp";
  35. case Column::ProcessID:
  36. return "PID";
  37. case Column::ThreadID:
  38. return "TID";
  39. case Column::ExecutableName:
  40. return "Executable";
  41. case Column::LostSamples:
  42. return "Lost Samples";
  43. case Column::InnermostStackFrame:
  44. return "Innermost Frame";
  45. default:
  46. VERIFY_NOT_REACHED();
  47. }
  48. }
  49. GUI::Variant SamplesModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  50. {
  51. u32 event_index = m_profile.filtered_event_indices()[index.row()];
  52. auto& event = m_profile.events().at(event_index);
  53. if (role == GUI::ModelRole::Custom) {
  54. return event_index;
  55. }
  56. if (role == GUI::ModelRole::Display) {
  57. if (index.column() == Column::SampleIndex)
  58. return event_index;
  59. if (index.column() == Column::ProcessID)
  60. return event.pid;
  61. if (index.column() == Column::ThreadID)
  62. return event.tid;
  63. if (index.column() == Column::ExecutableName) {
  64. if (auto* process = m_profile.find_process(event.pid, event.timestamp))
  65. return process->executable;
  66. return "";
  67. }
  68. if (index.column() == Column::Timestamp) {
  69. return (u32)event.timestamp;
  70. }
  71. if (index.column() == Column::LostSamples) {
  72. return event.lost_samples;
  73. }
  74. if (index.column() == Column::InnermostStackFrame) {
  75. return event.frames.last().symbol;
  76. }
  77. return {};
  78. }
  79. return {};
  80. }
  81. void SamplesModel::update()
  82. {
  83. did_update(Model::InvalidateAllIndices);
  84. }
  85. }