Thursday, March 18, 2010

Monitor your processes

This class is used to get the % of cpu usage of a running thread by native thread id. It's a great way to be able to tell which thread is hammering your cpu. Usage:

To watch cpu usage from within the thread to be watched:

Dim CPUutil As New ThreadCPUusageWatcher
Dim PercentUsage as Short
CPUutil .Start()

' Use:
CPUutil..NativeThreadID ' to get the id if the current thread, or set the id of the thread to be watched.

PercentUsage  = CPUutil .CPUusage ' to get the current percentage of cpu use of the watched thread

To watch the cpu usage of background threads from another (the main?) thread:
declare CPUutil  public, and then set it new from with the thread to be watched. Ie:

Public  CPUutil as ThreadCPUusageWatcher

' Then, from within the thread to be watched:
CPUutil = New ThreadCPUusageWatcher
CPUutil.Start()

' CPUutil.CPUusage can then be called from any thread.

' Call:
CPUutil.StopWatcher() ' to stop the watcher when your app closes.

Imports System.Diagnostics
Imports System.Threading

Public Class ThreadCPUusageWatcher

' This class is used to get the % of cpu usage of a running thread by native thread id.
        ' It's a great way to be able to tell which thread is hammering your cpu. Usage:
        ' To watch cpu usage from within the thread to be watched:

        ' Dim CPUutil As New ThreadCPUusageWatcher
        ' Dim PercentUsage As Short
        ' CPUutil .Start()

        ' Use:
        ' CPUutil..NativeThreadID ' to get the id if the current thread, or set the id of the thread to be watched.

        ' PercentUsage  = CPUutil.CPUusage ' to get the current percentage of cpu use of the watched thread

        ' To watch the cpu usage of background threads from another (the main?) thread:
        ' declare CPUutil public, and then set it new from with the thread to be watched. Ie:

        ' Public CPUutil As ThreadCPUusageWatcher

        ' Then, from within the thread to be watched:
        ' CPUutil = New ThreadCPUusageWatcher
        ' CPUutil.Start()

        ' CPUutil.CPUusage can then be called from any thread.

        ' Call:
        ' CPUutil.StopWatcher() ' to stop the watcher when your app closes.

        Private threadID As Int16
        Private WatcherRunning As Boolean = False
        Private th1 As Thread
        Private Percentage As Long

        Public Sub New()
            threadID = AppDomain.GetCurrentThreadId
        End Sub

        Public Sub New(ByVal NativeThreadID As Int16)
            threadID = NativeThreadID
        End Sub

        Private Function GetCurrentNativeThreadID() As Int16
            GetCurrentNativeThreadID = AppDomain.GetCurrentThreadId
        End Function

        ' Set the native ID of a process thread to be watched, or get your native thread id
        Public Property NativeThreadID() As Int16
            Get
                Return GetCurrentNativeThreadID()
            End Get
            Set(ByVal value As Int16)
                threadID = value
            End Set
        End Property
        Public ReadOnly Property IsRunning() As Boolean
            Get
                Return WatcherRunning
            End Get
        End Property

        Public ReadOnly Property CPUusage() As Long
            Get
                Return Percentage
            End Get
        End Property

        Public Sub StopWatcher()
            WatcherRunning = False
        End Sub
        Public Sub Start()
            th1 = New System.Threading.Thread(AddressOf StartWatcher)
            th1.Start()
        End Sub
        Private Sub StartWatcher()
            Dim tx As System.Diagnostics.ProcessThreadCollection
            Dim t, tId, a, a1, a2, a3, a4, a5, CPUs As Int16
            Dim CPUtimeEnd, CPUtimeStart As Double

            CPUs = Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS")

            tx = System.Diagnostics.Process.GetCurrentProcess().Threads
            tId = 0

            For t = 0 To tx.Count - 1
                If tx.Item(t).Id = threadID Then
                    tId = t
                End If
            Next

            If tId = 0 Then
                MsgBox("Thread could not be found.")
                Exit Sub
            End If

            WatcherRunning = True

            Try
                Do While WatcherRunning = True

                    CPUtimeStart = tx.Item(tId).TotalProcessorTime.Milliseconds
                    Thread.Sleep(200)
                    CPUtimeEnd = tx.Item(tId).TotalProcessorTime.Milliseconds

                    If (CPUtimeEnd > CPUtimeStart) Or (CPUtimeEnd = CPUtimeStart) Then

                        a = a + 1
                        If a = 1 Then
                            a1 = CPUtimeEnd - CPUtimeStart
                        ElseIf a = 2 Then
                            a2 = CPUtimeEnd - CPUtimeStart
                        ElseIf a = 3 Then
                            a3 = CPUtimeEnd - CPUtimeStart
                        ElseIf a = 4 Then
                            a4 = CPUtimeEnd - CPUtimeStart
                        ElseIf a = 5 Then
                            a5 = CPUtimeEnd - CPUtimeStart
                        ElseIf a > 5 Then
                            a = 1
                        End If

                        Percentage = ((a1 + a2 + a3 + a4 + a5) / 5) / 2
                        Percentage = Percentage / CPUs
                        If Percentage > 100 Then Percentage = 100
                    End If

                Loop
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try

            WatcherRunning = False
        End Sub

    End Class

No comments:

Post a Comment

Search This Blog