CPU by thread

  • Thread starter Thread starter newscorrespondent
  • Start date Start date
N

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
 
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.
 
Hello Tom,
You might be better off using a CPU profiler
Google for .NET profiler
http://www.google.co.uk/search?hl=en&q=.net+profiler&btnG=Search

These are good at identifying the bottlenecks.
The only issue is that you need to have test cases to do exactly what
the users are doing to be able to invoke the correct methods with
euivalent amount of data.

Typically, you can aim at tuning 10 slowest methods and 10 most
frequently called methods.


Pranshu
 
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.
 
No, there no way to get this from within your own code, nor from a profiler.

Willy.

|
| 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
 
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
 
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?
 
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...)
 
[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.
 
|
| [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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top