C#监控-通过PerformanceCounter实现Process的Cpu占用率以及GC占用监控
2015-10-10 13:47
489 查看
监控每个进程占用的cpu,比如任务管理器的进程tab中的CPU
在代码里,可以通过新建PerformanceCounter来建立监控,其对应的Windows中的自带性能分析工具Perfmon
看下该类的定义中,只要有三个概念:
categoryName:
The name of the performance counter category (performance object) with which
this performance counter is associated.
counterName:
The name of the performance counter.
instanceName:
The name of the performance counter category instance, or an empty string
(""), if the category contains a single instance.
其对应关系如下:
由于instance是变化的,所以每次要根据Process得到instance,那么process跟instance间的关系如何,请看:
PerformanceCounter("Process", "ID Process", instance)这个指标取NextValue()就是ProcessID,就可以跟Process对应起来
但是注意,每个Catagory下面的instance都是不同的,所有Process下的instance到其他Category下就不识别了。
获取instance代码如下:
获取cpu占用率的PerformanceCounter:
获取GC占用率的PerformanceCounter:
GC选取了PerformanceCounter(".NET CLR Memory", "% Time in GC", instance2)这个counter,但注意,这两个属于不同Category,所以instance不能共用
代码如下:
在代码里,可以通过新建PerformanceCounter来建立监控,其对应的Windows中的自带性能分析工具Perfmon
看下该类的定义中,只要有三个概念:
categoryName:
The name of the performance counter category (performance object) with which
this performance counter is associated.
counterName:
The name of the performance counter.
instanceName:
The name of the performance counter category instance, or an empty string
(""), if the category contains a single instance.
其对应关系如下:
由于instance是变化的,所以每次要根据Process得到instance,那么process跟instance间的关系如何,请看:
PerformanceCounter("Process", "ID Process", instance)这个指标取NextValue()就是ProcessID,就可以跟Process对应起来
但是注意,每个Catagory下面的instance都是不同的,所有Process下的instance到其他Category下就不识别了。
获取instance代码如下:
public static string GetInstanceName(string categoryName, string counterName, Process p) { try { PerformanceCounterCategory processcounter = new PerformanceCounterCategory(categoryName); string[] instances = processcounter.GetInstanceNames(); foreach (string instance in instances) { PerformanceCounter counter = new PerformanceCounter(categoryName, counterName, instance); //Logger.Info("对比in mothod GetInstanceName," + counter.NextValue() + ":" + p.Id); if (counter.NextValue() == p.Id) { return instance; } } } catch (Exception ex) { } return null; }
获取cpu占用率的PerformanceCounter:
string instance1 = GetInstanceName("Process", "ID Process", p); if (instance1 != null) { PerformanceCounter cpucounter = new PerformanceCounter("Process", "% Processor Time", instance1); if (cpucounter != null) { cpucounter.NextValue(); System.Threading.Thread.Sleep(200); //等200ms(是测出能换取下个样本的最小时间间隔),让后系统获取下一个样本,因为第一个样本无效 return cpucounter; } else { Logger.Info("Name:" + name + "生成CPU监控失败" + instance1); } } else { Logger.Info("Name:" + name + "获取cpu监控实例失败" + instance1); }
获取GC占用率的PerformanceCounter:
GC选取了PerformanceCounter(".NET CLR Memory", "% Time in GC", instance2)这个counter,但注意,这两个属于不同Category,所以instance不能共用
代码如下:
// 获取GC占用率 PerformanceCounter string instance2 = GetInstanceName(".NET CLR Memory", "Process ID", p); if (instance2 != null) { PerformanceCounter gccounter = new PerformanceCounter(".NET CLR Memory", "% Time in GC", instance2); if (gccounter != null) { return gccounter; Logger.Info("Name:" + name + "生成GC监控成功" + instance2); } else { Logger.Info("Name:" + name + "生成GC监控失败" + instance2); } } else { Logger.Info("Name:" + name + "获取GC监控实例失败" + instance2); }
相关文章推荐
- C#给文件夹添加权限
- C# 理解Thread.Sleep()方法 ----转帖
- C#监控-机器资源监控:CPU、内存、C盘、D盘资源监控及邮件预警
- C# 用匿名委托的方式简化Thread 和 Controle.Invoke的写法
- xsd、wsdl生成C#类的命令行工具使用方法
- C#鼠标右键弹出菜单
- C#按Esc后退出对话框
- C#使用GET、POST请求获取结果
- C#控制台程序,运行完窗口不退出的方法
- C#中List的排序
- dynamic基础类型/C#与动态脚本语言交互/dynamic自定义类型
- 【转】关于c#中委托的解释
- C#生成并引用资源文件
- c# listview 插入数据在第一行显示
- C# 通过Environment 获取 "我的文档"路径
- 在C#代码中应用Log4Net 中配置文件的解释
- c#拓展方法将datatable转换成实体
- C#将dll打包到程序中
- C#基础----Linq之List<T>篇
- C#之结构、枚举和运算符