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