psi_memory_parser.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2021 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 COMPONENTS_METRICS_PSI_MEMORY_PARSER_H_
  5. #define COMPONENTS_METRICS_PSI_MEMORY_PARSER_H_
  6. #include <string>
  7. #include "base/gtest_prod_util.h"
  8. #include "base/strings/string_piece.h"
  9. namespace metrics {
  10. // Items in internal are - as the name implies - NOT for outside consumption.
  11. // Defined here to allow access to unit test.
  12. namespace internal {
  13. // Finds the bounds for a substring of |content| which is sandwiched between
  14. // the given |prefix| and |suffix| indices. Search only considers
  15. // the portion of the string starting from |search_start|.
  16. // Returns false if the prefix and/or suffix are not found, true otherwise.
  17. // |start| and |end| are output parameters populated with the indices
  18. // for the middle string.
  19. bool FindMiddleString(const base::StringPiece& content,
  20. size_t search_start,
  21. const base::StringPiece& prefix,
  22. const base::StringPiece& suffix,
  23. size_t* start,
  24. size_t* end);
  25. } // namespace internal
  26. // Values as logged in the histogram for memory pressure.
  27. constexpr int kMemPressureMin = 1; // As 0 is for underflow.
  28. constexpr int kMemPressureExclusiveMax = 10000;
  29. constexpr int kMemPressureHistogramBuckets = 100;
  30. // Enumeration representing success and various failure modes for parsing PSI
  31. // memory data. These values are persisted to logs. Entries should not be
  32. // renumbered and numeric values should never be reused.
  33. enum class ParsePSIMemStatus {
  34. kSuccess,
  35. kReadFileFailed,
  36. kUnexpectedDataFormat,
  37. kInvalidMetricFormat,
  38. kParsePSIValueFailed,
  39. // Magic constant used by the histogram macros.
  40. kMaxValue = kParsePSIValueFailed,
  41. };
  42. // PSIMemoryParser has logic to parse results from /proc/memory/pressure
  43. // in Linux, which can be used for memory pressure metrics.
  44. class PSIMemoryParser {
  45. public:
  46. explicit PSIMemoryParser(uint32_t period);
  47. ~PSIMemoryParser();
  48. // Parses PSI memory pressure from |content|, for the currently configured
  49. // metrics period (10, 60 or 300 seconds).
  50. // The some and full values are output to |metricSome| and |metricFull|,
  51. // respectively.
  52. // Returns status of the parse operation - ParsePSIMemStatus::kSuccess
  53. // or error code otherwise.
  54. ParsePSIMemStatus ParseMetrics(const base::StringPiece& content,
  55. int* metric_some,
  56. int* metric_full);
  57. // Raw buffer overload
  58. ParsePSIMemStatus ParseMetrics(const uint8_t* content,
  59. uint32_t len,
  60. int* metric_some,
  61. int* metric_full);
  62. uint32_t GetPeriod() const;
  63. void LogParseStatus(ParsePSIMemStatus stat);
  64. PSIMemoryParser(const PSIMemoryParser&) = delete;
  65. PSIMemoryParser& operator=(const PSIMemoryParser&) = delete;
  66. PSIMemoryParser() = delete;
  67. private:
  68. // Friend it so it can see private members for testing
  69. friend class PSIMemoryParserTest;
  70. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, CustomInterval);
  71. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, InvalidInterval);
  72. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, InternalsA);
  73. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, InternalsB);
  74. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, InternalsC);
  75. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, InternalsD);
  76. FRIEND_TEST_ALL_PREFIXES(PSIMemoryParserTest, InternalsE);
  77. ParsePSIMemStatus ParseMetricsInternal(const std::string& content,
  78. int* metric_some,
  79. int* metric_full);
  80. // Retrieves one metric value from |content|, for the currently configured
  81. // metrics category (10, 60 or 300 seconds).
  82. // Only considers the substring between |start| (inclusive) and |end|
  83. // (exclusive).
  84. // Returns the floating-point string representation converted into an integer
  85. // which has the value multiplied by 100 - (10.20 = 1020), for
  86. // histogram usage.
  87. int GetMetricValue(const base::StringPiece& content,
  88. size_t start,
  89. size_t end);
  90. std::string metric_prefix_;
  91. uint32_t period_;
  92. };
  93. } // namespace metrics
  94. #endif // COMPONENTS_METRICS_PSI_MEMORY_PARSER_H_