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