在Mono/Linux上使用PerformanceCounter
前几天有一SuperSocket用户报在Linux上面性能日志的各个参数都是0, 由于SuperSocket的性能日志是通过PerformanceCounter实现的,于是我暂时怀疑Mono中的PerformanceCounter在Linux上不被支持。我自己也上Linux上跑了一下,确实有这个问题,performance counter的value都是0. 当时的获取PerformanceCounter的代码如下:
Process process = Process.GetCurrentProcess(); m_CpuUsagePC = new PerformanceCounter("Process", "% Processor Time", process.ProcessName); m_ThreadCountPC = new PerformanceCounter("Process", "Thread Count", process.ProcessName); m_WorkingSetPC = new PerformanceCounter("Process", "Working Set", process.ProcessName);
上面这段代码在Windows上是可以取到正确的值的。
Linux上真的不支持PerformanceCounter吗?遍寻网络,唯独只发现Mono网站上有一篇关于PerformanceCounter的权威文章:
http://www.mono-project.com/Mono_Performance_Counters, 上面并没有说PerformanceCounter在Linux上不被支持,网上其它地方也没有找到类似的陈述。
后来又有兄弟说Mono/Linux有个工具可以监控进程的性能,而且他已经在Linux上正常运行了。于是乎我看稍微看了下这个工具的源代码,发现他确实是用PerformanceCounter来获取性能参数的。然后我就尝试在Linux上使用PerformanceCounterCategory来获取intanceName。测试代码如下:
var category = new PerformanceCounterCategory("Process"); foreach(var instance in category.GetInstanceNames()) { Console.WriteLine(instance); }
运行结果令我大惊:
Linux上PerformanceCounter的instanceName是"ID/NAME"格式的,难怪取出来都是0。
于是我修改SuperSocket获取性能参数的代码为:
var isUnix = Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX; var instanceName = isUnix ? string.Format("{0}/{1}", process.Id, process.ProcessName) : process.ProcessName; m_CpuUsagePC = new PerformanceCounter("Process", "% Processor Time", instanceName); m_ThreadCountPC = new PerformanceCounter("Process", "Thread Count", instanceName); m_WorkingSetPC = new PerformanceCounter("Process", "Working Set", instanceName);
在Linux上验证一下:
果然全都拿到了, 大功告成!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库