2014-10-21 12:38:46 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
#include "src/compiler/zone-stats.h"
|
2014-10-21 12:38:46 +00:00
|
|
|
#include "src/base/utils/random-number-generator.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
2022-04-06 11:56:49 +00:00
|
|
|
class ZoneStatsTest : public TestWithPlatform {
|
2014-10-21 12:38:46 +00:00
|
|
|
public:
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStatsTest() : zone_stats_(&allocator_) {}
|
2014-10-21 12:38:46 +00:00
|
|
|
|
|
|
|
protected:
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats* zone_stats() { return &zone_stats_; }
|
2014-10-21 12:38:46 +00:00
|
|
|
|
|
|
|
void ExpectForPool(size_t current, size_t max, size_t total) {
|
2016-10-10 19:00:31 +00:00
|
|
|
ASSERT_EQ(current, zone_stats()->GetCurrentAllocatedBytes());
|
|
|
|
ASSERT_EQ(max, zone_stats()->GetMaxAllocatedBytes());
|
|
|
|
ASSERT_EQ(total, zone_stats()->GetTotalAllocatedBytes());
|
2014-10-21 12:38:46 +00:00
|
|
|
}
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
void Expect(ZoneStats::StatsScope* stats, size_t current, size_t max,
|
2014-10-23 09:14:35 +00:00
|
|
|
size_t total) {
|
2014-10-21 12:38:46 +00:00
|
|
|
ASSERT_EQ(current, stats->GetCurrentAllocatedBytes());
|
|
|
|
ASSERT_EQ(max, stats->GetMaxAllocatedBytes());
|
2014-10-23 09:14:35 +00:00
|
|
|
ASSERT_EQ(total, stats->GetTotalAllocatedBytes());
|
2014-10-21 12:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t Allocate(Zone* zone) {
|
|
|
|
size_t bytes = rng.NextInt(25) + 7;
|
2015-02-12 12:46:58 +00:00
|
|
|
size_t size_before = zone->allocation_size();
|
2020-07-09 10:43:44 +00:00
|
|
|
zone->Allocate<void>(bytes);
|
2015-02-12 12:46:58 +00:00
|
|
|
return zone->allocation_size() - size_before;
|
2014-10-21 12:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator_;
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats zone_stats_;
|
2014-10-21 12:38:46 +00:00
|
|
|
base::RandomNumberGenerator rng;
|
|
|
|
};
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
TEST_F(ZoneStatsTest, Empty) {
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(0, 0, 0);
|
|
|
|
{
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats::StatsScope stats(zone_stats());
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&stats, 0, 0, 0);
|
2014-10-21 12:38:46 +00:00
|
|
|
}
|
|
|
|
ExpectForPool(0, 0, 0);
|
|
|
|
{
|
2016-10-17 12:12:30 +00:00
|
|
|
ZoneStats::Scope scope(zone_stats(), ZONE_NAME);
|
2014-10-21 12:38:46 +00:00
|
|
|
scope.zone();
|
|
|
|
}
|
|
|
|
ExpectForPool(0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
TEST_F(ZoneStatsTest, MultipleZonesWithDeletion) {
|
2014-10-21 12:38:46 +00:00
|
|
|
static const size_t kArraySize = 10;
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats::Scope* scopes[kArraySize];
|
2014-10-21 12:38:46 +00:00
|
|
|
|
|
|
|
// Initialize.
|
|
|
|
size_t before_stats = 0;
|
|
|
|
for (size_t i = 0; i < kArraySize; ++i) {
|
2016-10-17 12:12:30 +00:00
|
|
|
scopes[i] = new ZoneStats::Scope(zone_stats(), ZONE_NAME);
|
2014-10-21 12:38:46 +00:00
|
|
|
before_stats += Allocate(scopes[i]->zone()); // Add some stuff.
|
|
|
|
}
|
|
|
|
|
|
|
|
ExpectForPool(before_stats, before_stats, before_stats);
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats::StatsScope stats(zone_stats());
|
2014-10-21 12:38:46 +00:00
|
|
|
|
|
|
|
size_t before_deletion = 0;
|
|
|
|
for (size_t i = 0; i < kArraySize; ++i) {
|
|
|
|
before_deletion += Allocate(scopes[i]->zone()); // Add some stuff.
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&stats, before_deletion, before_deletion, before_deletion);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(before_stats + before_deletion, before_stats + before_deletion,
|
|
|
|
before_stats + before_deletion);
|
|
|
|
|
|
|
|
// Delete the scopes and create new ones.
|
|
|
|
for (size_t i = 0; i < kArraySize; ++i) {
|
|
|
|
delete scopes[i];
|
2016-10-17 12:12:30 +00:00
|
|
|
scopes[i] = new ZoneStats::Scope(zone_stats(), ZONE_NAME);
|
2014-10-21 12:38:46 +00:00
|
|
|
}
|
|
|
|
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&stats, 0, before_deletion, before_deletion);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(0, before_stats + before_deletion,
|
|
|
|
before_stats + before_deletion);
|
|
|
|
|
|
|
|
size_t after_deletion = 0;
|
|
|
|
for (size_t i = 0; i < kArraySize; ++i) {
|
|
|
|
after_deletion += Allocate(scopes[i]->zone()); // Add some stuff.
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&stats, after_deletion, std::max(after_deletion, before_deletion),
|
|
|
|
before_deletion + after_deletion);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(after_deletion,
|
|
|
|
std::max(after_deletion, before_stats + before_deletion),
|
|
|
|
before_stats + before_deletion + after_deletion);
|
|
|
|
|
|
|
|
// Cleanup.
|
|
|
|
for (size_t i = 0; i < kArraySize; ++i) {
|
|
|
|
delete scopes[i];
|
|
|
|
}
|
|
|
|
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&stats, 0, std::max(after_deletion, before_deletion),
|
|
|
|
before_deletion + after_deletion);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(0, std::max(after_deletion, before_stats + before_deletion),
|
|
|
|
before_stats + before_deletion + after_deletion);
|
|
|
|
}
|
|
|
|
|
2016-10-10 19:00:31 +00:00
|
|
|
TEST_F(ZoneStatsTest, SimpleAllocationLoop) {
|
2014-10-21 12:38:46 +00:00
|
|
|
int runs = 20;
|
|
|
|
size_t total_allocated = 0;
|
|
|
|
size_t max_loop_allocation = 0;
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats::StatsScope outer_stats(zone_stats());
|
2014-10-21 12:38:46 +00:00
|
|
|
{
|
2016-10-17 12:12:30 +00:00
|
|
|
ZoneStats::Scope outer_scope(zone_stats(), ZONE_NAME);
|
2014-10-21 12:38:46 +00:00
|
|
|
size_t outer_allocated = 0;
|
|
|
|
for (int i = 0; i < runs; ++i) {
|
|
|
|
{
|
|
|
|
size_t bytes = Allocate(outer_scope.zone());
|
|
|
|
outer_allocated += bytes;
|
|
|
|
total_allocated += bytes;
|
|
|
|
}
|
2016-10-10 19:00:31 +00:00
|
|
|
ZoneStats::StatsScope inner_stats(zone_stats());
|
2014-10-21 12:38:46 +00:00
|
|
|
size_t allocated = 0;
|
|
|
|
{
|
2016-10-17 12:12:30 +00:00
|
|
|
ZoneStats::Scope inner_scope(zone_stats(), ZONE_NAME);
|
2014-10-21 12:38:46 +00:00
|
|
|
for (int j = 0; j < 20; ++j) {
|
|
|
|
size_t bytes = Allocate(inner_scope.zone());
|
|
|
|
allocated += bytes;
|
|
|
|
total_allocated += bytes;
|
|
|
|
max_loop_allocation =
|
|
|
|
std::max(max_loop_allocation, outer_allocated + allocated);
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&inner_stats, allocated, allocated, allocated);
|
|
|
|
Expect(&outer_stats, outer_allocated + allocated, max_loop_allocation,
|
|
|
|
total_allocated);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(outer_allocated + allocated, max_loop_allocation,
|
|
|
|
total_allocated);
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&inner_stats, 0, allocated, allocated);
|
|
|
|
Expect(&outer_stats, outer_allocated, max_loop_allocation,
|
|
|
|
total_allocated);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(outer_allocated, max_loop_allocation, total_allocated);
|
|
|
|
}
|
|
|
|
}
|
2014-10-23 09:14:35 +00:00
|
|
|
Expect(&outer_stats, 0, max_loop_allocation, total_allocated);
|
2014-10-21 12:38:46 +00:00
|
|
|
ExpectForPool(0, max_loop_allocation, total_allocated);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|