1

I would like to determine the total amount of disk I/O read data and also write data (in bytes) for a specific process.

Not sure if this can be done through performance counters, because when I run this code example from MSDN I only see these related counter names which does not seem to be able retrieve the information that I need since they are in data/sec which seems to serve for calculating an average value only...

  • IO Read Operations/sec
  • IO Write Operations/sec
  • IO Data Operations/sec
  • IO Other Operations/sec
  • IO Read Bytes/sec
  • IO Write Bytes/sec
  • IO Data Bytes/sec
  • IO Other Bytes/sec

In case of I'm wrong, then please explain me which of those counters I need to use and how do I need to compare their values (using PerformanceCounter.NextValue() or PerformanceCounter.NextSample() + CounterSample.Calculate()).

I know this information can be retrieved by some task managers such as System Explorer:

enter image description here

I just would like to do the same.

ElektroStudios
  • 19,105
  • 33
  • 200
  • 417
  • Is it not a solution not to start monitoring the ProcessCounters manually to calculate the total IO Read bytes yourself? – Jacob JA Shanks Feb 18 '19 at 17:15
  • 1
    You could p/invoke GetProcessIoCounters() which is a simple call to get basic stats. – Alex K. Feb 18 '19 at 17:18
  • @Alex K. That solved my question and needs, thank you. Feel free to publish an answer suggesting to use that wndows API function. By the way, and if I'm not asking too much, do you know if exists an equivalent native function for processor times? (equivalent to "% Processor Time" performance counter) to retrieve the average CPU usage percentage of a process. Thanks again. – ElektroStudios Feb 18 '19 at 18:10
  • I don't know of a utility function for that, WMI can do it easily enough: https://stackoverflow.com/questions/22195277/get-the-cpu-usage-of-each-process-from-wmi – Alex K. Feb 19 '19 at 11:20

1 Answers1

3

GetProcessIoCounters() is an concise alternative to Perf counters/WMI.

struct IO_COUNTERS {
    public ulong ReadOperationCount;
    public ulong WriteOperationCount;
    public ulong OtherOperationCount;
    public ulong ReadTransferCount;
    public ulong WriteTransferCount;
    public ulong OtherTransferCount;
}

...

[DllImport(@"kernel32.dll", SetLastError = true)]
static extern bool GetProcessIoCounters(IntPtr hProcess, out IO_COUNTERS counters);

...

if (GetProcessIoCounters(Process.GetCurrentProcess().Handle, out IO_COUNTERS counters)) {
    Console.WriteLine(counters.ReadOperationCount);
    ...
Alex K.
  • 171,639
  • 30
  • 264
  • 288