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