|
@@ -53,8 +53,6 @@ namespace EfemDualRT.Resources
|
|
|
private PerformanceCounter _transferDisk;
|
|
|
private PeriodicJob _thread;
|
|
|
private bool _isTerminate;
|
|
|
- private Queue<PerformanceCounter> _pcCounterPool;
|
|
|
- private Dictionary<string, PerformanceCounter> _curMonitorPCCounter = new Dictionary<string, PerformanceCounter>();
|
|
|
public ResourceMonitor()
|
|
|
{
|
|
|
|
|
@@ -97,11 +95,6 @@ namespace EfemDualRT.Resources
|
|
|
_writeDisk = new PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Write", "_Total");
|
|
|
_transferDisk = new PerformanceCounter("PhysicalDisk", "Avg. Disk sec/Transfer", "_Total");
|
|
|
CpuOccupied = new PerformanceCounter("Processor", "% Processor Time", "_Total");
|
|
|
- _pcCounterPool = new Queue<PerformanceCounter>(Process.GetProcesses().Length * 3);
|
|
|
- for(int i=0; i < Process.GetProcesses().Length * 3; i++)
|
|
|
- {
|
|
|
- _pcCounterPool.Enqueue(new PerformanceCounter("Process", "% Processor Time"));
|
|
|
- }
|
|
|
_thread = new PeriodicJob(checkInterval, GetProcessInfo, "ResourceMonitor", true);
|
|
|
}
|
|
|
return true;
|
|
@@ -120,7 +113,6 @@ namespace EfemDualRT.Resources
|
|
|
public void InitNetAdapter()
|
|
|
{
|
|
|
_netAdapterNames.Clear();
|
|
|
- _curMonitorPCCounter.Clear();
|
|
|
NetworkInterface.GetAllNetworkInterfaces().ToList().ForEach(p => {
|
|
|
var name = p.Description.Replace('(', '[').Replace(')', ']');
|
|
|
var list = new PerformanceCounter[2];
|
|
@@ -143,7 +135,6 @@ namespace EfemDualRT.Resources
|
|
|
if (_isTerminate) return true;
|
|
|
string rtmonoitor = process_use(processRTname);
|
|
|
string uimonoitor = process_use(processUIname);
|
|
|
- set_cpu_counter_byprocess();
|
|
|
|
|
|
string systemMonitor = $"PC => {drive_use()}" +
|
|
|
$"{cpu_use()}{cpu_use_byprocess()}" +
|
|
@@ -171,49 +162,6 @@ namespace EfemDualRT.Resources
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
- private void set_cpu_counter_byprocess()
|
|
|
- {
|
|
|
- var curProccessList = Process.GetProcesses().Where(p => p.ProcessName.ToLower() != "Idle".ToLower()).Select(p => p.ProcessName + "_" + p.Id).ToList();
|
|
|
- if (curProccessList != null && curProccessList.Count() > 0)
|
|
|
- {
|
|
|
- var delCounter = _curMonitorPCCounter.Keys.Where(p => !curProccessList.Contains(p)).ToList();
|
|
|
- var newCounter = curProccessList.Where(p => !_curMonitorPCCounter.Keys.Contains(p));
|
|
|
- if (delCounter != null && delCounter.Count() > 0)
|
|
|
- {
|
|
|
- delCounter.ForEach(p =>
|
|
|
- {
|
|
|
- if (_curMonitorPCCounter.ContainsKey(p))
|
|
|
- {
|
|
|
- var idleCounter = _curMonitorPCCounter[p];
|
|
|
- if (idleCounter != null)
|
|
|
- {
|
|
|
- _pcCounterPool.Enqueue(idleCounter);
|
|
|
- }
|
|
|
- _curMonitorPCCounter.Remove(p);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- if (_pcCounterPool.Count <= newCounter.Count())
|
|
|
- {
|
|
|
- var pcCounter = _curMonitorPCCounter.Values;
|
|
|
- foreach (var item in _curMonitorPCCounter.Values)
|
|
|
- {
|
|
|
- _pcCounterPool.Enqueue(item);
|
|
|
- }
|
|
|
- _curMonitorPCCounter.Clear();
|
|
|
- }
|
|
|
- curProccessList.ForEach(p =>
|
|
|
- {
|
|
|
- if (!_curMonitorPCCounter.ContainsKey(p) && _pcCounterPool.Count > 0)
|
|
|
- {
|
|
|
- var pcCounter = _pcCounterPool.Dequeue();
|
|
|
- var processName = p.Split('_')[0];
|
|
|
- _curMonitorPCCounter[p] = new PerformanceCounter("Process", "% Processor Time", processName);
|
|
|
- }
|
|
|
- });
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
//process
|
|
|
private string process_use(string processname)
|
|
|
{
|
|
@@ -285,18 +233,20 @@ namespace EfemDualRT.Resources
|
|
|
private string cpu_use_byprocess()
|
|
|
{
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
- foreach (var item in _curMonitorPCCounter)
|
|
|
+ ManagementObjectCollection moc1 = ExecSearch("SELECT * FROM Win32_PerfFormattedData_PerfProc_Process");
|
|
|
+ if (moc1 != null)
|
|
|
{
|
|
|
- try
|
|
|
+ foreach (ManagementObject mo1 in moc1)
|
|
|
{
|
|
|
- var cupUsage = item.Value.NextValue() / Environment.ProcessorCount;
|
|
|
- if (cupUsage > 1)
|
|
|
+ var pid = Convert.ToInt64(mo1.Properties["IDProcess"].Value.ToString());
|
|
|
+ var name = mo1.Properties["Name"].Value.ToString();
|
|
|
+ var workingSet = Convert.ToSingle(mo1.Properties["WorkingSet"].Value.ToString()) / (1024 * 1024);
|
|
|
+ var percentProcessorTime = Convert.ToSingle(mo1.Properties["PercentProcessorTime"].Value.ToString()) / Environment.ProcessorCount;
|
|
|
+ if (percentProcessorTime > 1 || workingSet > 200)
|
|
|
{
|
|
|
- sb.Append($" {item.Key}:{cupUsage:F2}%\t");
|
|
|
+ sb.Append($"{name}_{pid} CPU:{percentProcessorTime:F2} % WorkingSet:{workingSet:F2} MB ");
|
|
|
}
|
|
|
}
|
|
|
- catch (Exception ex)
|
|
|
- { }
|
|
|
}
|
|
|
return sb.Length > 0 ? $" detail:[{sb.ToString()}] " : string.Empty;
|
|
|
}
|
|
@@ -372,6 +322,19 @@ namespace EfemDualRT.Resources
|
|
|
return MemoryUsedRate.HasValue ? Convert.ToDouble(MemoryUsedRate * 100) : 0;
|
|
|
}
|
|
|
|
|
|
+ private static ManagementObjectCollection ExecSearch(string command)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ ObjectQuery objQuery = new ObjectQuery(command);
|
|
|
+ return new ManagementObjectSearcher(objQuery).Get();
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ LOG.Error($"ExecSearch exception:{ex}");
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
|
|
|
#region 单位转换进制
|
|
|
|