Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / third_party / tcmalloc / chromium / src / tests / markidle_unittest.cc
blob2f150abdbe9f8a20d286201ec1585b8dc1fdb649
1 // Copyright (c) 2003, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 // ---
31 // Author: Sanjay Ghemawat
33 // MallocExtension::MarkThreadIdle() testing
34 #include <stdio.h>
36 #include "config_for_unittests.h"
37 #include "base/logging.h"
38 #include <gperftools/malloc_extension.h>
39 #include "tests/testutil.h" // for RunThread()
41 // Helper routine to do lots of allocations
42 static void TestAllocation() {
43 static const int kNum = 100;
44 void* ptr[kNum];
45 for (int size = 8; size <= 65536; size*=2) {
46 for (int i = 0; i < kNum; i++) {
47 ptr[i] = malloc(size);
49 for (int i = 0; i < kNum; i++) {
50 free(ptr[i]);
55 // Routine that does a bunch of MarkThreadIdle() calls in sequence
56 // without any intervening allocations
57 static void MultipleIdleCalls() {
58 for (int i = 0; i < 4; i++) {
59 MallocExtension::instance()->MarkThreadIdle();
63 // Routine that does a bunch of MarkThreadIdle() calls in sequence
64 // with intervening allocations
65 static void MultipleIdleNonIdlePhases() {
66 for (int i = 0; i < 4; i++) {
67 TestAllocation();
68 MallocExtension::instance()->MarkThreadIdle();
72 // Get current thread cache usage
73 static size_t GetTotalThreadCacheSize() {
74 size_t result;
75 CHECK(MallocExtension::instance()->GetNumericProperty(
76 "tcmalloc.current_total_thread_cache_bytes",
77 &result));
78 return result;
81 // Check that MarkThreadIdle() actually reduces the amount
82 // of per-thread memory.
83 static void TestIdleUsage() {
84 const size_t original = GetTotalThreadCacheSize();
86 TestAllocation();
87 const size_t post_allocation = GetTotalThreadCacheSize();
88 CHECK_GT(post_allocation, original);
90 MallocExtension::instance()->MarkThreadIdle();
91 const size_t post_idle = GetTotalThreadCacheSize();
92 CHECK_LE(post_idle, original);
94 // Log after testing because logging can allocate heap memory.
95 VLOG(0, "Original usage: %"PRIuS"\n", original);
96 VLOG(0, "Post allocation: %"PRIuS"\n", post_allocation);
97 VLOG(0, "Post idle: %"PRIuS"\n", post_idle);
100 int main(int argc, char** argv) {
101 RunThread(&TestIdleUsage);
102 RunThread(&TestAllocation);
103 RunThread(&MultipleIdleCalls);
104 RunThread(&MultipleIdleNonIdlePhases);
106 printf("PASS\n");
107 return 0;