-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CrOS: userspace swap: Small helper to find eligible VMAs
This CL contains two very small helpers, the first which takes a VMA and determines based on its properties if it would be eligible for swapping. The second will return a vector of all VMAs which are swap eligible for a given pid. This will be used in the userspace swap mechanism which is coming in a later CL, this is just split out to make reviewing everything easier. BUG=chromium:1067833 Change-Id: Ic60d65105b20dfa54cf221c92f3406016e68fc43 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2298161 Reviewed-by: François Doray <fdoray@chromium.org> Reviewed-by: Robert Sesek <rsesek@chromium.org> Commit-Queue: Brian Geffon <bgeffon@chromium.org> Cr-Commit-Position: refs/heads/master@{#789046}
- Loading branch information
Showing
5 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# These deps should be only the required deps for //chromeos/memory | ||
include_rules = [ | ||
"+third_party/zlib/google", | ||
"+services/resource_coordinator", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
chromeos/memory/userspace_swap/userspace_swap_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
// Copyright (c) 2020 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "chromeos/memory/userspace_swap/userspace_swap.h" | ||
|
||
#include <string> | ||
|
||
#include "chromeos/memory/userspace_swap/region.h" | ||
#include "services/resource_coordinator/public/cpp/memory_instrumentation/os_metrics.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace chromeos { | ||
namespace memory { | ||
namespace userspace_swap { | ||
|
||
namespace { | ||
using memory_instrumentation::mojom::VmRegion; | ||
using memory_instrumentation::mojom::VmRegionPtr; | ||
|
||
// DefaultEligibleRegion is a region which is eligible by default which can be | ||
// made ineligible by changing one or more of the properties. | ||
VmRegion DefaultEligibleRegion() { | ||
VmRegion r; | ||
r.start_address = 0xF00F00BA4; | ||
r.size_in_bytes = | ||
UserspaceSwapConfig::Get().vma_region_minimum_size_bytes + 1; | ||
r.protection_flags = | ||
VmRegion::kProtectionFlagsRead | VmRegion::kProtectionFlagsWrite; | ||
|
||
// These aren't necessary but we list them to be explicit about the | ||
// requirements. | ||
r.byte_locked = 0; | ||
r.mapped_file = std::string(); | ||
|
||
return r; | ||
} | ||
|
||
} // namespace | ||
|
||
TEST(EligibleVMA, DefaultIsEligible) { | ||
ASSERT_TRUE(IsVMASwapEligible(DefaultEligibleRegion().Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, SharedIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.protection_flags |= VmRegion::kProtectionFlagsMayshare; | ||
|
||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, ProtNoneIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.protection_flags = 0; | ||
|
||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, WrOnlyIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.protection_flags = VmRegion::kProtectionFlagsWrite; | ||
|
||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, RdOnlyIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.protection_flags = VmRegion::kProtectionFlagsRead; | ||
|
||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, ExecIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.protection_flags |= VmRegion::kProtectionFlagsExec; | ||
|
||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, FileBackedIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.mapped_file = "/some/file/foo.so"; | ||
|
||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, AnyLockedRegionIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.byte_locked = 20 << 10; // Any non-zero locked will do. | ||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, RegionTooSmallIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.size_in_bytes = | ||
UserspaceSwapConfig::Get().vma_region_minimum_size_bytes - 1; | ||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(EligibleVMA, RegionTooLargeIsNotEligible) { | ||
auto r = DefaultEligibleRegion(); | ||
r.size_in_bytes = | ||
UserspaceSwapConfig::Get().vma_region_maximum_size_bytes + 1; | ||
ASSERT_FALSE(IsVMASwapEligible(r.Clone())); | ||
} | ||
|
||
TEST(GetAllSwapEligibleVMAs, SimpleVerification) { | ||
std::vector<Region> regions; | ||
ASSERT_TRUE(GetAllSwapEligibleVMAs(getpid(), ®ions)); | ||
ASSERT_GT(regions.size(), 0u); | ||
} | ||
|
||
} // namespace userspace_swap | ||
} // namespace memory | ||
} // namespace chromeos |