PageRenderTime 57ms CodeModel.GetById 10ms app.highlight 42ms RepoModel.GetById 1ms app.codeStats 1ms

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