PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/newview/llfloatermemleak.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 227 lines | 168 code | 31 blank | 28 comment | 16 complexity | 096918eb5634fef9d3fc62a73d598c36 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llfloatermemleak.cpp
  3. * @brief LLFloatermemleak class definition
  4. *
  5. * $LicenseInfo:firstyear=2007&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 "llviewerprecompiledheaders.h"
  27. #include "llfloatermemleak.h"
  28. #include "lluictrlfactory.h"
  29. #include "llbutton.h"
  30. #include "llspinctrl.h"
  31. #include "llresmgr.h"
  32. #include "llmath.h"
  33. #include "llviewerwindow.h"
  34. U32 LLFloaterMemLeak::sMemLeakingSpeed = 0 ; //bytes leaked per frame
  35. U32 LLFloaterMemLeak::sMaxLeakedMem = 0 ; //maximum allowed leaked memory
  36. U32 LLFloaterMemLeak::sTotalLeaked = 0 ;
  37. S32 LLFloaterMemLeak::sStatus = LLFloaterMemLeak::STOP ;
  38. BOOL LLFloaterMemLeak::sbAllocationFailed = FALSE ;
  39. LLFloaterMemLeak::LLFloaterMemLeak(const LLSD& key)
  40. : LLFloater(key)
  41. {
  42. setTitle("Memory Leaking Simulation Floater");
  43. mCommitCallbackRegistrar.add("MemLeak.ChangeLeakingSpeed", boost::bind(&LLFloaterMemLeak::onChangeLeakingSpeed, this));
  44. mCommitCallbackRegistrar.add("MemLeak.ChangeMaxMemLeaking", boost::bind(&LLFloaterMemLeak::onChangeMaxMemLeaking, this));
  45. mCommitCallbackRegistrar.add("MemLeak.Start", boost::bind(&LLFloaterMemLeak::onClickStart, this));
  46. mCommitCallbackRegistrar.add("MemLeak.Stop", boost::bind(&LLFloaterMemLeak::onClickStop, this));
  47. mCommitCallbackRegistrar.add("MemLeak.Release", boost::bind(&LLFloaterMemLeak::onClickRelease, this));
  48. mCommitCallbackRegistrar.add("MemLeak.Close", boost::bind(&LLFloaterMemLeak::onClickClose, this));
  49. }
  50. //----------------------------------------------
  51. BOOL LLFloaterMemLeak::postBuild(void)
  52. {
  53. F32 a, b ;
  54. a = getChild<LLUICtrl>("leak_speed")->getValue().asReal();
  55. if(a > (F32)(0xFFFFFFFF))
  56. {
  57. sMemLeakingSpeed = 0xFFFFFFFF ;
  58. }
  59. else
  60. {
  61. sMemLeakingSpeed = (U32)a ;
  62. }
  63. b = getChild<LLUICtrl>("max_leak")->getValue().asReal();
  64. if(b > (F32)0xFFF)
  65. {
  66. sMaxLeakedMem = 0xFFFFFFFF ;
  67. }
  68. else
  69. {
  70. sMaxLeakedMem = ((U32)b) << 20 ;
  71. }
  72. sbAllocationFailed = FALSE ;
  73. return TRUE ;
  74. }
  75. LLFloaterMemLeak::~LLFloaterMemLeak()
  76. {
  77. release() ;
  78. sMemLeakingSpeed = 0 ; //bytes leaked per frame
  79. sMaxLeakedMem = 0 ; //maximum allowed leaked memory
  80. }
  81. void LLFloaterMemLeak::release()
  82. {
  83. if(mLeakedMem.empty())
  84. {
  85. return ;
  86. }
  87. for(S32 i = 0 ; i < (S32)mLeakedMem.size() ; i++)
  88. {
  89. delete[] mLeakedMem[i] ;
  90. }
  91. mLeakedMem.clear() ;
  92. sStatus = STOP ;
  93. sTotalLeaked = 0 ;
  94. sbAllocationFailed = FALSE ;
  95. }
  96. void LLFloaterMemLeak::stop()
  97. {
  98. sStatus = STOP ;
  99. sbAllocationFailed = TRUE ;
  100. }
  101. void LLFloaterMemLeak::idle()
  102. {
  103. if(STOP == sStatus)
  104. {
  105. return ;
  106. }
  107. sbAllocationFailed = FALSE ;
  108. if(RELEASE == sStatus)
  109. {
  110. release() ;
  111. return ;
  112. }
  113. char* p = NULL ;
  114. if(sMemLeakingSpeed > 0 && sTotalLeaked < sMaxLeakedMem)
  115. {
  116. p = new char[sMemLeakingSpeed] ;
  117. if(p)
  118. {
  119. mLeakedMem.push_back(p) ;
  120. sTotalLeaked += sMemLeakingSpeed ;
  121. }
  122. }
  123. if(!p)
  124. {
  125. sStatus = STOP ;
  126. sbAllocationFailed = TRUE ;
  127. }
  128. }
  129. //----------------------
  130. void LLFloaterMemLeak::onChangeLeakingSpeed()
  131. {
  132. F32 tmp ;
  133. tmp =getChild<LLUICtrl>("leak_speed")->getValue().asReal();
  134. if(tmp > (F32)0xFFFFFFFF)
  135. {
  136. sMemLeakingSpeed = 0xFFFFFFFF ;
  137. }
  138. else
  139. {
  140. sMemLeakingSpeed = (U32)tmp ;
  141. }
  142. }
  143. void LLFloaterMemLeak::onChangeMaxMemLeaking()
  144. {
  145. F32 tmp ;
  146. tmp =getChild<LLUICtrl>("max_leak")->getValue().asReal();
  147. if(tmp > (F32)0xFFF)
  148. {
  149. sMaxLeakedMem = 0xFFFFFFFF ;
  150. }
  151. else
  152. {
  153. sMaxLeakedMem = ((U32)tmp) << 20 ;
  154. }
  155. }
  156. void LLFloaterMemLeak::onClickStart()
  157. {
  158. sStatus = START ;
  159. }
  160. void LLFloaterMemLeak::onClickStop()
  161. {
  162. sStatus = STOP ;
  163. }
  164. void LLFloaterMemLeak::onClickRelease()
  165. {
  166. sStatus = RELEASE ;
  167. }
  168. void LLFloaterMemLeak::onClickClose()
  169. {
  170. setVisible(FALSE);
  171. }
  172. void LLFloaterMemLeak::draw()
  173. {
  174. //show total memory leaked
  175. if(sTotalLeaked > 0)
  176. {
  177. std::string bytes_string;
  178. LLResMgr::getInstance()->getIntegerString(bytes_string, sTotalLeaked >> 10 );
  179. getChild<LLUICtrl>("total_leaked_label")->setTextArg("[SIZE]", bytes_string);
  180. }
  181. else
  182. {
  183. getChild<LLUICtrl>("total_leaked_label")->setTextArg("[SIZE]", LLStringExplicit("0"));
  184. }
  185. if(sbAllocationFailed)
  186. {
  187. getChild<LLUICtrl>("note_label_1")->setTextArg("[NOTE1]", LLStringExplicit("Memory leaking simulation stops. Reduce leaking speed or"));
  188. getChild<LLUICtrl>("note_label_2")->setTextArg("[NOTE2]", LLStringExplicit("increase max leaked memory, then press Start to continue."));
  189. }
  190. else
  191. {
  192. getChild<LLUICtrl>("note_label_1")->setTextArg("[NOTE1]", LLStringExplicit(""));
  193. getChild<LLUICtrl>("note_label_2")->setTextArg("[NOTE2]", LLStringExplicit(""));
  194. }
  195. LLFloater::draw();
  196. }