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!