Home All Groups Group Topic Archive Search About
Author
10 Jul 2006 2:18 PM
newscorrespondent
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

Author
10 Jul 2006 2:25 PM
Nicholas Paldino [.NET/C# MVP]
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.


--
          - Nicholas Paldino [.NET/C# MVP]
          - mvp@spam.guard.caspershouse.com

<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
Author
10 Jul 2006 3:42 PM
chanmm
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
Author
10 Jul 2006 5:22 PM
Salvador
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.


--
Salvador Alvarez Patuel
Exony Ltd - London, UK


Show quote
"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
>
Author
10 Jul 2006 7:52 PM
newscorrespondent
That counter does elpased time not CPU cycles used.
Author
10 Jul 2006 9:03 PM
Stephan Rose
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
Author
10 Jul 2006 8:14 PM
Willy Denoyette [MVP]
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
Author
12 Jul 2006 12:38 AM
Chris Mullins
"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.
Author
12 Jul 2006 9:32 PM
Willy Denoyette [MVP]
Show quote
"Chris Mullins" <cmull***@yahoo.com> wrote in message
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.
|
|

I'm a regular user of both VTune and CodeAnalyst (AMD) and none of these can
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.
Author
11 Jul 2006 8:44 PM
newscorrespondent
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
Author
11 Jul 2006 10:03 PM
newscorrespondent
Got things a little reversed it should have read

Sometimes I am measuring
more CPU time than elapsed time which does not make sense?
Author
12 Jul 2006 12:31 AM
Chris Mullins
<newscorrespond***@charter.net> wrote in message
>
> 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?
>

I don't think there's an easy way to mearure this. Certainly not using .Net.

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.

AddThis Social Bookmark Button