PageRenderTime 42ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/llcommon/llallocator.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 134 lines | 75 code | 19 blank | 40 comment | 3 complexity | a0de6d917014de4ca626315f3ec017f1 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llallocator.cpp
  3. * @brief Implementation of the LLAllocator class.
  4. *
  5. * $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2010, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #include "linden_common.h"
  27. #include "llallocator.h"
  28. #if LL_USE_TCMALLOC
  29. #include "google/heap-profiler.h"
  30. #include "google/commandlineflags_public.h"
  31. DECLARE_bool(heap_profile_use_stack_trace);
  32. //DECLARE_double(tcmalloc_release_rate);
  33. // static
  34. void LLAllocator::pushMemType(S32 type)
  35. {
  36. if(isProfiling())
  37. {
  38. PushMemType(type);
  39. }
  40. }
  41. // static
  42. S32 LLAllocator::popMemType()
  43. {
  44. if (isProfiling())
  45. {
  46. return PopMemType();
  47. }
  48. else
  49. {
  50. return -1;
  51. }
  52. }
  53. void LLAllocator::setProfilingEnabled(bool should_enable)
  54. {
  55. // NULL disables dumping to disk
  56. static char const * const PREFIX = NULL;
  57. if(should_enable)
  58. {
  59. HeapProfilerSetUseStackTrace(false);
  60. HeapProfilerStart(PREFIX);
  61. }
  62. else
  63. {
  64. HeapProfilerStop();
  65. }
  66. }
  67. // static
  68. bool LLAllocator::isProfiling()
  69. {
  70. return IsHeapProfilerRunning();
  71. }
  72. std::string LLAllocator::getRawProfile()
  73. {
  74. // *TODO - fix google-perftools to accept an buffer to avoid this
  75. // malloc-copy-free cycle.
  76. char * buffer = GetHeapProfile();
  77. std::string ret = buffer;
  78. free(buffer);
  79. return ret;
  80. }
  81. #else // LL_USE_TCMALLOC
  82. //
  83. // stub implementations for when tcmalloc is disabled
  84. //
  85. // static
  86. void LLAllocator::pushMemType(S32 type)
  87. {
  88. }
  89. // static
  90. S32 LLAllocator::popMemType()
  91. {
  92. return -1;
  93. }
  94. void LLAllocator::setProfilingEnabled(bool should_enable)
  95. {
  96. }
  97. // static
  98. bool LLAllocator::isProfiling()
  99. {
  100. return false;
  101. }
  102. std::string LLAllocator::getRawProfile()
  103. {
  104. return std::string();
  105. }
  106. #endif // LL_USE_TCMALLOC
  107. LLAllocatorHeapProfile const & LLAllocator::getProfile()
  108. {
  109. mProf.mLines.clear();
  110. // *TODO - avoid making all these extra copies of things...
  111. std::string prof_text = getRawProfile();
  112. //std::cout << prof_text << std::endl;
  113. mProf.parse(prof_text);
  114. return mProf;
  115. }