Skip to content

Commit 727d763

Browse files
Thiabaud Engelbrechtcopybara-github
authored andcommitted
[base] Use ByteCount in SmapsRollup
Bug: 429140103 Change-Id: I33859fb23a3378ce3f81b9c4dc6d6358bd678e2d Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6764412 Reviewed-by: Francois Pierre Doray <[email protected]> Commit-Queue: Thiabaud Engelbrecht <[email protected]> Cr-Commit-Position: refs/heads/main@{#1500159} NOKEYCHECK=True GitOrigin-RevId: 4e8c8ee1af3fd270ef21b2a21ecfa9b5e4f8616b
1 parent 36ea335 commit 727d763

File tree

5 files changed

+33
-35
lines changed

5 files changed

+33
-35
lines changed

android/self_compaction_manager.cc

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ bool IsMadvisePageoutSupported() {
8383
// be more than enough.
8484
constexpr base::TimeDelta kCompactionTimeout = base::Seconds(10);
8585

86-
uint64_t BytesToMiB(uint64_t v) {
87-
return v / 1024 / 1024;
88-
}
89-
9086
uint64_t MiBToBytes(uint64_t v) {
9187
return v * 1024 * 1024;
9288
}
@@ -515,11 +511,11 @@ void SelfCompactionManager::CompactionMetric::MaybeRecordCompactionMetrics() {
515511
}
516512

517513
void SelfCompactionManager::CompactionMetric::RecordCompactionMetric(
518-
size_t value_bytes,
514+
ByteCount value_bytes,
519515
std::string_view metric_name,
520516
std::string_view suffix) {
521517
UmaHistogramMemoryMB(GetMetricName(metric_name, suffix),
522-
static_cast<int>(BytesToMiB(value_bytes)));
518+
static_cast<int>(value_bytes.InMiB()));
523519
}
524520

525521
void SelfCompactionManager::CompactionMetric::RecordCompactionMetrics(
@@ -533,12 +529,13 @@ void SelfCompactionManager::CompactionMetric::RecordCompactionMetrics(
533529
}
534530

535531
void SelfCompactionManager::CompactionMetric::RecordCompactionDiffMetric(
536-
size_t before_value_bytes,
537-
size_t after_value_bytes,
532+
ByteCount before_value_bytes,
533+
ByteCount after_value_bytes,
538534
std::string_view name,
539535
std::string_view suffix) {
540-
size_t diff_non_negative = std::max(before_value_bytes, after_value_bytes) -
541-
std::min(before_value_bytes, after_value_bytes);
536+
ByteCount diff_non_negative =
537+
std::max(before_value_bytes, after_value_bytes) -
538+
std::min(before_value_bytes, after_value_bytes);
542539
const std::string full_suffix = StrCat(
543540
{"Diff.", suffix, ".",
544541
before_value_bytes < after_value_bytes ? "Increase" : "Decrease"});

android/self_compaction_manager.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// TODO(thiabaud): remove this include once we have separated the locks
99
// between these two classes.
1010
#include "base/android/pre_freeze_background_memory_trimmer.h"
11+
#include "base/byte_count.h"
1112
#include "base/debug/proc_maps_linux.h"
1213
#include "base/no_destructor.h"
1314
#include "base/profiler/sample_metadata.h"
@@ -68,14 +69,14 @@ class BASE_EXPORT SelfCompactionManager {
6869
std::string_view suffix) const;
6970
void RecordCompactionMetrics(const debug::SmapsRollup& value,
7071
std::string_view suffix);
71-
void RecordCompactionMetric(size_t value_bytes,
72+
void RecordCompactionMetric(ByteCount value_bytes,
7273
std::string_view metric_name,
7374
std::string_view suffix);
7475
void RecordCompactionDiffMetrics(const debug::SmapsRollup& before,
7576
const debug::SmapsRollup& after,
7677
std::string_view suffix);
77-
void RecordCompactionDiffMetric(size_t before_value_bytes,
78-
size_t after_value_bytes,
78+
void RecordCompactionDiffMetric(ByteCount before_value_bytes,
79+
ByteCount after_value_bytes,
7980
std::string_view name,
8081
std::string_view suffix);
8182

debug/proc_maps_linux.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ std::optional<SmapsRollup> ParseSmapsRollup(const std::string& buffer) {
188188
std::vector<std::string> lines =
189189
SplitString(buffer, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
190190

191-
std::unordered_map<std::string, size_t> tmp;
191+
std::unordered_map<std::string, ByteCount> tmp;
192192
for (const auto& line : lines) {
193193
// This should be more than enough space for any output we get (but we also
194194
// verify the size below).
@@ -200,7 +200,7 @@ std::optional<SmapsRollup> ParseSmapsRollup(const std::string& buffer) {
200200
// here. |resize| does not count the length of the nul-byte, and we want
201201
// to trim off the trailing colon at the end, so we use |strlen - 1| here.
202202
key.resize(strlen(key.c_str()) - 1);
203-
tmp[key] = val * 1024;
203+
tmp[key] = KiB(val);
204204
}
205205
}
206206

debug/proc_maps_linux.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <vector>
1313

1414
#include "base/base_export.h"
15+
#include "base/byte_count.h"
1516

1617
namespace base::debug {
1718

@@ -101,16 +102,15 @@ BASE_EXPORT bool ReadProcMaps(std::string* proc_maps);
101102
BASE_EXPORT bool ParseProcMaps(const std::string& input,
102103
std::vector<MappedMemoryRegion>* regions);
103104

104-
// All values are in bytes.
105105
struct SmapsRollup {
106-
size_t rss = 0;
107-
size_t pss = 0;
108-
size_t pss_anon = 0;
109-
size_t pss_file = 0;
110-
size_t pss_shmem = 0;
111-
size_t private_dirty = 0;
112-
size_t swap = 0;
113-
size_t swap_pss = 0;
106+
ByteCount rss = ByteCount(0);
107+
ByteCount pss = ByteCount(0);
108+
ByteCount pss_anon = ByteCount(0);
109+
ByteCount pss_file = ByteCount(0);
110+
ByteCount pss_shmem = ByteCount(0);
111+
ByteCount private_dirty = ByteCount(0);
112+
ByteCount swap = ByteCount(0);
113+
ByteCount swap_pss = ByteCount(0);
114114
};
115115

116116
// Attempts to read /proc/self/smaps_rollup. Returns nullopt on error.

debug/proc_maps_linux_unittest.cc

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -372,9 +372,9 @@ TEST(SmapsRollupTest, ReadAndParse) {
372372

373373
SmapsRollup smaps_rollup = result.value();
374374

375-
EXPECT_GT(smaps_rollup.rss, 0u);
376-
EXPECT_GT(smaps_rollup.pss, 0u);
377-
EXPECT_GT(smaps_rollup.private_dirty, 0u);
375+
EXPECT_GT(smaps_rollup.rss.InKiB(), 0u);
376+
EXPECT_GT(smaps_rollup.pss.InKiB(), 0u);
377+
EXPECT_GT(smaps_rollup.private_dirty.InKiB(), 0u);
378378
}
379379

380380
TEST(SmapsRollupTest, Valid) {
@@ -411,14 +411,14 @@ Locked: 0 kB
411411

412412
SmapsRollup smaps_rollup = result.value();
413413

414-
EXPECT_EQ(smaps_rollup.rss, 1024 * 1908u);
415-
EXPECT_EQ(smaps_rollup.pss, 1024 * 573u);
416-
EXPECT_EQ(smaps_rollup.private_dirty, 1024 * 104u);
417-
EXPECT_EQ(smaps_rollup.pss_anon, 1024 * 100u);
418-
EXPECT_EQ(smaps_rollup.pss_file, 1024 * 469u);
419-
EXPECT_EQ(smaps_rollup.pss_shmem, 1024 * 12u);
420-
EXPECT_EQ(smaps_rollup.swap, 1024 * 10u);
421-
EXPECT_EQ(smaps_rollup.swap_pss, 1024 * 20u);
414+
EXPECT_EQ(smaps_rollup.rss.InKiB(), 1908u);
415+
EXPECT_EQ(smaps_rollup.pss.InKiB(), 573u);
416+
EXPECT_EQ(smaps_rollup.private_dirty.InKiB(), 104u);
417+
EXPECT_EQ(smaps_rollup.pss_anon.InKiB(), 100u);
418+
EXPECT_EQ(smaps_rollup.pss_file.InKiB(), 469u);
419+
EXPECT_EQ(smaps_rollup.pss_shmem.InKiB(), 12u);
420+
EXPECT_EQ(smaps_rollup.swap.InKiB(), 10u);
421+
EXPECT_EQ(smaps_rollup.swap_pss.InKiB(), 20u);
422422
}
423423

424424
} // namespace base::debug

0 commit comments

Comments
 (0)