zoukankan      html  css  js  c++  java
  • 在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的权威文章:

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

    运行结果令我大惊:

    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

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

    作者:江振宇
    出处:http://jzywh.cnblogs.com 
    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
    分类: MonoLinux/Unix
  • 相关阅读:
    洛谷 P1074 靶形数独 Label:search 不会
    TYVJ P3522 &&洛谷 P1135 奇怪的电梯 Label:bfs
    洛谷 P1160 队列安排 Label:链表 数据结构
    uestc 1073 秋实大哥与线段树 Label:线段树
    TYVJ P3407 佳佳的魔法照片 Label:语文很重要 语文很重要 语文很重要
    TYVJ P1103 多项式输出 Label:模拟 有点儿坑
    A+B Problem 详细解答 (转载)
    如何批量修改文件名
    c++ 在windows下获取时间和计算时间差的几种方法总结
    SQL Server 2008在Windows 10上不支持
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2615995.html
Copyright © 2011-2022 走看看