|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CPU by threadThere are many examples of how to get the elapsed time for a method but I
can't find any example of how to measure the amount of CPU consumed by my thread in a method. Elapsed time in a multithreaded application particularly on a single processor CPU is not very useful. Is there a way to measure this? Thanks. Tom Tom,
For something like this, you could probably use the PerformanceCounter class to get the values that you need at any particular point in time. Hope this helps. -- Show quote- Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com <newscorrespond***@charter.net> wrote in message news:8ntsg.3$Sk5.0@fe06.lga... > > There are many examples of how to get the elapsed time for a method but I > can't find any example of how to measure the amount of CPU consumed by my > thread in a method. Elapsed time in a multithreaded application > particularly > on a single processor CPU is not very useful. > > Is there a way to measure this? > > Thanks. > Tom I have seen it here but can't remember where is it exactly. Have a try:
http://or1cedar.cps.intel.com/softwarecollege/ chanmm <newscorrespond***@charter.net> wrote in message Show quote news:8ntsg.3$Sk5.0@fe06.lga... > > There are many examples of how to get the elapsed time for a method but I > can't find any example of how to measure the amount of CPU consumed by my > thread in a method. Elapsed time in a multithreaded application > particularly > on a single processor CPU is not very useful. > > Is there a way to measure this? > > Thanks. > Tom Hi, You can use a high resolution timer (or System.Environment.TickCount -
but only on your test env as its reset to 0 after long operations). /// <summary> /// get os perfomance counter value /// </summary> [System.Runtime.InteropServices.DllImport("kernel32.dll")] public extern static short QueryPerformanceCounter(ref long cycles); /// <summary> /// get os performance counter frequency (cycles ber second) /// </summary> [System.Runtime.InteropServices.DllImport("kernel32.dll")] public extern static short QueryPerformanceFrequency(ref long cycles); You determine the frequency with the "QueryPerformanceFrequency" Then when you start measuring call long start; NativeMethods.QueryPerformanceCounter(ref start); And you can get a sample using: /// <summary> /// Sample the timer. /// </summary> /// <returns></returns> public decimal Sample() { long sampleDuration = 0; NativeMethods.QueryPerformanceCounter(ref sampleDuration); sampleDuration -= start; return (decimal)sampleDuration / (decimal)frequency * 1000M; } Or you can save your duration and gets the milliseconds: return (decimal)duration / (decimal)frequency * 1000M; Well, this high resolution timer works for us for very precise measures. Hope this helps, or at least copy it to keep it in your library. -- Show quoteSalvador Alvarez Patuel Exony Ltd - London, UK "newscorrespond***@charter.net" wrote: > > There are many examples of how to get the elapsed time for a method but I > can't find any example of how to measure the amount of CPU consumed by my > thread in a method. Elapsed time in a multithreaded application particularly > on a single processor CPU is not very useful. > > Is there a way to measure this? > > Thanks. > Tom > On Mon, 10 Jul 2006 19:52:42 GMT, newscorrespond***@charter.net wrote:
>That counter does elpased time not CPU cycles used. More CPU cycles = More Time...less CPU cycles = Less time.I use the described method quite successfully when needing to optimize. -- Stephan 2003 Yamaha R6 kimi no koto omoidasu hi nante nai no wa kimi no koto wasureta toki ga nai kara No, there no way to get this from within your own code, nor from a profiler.
Willy. <newscorrespond***@charter.net> wrote in message Show quote news:8ntsg.3$Sk5.0@fe06.lga... | | There are many examples of how to get the elapsed time for a method but I | can't find any example of how to measure the amount of CPU consumed by my | thread in a method. Elapsed time in a multithreaded application particularly | on a single processor CPU is not very useful. | | Is there a way to measure this? | | Thanks. | Tom "Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote [Detailed Thread Execution time by processor]> No, there no way to get this from within your own code, nor http://www.intel.com/cd/software/products/asmo-na/eng/vtune/index.htm> from a profiler. The Intel VTune profiler will (if I remember right) provide the information he's looking for, and a whole lot more. The data presentation is very hard to wade through, but the data there is just staggering. I've used a number of other Profilers (Compuware, Ants, VS.Net 2005 Team Suite, etc), and none of them provided anywhere even close to the data provided by VTune. The problem was the VTune was so different, and so overwhelming in it's data presentation, that it wasn't usefull. There's a trial version to play with. It's kinda fun (in a sick way) to peek under the hood and see what modern processors are doing with all their time. Frightening, but fun. -- Chris Mullins Coversant, Inc.
Show quote
"Chris Mullins" <cmull***@yahoo.com> wrote in message I'm a regular user of both VTune and CodeAnalyst (AMD) and none of these can news:%23UJkutUpGHA.1796@TK2MSFTNGP03.phx.gbl... | "Willy Denoyette [MVP]" <willy.denoye***@telenet.be> wrote | | [Detailed Thread Execution time by processor] | | > No, there no way to get this from within your own code, nor | > from a profiler. | | http://www.intel.com/cd/software/products/asmo-na/eng/vtune/index.htm | | The Intel VTune profiler will (if I remember right) provide the information | he's looking for, and a whole lot more. The data presentation is very hard | to wade through, but the data there is just staggering. | | I've used a number of other Profilers (Compuware, Ants, VS.Net 2005 Team | Suite, etc), and none of them provided anywhere even close to the data | provided by VTune. The problem was the VTune was so different, and so | overwhelming in it's data presentation, that it wasn't usefull. | | There's a trial version to play with. It's kinda fun (in a sick way) to peek | under the hood and see what modern processors are doing with all their time. | Frightening, but fun. | | -- | Chris Mullins | Coversant, Inc. | | offer exactly what the OP is looking for, sure they have some thread profiling support but this is restricted to what the OS has to offer, and that's nothing more than the time a thread spends in user vs. kernel space. Willy. Thanks to all of you who spent some time thinking about this.
I have come up with this code: Process CurrentProcess = Process.GetCurrentProcess(); ProcessThreadCollection MyThreads = CurrentProcess.Threads; foreach (ProcessThread Athread in MyThreads) { Athread.PrivilegedProcessorTime; Athread.UserProcessorTime; Athread.TotalProcessorTime; } The ProcessThread class does have the value I am looking for (and even more) but I can't determine how often it is posted. The frequency does not appear to me to be granular enough for short cpu routines. Sometimes I am measuring more elapsed time than CPU time which does not make sense? The ProcessThread class has an ID that is a very different number than ManagedThreadId: Thread ThisThread = Thread.CurrentThread; ThreadID = ThisThread.ManagedThreadId; I can't find a definition for ManagedThreadId. Is there a way to correlate the two? Do either of these relate to a Windows API Thread? Thanks Tom Got things a little reversed it should have read
Sometimes I am measuring more CPU time than elapsed time which does not make sense? <newscorrespond***@charter.net> wrote in message
> I don't think there's an easy way to mearure this. Certainly not using .Net.> There are many examples of how to get the elapsed time for a method but I > can't find any example of how to measure the amount of CPU consumed by my > thread in a method. Elapsed time in a multithreaded application > particularly > on a single processor CPU is not very useful. > > Is there a way to measure this? > Using the System.Diagnostics.ProcessThread class gives you a decent view into the thread itself. For example, it has the PrivilegedProcessorTime, Thread Affinity Mask, and so forth for the thread. Also present is the ThreadId, which (if I remember right) is the Win32 Native Thread Id. You can use this to call into the Win32 threading API's and get whatever information you need. The only profiler that I've used that would easily give you what you want are the Intel Profilers for .Net. I believe you can download trial versions of them from the Intel web site. These profilers give you a huge amount of data about your threads - L1, L2, L3 cache misses, instruction branch mispredictions, and all the other good stuff you generally never want to know about. Don't forget though that .Net threads are not mapped 1:1 to native threads. Unless your threads are using "Threading.Thread.BeginThreadAffinity" to make sure they're tied to a real thread for the duration of an operation, you may get some results that are confusing. (This hasn't come up for me before, so I'm not sure if it's a real problem or not...) -- Chris Mullins Coversant, Inc. |
|||||||||||||||||||||||