File: psi_memory_parser_linux.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (172 lines) | stat: -rw-r--r-- 5,333 bytes parent folder | download | duplicates (4)
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
// 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 <string_view>

#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 std::string_view kContentPrefixSome = "some";
constexpr std::string_view kContentPrefixFull = "full";
constexpr std::string_view kContentTerminator = " total=";
constexpr std::string_view 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(std::string_view 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 std::string_view 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(std::string_view 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;
}

namespace internal {

bool FindMiddleString(std::string_view content,
                      size_t search_start,
                      std::string_view prefix,
                      std::string_view 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