Skip to contentSkip to conversation

linux missing support for _POSIX_THREAD_CPUTIME?

DISCUSSION Chris Friesen 3 participants 7 messages May 27, 2009

lore
Share & tools
lore
#0 lore raw

Hi all,

POSIX defines optional support for clock ID values obtained by invoking pthread_getcpuclockid(). When passed to clock_gettime(), this allows the caller to request the CPU-time clock of an arbitrary thread within the same process as the caller.

It appears that linux doesn't support this functionality--is that correct? Is there any plan to enable this?

Is there any current way to obtain the runtime of a particular thread without knowing its tid (since the thread_id to tid mapping is known only to glibc)?

Thanks,

Chris

#1 lore raw

On Wed, 27 May 2009, Chris Friesen wrote:

That's more a question for the glibc folks. CC'ed.

Quoted 18 lines

Hi all,

POSIX defines optional support for clock ID values obtained by invoking pthread_getcpuclockid(). When passed to clock_gettime(), this allows the caller to request the CPU-time clock of an arbitrary thread within the same process as the caller.

It appears that linux doesn't support this functionality--is that correct? Is there any plan to enable this?

Is there any current way to obtain the runtime of a particular thread without knowing its tid (since the thread_id to tid mapping is known only to glibc)?

Thanks,

Chris

#6 lore raw

Re: linux missing support for _POSIX_THREAD_CPUTIME ?

In reply to Chris Friesen
Quoted 2 lines · Chris Friesen

It appears that linux doesn't support this functionality--is that correct? Is there any plan to enable this?

In particular, it seems to fail the invalid_clockid() test. Is that function expecting the clock ID to be negative for this case?

Thanks,

Chris

#2 lore raw
In reply to Thomas Gleixner
Quoted 3 lines · Thomas Gleixner

On Wed, 27 May 2009, Chris Friesen wrote:

That's more a question for the glibc folks. CC'ed.

Actually, I'm looking at the kernel code here. It looks like posix_cpu_clock_get() should be able to handle a per-task cpu clock for another thread within the same process. However, it's only ever called by thread_cpu_clock_get(), which hardcodes a clock_id of THREAD_CLOCK, which corresponds to the current thread.

Similarly, invalid_clockid() will say that the clock_id is invalid if it's positive but greater than 15. If my math is right, this means that any pid > 2 will result in invalid_clockid() failing.

Chris

#3 lore raw
Quoted 10 lines · Chris Friesen
Quoted 3 lines · Thomas Gleixner

On Wed, 27 May 2009, Chris Friesen wrote:

That's more a question for the glibc folks. CC'ed.

Actually, I'm looking at the kernel code here. It looks like posix_cpu_clock_get() should be able to handle a per-task cpu clock for another thread within the same process. However, it's only ever called by thread_cpu_clock_get(), which hardcodes a clock_id of THREAD_CLOCK, which corresponds to the current thread.

No, it's also called via the CLOCK_DISPATCH magic in posix_timers.c

Quoted 3 lines

Similarly, invalid_clockid() will say that the clock_id is invalid if it's positive but greater than 15. If my math is right, this means that any pid > 2 will result in invalid_clockid() failing.

posix_cpu_clock_get() does:

  const pid_t pid = CPUCLOCK_PID(which_clock);

CPUCLOCK_PID is defined as:

#define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))

include/linux/posix-timers.h has the helper macros to build the clock_id value for posix_cpu_clock_get()

#define MAKE_PROCESS_CPUCLOCK(pid, clock) \
        ((~(clockid_t) (pid) << 3) | (clockid_t) (clock))

#define MAKE_THREAD_CPUCLOCK(tid, clock) \
MAKE_PROCESS_CPUCLOCK((tid), (clock) | CPUCLOCK_PERTHREAD_MASK)

Hope that helps,

tglx

#5 lore raw
In reply to Thomas Gleixner
Quoted 7 lines

POSIX defines optional support for clock ID values obtained by invoking pthread_getcpuclockid(). When passed to clock_gettime(), this allows the caller to request the CPU-time clock of an arbitrary thread within the same process as the caller.

It appears that linux doesn't support this functionality--is that correct? Is there any plan to enable this?

glibc does implement pthread_getcpuclockid using the kernel support for it. It uses MAKE_THREAD_CPUCLOCK(tid, CPUCLOCK_SCHED) (see linux/posix-timers.h).

Quoted 3 lines

Is there any current way to obtain the runtime of a particular thread without knowing its tid (since the thread_id to tid mapping is known only to glibc)?

You need a tid to construct the clock ID with MAKE_THREAD_CPUCLOCK(). The pthread_getcpuclockid function exists to do this for you when you are using pthreads. If you are not using pthreads, then certainly you have to figure out what a thread's tid is yourself.

Thanks,
Roland

#4 lore raw
In reply to Thomas Gleixner
Quoted 9 lines · Thomas Gleixner
Quoted 6 lines · Chris Friesen
Quoted 3 lines · Thomas Gleixner

On Wed, 27 May 2009, Chris Friesen wrote:

That's more a question for the glibc folks. CC'ed.

Quoted 12 lines
Quoted 5 lines

Actually, I'm looking at the kernel code here. It looks like posix_cpu_clock_get() should be able to handle a per-task cpu clock for another thread within the same process. However, it's only ever called by thread_cpu_clock_get(), which hardcodes a clock_id of THREAD_CLOCK, which corresponds to the current thread.

No, it's also called via the CLOCK_DISPATCH magic in posix_timers.c

Quoted 3 lines

Similarly, invalid_clockid() will say that the clock_id is invalid if it's positive but greater than 15. If my math is right, this means that any pid > 2 will result in invalid_clockid() failing.

Both invalid_clockid() and CLOCK_DISPATCH assume that the clock_id will be negative in order to call posix_cpu_clock_get().

In pthread_getcpuclockid() in glibc, I couldn't see how we were ending up with a negative clock_id. However, I finally clued in to the fact that there are multiple implementations of this function, and in one code path in one of the implementations we call MAKE_THREAD_CPUCLOCK() which can result in a negative clock_id.

Thanks for the help.

Chris