1. 程式人生 > >在Mono/Linux上使用PerformanceCounter

在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的權威文章:

後來又有兄弟說Mono/Linux有個工具可以監控程序的效能,而且他已經在Linux上正常運行了。於是乎我看稍微看了下這個工具的原始碼,發現他確實是用PerformanceCounter來獲取效能引數的。然後我就嘗試在Linux上使用PerformanceCounterCategory來獲取intanceName。測試程式碼如下:

var category = new PerformanceCounterCategory("Process");

foreach(var instance in category.GetInstanceNames())
{
       Console.WriteLine(instance);    
}

執行結果令我大驚:

linuxpc

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上驗證一下:

perflog

果然全都拿到了, 大功告成!