LindenUDPInfoModule.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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.Console;
  38. using OpenSim.Framework.Monitoring;
  39. using OpenSim.Region.ClientStack.LindenUDP;
  40. using OpenSim.Region.Framework.Interfaces;
  41. using OpenSim.Region.Framework.Scenes;
  42. namespace OpenSim.Region.OptionalModules.UDP.Linden
  43. {
  44. /// <summary>
  45. /// A module that just holds commands for inspecting the current state of the Linden UDP stack.
  46. /// </summary>
  47. /// <remarks>
  48. /// All actual client stack functionality remains in OpenSim.Region.ClientStack.LindenUDP
  49. /// </remarks>
  50. [Extension(Path = "/OpenSim/RegionModules", NodeName = "RegionModule", Id = "LindenUDPInfoModule")]
  51. public class LindenUDPInfoModule : ISharedRegionModule
  52. {
  53. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  54. protected Dictionary<UUID, Scene> m_scenes = new Dictionary<UUID, Scene>();
  55. public string Name { get { return "Linden UDP Module"; } }
  56. public Type ReplaceableInterface { get { return null; } }
  57. public void Initialise(IConfigSource source)
  58. {
  59. // m_log.DebugFormat("[LINDEN UDP INFO MODULE]: INITIALIZED MODULE");
  60. }
  61. public void PostInitialise()
  62. {
  63. // m_log.DebugFormat("[LINDEN UDP INFO MODULE]: POST INITIALIZED MODULE");
  64. }
  65. public void Close()
  66. {
  67. // m_log.DebugFormat("[LINDEN UDP INFO MODULE]: CLOSED MODULE");
  68. }
  69. public void AddRegion(Scene scene)
  70. {
  71. // m_log.DebugFormat("[LINDEN UDP INFO MODULE]: REGION {0} ADDED", scene.RegionInfo.RegionName);
  72. lock (m_scenes)
  73. m_scenes[scene.RegionInfo.RegionID] = scene;
  74. scene.AddCommand(
  75. "Comms", this, "show pqueues",
  76. "show pqueues [full]",
  77. "Show priority queue data for each client",
  78. "Without the 'full' option, only root agents are shown."
  79. + " With the 'full' option child agents are also shown.",
  80. (mod, cmd) => MainConsole.Instance.Output(GetPQueuesReport(cmd)));
  81. scene.AddCommand(
  82. "Comms", this, "show queues",
  83. "show queues [full]",
  84. "Show queue data for each client",
  85. "Without the 'full' option, only root agents are shown.\n"
  86. + "With the 'full' option child agents are also shown.\n\n"
  87. + "Type - Rt is a root (avatar) client whilst cd is a child (neighbour interacting) client.\n"
  88. + "Since Last In - Time in milliseconds since last packet received.\n"
  89. + "Pkts In - Number of packets processed from the client.\n"
  90. + "Pkts Out - Number of packets sent to the client.\n"
  91. + "Pkts Resent - Number of packets resent to the client.\n"
  92. + "Bytes Unacked - Number of bytes transferred to the client that are awaiting acknowledgement.\n"
  93. + "Q Pkts * - Number of packets of various types (land, wind, etc.) to be sent to the client that are waiting for available bandwidth.\n",
  94. (mod, cmd) => MainConsole.Instance.Output(GetQueuesReport(cmd)));
  95. scene.AddCommand(
  96. "Comms", this, "show image queues",
  97. "show image queues <first-name> <last-name>",
  98. "Show the image queues (textures downloaded via UDP) for a particular client.",
  99. (mod, cmd) => MainConsole.Instance.Output(GetImageQueuesReport(cmd)));
  100. scene.AddCommand(
  101. "Comms", this, "clear image queues",
  102. "clear image queues <first-name> <last-name>",
  103. "Clear the image queues (textures downloaded via UDP) for a particular client.",
  104. (mod, cmd) => MainConsole.Instance.Output(HandleImageQueuesClear(cmd)));
  105. scene.AddCommand(
  106. "Comms", this, "show throttles",
  107. "show throttles [full]",
  108. "Show throttle settings for each client and for the server overall",
  109. "Without the 'full' option, only root agents are shown."
  110. + " With the 'full' option child agents are also shown.",
  111. (mod, cmd) => MainConsole.Instance.Output(GetThrottlesReport(cmd)));
  112. }
  113. public void RemoveRegion(Scene scene)
  114. {
  115. // m_log.DebugFormat("[LINDEN UDP INFO MODULE]: REGION {0} REMOVED", scene.RegionInfo.RegionName);
  116. lock (m_scenes)
  117. m_scenes.Remove(scene.RegionInfo.RegionID);
  118. }
  119. public void RegionLoaded(Scene scene)
  120. {
  121. // m_log.DebugFormat("[LINDEN UDP INFO MODULE]: REGION {0} LOADED", scene.RegionInfo.RegionName);
  122. }
  123. protected string HandleImageQueuesClear(string[] cmd)
  124. {
  125. if (cmd.Length != 5)
  126. return "Usage: image queues clear <first-name> <last-name>";
  127. string firstName = cmd[3];
  128. string lastName = cmd[4];
  129. List<ScenePresence> foundAgents = new List<ScenePresence>();
  130. lock (m_scenes)
  131. {
  132. foreach (Scene scene in m_scenes.Values)
  133. {
  134. ScenePresence sp = scene.GetScenePresence(firstName, lastName);
  135. if (sp != null)
  136. foundAgents.Add(sp);
  137. }
  138. }
  139. if (foundAgents.Count == 0)
  140. return string.Format("No agents found for {0} {1}", firstName, lastName);
  141. StringBuilder report = new StringBuilder();
  142. foreach (ScenePresence agent in foundAgents)
  143. {
  144. LLClientView client = agent.ControllingClient as LLClientView;
  145. if (client == null)
  146. return "This command is only supported for LLClientView";
  147. int requestsDeleted = client.ImageManager.ClearImageQueue();
  148. report.AppendFormat(
  149. "In region {0} ({1} agent) cleared {2} requests\n",
  150. agent.Scene.RegionInfo.RegionName, agent.IsChildAgent ? "child" : "root", requestsDeleted);
  151. }
  152. return report.ToString();
  153. }
  154. protected string GetColumnEntry(string entry, int maxLength, int columnPadding)
  155. {
  156. return string.Format(
  157. "{0,-" + maxLength + "}{1,-" + columnPadding + "}",
  158. entry.Length > maxLength ? entry.Substring(0, maxLength) : entry,
  159. "");
  160. }
  161. /// <summary>
  162. /// Generate UDP Queue data report for each client
  163. /// </summary>
  164. /// <param name="showParams"></param>
  165. /// <returns></returns>
  166. protected string GetPQueuesReport(string[] showParams)
  167. {
  168. bool showChildren = false;
  169. string pname = "";
  170. if (showParams.Length > 2 && showParams[2] == "full")
  171. showChildren = true;
  172. else if (showParams.Length > 3)
  173. pname = showParams[2] + " " + showParams[3];
  174. StringBuilder report = new StringBuilder();
  175. int columnPadding = 2;
  176. int maxNameLength = 18;
  177. int maxRegionNameLength = 14;
  178. int maxTypeLength = 4;
  179. // int totalInfoFieldsLength = maxNameLength + columnPadding + maxRegionNameLength + columnPadding + maxTypeLength + columnPadding;
  180. report.Append(GetColumnEntry("User", maxNameLength, columnPadding));
  181. report.Append(GetColumnEntry("Region", maxRegionNameLength, columnPadding));
  182. report.Append(GetColumnEntry("Type", maxTypeLength, columnPadding));
  183. report.AppendFormat(
  184. "{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",
  185. "Pri 0",
  186. "Pri 1",
  187. "Pri 2",
  188. "Pri 3",
  189. "Pri 4",
  190. "Pri 5",
  191. "Pri 6",
  192. "Pri 7",
  193. "Pri 8",
  194. "Pri 9",
  195. "Pri 10",
  196. "Pri 11");
  197. lock (m_scenes)
  198. {
  199. foreach (Scene scene in m_scenes.Values)
  200. {
  201. scene.ForEachClient(
  202. delegate(IClientAPI client)
  203. {
  204. if (client is LLClientView)
  205. {
  206. bool isChild = client.SceneAgent.IsChildAgent;
  207. if (isChild && !showChildren)
  208. return;
  209. string name = client.Name;
  210. if (pname != "" && name != pname)
  211. return;
  212. string regionName = scene.RegionInfo.RegionName;
  213. report.Append(GetColumnEntry(name, maxNameLength, columnPadding));
  214. report.Append(GetColumnEntry(regionName, maxRegionNameLength, columnPadding));
  215. report.Append(GetColumnEntry(isChild ? "Cd" : "Rt", maxTypeLength, columnPadding));
  216. report.AppendLine(((LLClientView)client).EntityUpdateQueue.ToString());
  217. }
  218. });
  219. }
  220. }
  221. return report.ToString();
  222. }
  223. /// <summary>
  224. /// Generate an image queue report
  225. /// </summary>
  226. /// <param name="showParams"></param>
  227. /// <returns></returns>
  228. private string GetImageQueuesReport(string[] showParams)
  229. {
  230. if (showParams.Length < 5 || showParams.Length > 6)
  231. return "Usage: show image queues <first-name> <last-name> [full]";
  232. string firstName = showParams[3];
  233. string lastName = showParams[4];
  234. bool showChildAgents = showParams.Length == 6;
  235. List<ScenePresence> foundAgents = new List<ScenePresence>();
  236. lock (m_scenes)
  237. {
  238. foreach (Scene scene in m_scenes.Values)
  239. {
  240. ScenePresence sp = scene.GetScenePresence(firstName, lastName);
  241. if (sp != null && (showChildAgents || !sp.IsChildAgent))
  242. foundAgents.Add(sp);
  243. }
  244. }
  245. if (foundAgents.Count == 0)
  246. return string.Format("No agents found for {0} {1}", firstName, lastName);
  247. StringBuilder report = new StringBuilder();
  248. foreach (ScenePresence agent in foundAgents)
  249. {
  250. LLClientView client = agent.ControllingClient as LLClientView;
  251. if (client == null)
  252. return "This command is only supported for LLClientView";
  253. J2KImage[] images = client.ImageManager.GetImages();
  254. report.AppendFormat(
  255. "In region {0} ({1} agent)\n",
  256. agent.Scene.RegionInfo.RegionName, agent.IsChildAgent ? "child" : "root");
  257. report.AppendFormat("Images in queue: {0}\n", images.Length);
  258. if (images.Length > 0)
  259. {
  260. report.AppendFormat(
  261. "{0,-36} {1,-8} {2,-10} {3,-9} {4,-9} {5,-7}\n",
  262. "Texture ID",
  263. "Last Seq",
  264. "Priority",
  265. "Start Pkt",
  266. "Has Asset",
  267. "Decoded");
  268. foreach (J2KImage image in images)
  269. report.AppendFormat(
  270. "{0,36} {1,8} {2,10} {3,10} {4,9} {5,7}\n",
  271. image.TextureID, image.LastSequence, image.Priority, image.StartPacket, image.HasAsset, image.IsDecoded);
  272. }
  273. }
  274. return report.ToString();
  275. }
  276. /// <summary>
  277. /// Generate UDP Queue data report for each client
  278. /// </summary>
  279. /// <param name="showParams"></param>
  280. /// <returns></returns>
  281. protected string GetQueuesReport(string[] showParams)
  282. {
  283. bool showChildren = false;
  284. string pname = "";
  285. if (showParams.Length > 2 && showParams[2] == "full")
  286. showChildren = true;
  287. else if (showParams.Length > 3)
  288. pname = showParams[2] + " " + showParams[3];
  289. StringBuilder report = new StringBuilder();
  290. int columnPadding = 2;
  291. int maxNameLength = 18;
  292. int maxRegionNameLength = 14;
  293. int maxTypeLength = 4;
  294. int totalInfoFieldsLength
  295. = maxNameLength + columnPadding
  296. + maxRegionNameLength + columnPadding
  297. + maxTypeLength + columnPadding;
  298. report.Append(GetColumnEntry("User", maxNameLength, columnPadding));
  299. report.Append(GetColumnEntry("Region", maxRegionNameLength, columnPadding));
  300. report.Append(GetColumnEntry("Type", maxTypeLength, columnPadding));
  301. report.AppendFormat(
  302. "{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",
  303. "Since",
  304. "Pkts",
  305. "Pkts",
  306. "Pkts",
  307. "Bytes",
  308. "Q Pkts",
  309. "Q Pkts",
  310. "Q Pkts",
  311. "Q Pkts",
  312. "Q Pkts",
  313. "Q Pkts",
  314. "Q Pkts");
  315. report.AppendFormat("{0,-" + totalInfoFieldsLength + "}", "");
  316. report.AppendFormat(
  317. "{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",
  318. "Last In",
  319. "In",
  320. "Out",
  321. "Resent",
  322. "Unacked",
  323. "Resend",
  324. "Land",
  325. "Wind",
  326. "Cloud",
  327. "Task",
  328. "Texture",
  329. "Asset");
  330. lock (m_scenes)
  331. {
  332. foreach (Scene scene in m_scenes.Values)
  333. {
  334. scene.ForEachClient(
  335. delegate(IClientAPI client)
  336. {
  337. if (client is IStatsCollector)
  338. {
  339. bool isChild = client.SceneAgent.IsChildAgent;
  340. if (isChild && !showChildren)
  341. return;
  342. string name = client.Name;
  343. if (pname != "" && name != pname)
  344. return;
  345. string regionName = scene.RegionInfo.RegionName;
  346. report.Append(GetColumnEntry(name, maxNameLength, columnPadding));
  347. report.Append(GetColumnEntry(regionName, maxRegionNameLength, columnPadding));
  348. report.Append(GetColumnEntry(isChild ? "Cd" : "Rt", maxTypeLength, columnPadding));
  349. IStatsCollector stats = (IStatsCollector)client;
  350. report.AppendLine(stats.Report());
  351. }
  352. });
  353. }
  354. }
  355. return report.ToString();
  356. }
  357. /// <summary>
  358. /// Show throttle data
  359. /// </summary>
  360. /// <param name="showParams"></param>
  361. /// <returns></returns>
  362. protected string GetThrottlesReport(string[] showParams)
  363. {
  364. bool showChildren = false;
  365. string pname = "";
  366. if (showParams.Length > 2 && showParams[2] == "full")
  367. showChildren = true;
  368. else if (showParams.Length > 3)
  369. pname = showParams[2] + " " + showParams[3];
  370. StringBuilder report = new StringBuilder();
  371. int columnPadding = 2;
  372. int maxNameLength = 18;
  373. int maxRegionNameLength = 14;
  374. int maxTypeLength = 4;
  375. int totalInfoFieldsLength = maxNameLength + columnPadding + maxRegionNameLength + columnPadding + maxTypeLength + columnPadding;
  376. report.Append(GetColumnEntry("User", maxNameLength, columnPadding));
  377. report.Append(GetColumnEntry("Region", maxRegionNameLength, columnPadding));
  378. report.Append(GetColumnEntry("Type", maxTypeLength, columnPadding));
  379. report.AppendFormat(
  380. "{0,8} {1,8} {2,7} {3,8} {4,7} {5,7} {6,7} {7,7} {8,9} {9,7}\n",
  381. "Max",
  382. "Target",
  383. "Actual",
  384. "Resend",
  385. "Land",
  386. "Wind",
  387. "Cloud",
  388. "Task",
  389. "Texture",
  390. "Asset");
  391. report.AppendFormat("{0,-" + totalInfoFieldsLength + "}", "");
  392. report.AppendFormat(
  393. "{0,8} {1,8} {2,7} {3,8} {4,7} {5,7} {6,7} {7,7} {8,9} {9,7}\n",
  394. "kb/s",
  395. "kb/s",
  396. "kb/s",
  397. "kb/s",
  398. "kb/s",
  399. "kb/s",
  400. "kb/s",
  401. "kb/s",
  402. "kb/s",
  403. "kb/s");
  404. report.AppendLine();
  405. lock (m_scenes)
  406. {
  407. foreach (Scene scene in m_scenes.Values)
  408. {
  409. scene.ForEachClient(
  410. delegate(IClientAPI client)
  411. {
  412. if (client is LLClientView)
  413. {
  414. LLClientView llClient = client as LLClientView;
  415. bool isChild = client.SceneAgent.IsChildAgent;
  416. if (isChild && !showChildren)
  417. return;
  418. string name = client.Name;
  419. if (pname != "" && name != pname)
  420. return;
  421. string regionName = scene.RegionInfo.RegionName;
  422. LLUDPClient llUdpClient = llClient.UDPClient;
  423. ClientInfo ci = llUdpClient.GetClientInfo();
  424. report.Append(GetColumnEntry(name, maxNameLength, columnPadding));
  425. report.Append(GetColumnEntry(regionName, maxRegionNameLength, columnPadding));
  426. report.Append(GetColumnEntry(isChild ? "Cd" : "Rt", maxTypeLength, columnPadding));
  427. report.AppendFormat(
  428. "{0,8} {1,8} {2,7} {3,8} {4,7} {5,7} {6,7} {7,7} {8,9} {9,7}\n",
  429. ci.maxThrottle > 0 ? ((ci.maxThrottle * 8) / 1000).ToString() : "-",
  430. llUdpClient.FlowThrottle.AdaptiveEnabled
  431. ? ((ci.targetThrottle * 8) / 1000).ToString()
  432. : (llUdpClient.FlowThrottle.TotalDripRequest * 8 / 1000).ToString(),
  433. (ci.totalThrottle * 8) / 1000,
  434. (ci.resendThrottle * 8) / 1000,
  435. (ci.landThrottle * 8) / 1000,
  436. (ci.windThrottle * 8) / 1000,
  437. (ci.cloudThrottle * 8) / 1000,
  438. (ci.taskThrottle * 8) / 1000,
  439. (ci.textureThrottle * 8) / 1000,
  440. (ci.assetThrottle * 8) / 1000);
  441. }
  442. });
  443. }
  444. }
  445. return report.ToString();
  446. }
  447. private void PrintRequests(string type, Dictionary<string, int> sortedDict, int sum)
  448. {
  449. m_log.InfoFormat("[INFO]:");
  450. m_log.InfoFormat("[INFO]: {0,25}", type);
  451. foreach (KeyValuePair<string, int> kvp in sortedDict.Take(12))
  452. m_log.InfoFormat("[INFO]: {0,25} {1,-6}", kvp.Key, kvp.Value);
  453. m_log.InfoFormat("[INFO]: {0,25}", "...");
  454. m_log.InfoFormat("[INFO]: {0,25} {1,-6}", "Total", sum);
  455. }
  456. }
  457. }