jjzjj

.net - 性能计数器在 ReadOnly=false 时显示不同的值

coder 2023-11-11 原文

我试图弄清楚为什么某些性能计数器没有在我们的生产服务器中更新,当我遇到这个奇怪的问题时 - 计数器似乎返回不同的 RawValue 值,但它没有只读。当只读时它始终为零,当不是只读时它显示不同的值。

这是我的 PowerShell session 显示的内容:

PS C:\Users\doron> $counter = new-object Diagnostics.PerformanceCounter
PS C:\Users\doron> $counter.CategoryName = "My category"
PS C:\Users\doron> $counter.CounterName = "My counter name"
PS C:\Users\doron> $counter.ReadOnly = 1
PS C:\Users\doron> $counter


CategoryName     : My category
CounterHelp      : My counter name
CounterName      : My counter name
CounterType      : NumberOfItems64
InstanceLifetime : Global
InstanceName     :
ReadOnly         : True
MachineName      : .
RawValue         : 0
Site             :
Container        :



PS C:\Users\doron> $counter.ReadOnly = 0
PS C:\Users\doron> $counter


CategoryName     : My category
CounterHelp      : My counter name
CounterName      : My counter name
CounterType      : NumberOfItems64
InstanceLifetime : Global
InstanceName     :
ReadOnly         : False
MachineName      : .
RawValue         : 20
Site             :
Container        :



PS C:\Users\doron> $counter.ReadOnly = 1
PS C:\Users\doron> $counter


CategoryName     : My category
CounterHelp      : My counter name
CounterName      : My counter name
CounterType      : NumberOfItems64
InstanceLifetime : Global
InstanceName     :
ReadOnly         : True
MachineName      : .
RawValue         : 0
Site             :
Container        :

服务器是Windows 2008 R2,运行.NET 4.5。

重要的一点是,我所有其他性能计数器的行为都不是这样,只有一些最近添加的性能计数器(这些是不工作的)。对于所有其他计数器,RawValue id 始终相同,无论是否为 ReadOnly。

知道是什么原因造成的吗?

最佳答案

由于您没有提供很多关于如何创建性能计数器以及谁在什么时候更新计数器的信息,我只能做出有根据的猜测并告诉您两者之间的区别。

ReadOnly=TrueReadonly=FalseRawValue 的区别

根据 MSDN:

If the counter that you are reading is read-only, getting the RawValue property samples the counter at the time that the property is called.This action is equivalent to making an initial call to the NextSample method.

来源: http://msdn.microsoft.com/de-de/library/system.diagnostics.performancecounter.rawvalue.aspx

事实上,您可以通过查看 RawValue 属性的实际框架源来确认这一点:

public long RawValue
{
  get
  {
    if (this.ReadOnly)
      return this.NextSample().RawValue;
    this.Initialize();
    return this.sharedCounter.Value;
  }
  ...
}

MSDN 没有说明当 ReadOnly 设置为 False 时会发生什么,但是查看上面的代码片段您会发现它会调用 Initialize 然后返回内部 sharedCounter 对象的值,该对象由 Initialize 创建。 Initialize 虽然在不同的地方被调用,但只有第一个调用才会真正初始化对象。

因为代码比较复杂,没看懂完整的过程,不过好像性能计数器不是只读的时候会缓存这个值,所以你会看到一个旧值。

可能的解释

我不得不在这里猜测,因为你没有说你实际上是如何进行测试的,但是如果你在更新任何值之前初始化性能计数器,那么我会假设你每次都看到 RawValue 为 0,即使计数器同时更新。

现在只要将 ReadOnly 设置为 True 并再次查询 RawValue,您就会看到此时 PerformanceCounter 的实际值。

一个有趣的实验是调用 NextSample() 而不是使用 RawValue。在我看来,您应该在这两种情况下得到相同的结果。

希望这对您有所帮助!

关于.net - 性能计数器在 ReadOnly=false 时显示不同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14479544/

有关.net - 性能计数器在 ReadOnly=false 时显示不同的值的更多相关文章

  1. ruby-on-rails - Ruby net/ldap 模块中的内存泄漏 - 2

    作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代

  2. ruby - 默认情况下使选项为 false - 2

    这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb

  3. ruby - 如何模拟 Net::HTTP::Post? - 2

    是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟此方法:defmethod_to_testurl=URI.parseurireq=Net::HTTP::Post.newurl.pathres=Net::HTTP.start(url.host,url.port)do|http|http.requestreq,foo:1endresend这是RSpec:let(:uri){'http://example.com'}specify'HTTPcall'dohttp=mock:httpNet::HTTP.stub!(:start).and_yieldhttphttp.shou

  4. ruby - 如果指定键的值在数组中相同,如何合并哈希 - 2

    我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat

  5. ruby - 检查字符串是否包含散列中的任何键并返回它包含的键的值 - 2

    我有一个包含多个键的散列和一个字符串,该字符串不包含散列中的任何键或包含一个键。h={"k1"=>"v1","k2"=>"v2","k3"=>"v3"}s="thisisanexamplestringthatmightoccurwithakeysomewhereinthestringk1(withspecialcharacterslike(^&*$#@!^&&*))"检查s是否包含h中的任何键的最佳方法是什么,如果包含,则返回它包含的键的值?例如,对于上面的h和s的例子,输出应该是v1。编辑:只有字符串是用户定义的。哈希将始终相同。 最佳答案

  6. ruby - 获取模块中定义的所有常量的值 - 2

    我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c

  7. ruby-on-rails - Ruby on Rails 计数器缓存错误 - 2

    尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot

  8. ruby - Net::HTTP 获取源代码和状态 - 2

    我目前正在使用以下方法获取页面的源代码:Net::HTTP.get(URI.parse(page.url))我还想获取HTTP状态,而无需发出第二个请求。有没有办法用另一种方法做到这一点?我一直在查看文档,但似乎找不到我要找的东西。 最佳答案 在我看来,除非您需要一些真正的低级访问或控制,否则最好使用Ruby的内置Open::URI模块:require'open-uri'io=open('http://www.example.org/')#=>#body=io.read[0,50]#=>"["200","OK"]io.base_ur

  9. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  10. ruby - 为什么 Integer.respond_to?( :even? ) 返回 false? - 2

    我一直在研究RubyKoans,我发现about_open_classes.rbkoan很有趣。特别是他们修改Integer#even?方法的最后一个测试。我想尝试一下这个概念,所以我打开了Irb并尝试运行Integer.respond_to?(:even?),但令我惊讶的是我得到了错误。然后我尝试了Fixnum.respond_to?(:even?)并得到了错误。我还尝试了Integer.respond_to?(:respond_to?)并得到了true,当我执行2.even?时,我也得到了true。我不知道发生了什么。谁能告诉我缺少什么? 最佳答案

随机推荐