ProcessMemoryMapWidget.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ProcessMemoryMapWidget.h"
  7. #include <LibCore/Timer.h>
  8. #include <LibGUI/BoxLayout.h>
  9. #include <LibGUI/JsonArrayModel.h>
  10. #include <LibGUI/Painter.h>
  11. #include <LibGUI/SortingProxyModel.h>
  12. #include <LibGUI/TableView.h>
  13. #include <LibGfx/Palette.h>
  14. class PagemapPaintingDelegate final : public GUI::TableCellPaintingDelegate {
  15. public:
  16. virtual ~PagemapPaintingDelegate() override { }
  17. virtual void paint(GUI::Painter& painter, const Gfx::IntRect& a_rect, const Gfx::Palette&, const GUI::ModelIndex& index) override
  18. {
  19. auto rect = a_rect.shrunken(2, 2);
  20. auto pagemap = index.data(GUI::ModelRole::Custom).to_string();
  21. float scale_factor = (float)pagemap.length() / (float)rect.width();
  22. for (int i = 0; i < rect.width(); ++i) {
  23. int x = rect.x() + i;
  24. char c = pagemap[(float)i * scale_factor];
  25. Color color;
  26. if (c == 'N') // Null (no page at all, typically an inode-backed page that hasn't been paged in.)
  27. color = Color::White;
  28. else if (c == 'Z') // Zero (globally shared zero page, typically an untouched anonymous page.)
  29. color = Color::from_rgb(0xc0c0ff);
  30. else if (c == 'P') // Physical (a resident page)
  31. color = Color::Black;
  32. else
  33. VERIFY_NOT_REACHED();
  34. painter.draw_line({ x, rect.top() }, { x, rect.bottom() }, color);
  35. }
  36. painter.draw_rect(rect, Color::Black);
  37. }
  38. };
  39. ProcessMemoryMapWidget::ProcessMemoryMapWidget()
  40. {
  41. set_layout<GUI::VerticalBoxLayout>();
  42. layout()->set_margins({ 4, 4, 4, 4 });
  43. m_table_view = add<GUI::TableView>();
  44. Vector<GUI::JsonArrayModel::FieldSpec> pid_vm_fields;
  45. pid_vm_fields.empend(
  46. "Address", Gfx::TextAlignment::CenterLeft,
  47. [](auto& object) { return String::formatted("{:p}", object.get("address").to_u64()); },
  48. [](auto& object) { return object.get("address").to_u64(); });
  49. pid_vm_fields.empend("size", "Size", Gfx::TextAlignment::CenterRight);
  50. pid_vm_fields.empend("amount_resident", "Resident", Gfx::TextAlignment::CenterRight);
  51. pid_vm_fields.empend("amount_dirty", "Dirty", Gfx::TextAlignment::CenterRight);
  52. pid_vm_fields.empend("Access", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  53. StringBuilder builder;
  54. if (object.get("readable").to_bool())
  55. builder.append('R');
  56. if (object.get("writable").to_bool())
  57. builder.append('W');
  58. if (object.get("executable").to_bool())
  59. builder.append('X');
  60. if (object.get("shared").to_bool())
  61. builder.append('S');
  62. if (object.get("syscall").to_bool())
  63. builder.append('C');
  64. if (object.get("stack").to_bool())
  65. builder.append('T');
  66. return builder.to_string();
  67. });
  68. pid_vm_fields.empend("VMObject type", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  69. auto type = object.get("vmobject").to_string();
  70. if (type.ends_with("VMObject"))
  71. type = type.substring(0, type.length() - 8);
  72. return type;
  73. });
  74. pid_vm_fields.empend("Purgeable", Gfx::TextAlignment::CenterLeft, [](auto& object) {
  75. if (object.get("volatile").to_bool())
  76. return "Volatile";
  77. return "Non-volatile";
  78. });
  79. pid_vm_fields.empend(
  80. "Page map", Gfx::TextAlignment::CenterLeft,
  81. [](auto&) {
  82. return GUI::Variant();
  83. },
  84. [](auto&) {
  85. return GUI::Variant(0);
  86. },
  87. [](const JsonObject& object) {
  88. auto pagemap = object.get("pagemap").as_string_or({});
  89. return pagemap;
  90. });
  91. pid_vm_fields.empend("cow_pages", "# CoW", Gfx::TextAlignment::CenterRight);
  92. pid_vm_fields.empend("name", "Name", Gfx::TextAlignment::CenterLeft);
  93. m_json_model = GUI::JsonArrayModel::create({}, move(pid_vm_fields));
  94. m_table_view->set_model(GUI::SortingProxyModel::create(*m_json_model));
  95. m_table_view->set_column_painting_delegate(7, make<PagemapPaintingDelegate>());
  96. m_table_view->set_key_column_and_sort_order(0, GUI::SortOrder::Ascending);
  97. m_timer = add<Core::Timer>(1000, [this] { refresh(); });
  98. }
  99. ProcessMemoryMapWidget::~ProcessMemoryMapWidget()
  100. {
  101. }
  102. void ProcessMemoryMapWidget::set_pid(pid_t pid)
  103. {
  104. if (m_pid == pid)
  105. return;
  106. m_pid = pid;
  107. m_json_model->set_json_path(String::formatted("/proc/{}/vm", pid));
  108. }
  109. void ProcessMemoryMapWidget::refresh()
  110. {
  111. if (m_pid != -1)
  112. m_json_model->invalidate();
  113. }