memory_status.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_HUD_DISPLAY_MEMORY_STATUS_H_
  5. #define ASH_HUD_DISPLAY_MEMORY_STATUS_H_
  6. #include <cstdint>
  7. #include <string>
  8. #include "base/containers/flat_set.h"
  9. #include "base/process/process_handle.h"
  10. namespace ash {
  11. namespace hud_display {
  12. // Should run on the file thread.
  13. class MemoryStatus {
  14. public:
  15. // Must be created on io-enabled thread.
  16. MemoryStatus();
  17. MemoryStatus(const MemoryStatus&) = delete;
  18. MemoryStatus& operator=(const MemoryStatus&) = delete;
  19. int64_t total_ram_size() const { return total_ram_size_; }
  20. int64_t total_free() const { return total_free_; }
  21. int64_t gpu_kernel() const { return gpu_kernel_; }
  22. int64_t browser_rss() const { return browser_rss_; }
  23. int64_t browser_rss_shared() const { return browser_rss_shared_; }
  24. int64_t arc_rss() const { return arc_.rss(); }
  25. int64_t arc_rss_shared() const { return arc_.rss_shared(); }
  26. int64_t gpu_rss() const { return gpu_.rss(); }
  27. int64_t gpu_rss_shared() const { return gpu_.rss_shared(); }
  28. int64_t renderers_rss() const { return renderers_.rss(); }
  29. int64_t renderers_rss_shared() const { return renderers_.rss_shared(); }
  30. private:
  31. // This is designed to calculate statistics for a set of processes that
  32. // have some command-line flag.
  33. // So it is initialized with command-line flag, and then TryRead()
  34. // is called for each potential match.
  35. class ProcessMemoryCountersByFlag {
  36. public:
  37. explicit ProcessMemoryCountersByFlag(const std::string& cmd_line_flag);
  38. ~ProcessMemoryCountersByFlag();
  39. ProcessMemoryCountersByFlag(const ProcessMemoryCountersByFlag&) = delete;
  40. ProcessMemoryCountersByFlag& operator=(const ProcessMemoryCountersByFlag&) =
  41. delete;
  42. // Returns true if |pid| belongs to the class matched by this object.
  43. bool TryRead(const base::ProcessId& pid, const std::string& cmdline);
  44. int64_t rss() const { return rss_; }
  45. int64_t rss_shared() const { return rss_shared_; }
  46. private:
  47. const std::string flag_;
  48. int64_t rss_ = 0;
  49. int64_t rss_shared_ = 0;
  50. };
  51. // This is designed to calculate statistics for a set of processes that
  52. // are within the same cgroup.
  53. // So it is initialized with cgroup name (and reads list of pigs in a group
  54. // internally).
  55. class ProcessMemoryCountersByCgroup {
  56. public:
  57. explicit ProcessMemoryCountersByCgroup(const std::string& expected_cgroup);
  58. ~ProcessMemoryCountersByCgroup();
  59. ProcessMemoryCountersByCgroup(const ProcessMemoryCountersByCgroup&) =
  60. delete;
  61. ProcessMemoryCountersByCgroup& operator=(
  62. const ProcessMemoryCountersByCgroup&) = delete;
  63. // Returns true if |pid| belongs to the class matched by this object.
  64. bool TryRead(const base::ProcessId& pid);
  65. int64_t rss() const { return rss_; }
  66. int64_t rss_shared() const { return rss_shared_; }
  67. private:
  68. base::flat_set<base::ProcessId> pids_;
  69. int64_t rss_ = 0;
  70. int64_t rss_shared_ = 0;
  71. };
  72. void UpdatePerProcessStat();
  73. void UpdateMeminfo();
  74. int64_t total_ram_size_ = 0;
  75. int64_t total_free_ = 0;
  76. int64_t browser_rss_ = 0;
  77. int64_t browser_rss_shared_ = 0;
  78. int64_t gpu_kernel_ = 0;
  79. ProcessMemoryCountersByFlag renderers_{"--type=renderer\0"};
  80. ProcessMemoryCountersByFlag gpu_{"--type=gpu-process\0"};
  81. ProcessMemoryCountersByCgroup arc_{"session_manager_containers/android"};
  82. };
  83. } // namespace hud_display
  84. } // namespace ash
  85. #endif // ASH_HUD_DISPLAY_MEMORY_STATUS_H_