6

I have an instance of PerformanceCounter, I call NextSample() on it and get a CounterSample. In it, there are several time-related fields: CounterFrequency, SystemFrequency, CounterTimeStamp, Timestamp and TimeStamp100nSec.

The MSDN page for CounterSample only says "Gets the raw counter frequency" and similar unhelpful descriptions, and a sample that prints the value without explanations.

  • What do these mean exactly?
  • In what units are they? I tried all DateTime.FromX() functions, but none produces a reasonable result.
Jonathan
  • 6,939
  • 4
  • 44
  • 61
  • 1
    have a look at the doc of [PerformanceCounter](http://msdn.microsoft.com/de-de/library/system.diagnostics.performancecounter%28v=vs.80%29.aspx) – juergen d Apr 03 '12 at 15:25
  • 4
    As the question indicates, the MSDN documentation on this is useless. – Peter Baer Aug 05 '12 at 23:29
  • You can get the UTC timestamp of the counter sample from the following expression: `DateTime.FromFileTimeUtc(sample.TimeStamp100nSec)`. – Martin Liversage Jun 12 '16 at 12:08

1 Answers1

6
(sample2.Timestamp - sample1.Timestamp) / sample2.SystemFrequency = elapsed seconds

(sample2.TimeStamp100nSec - sample1.TimeStamp100nSec) / 10000000 = elapsed seconds

I was able to reverse engineer this when I was analyzing how LOGMAN.EXE would read perf counters and store them in a SQL database. CounterSample class (from various disassemblies) wraps this C structure

PDH_RAW_COUNTER

In this C structure, SecondValue has either TimeStamp or TimeStamp100nSec depending on the counter type. By the time it gets wrapped in .NET CounterSample struct, you can get either via the properties.

Eonasdan
  • 7,563
  • 8
  • 55
  • 82
triple.vee
  • 126
  • 1
  • 7
  • this pretty much confirms my findings: http://typedescriptor.net/browse/members/335094-System.Diagnostics.CounterSampleCalculator.GetElapsedTime(CounterSample,CounterSample) – triple.vee Oct 18 '12 at 22:50