views:

601

answers:

3

Hi All,

I am looking a Tool on How to Find CPU Utilization of a Single Thread within a Process in VC++.

It would be great full if any one could provide me a tool.

Also it could be better if you guys provide how to do programmatically.

Thank you in Advance.

+1  A: 

Perhaps using GetThreadTimes would help ?

To elaborate if the thread belongs to another executable, that would be something (not tested) in the lines of:

// Returns true if thread times could be queried and its results are usable,
// false otherwise. Error handling is minimal, considering throwing detailed
// exceptions instead of returning a simple boolean.
bool get_remote_thread_times(DWORD thread_id, FILETIME & kernel_time, FILETIME & user_time)
{
  FILETIME creation_time = { 0 };
  FILETIME exit_time = { 0 };
  HANDLE thread_handle = OpenThread(THREAD_QUERY_INFORMATION, FALSE, thread_id);
  if (thread_handle == INVALID_HANDLE) return false;

  bool success = GetThreadTimes(thread_handle, &creation_time, &exit_time, &kernel_time, &user_time) != 0;

  CloseHandle(thread_handle);
  return success;
}
bltxd
+3  A: 

try using process explorer.. (tool).. pretty useful..

http://download.cnet.com/Process-Explorer/3000-2094_4-10223605.html

Madi D.
I also used process explorer. its good tool
anil
+1 this tool is really nice.
bltxd
A: 

I'm sure you're asking about Windows here, but for completeness sake, I'll describe one way this can be done on Unix systems.

The /proc file system contains information about all of the running processes on your machine. In this directory you'll find sub directory's for every process on the system (named by pid), inside each of these directory's is a file called stat. Look at 'man proc' and search for the "stat" entry. This file contains a bunch of information, but a couple of the fields can be used to determine how much user and kernel mode time this process has consumed.

With this knowledge in hand, look for a sub directory of a process called "task"... In here you'll find all the child processes spawned by the outer process.. and if you cd into those, you'll find that each has a stat file.

dicroce