Пример #1
0
        private bool InstanceMatchesArgs(XMRInstance ins, string[] args, int indx)
        {
            bool hadSomethingToCompare = false;

            for (int i = indx; i < args.Length; i++)
            {
                if (args[i][0] != '-')
                {
                    hadSomethingToCompare = true;
                    if (ins.m_DescName.Contains(args[i]))
                    {
                        return(true);
                    }
                    if (ins.ItemID.ToString().Contains(args[i]))
                    {
                        return(true);
                    }
                    if (ins.AssetID.ToString().Contains(args[i]))
                    {
                        return(true);
                    }
                }
            }
            return(!hadSomethingToCompare);
        }
Пример #2
0
        /**
         * @brief Insert instance before another element in queue
         * @param inst  = instance to insert
         * @param after = element that is to come after one being inserted
         */
        public void InsertBefore(XMRInstance inst, XMRInstance after)
        {
            if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst))
            {
                throw new Exception("already in list");
            }

            if (after == null)
            {
                InsertTail(inst);
            }
            else
            {
                inst.m_NextInst = after;
                inst.m_PrevInst = after.m_PrevInst;
                if (inst.m_PrevInst == null)
                {
                    m_Head = inst;
                }
                else
                {
                    inst.m_PrevInst.m_NextInst = inst;
                }
                after.m_PrevInst = inst;
            }
        }
Пример #3
0
        /**
         * @brief A script instance was just removed from the Start or Yield Queue.
         *        So run it for a little bit then stick in whatever queue it should go in.
         */
        private void RunInstance(XMRInstance inst, int tid)
        {
            m_LastRanAt       = DateTime.UtcNow;
            m_ScriptExecTime -= (long)(m_LastRanAt - DateTime.MinValue).TotalMilliseconds;
            inst.m_IState     = XMRInstState.RUNNING;

            lock (m_WakeUpLock)
                m_RunningInstances[tid] = inst;

            XMRInstState newIState = inst.RunOne();

            lock (m_WakeUpLock)
                m_RunningInstances[tid] = null;

            HandleNewIState(inst, newIState);
            m_ScriptExecTime += (long)(DateTime.UtcNow - DateTime.MinValue).TotalMilliseconds;
        }
Пример #4
0
        /**
         * @brief Insert instance at tail of queue (behind all others)
         * @param inst = instance to insert
         */
        public void InsertTail(XMRInstance inst)
        {
            if ((inst.m_PrevInst != inst) || (inst.m_NextInst != inst))
            {
                throw new Exception("already in list");
            }

            inst.m_NextInst = null;
            if ((inst.m_PrevInst = m_Tail) == null)
            {
                m_Head = inst;
            }
            else
            {
                m_Tail.m_NextInst = inst;
            }

            m_Tail = inst;
        }
Пример #5
0
 private static int CompareInstancesByCPUTime(XMRInstance a, XMRInstance b)
 {
     if (a == null)
     {
         return((b == null) ? 0 : 1);
     }
     if (b == null)
     {
         return(-1);
     }
     if (b.m_CPUTime < a.m_CPUTime)
     {
         return(-1);
     }
     if (b.m_CPUTime > a.m_CPUTime)
     {
         return(1);
     }
     return(0);
 }
Пример #6
0
        /**
         * @brief Remove last element from queue, if any
         * @returns null if queue is empty
         *          else returns last element in queue and removes it
         */
        public XMRInstance RemoveTail()
        {
            XMRInstance inst = m_Tail;

            if (inst != null)
            {
                if ((m_Tail = inst.m_PrevInst) == null)
                {
                    m_Head = null;
                }
                else
                {
                    m_Tail.m_NextInst = null;
                }

                inst.m_NextInst = inst;
                inst.m_PrevInst = inst;
            }
            return(inst);
        }
Пример #7
0
        /**
         * @brief Remove arbitrary element from queue, if any
         * @param inst = element to remove (assumed to be in the queue)
         * @returns with element removed
         */
        public void Remove(XMRInstance inst)
        {
            XMRInstance next = inst.m_NextInst;
            XMRInstance prev = inst.m_PrevInst;

            if ((prev == inst) || (next == inst))
            {
                throw new Exception("not in a list");
            }

            if (next == null)
            {
                if (m_Tail != inst)
                {
                    throw new Exception("not in this list");
                }

                m_Tail = prev;
            }
            else
            {
                next.m_PrevInst = prev;
            }

            if (prev == null)
            {
                if (m_Head != inst)
                {
                    throw new Exception("not in this list");
                }

                m_Head = next;
            }
            else
            {
                prev.m_NextInst = next;
            }

            inst.m_NextInst = inst;
            inst.m_PrevInst = inst;
        }
Пример #8
0
 private void LsQueue(TextWriter outFile, string name, XMRInstQueue queue, string[] args, int indx)
 {
     outFile.WriteLine("Queue " + name + ":");
     lock (queue)
     {
         for (XMRInstance inst = queue.PeekHead(); inst != null; inst = inst.m_NextInst)
         {
             try
             {
                 // Try to print instance name.
                 if (InstanceMatchesArgs(inst, args, indx))
                 {
                     outFile.WriteLine("   " + inst.ItemID.ToString() + " " + inst.m_DescName);
                 }
             }
             catch (Exception e)
             {
                 // Sometimes there are instances in the queue that are disposed.
                 outFile.WriteLine("   " + inst.ItemID.ToString() + " " + inst.m_DescName + ": " + e.Message);
             }
         }
     }
 }
Пример #9
0
        private void XmrTestLs(string[] args, int indx)
        {
            bool   flagFull   = false;
            bool   flagQueues = false;
            bool   flagTopCPU = false;
            int    maxScripts = 0x7FFFFFFF;
            int    numScripts = 0;
            string outName    = null;

            XMRInstance[] instances;

            // Decode command line options.
            for (int i = indx; i < args.Length; i++)
            {
                if (args[i] == "-full")
                {
                    flagFull = true;
                    continue;
                }
                if (args[i] == "-help")
                {
                    m_log.Info("[YEngine]: yeng ls -full -max=<number> -out=<filename> -queues -topcpu");
                    return;
                }
                if (args[i].StartsWith("-max="))
                {
                    try
                    {
                        maxScripts = Convert.ToInt32(args[i].Substring(5));
                    }
                    catch (Exception e)
                    {
                        m_log.Error("[YEngine]: bad max " + args[i].Substring(5) + ": " + e.Message);
                        return;
                    }
                    continue;
                }
                if (args[i].StartsWith("-out="))
                {
                    outName = args[i].Substring(5);
                    continue;
                }
                if (args[i] == "-queues")
                {
                    flagQueues = true;
                    continue;
                }
                if (args[i] == "-topcpu")
                {
                    flagTopCPU = true;
                    continue;
                }
                if (args[i][0] == '-')
                {
                    m_log.Error("[YEngine]: unknown option " + args[i] + ", try 'yeng ls -help'");
                    return;
                }
            }

            TextWriter outFile = null;

            if (outName != null)
            {
                try
                {
                    outFile = File.CreateText(outName);
                }
                catch (Exception e)
                {
                    m_log.Error("[YEngine]: error creating " + outName + ": " + e.Message);
                    return;
                }
            }
            else
            {
                outFile = new LogInfoTextWriter(m_log);
            }

            try
            {
                // Scan instance list to find those that match selection criteria.
                if (!Monitor.TryEnter(m_InstancesDict, 100))
                {
                    m_log.Error("[YEngine]: deadlock m_LockedDict=" + m_LockedDict);
                    return;
                }
                try
                {
                    instances = new XMRInstance[m_InstancesDict.Count];
                    foreach (XMRInstance ins in m_InstancesDict.Values)
                    {
                        if (InstanceMatchesArgs(ins, args, indx))
                        {
                            instances[numScripts++] = ins;
                        }
                    }
                }
                finally
                {
                    Monitor.Exit(m_InstancesDict);
                }

                // Maybe sort by descending CPU time.
                if (flagTopCPU)
                {
                    Array.Sort <XMRInstance>(instances, CompareInstancesByCPUTime);
                }

                // Print the entries.
                if (!flagFull)
                {
                    outFile.WriteLine("                              ItemID" +
                                      "   CPU(ms)" +
                                      " NumEvents" +
                                      " Status    " +
                                      " World Position                  " +
                                      " <Part>:<Item>");
                }
                for (int i = 0; (i < numScripts) && (i < maxScripts); i++)
                {
                    outFile.WriteLine(instances[i].RunTestLs(flagFull));
                }

                // Print number of scripts that match selection criteria,
                // even if we were told to print fewer.
                outFile.WriteLine("total of {0} script(s)", numScripts);

                // If -queues given, print out queue contents too.
                if (flagQueues)
                {
                    LsQueue(outFile, "start", m_StartQueue, args, indx);
                    LsQueue(outFile, "sleep", m_SleepQueue, args, indx);
                    LsQueue(outFile, "yield", m_YieldQueue, args, indx);
                }
            }
            finally
            {
                outFile.Close();
            }
        }
Пример #10
0
        private void XmrTestReset(string[] args, int indx)
        {
            bool flagAll    = false;
            int  numScripts = 0;

            XMRInstance[] instances;

            if (args.Length <= indx)
            {
                m_log.Error("[YEngine]: must specify part of script name or -all for all scripts");
                return;
            }

            // Decode command line options.
            for (int i = indx; i < args.Length; i++)
            {
                if (args[i] == "-all")
                {
                    flagAll = true;
                    continue;
                }
                if (args[i] == "-help")
                {
                    m_log.Info("[YEngine]: yeng reset -all | <part-of-script-name>");
                    return;
                }
                if (args[i][0] == '-')
                {
                    m_log.Error("[YEngine]: unknown option " + args[i] + ", try 'yeng reset -help'");
                    return;
                }
            }

            // Scan instance list to find those that match selection criteria.
            if (!Monitor.TryEnter(m_InstancesDict, 100))
            {
                m_log.Error("[YEngine]: deadlock m_LockedDict=" + m_LockedDict);
                return;
            }

            try
            {
                instances = new XMRInstance[m_InstancesDict.Count];
                foreach (XMRInstance ins in m_InstancesDict.Values)
                {
                    if (flagAll || InstanceMatchesArgs(ins, args, indx))
                    {
                        instances[numScripts++] = ins;
                    }
                }
            }
            finally
            {
                Monitor.Exit(m_InstancesDict);
            }

            // Reset the instances as if someone clicked their "Reset" button.
            for (int i = 0; i < numScripts; i++)
            {
                XMRInstance inst = instances[i];
                m_log.Info("[YEngine]: resetting " + inst.m_DescName);
                inst.Reset();
            }
        }
Пример #11
0
        private void XmrTestPev(string[] args, int indx)
        {
            bool flagAll    = false;
            int  numScripts = 0;

            XMRInstance[] instances;

            // Decode command line options.
            int           i, j;
            List <string> selargs = new List <string>(args.Length);

            MethodInfo[] eventmethods = typeof(IEventHandlers).GetMethods();
            MethodInfo   eventmethod;

            for (i = indx; i < args.Length; i++)
            {
                string arg = args[i];
                if (arg == "-all")
                {
                    flagAll = true;
                    continue;
                }
                if (arg == "-help")
                {
                    m_log.Info("[YEngine]: yeng pev -all | <part-of-script-name> <event-name> <params...>");
                    return;
                }
                if (arg[0] == '-')
                {
                    m_log.Error("[YEngine]: unknown option " + arg + ", try 'yeng pev -help'");
                    return;
                }
                for (j = 0; j < eventmethods.Length; j++)
                {
                    eventmethod = eventmethods[j];
                    if (eventmethod.Name == arg)
                    {
                        goto gotevent;
                    }
                }
                selargs.Add(arg);
            }
            m_log.Error("[YEngine]: missing <event-name> <params...>, try 'yeng pev -help'");
            return;

gotevent:
            string eventname = eventmethod.Name;
            StringBuilder sourcesb = new StringBuilder();

            while (++i < args.Length)
            {
                sourcesb.Append(' ');
                sourcesb.Append(args[i]);
            }
            string sourcest = sourcesb.ToString();
            string sourcehash;

            youveanerror = false;
            Token t = TokenBegin.Construct("", null, ErrorMsg, sourcest, out sourcehash);

            if (youveanerror)
            {
                return;
            }
            ParameterInfo[] paraminfos  = eventmethod.GetParameters();
            object[]        paramvalues = new object[paraminfos.Length];
            i = 0;
            while (!((t = t.nextToken) is TokenEnd))
            {
                if (i >= paramvalues.Length)
                {
                    ErrorMsg(t, "extra parameter(s)");
                    return;
                }
                paramvalues[i] = ParseParamValue(ref t);
                if (paramvalues[i] == null)
                {
                    return;
                }
                i++;
            }
            ScriptEngine.Shared.EventParams eps =
                new ScriptEngine.Shared.EventParams(eventname, paramvalues, zeroDetectParams);

            // Scan instance list to find those that match selection criteria.
            if (!Monitor.TryEnter(m_InstancesDict, 100))
            {
                m_log.Error("[YEngine]: deadlock m_LockedDict=" + m_LockedDict);
                return;
            }

            try
            {
                instances = new XMRInstance[m_InstancesDict.Count];
                foreach (XMRInstance ins in m_InstancesDict.Values)
                {
                    if (flagAll || InstanceMatchesArgs(ins, selargs.ToArray(), 0))
                    {
                        instances[numScripts++] = ins;
                    }
                }
            }
            finally
            {
                Monitor.Exit(m_InstancesDict);
            }

            // Post event to the matching instances.
            for (i = 0; i < numScripts; i++)
            {
                XMRInstance inst = instances[i];
                m_log.Info("[YEngine]: post " + eventname + " to " + inst.m_DescName);
                inst.PostEvent(eps);
            }
        }
Пример #12
0
 public void InitXMRLSLApi(XMRInstance i)
 {
     acm  = AsyncCommands;
     inst = i;
 }