psp
psp

Reputation: 121

Get number of processors a particular process is running on

I have the processID associated with a process. I have created this process using the CreateProcess() function. During the run of it, I want to track how many processors it runs on and how much time this executable has used on multicore machines.

I want to write C++ code for the same; can anyone help me on this?

I am using Win XP multicore machines.

Upvotes: 0

Views: 383

Answers (2)

arx
arx

Reputation: 16904

You can capture this level of detail on Vista or later using Event Tracing for Windows (ETW) and the CSwitch event (which is emitted on every context switch).

Various tools (e.g. the Windows Performance Toolkit) capture and visualize this data.

However, this isn't supported on Windows XP.

If you just want know what your typical concurrency is (i.e. how many of your threads are running at a given time) you could regularly sample the perfmon Thread data (from HKEY_PERFORMANCE_DATA). The "Thread State" counter will give you the instantaneous state of every thread in your process (i.e. whether each thread is running or not). Obviously this sampling process will limit the maximum concurrency to (number of processors - 1).

But do you really need this much detail? GetProcessTimes is usually enough.

Update

You can run your app on a test machine and simply measure the utilization of each CPU using perfmon. You should also measure the CPU utilization of each process to ensure nothing else is running unexpectedly.

To capture data for a report, run perfmon as an Administrator.

  • Navigate to "Performance Monitor" on the right hand side to display the real-time performance chart. Select the objects/counters you want to monitor (i.e. "% Processor Time" for all Processors and Processes). Perfmon should start capturing the data in real time.
  • Right-click on the graph and select the capture frequency (e.g. if your app is running for hours you probably don't want data every second).
  • Right-click on the "Performance Monitor" node in the right-hand tree and select "New|Data Collector Set". Enter a name for it and click through the other defaults.
  • Navigate to your Data Collector Set on the right (under "Data Collector Sets|User Defined"). You can start and stop data collection using the toolbar buttons (or by right-clicking).
  • Now you've got some data return to the performance monitor graph and select "View Log Data" (the second toolbar button). Select your log file from the Source tab. This displays a graph of your captured data.
  • Right-click on the graph and select "Save Data As..." You can choose CSV or TSV.

And that's it.

Upvotes: 0

Roman Ryltsov
Roman Ryltsov

Reputation: 69734

GetProcessAffinityMask:

Retrieves the process affinity mask for the specified process and the system affinity mask for the system.

GetProcessTimes:

Retrieves timing information for the specified process.

Upvotes: 1

Related Questions