PageRenderTime 104ms CodeModel.GetById 40ms app.highlight 29ms RepoModel.GetById 31ms app.codeStats 0ms

/indra/newview/llviewerthrottle.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 333 lines | 237 code | 53 blank | 43 comment | 24 complexity | fce0061d70f8fe55b2c175b230744a47 MD5 | raw file
  1/** 
  2 * @file llviewerthrottle.cpp
  3 * @brief LLViewerThrottle class implementation
  4 *
  5 * $LicenseInfo:firstyear=2002&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#include "llviewerthrottle.h"
 29
 30#include "llviewercontrol.h"
 31#include "message.h"
 32#include "llagent.h"
 33#include "llframetimer.h"
 34#include "llviewerstats.h"
 35#include "lldatapacker.h"
 36
 37using namespace LLOldEvents;
 38
 39// consts
 40
 41// The viewer is allowed to set the under-the-hood bandwidth to 50%
 42// greater than the prefs UI shows, under the assumption that the
 43// viewer won't receive all the different message types at once.
 44// I didn't design this, don't know who did. JC
 45const F32 MAX_FRACTIONAL = 1.5f;
 46const F32 MIN_FRACTIONAL = 0.2f;
 47
 48const F32 MIN_BANDWIDTH = 50.f;
 49const F32 MAX_BANDWIDTH = 3000.f;
 50const F32 STEP_FRACTIONAL = 0.1f;
 51const F32 TIGHTEN_THROTTLE_THRESHOLD = 3.0f; // packet loss % per s
 52const F32 EASE_THROTTLE_THRESHOLD = 0.5f; // packet loss % per s
 53const F32 DYNAMIC_UPDATE_DURATION = 5.0f; // seconds
 54
 55LLViewerThrottle gViewerThrottle;
 56
 57// static
 58const std:: string LLViewerThrottle::sNames[TC_EOF] = {
 59							"Resend",
 60							"Land",
 61							"Wind",
 62							"Cloud",
 63							"Task",
 64							"Texture",
 65							"Asset"
 66							};
 67
 68
 69// Bandwidth settings for different bit rates, they're interpolated/extrapolated.
 70//                                Resend Land Wind Cloud Task Texture Asset
 71const F32 BW_PRESET_50[TC_EOF]   = {   5,  10,   3,   3,  10,  10,   9 };
 72const F32 BW_PRESET_300[TC_EOF]  = {  30,  40,   9,   9,  86,  86,  40 };
 73const F32 BW_PRESET_500[TC_EOF]  = {  50,  70,  14,  14, 136, 136,  80 };
 74const F32 BW_PRESET_1000[TC_EOF] = { 100, 100,  20,  20, 310, 310, 140 };
 75
 76LLViewerThrottleGroup::LLViewerThrottleGroup()
 77{
 78	S32 i;
 79	for (i = 0; i < TC_EOF; i++)
 80	{
 81		mThrottles[i] = 0.f;
 82	}
 83	mThrottleTotal = 0.f;
 84}
 85
 86
 87LLViewerThrottleGroup::LLViewerThrottleGroup(const F32 settings[])
 88{
 89	mThrottleTotal = 0.f;
 90	S32 i;
 91	for (i = 0; i < TC_EOF; i++)
 92	{
 93		mThrottles[i] = settings[i];
 94		mThrottleTotal += settings[i];
 95	}
 96}
 97
 98
 99LLViewerThrottleGroup LLViewerThrottleGroup::operator*(const F32 frac) const
100{
101	LLViewerThrottleGroup res;
102	res.mThrottleTotal = 0.f;
103
104	S32 i;
105	for (i = 0; i < TC_EOF; i++)
106	{
107		res.mThrottles[i] = mThrottles[i] * frac;
108		res.mThrottleTotal += res.mThrottles[i];
109	}
110
111	return res;
112}
113
114
115LLViewerThrottleGroup LLViewerThrottleGroup::operator+(const LLViewerThrottleGroup &b) const
116{
117	LLViewerThrottleGroup res;
118	res.mThrottleTotal = 0.f;
119
120	S32 i;
121	for (i = 0; i < TC_EOF; i++)
122	{
123		res.mThrottles[i] = mThrottles[i] + b.mThrottles[i];
124		res.mThrottleTotal += res.mThrottles[i];
125	}
126
127	return res;
128}
129
130
131LLViewerThrottleGroup LLViewerThrottleGroup::operator-(const LLViewerThrottleGroup &b) const
132{
133	LLViewerThrottleGroup res;
134	res.mThrottleTotal = 0.f;
135
136	S32 i;
137	for (i = 0; i < TC_EOF; i++)
138	{
139		res.mThrottles[i] = mThrottles[i] - b.mThrottles[i];
140		res.mThrottleTotal += res.mThrottles[i];
141	}
142
143	return res;
144}
145
146
147void LLViewerThrottleGroup::sendToSim() const
148{
149	llinfos << "Sending throttle settings, total BW " << mThrottleTotal << llendl;
150	LLMessageSystem* msg = gMessageSystem;
151
152	msg->newMessageFast(_PREHASH_AgentThrottle);
153	msg->nextBlockFast(_PREHASH_AgentData);
154	msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
155	msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
156	msg->addU32Fast(_PREHASH_CircuitCode, msg->mOurCircuitCode);
157
158	msg->nextBlockFast(_PREHASH_Throttle);
159	msg->addU32Fast(_PREHASH_GenCounter, 0);
160
161	// Pack up the throttle data
162	U8 tmp[64];
163	LLDataPackerBinaryBuffer dp(tmp, MAX_THROTTLE_SIZE);
164	S32 i;
165	for (i = 0; i < TC_EOF; i++)
166	{
167		//sim wants BPS, not KBPS
168		dp.packF32(mThrottles[i] * 1024.0f, "Throttle");
169	}
170	S32 len = dp.getCurrentSize();
171	msg->addBinaryDataFast(_PREHASH_Throttles, tmp, len);
172
173	gAgent.sendReliableMessage();
174}
175
176
177void LLViewerThrottleGroup::dump()
178{
179	S32 i;
180	for (i = 0; i < TC_EOF; i++)
181	{
182		LL_DEBUGS("Throttle") << LLViewerThrottle::sNames[i] << ": " << mThrottles[i] << LL_ENDL;
183	}
184	LL_DEBUGS("Throttle") << "Total: " << mThrottleTotal << LL_ENDL;
185}
186
187class LLBPSListener : public LLSimpleListener
188{
189public:
190	virtual bool handleEvent(LLPointer<LLEvent> event, const LLSD& userdata)
191	{
192		gViewerThrottle.setMaxBandwidth((F32) event->getValue().asReal()*1024);
193		return true;
194	}
195};
196
197LLViewerThrottle::LLViewerThrottle() :
198	mMaxBandwidth(0.f),
199	mCurrentBandwidth(0.f),
200	mThrottleFrac(1.f)
201{
202	// Need to be pushed on in bandwidth order
203	mPresets.push_back(LLViewerThrottleGroup(BW_PRESET_50));
204	mPresets.push_back(LLViewerThrottleGroup(BW_PRESET_300));
205	mPresets.push_back(LLViewerThrottleGroup(BW_PRESET_500));
206	mPresets.push_back(LLViewerThrottleGroup(BW_PRESET_1000));
207}
208
209
210void LLViewerThrottle::setMaxBandwidth(F32 kbits_per_second, BOOL from_event)
211{
212	if (!from_event)
213	{
214		gSavedSettings.setF32("ThrottleBandwidthKBPS", kbits_per_second);
215	}
216	gViewerThrottle.load();
217
218	if (gAgent.getRegion())
219	{
220		gViewerThrottle.sendToSim();
221	}
222}
223
224void LLViewerThrottle::load()
225{
226	mMaxBandwidth = gSavedSettings.getF32("ThrottleBandwidthKBPS")*1024;
227	resetDynamicThrottle();
228	mCurrent.dump();
229}
230
231
232void LLViewerThrottle::save() const
233{
234	gSavedSettings.setF32("ThrottleBandwidthKBPS", mMaxBandwidth/1024);
235}
236
237
238void LLViewerThrottle::sendToSim() const
239{
240	mCurrent.sendToSim();
241}
242
243
244LLViewerThrottleGroup LLViewerThrottle::getThrottleGroup(const F32 bandwidth_kbps)
245{
246	//Clamp the bandwidth users can set.
247	F32 set_bandwidth = llclamp(bandwidth_kbps, MIN_BANDWIDTH, MAX_BANDWIDTH);
248
249	S32 count = mPresets.size();
250
251	S32 i;
252	for (i = 0; i < count; i++)
253	{
254		if (mPresets[i].getTotal() > set_bandwidth)
255		{
256			break;
257		}
258	}
259
260	if (i == 0)
261	{
262		// We return the minimum if it's less than the minimum
263		return mPresets[0];
264	}
265	else if (i == count)
266	{
267		// Higher than the highest preset, we extrapolate out based on the
268		// last two presets.  This allows us to keep certain throttle channels from
269		// growing in bandwidth
270		F32 delta_bw = set_bandwidth - mPresets[count-1].getTotal();
271		LLViewerThrottleGroup delta_throttle = mPresets[count - 1] - mPresets[count - 2];
272		F32 delta_total = delta_throttle.getTotal();
273		F32 delta_frac = delta_bw / delta_total;
274		delta_throttle = delta_throttle * delta_frac;
275		return mPresets[count-1] + delta_throttle;
276	}
277	else
278	{
279		// In between two presets, just interpolate
280		F32 delta_bw = set_bandwidth - mPresets[i - 1].getTotal();
281		LLViewerThrottleGroup delta_throttle = mPresets[i] - mPresets[i - 1];
282		F32 delta_total = delta_throttle.getTotal();
283		F32 delta_frac = delta_bw / delta_total;
284		delta_throttle = delta_throttle * delta_frac;
285		return mPresets[i - 1] + delta_throttle;
286	}
287}
288
289
290// static
291void LLViewerThrottle::resetDynamicThrottle()
292{
293	mThrottleFrac = MAX_FRACTIONAL;
294
295	mCurrentBandwidth = mMaxBandwidth*MAX_FRACTIONAL;
296	mCurrent = getThrottleGroup(mCurrentBandwidth / 1024.0f);
297}
298
299void LLViewerThrottle::updateDynamicThrottle()
300{
301	if (mUpdateTimer.getElapsedTimeF32() < DYNAMIC_UPDATE_DURATION)
302	{
303		return;
304	}
305	mUpdateTimer.reset();
306
307	if (LLViewerStats::getInstance()->mPacketsLostPercentStat.getMean() > TIGHTEN_THROTTLE_THRESHOLD)
308	{
309		if (mThrottleFrac <= MIN_FRACTIONAL || mCurrentBandwidth / 1024.0f <= MIN_BANDWIDTH)
310		{
311			return;
312		}
313		mThrottleFrac -= STEP_FRACTIONAL;
314		mThrottleFrac = llmax(MIN_FRACTIONAL, mThrottleFrac);
315		mCurrentBandwidth = mMaxBandwidth * mThrottleFrac;
316		mCurrent = getThrottleGroup(mCurrentBandwidth / 1024.0f);
317		mCurrent.sendToSim();
318		llinfos << "Tightening network throttle to " << mCurrentBandwidth << llendl;
319	}
320	else if (LLViewerStats::getInstance()->mPacketsLostPercentStat.getMean() <= EASE_THROTTLE_THRESHOLD)
321	{
322		if (mThrottleFrac >= MAX_FRACTIONAL || mCurrentBandwidth / 1024.0f >= MAX_BANDWIDTH)
323		{
324			return;
325		}
326		mThrottleFrac += STEP_FRACTIONAL;
327		mThrottleFrac = llmin(MAX_FRACTIONAL, mThrottleFrac);
328		mCurrentBandwidth = mMaxBandwidth * mThrottleFrac;
329		mCurrent = getThrottleGroup(mCurrentBandwidth/1024.0f);
330		mCurrent.sendToSim();
331		llinfos << "Easing network throttle to " << mCurrentBandwidth << llendl;
332	}
333}