在Mono/Linux上使用PerformanceCounter

來源:互聯網
上載者:User

前幾天有一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上驗證一下:

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.