File: psi_memory_parser_linux.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (185 lines) | stat: -rw-r--r-- 5,924 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/metrics/psi_memory_parser.h"

#include <stddef.h>

#include <cinttypes>
#include <map>
#include <memory>
#include <string>

#include "base/metrics/histogram.h"
#include "base/metrics/histogram_functions.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "components/metrics/metrics_log_store.h"

namespace metrics {

namespace {

// Periods supported by standard Linux PSI metricvs.
constexpr uint32_t kMinCollectionInterval = 10;
constexpr uint32_t kMidCollectionInterval = 60;
constexpr uint32_t kMaxCollectionInterval = 300;

constexpr uint32_t kDefaultCollectionInterval = kMinCollectionInterval;

// Name of the histogram that represents the success and various failure modes
// for parsing PSI memory data.
const char kParsePSIMemoryHistogramName[] = "ChromeOS.CWP.ParsePSIMemory";

constexpr base::StringPiece kContentPrefixSome = "some";
constexpr base::StringPiece kContentPrefixFull = "full";
constexpr base::StringPiece kContentTerminator = " total=";
constexpr base::StringPiece kMetricTerminator = " ";

const char kMetricPrefixFormat[] = "avg%d=";

}  // namespace

PSIMemoryParser::PSIMemoryParser(uint32_t period)
    : period_(kDefaultCollectionInterval) {
  if (period == kMinCollectionInterval || period == kMidCollectionInterval ||
      period == kMaxCollectionInterval) {
    period_ = period;
  } else {
    LOG(WARNING) << "Ignoring invalid interval [" << period << "]";
  }

  metric_prefix_ = base::StringPrintf(kMetricPrefixFormat, period_);
}

PSIMemoryParser::~PSIMemoryParser() = default;

uint32_t PSIMemoryParser::GetPeriod() const {
  return period_;
}

int PSIMemoryParser::GetMetricValue(const base::StringPiece& content,
                                    size_t start,
                                    size_t end) {
  size_t value_start;
  size_t value_end;
  if (!internal::FindMiddleString(content, start, metric_prefix_,
                                  kMetricTerminator, &value_start,
                                  &value_end)) {
    return -1;
  }
  if (value_end > end) {
    return -1;  // Out of bounds of the search area.
  }

  double n;
  const base::StringPiece metric_value_text =
      content.substr(value_start, value_end - value_start);
  if (!base::StringToDouble(metric_value_text, &n)) {
    return -1;  // Unable to convert string to number
  }

  // Want to multiply by 100, but to avoid integer truncation,
  // do best-effort rounding.
  const int preround = static_cast<int>(n * 1000);
  return (preround + 5) / 10;
}

void PSIMemoryParser::LogParseStatus(ParsePSIMemStatus stat) {
  constexpr int statCeiling =
      static_cast<int>(ParsePSIMemStatus::kMaxValue) + 1;
  base::UmaHistogramExactLinear(kParsePSIMemoryHistogramName,
                                static_cast<int>(stat), statCeiling);
}

ParsePSIMemStatus PSIMemoryParser::ParseMetrics(
    const base::StringPiece& content,
    int* metric_some,
    int* metric_full) {
  size_t str_some_start;
  size_t str_some_end;
  size_t str_full_start;
  size_t str_full_end;

  // Example of content:
  //  some avg10=0.00 avg60=0.00 avg300=0.00 total=417963
  //  full avg10=0.00 avg60=0.00 avg300=0.00 total=205933
  // we will pick one of the columns depending on the colleciton period set

  DCHECK_NE(metric_some, nullptr);
  DCHECK_NE(metric_full, nullptr);

  if (!internal::FindMiddleString(content, 0, kContentPrefixSome,
                                  kContentTerminator, &str_some_start,
                                  &str_some_end)) {
    return ParsePSIMemStatus::kUnexpectedDataFormat;
  }

  if (!internal::FindMiddleString(content,
                                  str_some_end + kContentTerminator.length(),
                                  kContentPrefixFull, kContentTerminator,
                                  &str_full_start, &str_full_end)) {
    return ParsePSIMemStatus::kUnexpectedDataFormat;
  }

  int compute_some = GetMetricValue(content, str_some_start, str_some_end);
  if (compute_some < 0) {
    return ParsePSIMemStatus::kInvalidMetricFormat;
  }

  int compute_full = GetMetricValue(content, str_full_start, str_full_end);
  if (compute_full < 0) {
    return ParsePSIMemStatus::kInvalidMetricFormat;
  }

  *metric_some = compute_some;
  *metric_full = compute_full;

  return ParsePSIMemStatus::kSuccess;
}

ParsePSIMemStatus PSIMemoryParser::ParseMetrics(const uint8_t* content,
                                                uint32_t len,
                                                int* metric_some,
                                                int* metric_full) {
  // The cast below is admittedly sneaky, but inherently safe because
  // we are translating a const pointer into another const pointer,
  // and the data sizes of the pointed object are the same.
  const char* string_content = reinterpret_cast<const char*>(content);

  return ParseMetrics(base::StringPiece(string_content, len), metric_some,
                      metric_full);
}

namespace internal {

bool FindMiddleString(const base::StringPiece& content,
                      size_t search_start,
                      const base::StringPiece& prefix,
                      const base::StringPiece& suffix,
                      size_t* start,
                      size_t* end) {
  DCHECK_NE(start, nullptr);
  DCHECK_NE(end, nullptr);

  size_t compute_start = content.find(prefix, search_start);
  if (compute_start == std::string::npos) {
    return false;
  }
  compute_start += prefix.length();

  size_t compute_end = content.find(suffix, compute_start);
  if (compute_end == std::string::npos) {
    return false;
  }

  *start = compute_start;
  *end = compute_end;

  return true;
}

}  // namespace internal

}  // namespace metrics