locked
Thread Info RRS feed

  • Question

  • I have a multi threaded application which is spiking,

    1. I need to know if it is possible to pair the managed thread id with the system thread id or is this not possible

    2. Is it possible to calculate how much of the CPU a thread is using as perhaps a snapshot?




    Thursday, September 29, 2016 3:27 PM

Answers

  • That was my point, unfortunately there is no direct relation. Will continue to try and find how I can get the CPU usage of a specific function. 

    Well I don't know how to run a Function in a separate thread so I'm unsure how to go about that.

    Regardless I suspect that each spawned thread can be provided a name, but maybe not in how you are using threading, as this old link says Multithreaded Programming with Visual Basic .NET.

    Then if that is possible a thread launched to run a Function would have a name associated to it. And since that thread will also have an ID then you should be able to use code I provided previously to use performance counter to get total CPU usage for a one second period and get the threads total processor time at the start of a 1 second interval and at the end of a 1 second interval to subtract the start timr from the end time to know for 1 second what time was used during 1 second of the processor time. And then figure out how to compare that to total processor time to see what % of the total processor time was used by that specific thread.

    Also see below threads if necessary to get an idea of that.

    How to Geek - Get a List of Threads in a Process

    Starting multiple threads and keeping track of them from my .NET application

    I can't try any of it cause I have other things I have to start doing soon.


    La vida loca

    Monday, October 3, 2016 2:52 PM

All replies

  • You need to import System.Diagnostics and create a PerformanceCounter.

    There is a nice article about Thread and CPU in codeproject: http://www.codeproject.com/Articles/10258/How-to-get-CPU-usage-of-processes-and-threads

    It's C# but readily transposable to VB.

    Thursday, September 29, 2016 3:41 PM
  • I already have performance counters in place, I have 200 threads running and need more specific info as stated above
    Also that article is pretty out of date and uses deprecated methods but thanks for the help
    Thursday, September 29, 2016 8:59 PM
  • I believe with the below code there is enough information for you to determine for a thread what amount of CPU usage it is using unless the methods are deprecated. I didn't perform the math for doing that but I'm fairly certain all the information is available for doing so.

    I don't know what pairing thread ID's means.

    You can see in the image pics that the BackgroundWorker thread is 4528.

    Option Strict On
    
    Public Class Form1
    
        Dim PT As New PerformanceCounter("Processor", "% Processor Time", "_Total")
        Dim PTC1 As CounterSample
        Dim PTC2 As CounterSample
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
            Timer1.Interval = 1000
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            StopBW = True
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Timer1.Enabled = False Then
                Timer1.Enabled = True
                PTC1 = PT.NextSample()
                BackgroundWorker1.RunWorkerAsync()
            Else
                Timer1.Enabled = False
                StopBW = True
            End If
        End Sub
    
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            PTC2 = PT.NextSample()
            Dim finalCpuCounter As Single = CounterSample.Calculate(PTC1, PTC2)
            RichTextBox1.Text = finalCpuCounter.ToString & vbCrLf & PTC1.RawValue.ToString & " | " & PTC2.RawValue.ToString & vbCrLf & vbCrLf
            PTC1 = PT.NextSample()
    
            Dim p As Process = Process.GetCurrentProcess()
    
            For Each pt As ProcessThread In p.Threads
                RichTextBox1.AppendText(pt.Id.ToString & " | " & pt.TotalProcessorTime.Ticks.ToString & vbCrLf)
            Next
        End Sub
    
    
        Dim StopBW As Boolean = False
    
        Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            Dim i As Integer = 1
            Do Until i = 1000000
                i += 1
                If i = 900000 Then i = 1
                If StopBW = True Then Exit Do
            Loop
        End Sub
    
    End Class


    La vida loca

    • Proposed as answer by Neda Zhang Friday, September 30, 2016 4:43 AM
    Friday, September 30, 2016 2:04 AM
  • The issue with this is The ProcessThread describes an operating system (OS) thread where as the Thread class describes a .Net framework, I need the CPU time of the thread class. 

    Friday, September 30, 2016 7:29 AM
  • I'm not sure what that means. Don't threads for your app run within your app?

    Doesn't the below code provide information for all your threads by ID? If this is not what you are refering to then please explain. I thought you wanted to check which thread in your app is "spiking". Although I suppose that will not provide detail on what code is related to which thread.

    And with the other code I provided for Total CPU percentage being used I'm fairly certain for each thread you can get a determination of how much of the CPU percentage is being used by each thread.

            Dim p As Process = Process.GetCurrentProcess()
    
            For Each pt As ProcessThread In p.Threads
                RichTextBox1.AppendText(pt.Id.ToString & " | " & pt.TotalProcessorTime.Ticks.ToString & vbCrLf)
            Next


    La vida loca

    Friday, September 30, 2016 9:17 AM
  • Also see this thread (no pun intended) Getting the thread ID from a thread and this link mentioned in a post in the thread Thread Class and see the post in the thread that info below is quoted from.

    "An operating-system ThreadId has no  fixed relationship to a managed  thread, because an unmanaged host can  control the relationship between  managed and unmanaged threads.  Specifically, a sophisticated host can  use the CLR Hosting API to schedule  many managed threads against the same  operating system thread, or to move a  managed thread between different  operating system threads."


    La vida loca


    Friday, September 30, 2016 9:27 AM
  • That was my point, unfortunately there is no direct relation. Will continue to try and find how I can get the CPU usage of a specific function. 

    Monday, October 3, 2016 1:50 PM
  • I already have performance counters in place, I have 200 threads running and need more specific info as stated above
    Also that article is pretty out of date and uses deprecated methods but thanks for the help

    How many CPU cores do you have??? That is a lot of threads.

    You can run a performance profiling debug session from within VS and that will show you the hot-path function(s) which are eating up time.

    But I would suspect the root of the issue lies in all the context switching associated with running way more threads than you have CPU cores.  What do these threads do?  Are there points at which they each need to communicate back to the main thread, or do they share some common resource(s)?

    This problem likely stems from a design issue which can ultimately only be corrected by redesigning the use of threading.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, October 3, 2016 2:10 PM
  • That was my point, unfortunately there is no direct relation. Will continue to try and find how I can get the CPU usage of a specific function. 

    Well I don't know how to run a Function in a separate thread so I'm unsure how to go about that.

    Regardless I suspect that each spawned thread can be provided a name, but maybe not in how you are using threading, as this old link says Multithreaded Programming with Visual Basic .NET.

    Then if that is possible a thread launched to run a Function would have a name associated to it. And since that thread will also have an ID then you should be able to use code I provided previously to use performance counter to get total CPU usage for a one second period and get the threads total processor time at the start of a 1 second interval and at the end of a 1 second interval to subtract the start timr from the end time to know for 1 second what time was used during 1 second of the processor time. And then figure out how to compare that to total processor time to see what % of the total processor time was used by that specific thread.

    Also see below threads if necessary to get an idea of that.

    How to Geek - Get a List of Threads in a Process

    Starting multiple threads and keeping track of them from my .NET application

    I can't try any of it cause I have other things I have to start doing soon.


    La vida loca

    Monday, October 3, 2016 2:52 PM