Esempio n. 1
0
        private Dictionary <int, PerfProcess> GetProcessList()
        {
            Dictionary <int, PerfProcess> procList1 = new Dictionary <int, PerfProcess>();

            Process[] plist1 = Process.GetProcesses();
            foreach (Process p in plist1)
            {
                if (_policy.IncludeProcesses.Count > 0)
                {
                    if (p.Id == 0 || _policy.IgnoreProcesses.Contains(p.ProcessName.ToLower()) || !_policy.IncludeProcesses.Contains(p.ProcessName.ToLower()))
                    {
                        continue;
                    }
                }
                else
                {
                    if (p.Id == 0 || _policy.IgnoreProcesses.Contains(p.ProcessName.ToLower()))
                    {
                        continue;
                    }
                }

                try
                {
                    PerfProcess proc = new PerfProcess(p.Id, p.ProcessName, p.TotalProcessorTime.TotalMilliseconds, p.BasePriority);
                    procList1.Add(proc.Id, proc);
                }
                catch
                {
                    continue;
                }
            }
            return(procList1);
        }
        private void RunLoop()
        {
            _run = true;
            _isRunning = true;

            Dictionary<int, PerfProcess> procList1 = GetProcessList();

            Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();

            while (_run)
            {

                Thread.Sleep(_policy.WmiWatcherInterval); // 1 second wait

                StringBuilder sb = new StringBuilder();

                Dictionary<int, PerfProcess> procList2 = new Dictionary<int, PerfProcess>();
                Process[] plist2 = Process.GetProcesses();
                foreach (Process p2 in plist2)
                {
                    if (p2.Id == 0 || _ignoredProcessNames.Contains(p2.ProcessName.ToLower()))
                        continue;

                    try
                    {
                        PerfProcess proc = new PerfProcess(p2.Id, p2.ProcessName, p2.TotalProcessorTime.TotalMilliseconds, p2.BasePriority);
                        procList2.Add(proc.Id, proc);

                    }
                    catch {
                        continue;
                    }

                    double tpt1 = 0;

                    PerfProcess proc1 = null;
                    bool foundProc1 = procList1.TryGetValue(p2.Id, out proc1);

                    if (foundProc1 && proc1.Name == p2.ProcessName)
                    {
                        tpt1 = proc1.TotalProcessorTimeInMilliseconds;
                    }

                    double processUsedProcessorTimeInMilliseconds = p2.TotalProcessorTime.TotalMilliseconds - tpt1;
                    double elapsedTime = stopwatch.Elapsed.TotalMilliseconds;
                    double processUsedProcessorPercent = ((100 * processUsedProcessorTimeInMilliseconds) / elapsedTime) / _numberOfProcessors;

                    #region Begin Management Action

                    lock (((System.Collections.ICollection)_managedProcesses).SyncRoot)
                    {
                        if (processUsedProcessorPercent >= _policy.GovernorProcessorPercent)
                        {
                            PerfManagedProcess mp;
                            if (_managedProcesses.TryGetValue(p2.Id, out mp) == false)
                            {
                                mp = new PerfManagedProcess();
                                mp.GovernorTimeInterval = 0;
                                mp.RelaxedTimeInterval = 0;
                                mp.PriorityOriginal = p2.BasePriority;
                                mp.Name = p2.ProcessName;

                                _managedProcesses.Add(p2.Id, mp);
                            }
                            else
                            {
                                if (mp.Name != p2.ProcessName)
                                {
                                    mp.Name = p2.ProcessName;
                                    mp.GovernorTimeInterval = 0;
                                    mp.RelaxedTimeInterval = 0;
                                    mp.PriorityOriginal = p2.BasePriority;
                                }
                            }

                            mp.Priority = p2.BasePriority;

                            // processor auslastung ist größer als erlaubt

                            if (mp.GovernorTimeInterval >= (_policy.GovernorTimeInterval))
                            {
                                // processor auslastung ist länger als erlaubt
                                // Management Action
                                DecrementProcessPriority(p2);

                                mp.GovernorTimeInterval = 0;

                            }
                            else
                            {
                                mp.GovernorTimeInterval += elapsedTime;
                            }
                            mp.RelaxedTimeInterval = 0;

                        }
                        else
                        {

                            PerfManagedProcess mp;
                            if (_managedProcesses.TryGetValue(p2.Id, out mp))
                            {
                                mp.Priority = p2.BasePriority;

                                if (mp.PriorityOriginal > p2.BasePriority)
                                {
                                    if (processUsedProcessorPercent <= _policy.RelaxProcessorPercent)
                                    {
                                        mp.RelaxedTimeInterval += elapsedTime;

                                        if (mp.RelaxedTimeInterval >= (_policy.RelaxTimeInterval))
                                        {
                                            // Priority erhöhen
                                            IncrementProcessPriority(p2);

                                            mp.RelaxedTimeInterval = 0;
                                        }
                                    }
                                    else
                                    {
                                        mp.RelaxedTimeInterval = 0;
                                    }
                                    mp.GovernorTimeInterval = 0;
                                }
                                else
                                {

                                    _managedProcesses.Remove(p2.Id);

                                }
                            }

                        }

                    }
                    #endregion

                }

                stopwatch.Reset();
                stopwatch.Start();

                procList1 = procList2;
            }

            _isRunning = false;
        }
        private Dictionary<int, PerfProcess> GetProcessList()
        {
            Dictionary<int, PerfProcess> procList1 = new Dictionary<int, PerfProcess>();

            Process[] plist1 = Process.GetProcesses();
            foreach (Process p in plist1)
            {
                if (p.Id == 0 || _ignoredProcessNames.Contains(p.ProcessName))
                    continue;

                try
                {
                    PerfProcess proc = new PerfProcess(p.Id, p.ProcessName, p.TotalProcessorTime.TotalMilliseconds, p.BasePriority);
                    procList1.Add(proc.Id, proc);
                }
                catch
                {
                    continue;
                }
            }
            return procList1;
        }
Esempio n. 4
0
        private void RunLoop()
        {
            _run       = true;
            _isRunning = true;


            Dictionary <int, PerfProcess> procList1 = GetProcessList();


            Stopwatch stopwatch = System.Diagnostics.Stopwatch.StartNew();


            while (_run)
            {
                Thread.Sleep(_policy.WmiWatcherInterval); // 1 second wait

                StringBuilder sb = new StringBuilder();

                Dictionary <int, PerfProcess> procList2 = new Dictionary <int, PerfProcess>();
                Process[] plist2 = Process.GetProcesses();
                foreach (Process p2 in plist2)
                {
                    if (_policy.IncludeProcesses.Count > 0)
                    {
                        if (p2.Id == 0 || _policy.IgnoreProcesses.Contains(p2.ProcessName.ToLower()) || !_policy.IncludeProcesses.Contains(p2.ProcessName.ToLower()))
                        {
                            continue;
                        }
                    }
                    else
                    {
                        if (p2.Id == 0 || _policy.IgnoreProcesses.Contains(p2.ProcessName.ToLower()))
                        {
                            continue;
                        }
                    }

                    try
                    {
                        PerfProcess proc = new PerfProcess(p2.Id, p2.ProcessName, p2.TotalProcessorTime.TotalMilliseconds, p2.BasePriority);
                        procList2.Add(proc.Id, proc);
                    }
                    catch
                    {
                        continue;
                    }

                    double tpt1 = 0;

                    PerfProcess proc1      = null;
                    bool        foundProc1 = procList1.TryGetValue(p2.Id, out proc1);

                    if (foundProc1 && proc1.Name == p2.ProcessName)
                    {
                        tpt1 = proc1.TotalProcessorTimeInMilliseconds;
                    }


                    double processUsedProcessorTimeInMilliseconds = p2.TotalProcessorTime.TotalMilliseconds - tpt1;
                    double elapsedTime = stopwatch.Elapsed.TotalMilliseconds;
                    double processUsedProcessorPercent = ((100 * processUsedProcessorTimeInMilliseconds) / elapsedTime) / _numberOfProcessors;



                    #region Begin Management Action

                    lock (((System.Collections.ICollection)_managedProcesses).SyncRoot)
                    {
                        if (processUsedProcessorPercent >= _policy.GovernorProcessorPercent)
                        {
                            PerfManagedProcess mp;
                            if (_managedProcesses.TryGetValue(p2.Id, out mp) == false)
                            {
                                mp = new PerfManagedProcess();
                                mp.GovernorTimeInterval = 0;
                                mp.RelaxedTimeInterval  = 0;
                                mp.PriorityOriginal     = p2.BasePriority;
                                mp.Name = p2.ProcessName;

                                _managedProcesses.Add(p2.Id, mp);
                            }
                            else
                            {
                                if (mp.Name != p2.ProcessName)
                                {
                                    mp.Name = p2.ProcessName;
                                    mp.GovernorTimeInterval = 0;
                                    mp.RelaxedTimeInterval  = 0;
                                    mp.PriorityOriginal     = p2.BasePriority;
                                }
                            }


                            mp.Priority = p2.BasePriority;

                            // processor auslastung ist größer als erlaubt

                            if (mp.GovernorTimeInterval >= (_policy.GovernorTimeInterval))
                            {
                                // processor auslastung ist länger als erlaubt
                                // Management Action
                                DecrementProcessPriority(p2);

                                mp.GovernorTimeInterval = 0;
                            }
                            else
                            {
                                mp.GovernorTimeInterval += elapsedTime;
                            }
                            mp.RelaxedTimeInterval = 0;
                        }
                        else
                        {
                            PerfManagedProcess mp;
                            if (_managedProcesses.TryGetValue(p2.Id, out mp))
                            {
                                mp.Priority = p2.BasePriority;

                                if (mp.PriorityOriginal > p2.BasePriority)
                                {
                                    if (processUsedProcessorPercent <= _policy.RelaxProcessorPercent)
                                    {
                                        mp.RelaxedTimeInterval += elapsedTime;

                                        if (mp.RelaxedTimeInterval >= (_policy.RelaxTimeInterval))
                                        {
                                            // Priority erhöhen
                                            IncrementProcessPriority(p2);

                                            mp.RelaxedTimeInterval = 0;
                                        }
                                    }
                                    else
                                    {
                                        mp.RelaxedTimeInterval = 0;
                                    }
                                    mp.GovernorTimeInterval = 0;
                                }
                                else
                                {
                                    _managedProcesses.Remove(p2.Id);
                                }
                            }
                        }
                    }
                    #endregion
                }


                stopwatch.Reset();
                stopwatch.Start();

                procList1 = procList2;
            }

            _isRunning = false;
        }