llviewerthrottle.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * @file llviewerthrottle.cpp
  3. * @brief LLViewerThrottle class implementation
  4. *
  5. * $LicenseInfo:firstyear=2002&license=viewergpl$
  6. *
  7. * Copyright (c) 2002-2009, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #include "llviewerprecompiledheaders.h"
  33. #include "llviewerthrottle.h"
  34. #include "llevent.h"
  35. #include "lldatapacker.h"
  36. #include "llmessage.h"
  37. #include "llagent.h"
  38. #include "llgridmanager.h" // For gIsInSecondLife
  39. #include "llviewercontrol.h"
  40. #include "llviewerstats.h"
  41. // Ignore a bogus clang v15.0 warning
  42. #if CLANG_VERSION >= 150000
  43. # pragma clang diagnostic ignored "-Warray-parameter"
  44. #endif
  45. using namespace LLOldEvents;
  46. // consts
  47. // The viewer is allowed to set the under-the-hood bandwidth to 50% greater
  48. // than the prefs UI shows, under the assumption that the viewer won't
  49. // receive all the different message types at once.
  50. constexpr F32 MAX_FRACTIONAL = 1.5f;
  51. constexpr F32 MIN_FRACTIONAL = 0.2f;
  52. constexpr U32 MIN_BANDWIDTH = 256;
  53. constexpr U32 MAX_BANDWIDTH = 32768;
  54. constexpr F32 STEP_FRACTIONAL = 0.1f;
  55. constexpr F32 TIGHTEN_THROTTLE_THRESHOLD = 3.f; // Packet loss % per second
  56. constexpr F32 EASE_THROTTLE_THRESHOLD = 0.5f; // Packet loss % per second
  57. constexpr F32 DYNAMIC_UPDATE_DURATION = 5.f; // In seconds
  58. LLViewerThrottle gViewerThrottle;
  59. // Bandwidth settings for different bit rates, they are interpolated /
  60. // extrapolated.
  61. // The values are for: Resend, Land, Wind, Cloud, Task, Texture, Asset
  62. static const U32 BW_PRESET_50[TC_EOF] = { 5, 10, 3, 3, 10, 10, 9 };
  63. static const U32 BW_PRESET_300[TC_EOF] = { 30, 40, 9, 9, 86, 86, 40 };
  64. static const U32 BW_PRESET_500[TC_EOF] = { 50, 70, 14, 14, 136, 136, 80 };
  65. static const U32 BW_PRESET_1000[TC_EOF] = { 100, 100, 20, 20, 310, 310, 140 };
  66. static const U32 BW_PRESET_2000[TC_EOF] = { 200, 200, 25, 25, 450, 800, 300 };
  67. static const U32 BW_PRESET_10000[TC_EOF] = { 1000, 500, 25, 25, 1450, 5000, 2000 };
  68. LLViewerThrottleGroup::LLViewerThrottleGroup()
  69. {
  70. for (S32 i = 0; i < TC_EOF; ++i)
  71. {
  72. mThrottles[i] = 0;
  73. }
  74. mThrottleTotal = 0;
  75. }
  76. LLViewerThrottleGroup::LLViewerThrottleGroup(const U32 settings[])
  77. {
  78. mThrottleTotal = 0;
  79. for (S32 i = 0; i < TC_EOF; ++i)
  80. {
  81. mThrottles[i] = settings[i];
  82. mThrottleTotal += settings[i];
  83. }
  84. }
  85. LLViewerThrottleGroup LLViewerThrottleGroup::operator*(F32 frac) const
  86. {
  87. LLViewerThrottleGroup res;
  88. res.mThrottleTotal = 0;
  89. for (S32 i = 0; i < TC_EOF; ++i)
  90. {
  91. res.mThrottles[i] = F32(mThrottles[i]) * frac;
  92. res.mThrottleTotal += res.mThrottles[i];
  93. }
  94. return res;
  95. }
  96. LLViewerThrottleGroup LLViewerThrottleGroup::operator+(const LLViewerThrottleGroup& b) const
  97. {
  98. LLViewerThrottleGroup res;
  99. res.mThrottleTotal = 0;
  100. for (S32 i = 0; i < TC_EOF; i++)
  101. {
  102. res.mThrottles[i] = mThrottles[i] + b.mThrottles[i];
  103. res.mThrottleTotal += res.mThrottles[i];
  104. }
  105. return res;
  106. }
  107. LLViewerThrottleGroup LLViewerThrottleGroup::operator-(const LLViewerThrottleGroup& b) const
  108. {
  109. LLViewerThrottleGroup res;
  110. res.mThrottleTotal = 0;
  111. for (S32 i = 0; i < TC_EOF; ++i)
  112. {
  113. res.mThrottles[i] = mThrottles[i] - b.mThrottles[i];
  114. res.mThrottleTotal += res.mThrottles[i];
  115. }
  116. return res;
  117. }
  118. void LLViewerThrottleGroup::sendToSim() const
  119. {
  120. llinfos << "Sending throttle settings, total BW " << mThrottleTotal
  121. << llendl;
  122. LLMessageSystem* msg = gMessageSystemp;
  123. msg->newMessageFast(_PREHASH_AgentThrottle);
  124. msg->nextBlockFast(_PREHASH_AgentData);
  125. msg->addUUIDFast(_PREHASH_AgentID, gAgentID);
  126. msg->addUUIDFast(_PREHASH_SessionID, gAgentSessionID);
  127. msg->addU32Fast(_PREHASH_CircuitCode, msg->mOurCircuitCode);
  128. msg->nextBlockFast(_PREHASH_Throttle);
  129. msg->addU32Fast(_PREHASH_GenCounter, 0);
  130. // Pack up the throttle data
  131. U8 tmp[64];
  132. LLDataPackerBinaryBuffer dp(tmp, MAX_THROTTLE_SIZE);
  133. for (S32 i = 0; i < TC_EOF; ++i)
  134. {
  135. // Sim wants BPS, not KBPS
  136. dp.packF32(F32(mThrottles[i] * 1024), "Throttle");
  137. }
  138. S32 len = dp.getCurrentSize();
  139. msg->addBinaryDataFast(_PREHASH_Throttles, tmp, len);
  140. gAgent.sendReliableMessage();
  141. }
  142. void LLViewerThrottleGroup::dump()
  143. {
  144. static const std::string groups[TC_EOF] = {
  145. "Resend", "Land", "Wind", "Cloud", "Task", "Texture", "Asset"
  146. };
  147. for (S32 i = 0; i < TC_EOF; ++i)
  148. {
  149. LL_DEBUGS("Throttle") << groups[i] << ": " << mThrottles[i] << LL_ENDL;
  150. }
  151. LL_DEBUGS("Throttle") << "Total: " << mThrottleTotal << LL_ENDL;
  152. }
  153. LLViewerThrottle::LLViewerThrottle()
  154. : mMaxBandwidth(0),
  155. mCurrentBandwidth(0),
  156. mThrottleFrac(1.f),
  157. mBWSettingName("ThrottleBandwidthKbps")
  158. {
  159. // Need to be pushed on in bandwidth order
  160. mPresets.emplace_back(BW_PRESET_50);
  161. mPresets.emplace_back(BW_PRESET_300);
  162. mPresets.emplace_back(BW_PRESET_500);
  163. mPresets.emplace_back(BW_PRESET_1000);
  164. mPresets.emplace_back(BW_PRESET_2000);
  165. mPresets.emplace_back(BW_PRESET_10000);
  166. }
  167. void LLViewerThrottle::setMaxBandwidth(U32 kbps, bool from_event)
  168. {
  169. if (!from_event)
  170. {
  171. kbps = llclamp(kbps, MIN_BANDWIDTH, MAX_BANDWIDTH);
  172. gSavedSettings.setU32(getSettingName(), kbps);
  173. }
  174. gViewerThrottle.load();
  175. if (gAgent.getRegion())
  176. {
  177. gViewerThrottle.sendToSim();
  178. }
  179. }
  180. void LLViewerThrottle::load()
  181. {
  182. mBWSettingName = gIsInSecondLife ? "ThrottleBandwidthKbps"
  183. : "ThrottleBandwidthKbpsOS";
  184. U32 curr_value = gSavedSettings.getU32(getSettingName());
  185. if (curr_value < MIN_BANDWIDTH)
  186. {
  187. mMaxBandwidth = MIN_BANDWIDTH * 1024; // Convert to bps
  188. gSavedSettings.setU32(getSettingName(), MIN_BANDWIDTH);
  189. }
  190. else if (curr_value > MAX_BANDWIDTH)
  191. {
  192. mMaxBandwidth = MAX_BANDWIDTH * 1024; // Convert to bps
  193. gSavedSettings.setU32(getSettingName(), MAX_BANDWIDTH);
  194. }
  195. else
  196. {
  197. mMaxBandwidth = curr_value * 1024; // Convert to bps
  198. }
  199. resetDynamicThrottle();
  200. mCurrent.dump();
  201. }
  202. void LLViewerThrottle::save() const
  203. {
  204. gSavedSettings.setU32(getSettingName(), mMaxBandwidth / 1024);
  205. }
  206. void LLViewerThrottle::sendToSim() const
  207. {
  208. mCurrent.sendToSim();
  209. }
  210. LLViewerThrottleGroup LLViewerThrottle::getThrottleGroup(U32 bandwidth_kbps)
  211. {
  212. // Clamp the bandwidth users can set.
  213. U32 set_bandwidth = llclamp(bandwidth_kbps, MIN_BANDWIDTH, MAX_BANDWIDTH);
  214. S32 count = mPresets.size();
  215. S32 i;
  216. for (i = 0; i < count; ++i)
  217. {
  218. if (mPresets[i].getTotal() > set_bandwidth)
  219. {
  220. break;
  221. }
  222. }
  223. if (i == 0)
  224. {
  225. // We return the minimum if it is less than the minimum
  226. return mPresets[0];
  227. }
  228. if (i == count)
  229. {
  230. // Higher than the highest preset, we extrapolate out based on the
  231. // last two presets. This allows us to keep certain throttle channels
  232. // from growing in bandwidth
  233. F32 delta_bw = set_bandwidth - mPresets[count-1].getTotal();
  234. LLViewerThrottleGroup delta_throttle = mPresets[count - 1] -
  235. mPresets[count - 2];
  236. F32 delta_total = delta_throttle.getTotal();
  237. F32 delta_frac = delta_bw / delta_total;
  238. delta_throttle = delta_throttle * delta_frac;
  239. return mPresets[count - 1] + delta_throttle;
  240. }
  241. // In between two presets, just interpolate
  242. F32 delta_bw = set_bandwidth - mPresets[i - 1].getTotal();
  243. LLViewerThrottleGroup delta_throttle = mPresets[i] - mPresets[i - 1];
  244. F32 delta_total = delta_throttle.getTotal();
  245. F32 delta_frac = delta_bw / delta_total;
  246. delta_throttle = delta_throttle * delta_frac;
  247. return mPresets[i - 1] + delta_throttle;
  248. }
  249. // static
  250. void LLViewerThrottle::resetDynamicThrottle()
  251. {
  252. mThrottleFrac = MAX_FRACTIONAL;
  253. mCurrentBandwidth = U32(F32(mMaxBandwidth) * MAX_FRACTIONAL);
  254. mCurrent = getThrottleGroup(mCurrentBandwidth / 1024);
  255. }
  256. void LLViewerThrottle::updateDynamicThrottle()
  257. {
  258. if (mUpdateTimer.getElapsedTimeF32() < DYNAMIC_UPDATE_DURATION)
  259. {
  260. return;
  261. }
  262. mUpdateTimer.reset();
  263. if (gViewerStats.mPacketsLostPercentStat.getMean() >
  264. TIGHTEN_THROTTLE_THRESHOLD)
  265. {
  266. if (mThrottleFrac <= MIN_FRACTIONAL ||
  267. mCurrentBandwidth / 1024 <= MIN_BANDWIDTH)
  268. {
  269. return;
  270. }
  271. mThrottleFrac -= STEP_FRACTIONAL;
  272. mThrottleFrac = llmax(MIN_FRACTIONAL, mThrottleFrac);
  273. mCurrentBandwidth = mMaxBandwidth * mThrottleFrac;
  274. mCurrent = getThrottleGroup(mCurrentBandwidth / 1024);
  275. mCurrent.sendToSim();
  276. llinfos << "Tightening network throttle to " << mCurrentBandwidth << llendl;
  277. }
  278. else if (gViewerStats.mPacketsLostPercentStat.getMean() <=
  279. EASE_THROTTLE_THRESHOLD)
  280. {
  281. if (mThrottleFrac >= MAX_FRACTIONAL ||
  282. mCurrentBandwidth / 1024 >= MAX_BANDWIDTH)
  283. {
  284. return;
  285. }
  286. mThrottleFrac += STEP_FRACTIONAL;
  287. mThrottleFrac = llmin(MAX_FRACTIONAL, mThrottleFrac);
  288. mCurrentBandwidth = mMaxBandwidth * mThrottleFrac;
  289. constexpr F32 TO_KPBS = 1.f / 1024.f;
  290. mCurrent = getThrottleGroup(F32(mCurrentBandwidth) * TO_KPBS);
  291. mCurrent.sendToSim();
  292. llinfos << "Easing network throttle to " << mCurrentBandwidth << llendl;
  293. }
  294. }