1
0

XMREngXmrTestLs.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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 log4net;
  28. using System;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Reflection;
  32. using System.Text;
  33. using System.Threading;
  34. using LSL_Float = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLFloat;
  35. using LSL_Integer = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLInteger;
  36. using LSL_Key = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  37. using LSL_List = OpenSim.Region.ScriptEngine.Shared.LSL_Types.list;
  38. using LSL_Rotation = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Quaternion;
  39. using LSL_String = OpenSim.Region.ScriptEngine.Shared.LSL_Types.LSLString;
  40. using LSL_Vector = OpenSim.Region.ScriptEngine.Shared.LSL_Types.Vector3;
  41. namespace OpenSim.Region.ScriptEngine.Yengine
  42. {
  43. public partial class Yengine
  44. {
  45. private void XmrTestLs(string[] args, int indx)
  46. {
  47. bool flagFull = false;
  48. bool flagQueues = false;
  49. bool flagTopCPU = false;
  50. int maxScripts = 0x7FFFFFFF;
  51. int numScripts = 0;
  52. string outName = null;
  53. XMRInstance[] instances;
  54. // Decode command line options.
  55. for(int i = indx; i < args.Length; i++)
  56. {
  57. if(args[i] == "-full")
  58. {
  59. flagFull = true;
  60. continue;
  61. }
  62. if(args[i] == "-help")
  63. {
  64. m_log.Info("[YEngine]: yeng ls -full -max=<number> -out=<filename> -queues -topcpu");
  65. return;
  66. }
  67. if(args[i].StartsWith("-max="))
  68. {
  69. try
  70. {
  71. maxScripts = Convert.ToInt32(args[i].Substring(5));
  72. }
  73. catch(Exception e)
  74. {
  75. m_log.Error("[YEngine]: bad max " + args[i].Substring(5) + ": " + e.Message);
  76. return;
  77. }
  78. continue;
  79. }
  80. if(args[i].StartsWith("-out="))
  81. {
  82. outName = args[i].Substring(5);
  83. continue;
  84. }
  85. if(args[i] == "-queues")
  86. {
  87. flagQueues = true;
  88. continue;
  89. }
  90. if(args[i] == "-topcpu")
  91. {
  92. flagTopCPU = true;
  93. continue;
  94. }
  95. if(args[i][0] == '-')
  96. {
  97. m_log.Error("[YEngine]: unknown option " + args[i] + ", try 'yeng ls -help'");
  98. return;
  99. }
  100. }
  101. TextWriter outFile = null;
  102. if(outName != null)
  103. {
  104. try
  105. {
  106. outFile = File.CreateText(outName);
  107. }
  108. catch(Exception e)
  109. {
  110. m_log.Error("[YEngine]: error creating " + outName + ": " + e.Message);
  111. return;
  112. }
  113. }
  114. else
  115. {
  116. outFile = new LogInfoTextWriter(m_log);
  117. }
  118. try
  119. {
  120. // Scan instance list to find those that match selection criteria.
  121. if(!Monitor.TryEnter(m_InstancesDict, 100))
  122. {
  123. m_log.Error("[YEngine]: deadlock m_LockedDict=" + m_LockedDict);
  124. return;
  125. }
  126. try
  127. {
  128. instances = new XMRInstance[m_InstancesDict.Count];
  129. foreach(XMRInstance ins in m_InstancesDict.Values)
  130. {
  131. if(InstanceMatchesArgs(ins, args, indx))
  132. {
  133. instances[numScripts++] = ins;
  134. }
  135. }
  136. }
  137. finally
  138. {
  139. Monitor.Exit(m_InstancesDict);
  140. }
  141. // Maybe sort by descending CPU time.
  142. if(flagTopCPU)
  143. {
  144. Array.Sort<XMRInstance>(instances, CompareInstancesByCPUTime);
  145. }
  146. // Print the entries.
  147. if(!flagFull)
  148. {
  149. outFile.WriteLine(" ItemID" +
  150. " CPU(ms)" +
  151. " NumEvents" +
  152. " Status " +
  153. " World Position " +
  154. " <Part>:<Item>");
  155. }
  156. for(int i = 0; (i < numScripts) && (i < maxScripts); i++)
  157. {
  158. outFile.WriteLine(instances[i].RunTestLs(flagFull));
  159. }
  160. // Print number of scripts that match selection criteria,
  161. // even if we were told to print fewer.
  162. outFile.WriteLine("total of {0} script(s)", numScripts);
  163. // If -queues given, print out queue contents too.
  164. if(flagQueues)
  165. {
  166. LsQueue(outFile, "start", m_StartQueue, args, indx);
  167. LsQueue(outFile, "sleep", m_SleepQueue, args, indx);
  168. LsQueue(outFile, "yield", m_YieldQueue, args, indx);
  169. }
  170. }
  171. finally
  172. {
  173. outFile.Close();
  174. }
  175. }
  176. private void XmrTestPev(string[] args, int indx)
  177. {
  178. bool flagAll = false;
  179. int numScripts = 0;
  180. XMRInstance[] instances;
  181. // Decode command line options.
  182. int i, j;
  183. List<string> selargs = new List<string>(args.Length);
  184. MethodInfo[] eventmethods = typeof(IEventHandlers).GetMethods();
  185. MethodInfo eventmethod;
  186. for(i = indx; i < args.Length; i++)
  187. {
  188. string arg = args[i];
  189. if(arg == "-all")
  190. {
  191. flagAll = true;
  192. continue;
  193. }
  194. if(arg == "-help")
  195. {
  196. m_log.Info("[YEngine]: yeng pev -all | <part-of-script-name> <event-name> <params...>");
  197. return;
  198. }
  199. if(arg[0] == '-')
  200. {
  201. m_log.Error("[YEngine]: unknown option " + arg + ", try 'yeng pev -help'");
  202. return;
  203. }
  204. for(j = 0; j < eventmethods.Length; j++)
  205. {
  206. eventmethod = eventmethods[j];
  207. if(eventmethod.Name == arg)
  208. goto gotevent;
  209. }
  210. selargs.Add(arg);
  211. }
  212. m_log.Error("[YEngine]: missing <event-name> <params...>, try 'yeng pev -help'");
  213. return;
  214. gotevent:
  215. string eventname = eventmethod.Name;
  216. StringBuilder sourcesb = new StringBuilder();
  217. while(++i < args.Length)
  218. {
  219. sourcesb.Append(' ');
  220. sourcesb.Append(args[i]);
  221. }
  222. string sourcest = sourcesb.ToString();
  223. string sourcehash;
  224. youveanerror = false;
  225. Token t = TokenBegin.Construct("", null, ErrorMsg, sourcest, out sourcehash);
  226. if(youveanerror)
  227. return;
  228. ParameterInfo[] paraminfos = eventmethod.GetParameters();
  229. object[] paramvalues = new object[paraminfos.Length];
  230. i = 0;
  231. while(!((t = t.nextToken) is TokenEnd))
  232. {
  233. if(i >= paramvalues.Length)
  234. {
  235. ErrorMsg(t, "extra parameter(s)");
  236. return;
  237. }
  238. paramvalues[i] = ParseParamValue(ref t);
  239. if(paramvalues[i] == null)
  240. return;
  241. i++;
  242. }
  243. OpenSim.Region.ScriptEngine.Shared.EventParams eps =
  244. new OpenSim.Region.ScriptEngine.Shared.EventParams(eventname, paramvalues, zeroDetectParams);
  245. // Scan instance list to find those that match selection criteria.
  246. if(!Monitor.TryEnter(m_InstancesDict, 100))
  247. {
  248. m_log.Error("[YEngine]: deadlock m_LockedDict=" + m_LockedDict);
  249. return;
  250. }
  251. try
  252. {
  253. instances = new XMRInstance[m_InstancesDict.Count];
  254. foreach(XMRInstance ins in m_InstancesDict.Values)
  255. {
  256. if(flagAll || InstanceMatchesArgs(ins, selargs.ToArray(), 0))
  257. {
  258. instances[numScripts++] = ins;
  259. }
  260. }
  261. }
  262. finally
  263. {
  264. Monitor.Exit(m_InstancesDict);
  265. }
  266. // Post event to the matching instances.
  267. for(i = 0; i < numScripts; i++)
  268. {
  269. XMRInstance inst = instances[i];
  270. m_log.Info("[YEngine]: post " + eventname + " to " + inst.m_DescName);
  271. inst.PostEvent(eps);
  272. }
  273. }
  274. private object ParseParamValue(ref Token token)
  275. {
  276. if(token is TokenFloat)
  277. {
  278. return new LSL_Float(((TokenFloat)token).val);
  279. }
  280. if(token is TokenInt)
  281. {
  282. return new LSL_Integer(((TokenInt)token).val);
  283. }
  284. if(token is TokenStr)
  285. {
  286. return new LSL_String(((TokenStr)token).val);
  287. }
  288. if(token is TokenKwCmpLT)
  289. {
  290. List<double> valuelist = new List<double>();
  291. while(!((token = token.nextToken) is TokenKwCmpGT))
  292. {
  293. if(!(token is TokenKwComma))
  294. {
  295. object value = ParseParamValue(ref token);
  296. if(value == null)
  297. return null;
  298. if(value is int)
  299. value = (double)(int)value;
  300. if(!(value is double))
  301. {
  302. ErrorMsg(token, "must be float or integer constant");
  303. return null;
  304. }
  305. valuelist.Add((double)value);
  306. }
  307. else if(token.prevToken is TokenKwComma)
  308. {
  309. ErrorMsg(token, "missing constant");
  310. return null;
  311. }
  312. }
  313. double[] values = valuelist.ToArray();
  314. switch(values.Length)
  315. {
  316. case 3:
  317. {
  318. return new LSL_Vector(values[0], values[1], values[2]);
  319. }
  320. case 4:
  321. {
  322. return new LSL_Rotation(values[0], values[1], values[2], values[3]);
  323. }
  324. default:
  325. {
  326. ErrorMsg(token, "not rotation or vector");
  327. return null;
  328. }
  329. }
  330. }
  331. if(token is TokenKwBrkOpen)
  332. {
  333. List<object> valuelist = new List<object>();
  334. while(!((token = token.nextToken) is TokenKwBrkClose))
  335. {
  336. if(!(token is TokenKwComma))
  337. {
  338. object value = ParseParamValue(ref token);
  339. if(value == null)
  340. return null;
  341. valuelist.Add(value);
  342. }
  343. else if(token.prevToken is TokenKwComma)
  344. {
  345. ErrorMsg(token, "missing constant");
  346. return null;
  347. }
  348. }
  349. return new LSL_List(valuelist.ToArray());
  350. }
  351. if(token is TokenName)
  352. {
  353. FieldInfo field = typeof(OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass).GetField(((TokenName)token).val);
  354. if((field != null) && field.IsPublic && (field.IsLiteral || (field.IsStatic && field.IsInitOnly)))
  355. {
  356. return field.GetValue(null);
  357. }
  358. }
  359. ErrorMsg(token, "invalid constant");
  360. return null;
  361. }
  362. private bool youveanerror;
  363. private void ErrorMsg(Token token, string message)
  364. {
  365. youveanerror = true;
  366. m_log.Info("[YEngine]: " + token.posn + " " + message);
  367. }
  368. private void XmrTestReset(string[] args, int indx)
  369. {
  370. bool flagAll = false;
  371. int numScripts = 0;
  372. XMRInstance[] instances;
  373. if(args.Length <= indx)
  374. {
  375. m_log.Error("[YEngine]: must specify part of script name or -all for all scripts");
  376. return;
  377. }
  378. // Decode command line options.
  379. for(int i = indx; i < args.Length; i++)
  380. {
  381. if(args[i] == "-all")
  382. {
  383. flagAll = true;
  384. continue;
  385. }
  386. if(args[i] == "-help")
  387. {
  388. m_log.Info("[YEngine]: yeng reset -all | <part-of-script-name>");
  389. return;
  390. }
  391. if(args[i][0] == '-')
  392. {
  393. m_log.Error("[YEngine]: unknown option " + args[i] + ", try 'yeng reset -help'");
  394. return;
  395. }
  396. }
  397. // Scan instance list to find those that match selection criteria.
  398. if(!Monitor.TryEnter(m_InstancesDict, 100))
  399. {
  400. m_log.Error("[YEngine]: deadlock m_LockedDict=" + m_LockedDict);
  401. return;
  402. }
  403. try
  404. {
  405. instances = new XMRInstance[m_InstancesDict.Count];
  406. foreach(XMRInstance ins in m_InstancesDict.Values)
  407. {
  408. if(flagAll || InstanceMatchesArgs(ins, args, indx))
  409. {
  410. instances[numScripts++] = ins;
  411. }
  412. }
  413. }
  414. finally
  415. {
  416. Monitor.Exit(m_InstancesDict);
  417. }
  418. // Reset the instances as if someone clicked their "Reset" button.
  419. for(int i = 0; i < numScripts; i++)
  420. {
  421. XMRInstance inst = instances[i];
  422. m_log.Info("[YEngine]: resetting " + inst.m_DescName);
  423. inst.Reset();
  424. }
  425. }
  426. private static int CompareInstancesByCPUTime(XMRInstance a, XMRInstance b)
  427. {
  428. if(a == null)
  429. {
  430. return (b == null) ? 0 : 1;
  431. }
  432. if(b == null)
  433. {
  434. return -1;
  435. }
  436. if(b.m_CPUTime < a.m_CPUTime)
  437. return -1;
  438. if(b.m_CPUTime > a.m_CPUTime)
  439. return 1;
  440. return 0;
  441. }
  442. private void LsQueue(TextWriter outFile, string name, XMRInstQueue queue, string[] args, int indx)
  443. {
  444. outFile.WriteLine("Queue " + name + ":");
  445. lock(queue)
  446. {
  447. for(XMRInstance inst = queue.PeekHead(); inst != null; inst = inst.m_NextInst)
  448. {
  449. try
  450. {
  451. // Try to print instance name.
  452. if(InstanceMatchesArgs(inst, args, indx))
  453. {
  454. outFile.WriteLine(" " + inst.ItemID.ToString() + " " + inst.m_DescName);
  455. }
  456. }
  457. catch(Exception e)
  458. {
  459. // Sometimes there are instances in the queue that are disposed.
  460. outFile.WriteLine(" " + inst.ItemID.ToString() + " " + inst.m_DescName + ": " + e.Message);
  461. }
  462. }
  463. }
  464. }
  465. private bool InstanceMatchesArgs(XMRInstance ins, string[] args, int indx)
  466. {
  467. bool hadSomethingToCompare = false;
  468. for(int i = indx; i < args.Length; i++)
  469. {
  470. if(args[i][0] != '-')
  471. {
  472. hadSomethingToCompare = true;
  473. if(ins.m_DescName.Contains(args[i]))
  474. return true;
  475. if(ins.ItemID.ToString().Contains(args[i]))
  476. return true;
  477. if(ins.AssetID.ToString().Contains(args[i]))
  478. return true;
  479. }
  480. }
  481. return !hadSomethingToCompare;
  482. }
  483. }
  484. /**
  485. * @brief Make m_log.Info look like a text writer.
  486. */
  487. public class LogInfoTextWriter: TextWriter
  488. {
  489. private StringBuilder sb = new StringBuilder();
  490. private ILog m_log;
  491. public LogInfoTextWriter(ILog m_log)
  492. {
  493. this.m_log = m_log;
  494. }
  495. public override void Write(char c)
  496. {
  497. if(c == '\n')
  498. {
  499. m_log.Info("[YEngine]: " + sb.ToString());
  500. sb.Remove(0, sb.Length);
  501. }
  502. else
  503. {
  504. sb.Append(c);
  505. }
  506. }
  507. public override void Close()
  508. {
  509. }
  510. public override Encoding Encoding
  511. {
  512. get
  513. {
  514. return Encoding.UTF8;
  515. }
  516. }
  517. }
  518. }