ProcessMemoryMapWidget.cpp 4.6 KB

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