| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/discardable_memory/common/discardable_shared_memory_heap.h" | 5 #include "components/discardable_memory/common/discardable_shared_memory_heap.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <cstdlib> | 10 #include <cstdlib> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback_helpers.h" | 14 #include "base/callback_helpers.h" |
| 15 #include "base/memory/discardable_shared_memory.h" | 15 #include "base/memory/discardable_shared_memory.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/process/process_metrics.h" | 17 #include "base/process/process_metrics.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 #include "testing/perf/perf_test.h" | 19 #include "testing/perf/perf_test.h" |
| 20 | 20 |
| 21 namespace discardable_memory { | 21 namespace discardable_memory { |
| 22 namespace { | 22 namespace { |
| 23 | 23 |
| 24 const int kTimeLimitMs = 2000; | 24 const int kTimeLimitMs = 2000; |
| 25 const int kTimeCheckInterval = 8192; | 25 const int kTimeCheckInterval = 8192; |
| 26 | 26 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 52 int random_span[kTimeCheckInterval]; | 52 int random_span[kTimeCheckInterval]; |
| 53 size_t random_blocks[kTimeCheckInterval]; | 53 size_t random_blocks[kTimeCheckInterval]; |
| 54 for (int i = 0; i < kTimeCheckInterval; ++i) { | 54 for (int i = 0; i < kTimeCheckInterval; ++i) { |
| 55 random_span[i] = std::rand(); | 55 random_span[i] = std::rand(); |
| 56 // Exponentially distributed block size. | 56 // Exponentially distributed block size. |
| 57 const double kLambda = 2.0; | 57 const double kLambda = 2.0; |
| 58 double v = static_cast<double>(std::rand()) / RAND_MAX; | 58 double v = static_cast<double>(std::rand()) / RAND_MAX; |
| 59 random_blocks[i] = 1 + log(1.0 - v) / -kLambda * kBlocks; | 59 random_blocks[i] = 1 + log(1.0 - v) / -kLambda * kBlocks; |
| 60 } | 60 } |
| 61 | 61 |
| 62 std::vector<std::unique_ptr<base::ScopedClosureRunner>> spans; | 62 ScopedVector<base::ScopedClosureRunner> spans; |
| 63 | 63 |
| 64 base::TimeTicks start = base::TimeTicks::Now(); | 64 base::TimeTicks start = base::TimeTicks::Now(); |
| 65 base::TimeTicks end = start + base::TimeDelta::FromMilliseconds(kTimeLimitMs); | 65 base::TimeTicks end = start + base::TimeDelta::FromMilliseconds(kTimeLimitMs); |
| 66 base::TimeDelta accumulator; | 66 base::TimeDelta accumulator; |
| 67 int count = 0; | 67 int count = 0; |
| 68 while (start < end) { | 68 while (start < end) { |
| 69 for (int i = 0; i < kTimeCheckInterval; ++i) { | 69 for (int i = 0; i < kTimeCheckInterval; ++i) { |
| 70 // Search for a perfect fit if greater than kBlocks. | 70 // Search for a perfect fit if greater than kBlocks. |
| 71 size_t slack = | 71 size_t slack = |
| 72 random_blocks[i] < kBlocks ? kBlocks - random_blocks[i] : 0; | 72 random_blocks[i] < kBlocks ? kBlocks - random_blocks[i] : 0; |
| 73 std::unique_ptr<DiscardableSharedMemoryHeap::Span> span = | 73 std::unique_ptr<DiscardableSharedMemoryHeap::Span> span = |
| 74 heap.SearchFreeLists(random_blocks[i], slack); | 74 heap.SearchFreeLists(random_blocks[i], slack); |
| 75 if (span) { | 75 if (span) { |
| 76 spans.push_back(base::MakeUnique<base::ScopedClosureRunner>( | 76 spans.push_back(new base::ScopedClosureRunner( |
| 77 base::Bind(&DiscardableSharedMemoryHeap::MergeIntoFreeLists, | 77 base::Bind(&DiscardableSharedMemoryHeap::MergeIntoFreeLists, |
| 78 base::Unretained(&heap), base::Passed(&span)))); | 78 base::Unretained(&heap), base::Passed(&span)))); |
| 79 } else if (!spans.empty()) { | 79 } else if (!spans.empty()) { |
| 80 // Merge a random span back into the free list. | 80 // Merge a random span back into the free list. |
| 81 std::swap(spans[random_span[i] % spans.size()], spans.back()); | 81 std::swap(spans[random_span[i] % spans.size()], spans.back()); |
| 82 spans.pop_back(); | 82 spans.pop_back(); |
| 83 } | 83 } |
| 84 | 84 |
| 85 ++count; | 85 ++count; |
| 86 } | 86 } |
| 87 | 87 |
| 88 base::TimeTicks now = base::TimeTicks::Now(); | 88 base::TimeTicks now = base::TimeTicks::Now(); |
| 89 accumulator += now - start; | 89 accumulator += now - start; |
| 90 start = now; | 90 start = now; |
| 91 } | 91 } |
| 92 | 92 |
| 93 spans.clear(); | 93 spans.clear(); |
| 94 | 94 |
| 95 perf_test::PrintResult("search_free_list", "", "", | 95 perf_test::PrintResult("search_free_list", "", "", |
| 96 count / accumulator.InSecondsF(), "runs/s", true); | 96 count / accumulator.InSecondsF(), "runs/s", true); |
| 97 } | 97 } |
| 98 | 98 |
| 99 } // namespace | 99 } // namespace |
| 100 } // namespace discardable_memory | 100 } // namespace discardable_memory |
| OLD | NEW |