Welcome to Linux Forums! With a comprehensive Linux Forum, information on various types of Linux software and many Linux Reviews articles, we have all the knowledge you need a click away, or accessible via our knowledgeable members.
Write an article for LinuxForums Today! Win Great Prizes!
Find the answer to your Linux question:
New to Linux Forums? Register here for free!
    Linux Forums > GNU Linux Zone > Linux Programming & Scripting > pthreads : thread time vs. process time

Forgot Password?
 Linux Programming & Scripting   C, Perl, PHP, Bash Scripts, anything programming or script related post in here!

Site Navigation
Linux Articles
Linux Forums
Linux Downloads
Linux Hosting
Free Magazines
Job Board
IRC Chat
RSS Feeds
Linux Forum Topics
Linux Forums
Your Distro
Linux Resources
GNU Linux Zone
The Community
Reply
 
Thread Tools Display Modes
Old 08-20-2007   #1 (permalink)
Just Joined!
 
Join Date: Aug 2007
Posts: 2
pthreads : thread time vs. process time

Reading up the man pages about pthreads, I gather that up until Linux kernel version 2.6.9, the NPTL implementation of pthreads had a "bug" which I found quite useful in that times() and getrusage() would return values on a per-thread basis rather than process-wide. I was then able to know how much CPU-time had a particular thread worked. However, I understand that this was fixed in kernel 2.6.9 because it was non-conforming with the POSIX specifications. I miss this accidental feature and would like to know if there exists a method or a series of method that I could use to get that information.

I have a set of data on which I would like a bunch of threads to work for at least 2 CPU-seconds each, while I don't care about the wall time. Using process time can't work because in extreme cases, a process using 10 threads could have a single thread work for 2 seconds and all of the 10 threads would return a process-time of 2 seconds, while 9 of them didn't get a chance to work.

Did the kernel 2.6.9 fix completely remove the ability to check a thread CPU-time or did that feature get hidden in another system call of which I am unaware?

Thanks.
Sleeping is offline  



Reply With Quote
Old 12-10-2007   #2 (permalink)
Just Joined!
 
Join Date: Dec 2007
Posts: 2
Hi,
I actually had the same problem.
I found out that the function clock_gettime returns timing per thread with more or less nanosecond precision (see clock_getres() )
Code:
#include <time.h>
#include <unistd.h> // for sysconf
int err;
struct timespec t;
if (sysconf(_POSIX_THREAD_CPUTIME)){
  err = clock_gettime(CLOCK_THREAD_CPUTIME_ID,&t);
}
To link you must use -lrt.
It works per thread, except that the time returned is the sum of system time and user time.

So there are still unanswered questions:
1. Is there a method to split the system and user times?
2. Why POSIX standard doesn't allow to use getrusage per thread?
3. Is there a way to have all getrusage measurements per thread anyway?

Greetings,
Wojtek
Attached Files
File Type: zip pthread_clock_test.c.zip (972 Bytes, 30 views)
wojtek is offline   Reply With Quote
Old 12-10-2007   #3 (permalink)
Just Joined!
 
Join Date: Dec 2007
Posts: 2
Actually the program I attached does not show that the system time is added to user time by clock_gettime, but it is. I checked this in a program using the harddisk intensively.
wojtek is offline   Reply With Quote
Old 12-10-2007   #4 (permalink)
Just Joined!
 
Join Date: Aug 2007
Posts: 2
Cool Ugly hack

I managed to get the user time and system time of the thread with an ugly hack which might not be as portable as I would like it to be, but still mostly works until I find a POSIX way to do it. Here's what I've done:

The pseudo-file /proc/<PID>/task/<TID>/stat file contains a lot of info about the thread TID. Reading the file will yield 42 fields which are separated by whitespace. You can find out what each field means with "man 5 proc". utime (user time) and stime (system time) are respectively the 14th and 15th fields.

All you need to do then is parse(*) the string you get from reading /proc/<PID>/task/<TID>/stat and grab the 14th field, which represents the number of jiffies spent in user-time.

(*) While parsing the string, keep in mind that the second field (executable name) can contain whitespaces)

The operation isn't very fast, so it shouldn't be done in a tight loop, but it's more accurate than what rusage can provide me, and the processing is mostly done in system time, so it doesn't skew the results.

Code:
unsigned long GetThreadTime()
{
	char procFilename[256] ;
	char buffer[1024] ;

	pid_t pid = ::getpid() ;
	pid_t tid = ::gettid() ;

	sprintf(procFilename, "/proc/%d/task/%d/stat",pid,tid) ;
	int fd, num_read ;
	fd = open(procFilename, O_RDONLY, 0);
	num_read = read(fd, buffer, 1023);
	close(fd);
	buffer[num_read] = '\0';

	char* ptrUsr = strrchr(buffer, ')') + 1 ;
	for(int i = 3 ; i != 14 ; ++i) ptrUsr = strchr(ptrUsr+1, ' ') ;

	ptrUsr++;
	long jiffies_user = atol(ptrUsr) ;
	long jiffies_sys = atol(strchr(ptrUsr,' ') + 1) ;

	return jiffies_user ;
}
Sleeping is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Free Magazines
Run Your Own Web Server Using Linux & Apache - Free 191 Page Preview
Learn about everything you'll need to build and maintain your Linux servers, and to deploy Web applications to them.
subscribe
Open Source Security Myths Dispelled
Dispel the five major myths surrounding Open Source Security and gain the tools necessary to make a truly informed decision for your IT organization
subscribe
InformationWeek
InformationWeek is the only newsweekly you'll need to stay on top of the latest developments in information technology.
subscribe



All times are GMT. The time now is 03:21 PM.






© 2000 - - All Rights Reserved - Property of  MAS Media

Content Relevant URLs by vBSEO 3.3.1