예제 #1
0
 private string GetInstanceName(int processId)
 {
     instanceName = "";
     if (!string.IsNullOrEmpty(processName))
     {
         Process[] processes = Process.GetProcessesByName(processName);
         if (processes.Length > 0)
         {
             int i = 0;
             foreach (Process p in processes)
             {
                 instanceName = FormatInstanceName(p.ProcessName, i);
                 if (PerformanceCounterCategory.CounterExists("ID Process",
                                                              "Process"))
                 {
                     PerformanceCounter counter = new PerformanceCounter(
                         "Process", "ID Process", instanceName);
                     if (processId == counter.RawValue)
                     {
                         if (common.Debug)
                         {
                             common.LogMessage(instanceName +
                                               " : " + processId);
                         }
                         cpuCounter = new PerformanceCounter("Process",
                                                             "% Processor Time", instanceName);
                         break;
                     }
                     instanceName = "";
                 }
                 i++;
             }
         }
     }
     return(instanceName);
 }
예제 #2
0
        private void CheckNewWindows()
        {
            /*
             * Need this API, as the OnWindowCreate doesn't
             * get all the subwindow events (WindowOpenedEvent), let us add such window
             * with this API
             * */
            AutomationElementCollection c;
            InternalTreeWalker          w = new InternalTreeWalker();
            Condition condition           = new PropertyCondition(
                AutomationElement.ControlTypeProperty, ControlType.Window);
            AutomationElement element = w.walker.GetFirstChild(AutomationElement.RootElement);

            try
            {
                while (null != element)
                {
                    try
                    {
                        if (this.IndexOf(element) == -1)
                        {
                            // New parent window is available, add it to the list
                            this.Add(element);
                            common.LogMessage(element.Current.Name);
                        }
                    }
                    catch (System.UnauthorizedAccessException ex)
                    {
                        // https://bugzilla.gnome.org/show_bug.cgi?id=706992
                        // Cobra looses all objects after steps specified inside
                        common.LogMessage(ex);
                        common.Wait(1);
                        element = w.walker.GetFirstChild(AutomationElement.RootElement);
                        this.Clear();
                        continue;
                    }
                    c = element.FindAll(TreeScope.Subtree, condition);
                    foreach (AutomationElement e in c)
                    {
                        try // ".IndexOf" sometimes throws an exception on my system
                        {
                            if (this.IndexOf(e) == -1)
                            {
                                // New subwindow is available, add it to the list
                                this.Add(e);
                                common.LogMessage(e.Current.Name);
                            }
                        }
                        catch (System.UnauthorizedAccessException ex)
                        {
                            common.LogMessage(ex);
                            common.Wait(1);

                            // In case of an exception during IndexOf-call, the program adds the element as
                            // new element to the windowList. Same code like on other positions.
                            this.Add(e);
                        }
                    }
                    // Get next application in the list
                    element = w.walker.GetNextSibling(element);
                }
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
            }
            finally
            {
                c = null;
                w = null;
            }
        }
예제 #3
0
        static void SingleThreadExec()
        {
            bool debug = false;
            string ldtpDebugEnv = Environment.GetEnvironmentVariable("LDTP_DEBUG");
            string ldtpPort = Environment.GetEnvironmentVariable("LDTP_SERVER_PORT");
            string listenAllInterface = Environment.GetEnvironmentVariable(
                "LDTP_LISTEN_ALL_INTERFACE");
            if (!String.IsNullOrEmpty(ldtpDebugEnv))
                debug = true;
            if (String.IsNullOrEmpty(ldtpPort))
                ldtpPort = "4118";
            Common common = new Common(debug);
            /*
            // If planning to use Remoting instead of HTTP
            // use this commented portion of code
            // NOTE: To have this at work, you need to add your
            // app under Firewall
            IDictionary props = new Hashtable();
            props["name"] = "LdtpdService";
            props["port"] = 4118;
            HttpChannel channel = new HttpChannel(
               props,
               null,
               new XmlRpcServerFormatterSinkProvider()
            );
            ChannelServices.RegisterChannel(channel, false);

            RemotingConfiguration.RegisterWellKnownServiceType(
              typeof(LdtpdMain),
              "service.rem",
              WellKnownObjectMode.Singleton);
            Console.WriteLine("Press <ENTER> to shutdown");
            Console.ReadLine();
            /**/
            WindowList windowList = new WindowList(common);
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add("http://localhost:" + ldtpPort + "/");
            listener.Prefixes.Add("http://+:" + ldtpPort + "/");
            // Listen on all possible IP address
            if (String.IsNullOrEmpty(listenAllInterface))
            {
                if (debug)
                    Console.WriteLine("Listening on all interface");
                listener.Prefixes.Add("http://*:" + ldtpPort + "/");
            }
            else
            {
                // For Windows 8, still you need to add firewall rules
                // Refer: README.txt
                if (debug)
                    Console.WriteLine("Listening only on local interface");
            }
            listener.Start();
            XmlRpcListenerService svc = new Core(windowList, common, debug);
            try
            {
                while (true)
                {
                    GC.Collect();
                    try
                    {
                        if (debug)
                            Console.WriteLine("Waiting for clients");
                        HttpListenerContext context = listener.GetContext();
                        // Don't create LDTP instance here, this creates
                        // new object for every request !
                        // Moved before creating HttpListenerContext
                        //XmlRpcListenerService svc = new LdtpdMain();
                        if (debug)
                            Console.WriteLine("Processing request");
                        svc.ProcessRequest(context);
                        /*
                        // FIXME: If trying to do parallel process
                        // memory usage goes high and never comes back
                        // This is required for startprocessmonitor API
                        Thread parallelProcess = new Thread(delegate()
                        {
                            try
                            {
                                svc.ProcessRequest(context);
                            }
                            finally
                            {
                                context.Response.Close();
                                context = null;
                                GC.Collect();
                            }
                        });
                        parallelProcess.Start();
                        /* */
                        context.Response.Close();
                        context = null;
                    }
                    catch (InvalidOperationException ex)
                    {
                        common.LogMessage(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
            }
            finally
            {
                svc = null;
                windowList = null;
                listener.Stop();
            }
        }
예제 #4
0
 public void LogMessage(Object o)
 {
     common.LogMessage(o);
 }
예제 #5
0
        public long[] GetPhysicalMemoryUsage(string processName, bool monitor = false)
        {
            ArrayList memoryUsage = new ArrayList();

            try
            {
                processName = GetProcessName(processName);
                common.LogMessage(processName);
                Process[] ps = Process.GetProcessesByName(processName);
                foreach (Process p in ps)
                {
                    // Unable to get PrivateWorkingSet64 as displayed in
                    // Task Manager, so getting the closest one WorkingSet64
                    common.LogMessage("Memory used: {0}." +
                                      p.WorkingSet64 / (1024 * 1024));
                    if (monitor)
                    {
                        common.LogProcessStat(string.Format("MEMINFO-{0}({1}) - {2}",
                                                            p.ProcessName, p.Id, p.WorkingSet64 / (1024 * 1024)));
                    }
                    // Output in MB (Linux compatible output)
                    // Working memory will be in bytes, to convert it to MB
                    // divide it by 1024*1024
                    memoryUsage.Add((long)(p.WorkingSet64 / (1024 * 1024)));
                }
                if (common.Debug)
                {
                    foreach (long value in memoryUsage)
                    {
                        common.LogMessage(value);
                    }
                }
                return(memoryUsage.ToArray(typeof(long)) as long[]);
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
                return(memoryUsage.ToArray(typeof(long)) as long[]);
            }
            finally
            {
                memoryUsage = null;
            }
        }
예제 #6
0
        private void CleanUpWindowElements()
        {
            /*
             * Clean up handles that no longer exist
             * */
            List <AutomationElement> windowTmpList = new List <AutomationElement>();

            try
            {
                foreach (AutomationElement el in this)
                {
                    try
                    {
                        common.LogMessage(el.Current.Name);
                    }
                    catch (ElementNotAvailableException ex)
                    {
                        // Don't alter the current list, remove it later
                        windowTmpList.Add(el);
                        common.LogMessage(ex);
                    }
                    catch (Exception ex)
                    {
                        // Don't alter the current list, remove it later
                        windowTmpList.Add(el);
                        common.LogMessage(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                // Since window list is added / removed in different thread
                // values of windowList might be altered and an exception is thrown
                // Just handle the global exception
                common.LogMessage(ex);
            }
            try
            {
                foreach (AutomationElement el in windowTmpList)
                {
                    try
                    {
                        // Remove element from the list
                        this.Remove(el);
                    }
                    catch (Exception ex)
                    {
                        common.LogMessage(ex);
                    }
                }
            }
            catch (Exception ex)
            {
                common.LogMessage(ex);
            }
            windowTmpList = null;
            // With GC collect, noticed very less memory being used all the time
            GC.Collect();
        }