473,385 Members | 1,693 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Reading thread 'processor time' quickly

Hi,

I need to be able to read the amount of processor time a thread has taken
up. So far I've found the following methods open to me:

1) Find the relevant ProcessThread in the current process'
ProcessThreadCollection and read the TotalProcessorTime.

2) Find the PerformanceCounter for the Thread category that relates to the
current thread, and use the NextSample() method to read the processor time
value. (I think this is pretty much what the first method is doing under the
hood anyway)

The problem is, both of those approaches are too slow for me, as I need to
find the value many times in quick succession (I'm using this to find out the
performance cost of methods)

From what I can tell about the PerformanceCounter approach, it re-gets *all*
the performance counter information for all the running threads and processes
every time I call NextSample, which isn't ideal. Is there any way just read
the value for the performance counter instance I'm actually interested in?

Cheers,
Mike

Nov 22 '05 #1
6 3702
Hi Mike,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the CPU time of a
certain thread. If there is any misunderstanding, please feel free to let
me know.

As far as I know, we can use both methods you mentioned to get the CPU
time. For the PerformanceCounter, we needn't get all the performance
information for all the running threads and processes every time. We can
specify the instance name, category and counter name for the performance
counter object. When we call NextValue method, it only returns the
information we need. Both of the two methods are fast enough on my machine.
Here is an example.

System.Diagnostics.PerformanceCounter pc = new
System.Diagnostics.PerformanceCounter("Thread", "% Processor Time");
pc.InstanceName = "WindowsApplication82/0";
float f = pc.NextValue();

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #2
Hi Kevin, thanks for your reply

Your understanding is correct, and your example is exactly one of the
approaches I've tried.

Unforfunately some of the methods that I'll need to measure execute very
quickly, and just calling NextValue() takes on average 0.02s-0.03s, which is
too slow for my needs.

I actually think this is a bit of a dead-end anyway, because from what I can
tell, the resolution of the counter seems to be in the centisecond range
anyway - i.e. when I execute it in quick succession, I get the same values
returned, then the reported values jump up by 0.01s.

As a point of note, I used reflector to have a peek to see what was going on
under the hood of the NextValue() method, and from what I could see it has to
re-create the Counter object, parse all of the instance data into a memory
structure and then find the instance I'm interested in. From the looks of
things this is because when you read a counter you get back all the instance
data in one big chunk of memory. (If you can get at the source code, you
will probably be able to see this in the constructor of
System.Diagnostics.CategorySample)

So, unless there is another way of reading thread timings at a higher
resolution, this may be a no-go!

Thanks,
Mike

"Kevin Yu [MSFT]" wrote:
Hi Mike,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to get the CPU time of a
certain thread. If there is any misunderstanding, please feel free to let
me know.

As far as I know, we can use both methods you mentioned to get the CPU
time. For the PerformanceCounter, we needn't get all the performance
information for all the running threads and processes every time. We can
specify the instance name, category and counter name for the performance
counter object. When we call NextValue method, it only returns the
information we need. Both of the two methods are fast enough on my machine.
Here is an example.

System.Diagnostics.PerformanceCounter pc = new
System.Diagnostics.PerformanceCounter("Thread", "% Processor Time");
pc.InstanceName = "WindowsApplication82/0";
float f = pc.NextValue();

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #3
Hi Mike,

It seems to be by design that the NextValue takes that much time to execute
in .NET framework. If you need the process to be faster, I think you can
try to use WMI instead, which gets value directly. You can try to post in
microsoft.public.win32.programmer.wmi newsgroup for wmi question. There
will be more professionals in that group to answer your question. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #4
Thanks Kevin, I've used WMI before, so hopefully I'll be able to work this out!

I'll post a quick sample if I get it sorted.

Cheers,
Mike

"Kevin Yu [MSFT]" wrote:
Hi Mike,

It seems to be by design that the NextValue takes that much time to execute
in .NET framework. If you need the process to be faster, I think you can
try to use WMI instead, which gets value directly. You can try to post in
microsoft.public.win32.programmer.wmi newsgroup for wmi question. There
will be more professionals in that group to answer your question. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #5
I promised a sample, so here it is in case anyone else ever wants it - you'll
need to include the System.Management namespace.

Sadly, the method call still seems to take around the same sort of time as
the performance counter approach - it's slightly faster, but still in the
same sort of ballpark - and the resolution seems to only be down to ~0.0001s.
I admit my needs are fairly extreme, and for most situations this will be
fine - I expect I'll be able to live with it though!

This sample adds together the user mode time and kernel mode time to get the
overall time.

====

string threadId = AppDomain.GetCurrentThreadId().ToString();
string processId =
System.Diagnostics.Process.GetCurrentProcess().Id. ToString();

using (ManagementObject testObject = new
ManagementObject(@"\\.\root\cimv2:Win32_Thread.Han dle=""" + threadId +
"\",ProcessHandle=\"" + processId + "\""))
{
Console.WriteLine(((UInt64)testObject.Properties["UserModeTime"].Value +
(UInt64)testObject.Properties["KernelModeTime"].Value).ToString());
}

=======

Thanks for your time, Kevin,
Mike

"Mike Goatly" wrote:
Thanks Kevin, I've used WMI before, so hopefully I'll be able to work this out!

I'll post a quick sample if I get it sorted.

Cheers,
Mike

"Kevin Yu [MSFT]" wrote:
Hi Mike,

It seems to be by design that the NextValue takes that much time to execute
in .NET framework. If you need the process to be faster, I think you can
try to use WMI instead, which gets value directly. You can try to post in
microsoft.public.win32.programmer.wmi newsgroup for wmi question. There
will be more professionals in that group to answer your question. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #6
You're welcome, Mike.

Thanks for sharing your experience with all the people here. If you have
any questions, please feel free to post them in the community.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ola Natvig | last post by:
Anybody out there who knows if the 4suite implementation of XSLT are a threadsafe one? -- -------------------------------------- Ola Natvig <ola.natvig@infosense.no> infoSense AS / development
5
by: Mike Goatly | last post by:
Hi, I need to be able to read the amount of processor time a thread has taken up. So far I've found the following methods open to me: 1) Find the relevant ProcessThread in the current...
1
by: benmorganpowell | last post by:
I have a small windows service which connects to a POP3 server at defined intervals, scans the available messages, extracts the required information and inserts the data into a SQL database. I am...
4
by: kaiteriteri | last post by:
I have a time-consuming VB.net application that i'd like to thread over 2 processors (that's all i've got in my machine!) and, hopefully, get it done in half the time. On running, the application...
3
by: Joe Befumo | last post by:
This is my first attempt at multi-thread programming, and I'm encountering a program-logic problem that I can't quite put my finger on. The program is pretty simple. I'm trying to validate a...
8
by: Joe Withawk | last post by:
I have an application in which there exist a thread that handles directx rendering. It runs on a dual core system with windows vista and xp. It is set to have highest priority and simply loops with...
10
by: sophie_newbie | last post by:
Hi, I'm trying to write a piece of code that spawns a thread and prints dots every half second until the thread spawned is finished. Code is something like this: import threading class...
29
by: NvrBst | last post by:
I've read a bit online seeing that two writes are not safe, which I understand, but would 1 thread push()'ing and 1 thread pop()'ing be thread-safe? Basically my situation is the follows: ...
12
by: Ronny | last post by:
Thanks Chris, Looks nice but I miss the dual way communication. In the main thread to deliver paramters and data to the worker thread- how can I do that? Regards Ronny Take a look at the...
2
by: poldoga | last post by:
Here is my problem. I have a thread which constantly polls a piece of hardware's serial output. Every time it detects a certain serial sequence, it triggers an event handler wherein I have code which...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.