Use PerformanceCounter on Mono/Linux

Source: Internet
Author: User

A few days ago, a SuperSocket user reported that all the parameters in the performance log of Linux are 0, because the performance log of SuperSocket is implemented by PerformanceCounter, so I suspect that the PerformanceCounter in Mono is not supported on Linux. I ran it on Linux. This problem exists. The performance counter values are all 0. The code for obtaining PerformanceCounter is as follows:

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);

The above code can get the correct value on Windows.

Does Linux really not support PerformanceCounter? I searched for the network and found only one authoritative article about PerformanceCounter on Mono's website:

Http://www.mono-project.com/Mono_Performance_Counters, which does not say PerformanceCounter is not supported on Linux, and similar statements are not found elsewhere on the Internet.

Later, a brother said that Mono/Linux had a tool that could monitor the performance of the process and that it was already running normally on Linux. As a result, I have read the source code of this tool and found that it uses PerformanceCounter to obtain performance parameters. Then I tried to use PerformanceCounterCategory on Linux to get the intanceName. The test code is as follows:

var category = new PerformanceCounterCategory("Process");foreach(var instance in category.GetInstanceNames()){       Console.WriteLine(instance);    }

I was shocked by the running results:

On Linux, the instanceName of PerformanceCounter is in the "ID/NAME" format. It is no wonder that it is 0.

So the code for modifying SuperSocket to obtain performance parameters is:

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);

Verify on Linux:

It was all done!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.