前幾天有一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上驗證一下:
果然全都拿到了, 大功告成!