前兩天需要寫一個健康檢查,獲取當前進程在linux上使用cpu的方法,但是放上去怎么都要報錯,卻找不到解決之法,后來,經過多個網站的尋找,找到了方法。
附上鏈接:https://www.cnblogs.com/jzywh/archive/2012/07/30/2615715.html
以下是這邊文章的原文:
在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上驗證一下:
果然全都拿到了, 大功告成!