1
0

LindenUDPInfoModule.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Linq;
  30. using System.Reflection;
  31. using System.Text;
  32. using log4net;
  33. using Mono.Addins;
  34. using Nini.Config;
  35. using OpenMetaverse;
  36. using OpenSim.Framework;
  37. using OpenSim.Framework.Monitoring;
  38. using OpenSim.Region.ClientStack.LindenUDP;
  39. using OpenSim.Region.Framework.Interfaces;
  40. using OpenSim.Region.Framework.Scenes;
  41. namespace OpenSim.Region.OptionalModules.UDP.Linden
  42. {
  43. /// <summary>
  44. /// A module that just holds commands for inspecting the current state of the Linden UDP stack.
  45. /// </summary>
  46. /// <remarks>
  47. /// All actual client stack functionality remains in OpenSim.Region.ClientStack.LindenUDP
  48. /// </remarks>
  49. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LindenUDPInfoModule")]
  50. public class LindenUDPInfoModule : ISharedRegionModule
  51. {
  52. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  53. protected Dictionary<UUID, Scene> m_scenes = new();
  54. public string Name { get { return "Linden UDP Module"; } }
  55. public Type ReplaceableInterface { get { return null; } }
  56. public void Initialise(IConfigSource source)
  57. {
  58. }
  59. public void PostInitialise()
  60. {
  61. }
  62. public void Close()
  63. {
  64. }
  65. public void AddRegion(Scene scene)
  66. {
  67. //m_log.DebugFormat("[LINDEN UDP INFO MODULE]: REGION {0} ADDED", scene.RegionInfo.RegionName);
  68. lock (m_scenes)
  69. m_scenes[scene.RegionInfo.RegionID] = scene;
  70. scene.AddCommand(
  71. "Comms", this, "show pqueues",
  72. "show pqueues [full]",
  73. "Show priority queue data for each client",
  74. "Without the 'full' option, only root agents are shown."
  75. + " With the 'full' option child agents are also shown.",
  76. (mod, cmd) => MainConsole.Instance.Output(GetPQueuesReport(cmd)));
  77. scene.AddCommand(
  78. "Comms", this, "show queues",
  79. "show queues [full]",
  80. "Show queue data for each client",
  81. "Without the 'full' option, only root agents are shown.\n"
  82. + "With the 'full' option child agents are also shown.\n\n"
  83. + "Type - Rt is a root (avatar) client whilst cd is a child (neighbour interacting) client.\n"
  84. + "Since Last In - Time in milliseconds since last packet received.\n"
  85. + "Pkts In - Number of packets processed from the client.\n"
  86. + "Pkts Out - Number of packets sent to the client.\n"
  87. + "Pkts Resent - Number of packets resent to the client.\n"
  88. + "Bytes Unacked - Number of bytes transferred to the client that are awaiting acknowledgement.\n"
  89. + "Q Pkts * - Number of packets of various types (land, wind, etc.) to be sent to the client that are waiting for available bandwidth.\n",
  90. (mod, cmd) => MainConsole.Instance.Output(GetQueuesReport(cmd)));
  91. scene.AddCommand(
  92. "Comms", this, "show image queues",
  93. "show image queues <first-name> <last-name>",
  94. "Show the image queues (textures downloaded via UDP) for a particular client.",
  95. (mod, cmd) => MainConsole.Instance.Output(GetImageQueuesReport(cmd)));
  96. scene.AddCommand(
  97. "Comms", this, "clear image queues",
  98. "clear image queues <first-name> <last-name>",
  99. "Clear the image queues (textures downloaded via UDP) for a particular client.",
  100. (mod, cmd) => MainConsole.Instance.Output(HandleImageQueuesClear(cmd)));
  101. scene.AddCommand(
  102. "Comms", this, "show throttles",
  103. "show throttles [full]",
  104. "Show throttle settings for each client and for the server overall",
  105. "Without the 'full' option, only root agents are shown."
  106. + " With the 'full' option child agents are also shown.",
  107. (mod, cmd) => MainConsole.Instance.Output(GetThrottlesReport(cmd)));
  108. }
  109. public void RemoveRegion(Scene scene)
  110. {
  111. //m_log.DebugFormat("[LINDEN UDP INFO MODULE]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
  112. lock (m_scenes)
  113. m_scenes.Remove(scene.RegionInfo.RegionID);
  114. }
  115. public void RegionLoaded(Scene scene)
  116. {
  117. }
  118. protected string HandleImageQueuesClear(string[] cmd)
  119. {
  120. if (cmd.Length != 5)
  121. return "Usage: image queues clear <first-name> <last-name>";
  122. string firstName = cmd[3];
  123. string lastName = cmd[4];
  124. List<ScenePresence> foundAgents = new();
  125. lock (m_scenes)
  126. {
  127. foreach (Scene scene in m_scenes.Values)
  128. {
  129. ScenePresence sp = scene.GetScenePresence(firstName, lastName);
  130. if (sp is not null)
  131. foundAgents.Add(sp);
  132. }
  133. }
  134. if (foundAgents.Count == 0)
  135. return string.Format("No agents found for {0} {1}", firstName, lastName);
  136. StringBuilder report = new();
  137. foreach (ScenePresence agent in foundAgents)
  138. {
  139. if (agent.ControllingClient is not LLClientView client)
  140. return "This command is only supported for LLClientView";
  141. int requestsDeleted = client.ImageManager.ClearImageQueue();
  142. report.AppendFormat(
  143. "In region {0} ({1} agent) cleared {2} requests\n",
  144. agent.Scene.RegionInfo.RegionName, agent.IsChildAgent ? "child" : "root", requestsDeleted);
  145. }
  146. return report.ToString();
  147. }
  148. protected string GetColumnEntry(string entry, int maxLength, int columnPadding)
  149. {
  150. return string.Format(
  151. "{0,-" + maxLength + "}{1,-" + columnPadding + "}",
  152. entry.Length > maxLength ? entry[..maxLength] : entry,
  153. "");
  154. }
  155. /// <summary>
  156. /// Generate UDP Queue data report for each client
  157. /// </summary>
  158. /// <param name="showParams"></param>
  159. /// <returns></returns>
  160. protected string GetPQueuesReport(string[] showParams)
  161. {
  162. bool showChildren = false;
  163. string pname = "";
  164. if (showParams.Length > 2 && showParams[2] == "full")
  165. showChildren = true;
  166. else if (showParams.Length > 3)
  167. pname = showParams[2] + " " + showParams[3];
  168. StringBuilder report = new();
  169. const int columnPadding = 2;
  170. const int maxNameLength = 18;
  171. const int maxRegionNameLength = 14;
  172. const int maxTypeLength = 4;
  173. //int totalInfoFieldsLength = maxNameLength + columnPadding + maxRegionNameLength + columnPadding + maxTypeLength + columnPadding;
  174. report.Append(GetColumnEntry("User", maxNameLength, columnPadding));
  175. report.Append(GetColumnEntry("Region", maxRegionNameLength, columnPadding));
  176. report.Append(GetColumnEntry("Type", maxTypeLength, columnPadding));
  177. report.AppendFormat(
  178. "{0,7} {1,7} {2,7} {3,7} {4,7} {5,7} {6,7} {7,7} {8,7} {9,7} {10,7} {11,7}\n",
  179. "Pri 0",
  180. "Pri 1",
  181. "Pri 2",
  182. "Pri 3",
  183. "Pri 4",
  184. "Pri 5",
  185. "Pri 6",
  186. "Pri 7",
  187. "Pri 8",
  188. "Pri 9",
  189. "Pri 10",
  190. "Pri 11");
  191. lock (m_scenes)
  192. {
  193. foreach (Scene scene in m_scenes.Values)
  194. {
  195. scene.ForEachClient(
  196. delegate(IClientAPI client)
  197. {
  198. if (client is LLClientView llclient)
  199. {
  200. bool isChild = client.SceneAgent.IsChildAgent;
  201. if (isChild && !showChildren)
  202. return;
  203. if (pname != "" && client.Name != pname)
  204. return;
  205. string regionName = scene.RegionInfo.RegionName;
  206. report.Append(GetColumnEntry(client.Name, maxNameLength, columnPadding));
  207. report.Append(GetColumnEntry(regionName, maxRegionNameLength, columnPadding));
  208. report.Append(GetColumnEntry(isChild ? "Cd" : "Rt", maxTypeLength, columnPadding));
  209. report.AppendLine(llclient.EntityUpdateQueue.ToString());
  210. }
  211. });
  212. }
  213. }
  214. return report.ToString();
  215. }
  216. /// <summary>
  217. /// Generate an image queue report
  218. /// </summary>
  219. /// <param name="showParams"></param>
  220. /// <returns></returns>
  221. private string GetImageQueuesReport(string[] showParams)
  222. {
  223. if (showParams.Length < 5 || showParams.Length > 6)
  224. return "Usage: show image queues <first-name> <last-name> [full]";
  225. string firstName = showParams[3];
  226. string lastName = showParams[4];
  227. bool showChildAgents = showParams.Length == 6;
  228. List<ScenePresence> foundAgents = new();
  229. lock (m_scenes)
  230. {
  231. foreach (Scene scene in m_scenes.Values)
  232. {
  233. ScenePresence sp = scene.GetScenePresence(firstName, lastName);
  234. if (sp is not null && (showChildAgents || !sp.IsChildAgent))
  235. foundAgents.Add(sp);
  236. }
  237. }
  238. if (foundAgents.Count == 0)
  239. return string.Format("No agents found for {0} {1}", firstName, lastName);
  240. StringBuilder report = new();
  241. foreach (ScenePresence agent in foundAgents)
  242. {
  243. if (agent.ControllingClient is not LLClientView client)
  244. return "This command is only supported for LLClientView";
  245. J2KImage[] images = client.ImageManager.GetImages();
  246. report.AppendFormat("In region {0} ({1} agent)\n",
  247. agent.Scene.RegionInfo.RegionName, agent.IsChildAgent ? "child" : "root");
  248. report.AppendFormat("Images in queue: {0}\n", images.Length);
  249. if (images.Length > 0)
  250. {
  251. report.AppendFormat(
  252. "{0,-36} {1,-8} {2,-10} {3,-9} {4,-9} {5,-7}\n",
  253. "Texture ID",
  254. "Last Seq",
  255. "Priority",
  256. "Start Pkt",
  257. "Has Asset",
  258. "Decoded");
  259. foreach (J2KImage image in images)
  260. report.AppendFormat(
  261. "{0,36} {1,8} {2,10} {3,10} {4,9} {5,7}\n",
  262. image.TextureID, image.LastSequence, image.Priority, image.StartPacket, image.HasAsset, image.IsDecoded);
  263. }
  264. }
  265. return report.ToString();
  266. }
  267. /// <summary>
  268. /// Generate UDP Queue data report for each client
  269. /// </summary>
  270. /// <param name="showParams"></param>
  271. /// <returns></returns>
  272. protected string GetQueuesReport(string[] showParams)
  273. {
  274. bool showChildren = false;
  275. string pname = "";
  276. if (showParams.Length > 2 && showParams[2] == "full")
  277. showChildren = true;
  278. else if (showParams.Length > 3)
  279. pname = showParams[2] + " " + showParams[3];
  280. StringBuilder report = new();
  281. const int columnPadding = 2;
  282. const int maxNameLength = 18;
  283. const int maxRegionNameLength = 14;
  284. const int maxTypeLength = 4;
  285. int totalInfoFieldsLength = maxNameLength + columnPadding
  286. + maxRegionNameLength + columnPadding
  287. + maxTypeLength + columnPadding;
  288. report.Append(GetColumnEntry("User", maxNameLength, columnPadding));
  289. report.Append(GetColumnEntry("Region", maxRegionNameLength, columnPadding));
  290. report.Append(GetColumnEntry("Type", maxTypeLength, columnPadding));
  291. report.AppendFormat(
  292. "{0,7} {1,7} {2,7} {3,7} {4,9} {5,7} {6,7} {7,7} {8,7} {9,7} {10,8} {11,7}\n",
  293. "Since",
  294. "Pkts",
  295. "Pkts",
  296. "Pkts",
  297. "Bytes",
  298. "Q Pkts",
  299. "Q Pkts",
  300. "Q Pkts",
  301. "Q Pkts",
  302. "Q Pkts",
  303. "Q Pkts",
  304. "Q Pkts");
  305. report.AppendFormat("{0,-" + totalInfoFieldsLength + "}", "");
  306. report.AppendFormat(
  307. "{0,7} {1,7} {2,7} {3,7} {4,9} {5,7} {6,7} {7,7} {8,7} {9,7} {10,8} {11,7}\n",
  308. "Last In",
  309. "In",
  310. "Out",
  311. "Resent",
  312. "Unacked",
  313. "Resend",
  314. "Land",
  315. "Wind",
  316. "Cloud",
  317. "Task",
  318. "Texture",
  319. "Asset");
  320. lock (m_scenes)
  321. {
  322. foreach (Scene scene in m_scenes.Values)
  323. {
  324. scene.ForEachClient(
  325. delegate(IClientAPI client)
  326. {
  327. if (client is IStatsCollector collector)
  328. {
  329. bool isChild = client.SceneAgent.IsChildAgent;
  330. if (isChild && !showChildren)
  331. return;
  332. if (pname != "" && client.Name != pname)
  333. return;
  334. report.Append(GetColumnEntry(client.Name, maxNameLength, columnPadding));
  335. report.Append(GetColumnEntry(scene.RegionInfo.RegionName, maxRegionNameLength, columnPadding));
  336. report.Append(GetColumnEntry(isChild ? "Cd" : "Rt", maxTypeLength, columnPadding));
  337. IStatsCollector stats = collector;
  338. report.AppendLine(stats.Report());
  339. }
  340. });
  341. }
  342. }
  343. return report.ToString();
  344. }
  345. /// <summary>
  346. /// Show throttle data
  347. /// </summary>
  348. /// <param name="showParams"></param>
  349. /// <returns></returns>
  350. protected string GetThrottlesReport(string[] showParams)
  351. {
  352. bool showChildren = false;
  353. string pname = "";
  354. if (showParams.Length > 2 && showParams[2] == "full")
  355. showChildren = true;
  356. else if (showParams.Length > 3)
  357. pname = showParams[2] + " " + showParams[3];
  358. StringBuilder report = new();
  359. const int columnPadding = 2;
  360. const int maxNameLength = 18;
  361. const int maxRegionNameLength = 14;
  362. const int maxTypeLength = 4;
  363. int totalInfoFieldsLength = maxNameLength + columnPadding + maxRegionNameLength + columnPadding + maxTypeLength + columnPadding;
  364. report.Append(GetColumnEntry("User", maxNameLength, columnPadding));
  365. report.Append(GetColumnEntry("Region", maxRegionNameLength, columnPadding));
  366. report.Append(GetColumnEntry("Type", maxTypeLength, columnPadding));
  367. report.AppendFormat(
  368. "{0,8} {1,8} {2,7} {3,8} {4,7} {5,7} {6,7} {7,7} {8,9} {9,7}\n",
  369. "Max",
  370. "Target",
  371. "Actual",
  372. "Resend",
  373. "Land",
  374. "Wind",
  375. "Cloud",
  376. "Task",
  377. "Texture",
  378. "Asset");
  379. report.AppendFormat("{0,-" + totalInfoFieldsLength + "}", "");
  380. report.AppendFormat(
  381. "{0,8} {1,8} {2,7} {3,8} {4,7} {5,7} {6,7} {7,7} {8,9} {9,7}\n",
  382. "kb/s",
  383. "kb/s",
  384. "kb/s",
  385. "kb/s",
  386. "kb/s",
  387. "kb/s",
  388. "kb/s",
  389. "kb/s",
  390. "kb/s",
  391. "kb/s");
  392. report.AppendLine();
  393. lock (m_scenes)
  394. {
  395. foreach (Scene scene in m_scenes.Values)
  396. {
  397. scene.ForEachClient(
  398. delegate(IClientAPI client)
  399. {
  400. if (client is LLClientView llClient)
  401. {
  402. bool isChild = client.SceneAgent.IsChildAgent;
  403. if (isChild && !showChildren)
  404. return;
  405. if (pname != "" && client.Name != pname)
  406. return;
  407. LLUDPClient llUdpClient = llClient.UDPClient;
  408. ClientInfo ci = llUdpClient.GetClientInfo();
  409. report.Append(GetColumnEntry(client.Name, maxNameLength, columnPadding));
  410. report.Append(GetColumnEntry(scene.RegionInfo.RegionName, maxRegionNameLength, columnPadding));
  411. report.Append(GetColumnEntry(isChild ? "Cd" : "Rt", maxTypeLength, columnPadding));
  412. report.AppendFormat(
  413. "{0,8} {1,8} {2,7} {3,8} {4,7} {5,7} {6,7} {7,7} {8,9} {9,7}\n",
  414. ci.maxThrottle > 0 ? ((ci.maxThrottle * 8) / 1000).ToString() : "-",
  415. llUdpClient.FlowThrottle.AdaptiveEnabled
  416. ? ((ci.targetThrottle * 8) / 1000).ToString()
  417. : (llUdpClient.FlowThrottle.TotalDripRequest * 8 / 1000).ToString(),
  418. (ci.totalThrottle * 8) / 1000,
  419. (ci.resendThrottle * 8) / 1000,
  420. (ci.landThrottle * 8) / 1000,
  421. (ci.windThrottle * 8) / 1000,
  422. (ci.cloudThrottle * 8) / 1000,
  423. (ci.taskThrottle * 8) / 1000,
  424. (ci.textureThrottle * 8) / 1000,
  425. (ci.assetThrottle * 8) / 1000);
  426. }
  427. });
  428. }
  429. }
  430. return report.ToString();
  431. }
  432. private void PrintRequests(string type, Dictionary<string, int> sortedDict, int sum)
  433. {
  434. m_log.InfoFormat("[INFO]:");
  435. m_log.InfoFormat("[INFO]: {0,25}", type);
  436. foreach (KeyValuePair<string, int> kvp in sortedDict.Take(12))
  437. m_log.InfoFormat("[INFO]: {0,25} {1,-6}", kvp.Key, kvp.Value);
  438. m_log.InfoFormat("[INFO]: {0,25}", "...");
  439. m_log.InfoFormat("[INFO]: {0,25} {1,-6}", "Total", sum);
  440. }
  441. }
  442. }