LindenUDPInfoModule.cs 25 KB

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