markidle_unittest.cc revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
29
30// ---
31// Author: Sanjay Ghemawat
32//
33// MallocExtension::MarkThreadIdle() testing
34#include <stdio.h>
35
36#include "config_for_unittests.h"
37#include "base/logging.h"
38#include <gperftools/malloc_extension.h>
39#include "tests/testutil.h"   // for RunThread()
40
41// Helper routine to do lots of allocations
42static 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);
48    }
49    for (int i = 0; i < kNum; i++) {
50      free(ptr[i]);
51    }
52  }
53}
54
55// Routine that does a bunch of MarkThreadIdle() calls in sequence
56// without any intervening allocations
57static void MultipleIdleCalls() {
58  for (int i = 0; i < 4; i++) {
59    MallocExtension::instance()->MarkThreadIdle();
60  }
61}
62
63// Routine that does a bunch of MarkThreadIdle() calls in sequence
64// with intervening allocations
65static void MultipleIdleNonIdlePhases() {
66  for (int i = 0; i < 4; i++) {
67    TestAllocation();
68    MallocExtension::instance()->MarkThreadIdle();
69  }
70}
71
72// Get current thread cache usage
73static size_t GetTotalThreadCacheSize() {
74  size_t result;
75  CHECK(MallocExtension::instance()->GetNumericProperty(
76            "tcmalloc.current_total_thread_cache_bytes",
77            &result));
78  return result;
79}
80
81// Check that MarkThreadIdle() actually reduces the amount
82// of per-thread memory.
83static void TestIdleUsage() {
84  const size_t original = GetTotalThreadCacheSize();
85
86  TestAllocation();
87  const size_t post_allocation = GetTotalThreadCacheSize();
88  CHECK_GT(post_allocation, original);
89
90  MallocExtension::instance()->MarkThreadIdle();
91  const size_t post_idle = GetTotalThreadCacheSize();
92  CHECK_LE(post_idle, original);
93
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);
98}
99
100int main(int argc, char** argv) {
101  RunThread(&TestIdleUsage);
102  RunThread(&TestAllocation);
103  RunThread(&MultipleIdleCalls);
104  RunThread(&MultipleIdleNonIdlePhases);
105
106  printf("PASS\n");
107  return 0;
108}
109