OLD | NEW |
| (Empty) |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/common/discardable_shared_memory_heap.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <algorithm> | |
9 #include <cmath> | |
10 #include <cstdlib> | |
11 #include <utility> | |
12 | |
13 #include "base/bind.h" | |
14 #include "base/callback_helpers.h" | |
15 #include "base/memory/discardable_shared_memory.h" | |
16 #include "base/memory/scoped_vector.h" | |
17 #include "base/process/process_metrics.h" | |
18 #include "testing/gtest/include/gtest/gtest.h" | |
19 #include "testing/perf/perf_test.h" | |
20 | |
21 namespace content { | |
22 namespace { | |
23 | |
24 const int kTimeLimitMs = 2000; | |
25 const int kTimeCheckInterval = 8192; | |
26 | |
27 void NullTask() { | |
28 } | |
29 | |
30 TEST(DiscardableSharedMemoryHeapTest, SearchFreeLists) { | |
31 size_t block_size = base::GetPageSize(); | |
32 DiscardableSharedMemoryHeap heap(block_size); | |
33 | |
34 const size_t kBlocks = 4096; | |
35 const size_t kSegments = 16; | |
36 size_t segment_size = block_size * kBlocks; | |
37 int next_discardable_shared_memory_id = 0; | |
38 | |
39 for (size_t i = 0; i < kSegments; ++i) { | |
40 std::unique_ptr<base::DiscardableSharedMemory> memory( | |
41 new base::DiscardableSharedMemory); | |
42 ASSERT_TRUE(memory->CreateAndMap(segment_size)); | |
43 heap.MergeIntoFreeLists(heap.Grow(std::move(memory), segment_size, | |
44 next_discardable_shared_memory_id++, | |
45 base::Bind(NullTask))); | |
46 } | |
47 | |
48 unsigned kSeed = 1; | |
49 // Use kSeed as seed for random number generator. | |
50 srand(kSeed); | |
51 | |
52 // Pre-compute random values. | |
53 int random_span[kTimeCheckInterval]; | |
54 size_t random_blocks[kTimeCheckInterval]; | |
55 for (int i = 0; i < kTimeCheckInterval; ++i) { | |
56 random_span[i] = std::rand(); | |
57 // Exponentially distributed block size. | |
58 const double kLambda = 2.0; | |
59 double v = static_cast<double>(std::rand()) / RAND_MAX; | |
60 random_blocks[i] = 1 + log(1.0 - v) / -kLambda * kBlocks; | |
61 } | |
62 | |
63 ScopedVector<base::ScopedClosureRunner> spans; | |
64 | |
65 base::TimeTicks start = base::TimeTicks::Now(); | |
66 base::TimeTicks end = start + base::TimeDelta::FromMilliseconds(kTimeLimitMs); | |
67 base::TimeDelta accumulator; | |
68 int count = 0; | |
69 while (start < end) { | |
70 for (int i = 0; i < kTimeCheckInterval; ++i) { | |
71 // Search for a perfect fit if greater than kBlocks. | |
72 size_t slack = | |
73 random_blocks[i] < kBlocks ? kBlocks - random_blocks[i] : 0; | |
74 std::unique_ptr<DiscardableSharedMemoryHeap::Span> span = | |
75 heap.SearchFreeLists(random_blocks[i], slack); | |
76 if (span) { | |
77 spans.push_back(new base::ScopedClosureRunner( | |
78 base::Bind(&DiscardableSharedMemoryHeap::MergeIntoFreeLists, | |
79 base::Unretained(&heap), base::Passed(&span)))); | |
80 } else if (!spans.empty()) { | |
81 // Merge a random span back into the free list. | |
82 std::swap(spans[random_span[i] % spans.size()], spans.back()); | |
83 spans.pop_back(); | |
84 } | |
85 | |
86 ++count; | |
87 } | |
88 | |
89 base::TimeTicks now = base::TimeTicks::Now(); | |
90 accumulator += now - start; | |
91 start = now; | |
92 } | |
93 | |
94 spans.clear(); | |
95 | |
96 perf_test::PrintResult("search_free_list", "", "", | |
97 count / accumulator.InSecondsF(), "runs/s", true); | |
98 } | |
99 | |
100 } // namespace | |
101 } // namespace content | |
OLD | NEW |