blob: 829c503fe5f77387aada134cca9c8a837ec33f6c [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.
Brian Silverman20350ac2021-11-17 18:19:55 -08004//
Austin Schuh745610d2015-09-06 18:19:50 -07005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
Brian Silverman20350ac2021-11-17 18:19:55 -08008//
Austin Schuh745610d2015-09-06 18:19:50 -07009// * 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.
Brian Silverman20350ac2021-11-17 18:19:55 -080018//
Austin Schuh745610d2015-09-06 18:19:50 -070019// 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.
Brian Silverman20350ac2021-11-17 18:19:55 -080096 VLOG(0, "Original usage: %zu\n", original);
97 VLOG(0, "Post allocation: %zu\n", post_allocation);
98 VLOG(0, "Post idle: %zu\n", post_idle);
99}
100
101static void TestTemporarilyIdleUsage() {
102 const size_t original = MallocExtension::instance()->GetThreadCacheSize();
103
104 TestAllocation();
105 const size_t post_allocation = MallocExtension::instance()->GetThreadCacheSize();
106 CHECK_GT(post_allocation, original);
107
108 MallocExtension::instance()->MarkThreadIdle();
109 const size_t post_idle = MallocExtension::instance()->GetThreadCacheSize();
110 CHECK_EQ(post_idle, 0);
111
112 // Log after testing because logging can allocate heap memory.
113 VLOG(0, "Original usage: %zu\n", original);
114 VLOG(0, "Post allocation: %zu\n", post_allocation);
115 VLOG(0, "Post idle: %zu\n", post_idle);
Austin Schuh745610d2015-09-06 18:19:50 -0700116}
117
118int main(int argc, char** argv) {
119 RunThread(&TestIdleUsage);
120 RunThread(&TestAllocation);
121 RunThread(&MultipleIdleCalls);
122 RunThread(&MultipleIdleNonIdlePhases);
Brian Silverman20350ac2021-11-17 18:19:55 -0800123 RunThread(&TestTemporarilyIdleUsage);
Austin Schuh745610d2015-09-06 18:19:50 -0700124
125 printf("PASS\n");
126 return 0;
127}