views:

82

answers:

4

I have a multi-threaded application with (4) thread i want to know how much processing time spent in threads. I've created all these threads with ThreadPool

Thread1 doing job1
Thread2 doing job2
..
..

result would be:
Thread1 was running in 12 millisecond
Thread2 was running in 20 millisecond

I've actually download a web page in a job that each job is processing in one thread i want to know how much time it takes a web page is downloaded (without the affection of other threads context switch in calculated time

A: 

Can not be done. The problem is that unless you block it (hard to do- makes little sense) threads can be inteeruupted. So, while it TAKES THread2 20ms to complete, you do not know how much of that it was active.

The negative side of what is called preemtive multitasking.

TomTom
-1: This is of course patently false. You can get the user time associated with a process from [Process.TotalProcessorTime](http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.totalprocessortime.aspx), and for all the threads from [ProcessThread.TotalProcessorTime](http://msdn.microsoft.com/en-us/library/system.diagnostics.processthread.totalprocessortime.aspx). This gives you time spent executing code. For a simple "how long did it take", [Stopwatch](http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx) is good enough.
Lasse V. Karlsen
I stand corrected. Nice.
TomTom
+1  A: 

I found this code on codeproject:

http://www.codeproject.com/KB/dotnet/ExecutionStopwatch.aspx

Try it and report back ;)

Marcus Johansson
Your answer is exactly what i need, THANKS, but is it correct for the problem i stated at the bottom of my question ?
Ehsan
You will get the CPU-time for loading the web page, not the web page loading time. (If i understand the question correctly)
Marcus Johansson
+1  A: 

If you want to get the time you would get from a stopwatch, there's the Stopwatch class:

Stopwatch sw = Stopwatch.StartNew();
// execute code
sw.Stop();
// read and report on sw.ElapsedMilliseconds

If you want to find out how much time the thread was actually executing code (and not waiting for I/O etc.) you can examine the ProcessThread.TotalProcessorTime property, by enumerating the threads of the Process object for your application.

Note that threads in the thread pool are not destroyed after use, but left in the pool for reuse, which means your total time for a thread includes everything it has done before your current workload.

Lasse V. Karlsen
so is there any way to find out how much time is spent in a thread waiting for IO ??
Ehsan
Well, if you mean "all the time it wasn't executing code", the only way to do that would be to measure it using stopwatch, and subtract the total processor time. Note that by adding code to do this, you're adding overhead, small as it might be, and if your thread isn't doing a lot, so you got small numbers, the overhead might have a significant impact on the timings you get out of it. What specifically are you trying to do?
Lasse V. Karlsen
Dear Lasse, my app is downloading many wab pages each in a thread, I want to know the response time of each targeted web site to my request for example (i downloaded page1 from site1 in 12 millisecond) without the affection of other threads in the bandwidth downloading their pages, is it clear ?
Ehsan
+1  A: 

the WMI class Win32_Thared contains properties KernelModeTime and UserModeTime which will, if available, give you a count of 100ns units of actual execution.

But, from the documentation:

If this information is not available, a value of 0 (zero) should be used.

So this might be OS dependent (it is certainly populated here on Win7).

A query like: select * from win32_thread where ProcessHandle="x" will get the Win32_Thread instances for process id x (ignore the "handle" in the name). E.g., using PowerShell, looking at its own threads:

PS[64bit] > gwmi -Query "select * from win32_thread where ProcessHandle=""7064"""|
ft -AutoSize Handle,KernelModeTime,UserModeTime

Handle KernelModeTime UserModeTime
------ -------------- ------------
5548              218          312
6620                0            0
6112                0            0
7148                0           15
6888                0            0
7380                0            0
3992                0            0
8372                0            0
644                 0            0
1328                0           15

(And to confirm this is not elapsed time, the process start time is 16:44:50 2010-09-30.

Richard
That is a great thing to know ! but i think it does not solve the problem i've stated in comments of @Lasse V Karlsen
Ehsan