markidle_unittest.cc 4.4 KB

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