How do I get CPU usage from WMI?
-
Wednesday, July 07, 2010 1:14 PM
Hi people,
I started using WMI to get Drive, Memory, and CPU information. But I am not sure or don't know how to get CPU usage. This is part of a system monitoring functionality I am trying to implement. I need CPU's usage like the same as you get from Task Manager(in percentage). I don't need CPU's temperature.
Also, I need total memory(as in RAM). This is the same RAM from My Computer >> Properties. Example: 2.99GB of RAM. I got FreePhysicalMemeory and FreeVirtualMemeory from Win32_OperatingSystem but I still need the total RAM. I am thinking FreeVirtualMemory is from HardDrive and FreePhysicalMemory is from the RAM that is not being used, and thus free for use.
BlueMarker
All Replies
-
Wednesday, July 07, 2010 2:32 PM
Check out the WMI Code Creator. It's a fantastic utility.
- Marked As Answer by BlueMarker Wednesday, July 07, 2010 5:22 PM
-
Wednesday, July 07, 2010 3:01 PM
Just a small addtion to the previous great link:
Computer Memory you can get from Win32_ComputerSystem -> TotalPhysicalMemory
CPU counter you can get from Win32_PerfFormattedData_Counters_ProcessorInformation -> PercentProcessorTime; the first collection entry returns total for all processors and cores, the 2nd entry all cores of the first processor. If the system has just one processor they are the same -
Wednesday, July 07, 2010 3:05 PM
Something simple like this will work.
Forward Declarions:
private: System::Management::SelectQuery^ Info private: System::Management::ManagementObjectSearcher^ Searcher;
private: int cpuUsage (System::Object^ sender, System::EventArgs^ e) {
this->Info = gcnew System::Management::SelectQuery("Win32_PerfFormattedData_PerfOS_Processor ");
this->Searcher = gcnew System::Management::ManagementObjectSearcher(Info);
Object^ idleTime;
ManagementObject^ envVar;
for each (envVar in Searcher->Get()) { idleTime = envVar["PercentIdleTime"]; } return 100 - int::Parse(idleTime->ToString()); } -
Wednesday, July 07, 2010 3:23 PM
Hi UgnV,
This looks good but can you get me code in VB? if possible?
I can try the Win32_ComputerSystem -> TotalPhysicalMemory first. I assume this will give me the total RAM; the same RAM as you get from My Computer -> Properties? But how do I get total free RAM?
I will need some code to get the percentage of CPU's usage from Win32_PerfFormattedData_Counters_ProcessorInformation -> PercentProcessorTime.
BlueMarker
-
Wednesday, July 07, 2010 3:26 PM
Thanks for replying pyrox,
I am not sure how to implement your code. I assume its in c++ and I don't see the return value that I needed. I need total RAM and total free RAM.
And I also need CPU's usage in percentage.
BlueMarker
-
Wednesday, July 07, 2010 3:47 PM
Download the WMI Creator!!
It generates code in several languages.
-
Wednesday, July 07, 2010 4:01 PM
Hi Tergiver,
I downloaded and ran the creator. There's 472 classes. How the heck do I know which one to run? Besides the classes mentioned above?
As of now, I got the total RAM. I still need the CPU's usage in percentage.
BlueMarker
-
Wednesday, July 07, 2010 4:22 PM
Hi Tergiver,
The Creator have no:
Win32_PerfFormattedData_Counters_ProcessorInformation
under: root\CIMV2
Anyway, I got an error from the following when I tried to grab CPU's usage from below:
Dim scope5 = New ManagementScope() Dim queryBuilder5 = New StringBuilder() queryBuilder5.Append("SELECT PercentProcessorTime FROM Win32_PerfFormattedData_Counters_ProcessorInformation") Dim query5 = New ObjectQuery(queryBuilder5.ToString()) Dim search5 = New ManagementObjectSearcher(scope5, query5) Dim collectionOfResults5 As ManagementObjectCollection collectionOfResults5 = search5.Get() Dim currentObject5 As ManagementObject 'Display Total RAM information For Each currentObject5 In collectionOfResults5 '_collected.AddFirst("CPU's Usage(%): " + currentObject5("PercentProcessorTime").ToString())) lbDiskInformation.Items.Add("CPU's Usage(%): " + currentObject5("PercentProcessorTime").ToString()) NextI got an error at the end(at the For Each loop). The collectionOfResults5 yield nothing.
BlueMarker
-
Wednesday, July 07, 2010 4:31 PM
Hi BlueMarker,
To get free memory you can use Win32_PerfFormattedData_PerfOS_Memory ->AvailableBytes or AvailableMBytes
Below is the working code in VB that gets CPU percentage sample and free memoryImports System Imports System.Management 'Imports System.Windows.Forms Module Module1 Sub Main() WMISample.MyWMIQuery.Main() End Sub End Module Namespace WMISample Public Class MyWMIQuery Public Overloads Shared Function Main() As Integer Dim processorSearcher As New ManagementObjectSearcher( _ "root\CIMV2", _ "SELECT * FROM Win32_PerfFormattedData_Counters_ProcessorInformation") For Each queryObj As ManagementObject In processorSearcher.Get() Console.WriteLine("PercentProcessorTime: {0}", queryObj("PercentProcessorTime")) ' for system with single processor we exit after the 1st iteration Exit For Next Dim memorySearcher As New ManagementObjectSearcher( _ "root\CIMV2", _ "SELECT * FROM Win32_PerfFormattedData_PerfOS_Memory") For Each queryObj As ManagementObject In memorySearcher.Get() Console.WriteLine("AvailableBytes: {0}", queryObj("AvailableBytes")) Console.WriteLine("AvailableMBytes: {0}", queryObj("AvailableMBytes")) Next End Function End Class End NamespaceBest Regards,
Ugn- Marked As Answer by BlueMarker Wednesday, July 07, 2010 5:21 PM
-
Wednesday, July 07, 2010 5:21 PM
Thanks for the help.
Ugnv pointed to the appropiate class and code which help me.
Tergives pointed me the the WMI Creator which is helpful but I still need to know which class and which value to select.
I will mark both as answer.
Thanks again,
BlueMarker
-
Wednesday, July 07, 2010 5:42 PM
There's 472 classes. How the heck do I know which one to run?
I know you're going to hate this answer, but I love giving it:RTFM
http://msdn.microsoft.com/en-us/library/aa394554(VS.85).aspx -
Wednesday, July 07, 2010 5:54 PM
That is called connecting the dots. Get used to it. ;)There's 472 classes. How the heck do I know which one to run?
Mark the best replies as answers. "Fooling computers since 1971." -
Wednesday, July 07, 2010 6:04 PM
Already got this saved in My Favorites...
I was on my way to the answer...just need the needle in the hay stack :)
Thanks
-
Wednesday, July 07, 2010 6:06 PMYea, this could be fun. Problem is: I got a little deadline. Learn something new everyday....that's for sure.
-
Wednesday, July 07, 2010 6:59 PM
Already got this saved in My Favorites...
I was on my way to the answer...just need the needle in the hay stack :)
Thanks
Save a link in your code inside of a comment.// links
// http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/608a2bc0-3bf8-4845-b0d8-09f2624189f7#bb899507-8c45-478a-b484-3e381b933d22
Mark the best replies as answers. "Fooling computers since 1971."

