[M120 Migration][VD] Enable direct rendering for TVPlus
[platform/framework/web/chromium-efl.git] / components / metrics / psi_memory_parser.h
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 <cstdint>
11
12 #include "base/gtest_prod_util.h"
13 #include "base/strings/string_piece.h"
14
15 namespace metrics {
16
17 // Items in internal are - as the name implies - NOT for outside consumption.
18 // Defined here to allow access to unit test.
19 namespace internal {
20
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,
28                       size_t search_start,
29                       const base::StringPiece& prefix,
30                       const base::StringPiece& suffix,
31                       size_t* start,
32                       size_t* end);
33
34 }  // namespace internal
35
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;
40
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 {
45   kSuccess,
46   kReadFileFailed,
47   kUnexpectedDataFormat,
48   kInvalidMetricFormat,
49   kParsePSIValueFailed,
50   // Magic constant used by the histogram macros.
51   kMaxValue = kParsePSIValueFailed,
52 };
53
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 {
57  public:
58   explicit PSIMemoryParser(uint32_t period);
59   ~PSIMemoryParser();
60
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|,
64   // respectively.
65   // Returns status of the parse operation - ParsePSIMemStatus::kSuccess
66   // or error code otherwise.
67   ParsePSIMemStatus ParseMetrics(const base::StringPiece& content,
68                                  int* metric_some,
69                                  int* metric_full);
70
71   // Raw buffer overload
72   ParsePSIMemStatus ParseMetrics(const uint8_t* content,
73                                  uint32_t len,
74                                  int* metric_some,
75                                  int* metric_full);
76
77   uint32_t GetPeriod() const;
78   void LogParseStatus(ParsePSIMemStatus stat);
79
80   PSIMemoryParser(const PSIMemoryParser&) = delete;
81   PSIMemoryParser& operator=(const PSIMemoryParser&) = delete;
82   PSIMemoryParser() = delete;
83
84  private:
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);
94
95   ParsePSIMemStatus ParseMetricsInternal(const std::string& content,
96                                          int* metric_some,
97                                          int* metric_full);
98
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|
102   // (exclusive).
103   // Returns the floating-point string representation converted into an integer
104   // which has the value multiplied by 100 - (10.20 = 1020), for
105   // histogram usage.
106   int GetMetricValue(const base::StringPiece& content,
107                      size_t start,
108                      size_t end);
109
110   std::string metric_prefix_;
111   uint32_t period_;
112 };
113
114 }  // namespace metrics
115
116 #endif  // COMPONENTS_METRICS_PSI_MEMORY_PARSER_H_