clock_gettime(CLOCK_THREAD_CPUTIME_ID) behavior on x86
by kennthhz from LinuxQuestions.org on (#5QPEK)
My question on behavior of calling clock_gettime(CLOCK_THREAD_CPUTIME_ID). I looked at linux source code. There is a Posix implementation that coverts clock_id (CLOCK_THREAD_CPUTIME_ID) a specific kclock struct. Then does the per thread CPU tracking. But it seems like posix implementation will result in syscall. The code snippet is as follow https://elixir.bootlin.com/linux/lat...imers.c#L1085:
Code:SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
struct __kernel_timespec __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 kernel_tp;
int error;
if (!kc)
return -EINVAL;
error = kc->clock_get_timespec(which_clock, &kernel_tp);
if (!error && put_timespec64(&kernel_tp, tp))
error = -EFAULT;
return error;
}The second path is vdso which doesn't incur syscall. However, it doesn't seem to support CLOCK_THREAD_CPUTIME_ID. My linux is built for x86. which path will it take and how to rationalize that in the source code? https://elixir.bootlin.com/linux/lat...eofday.c#L228:
Code:static __always_inline int
__cvdso_clock_gettime_common(const struct vdso_data *vd, clockid_t clock,
struct __kernel_timespec *ts)
{
u32 msk;
/* Check for negative values or invalid clocks */
if (unlikely((u32) clock >= MAX_CLOCKS))
return -1;
/*
* Convert the clockid to a bitmask and use it to check which
* clocks are handled in the VDSO directly.
*/
msk = 1U << clock;
if (likely(msk & VDSO_HRES))
vd = &vd[CS_HRES_COARSE];
else if (msk & VDSO_COARSE)
return do_coarse(&vd[CS_HRES_COARSE], clock, ts);
else if (msk & VDSO_RAW)
vd = &vd[CS_RAW];
else
return -1;
return do_hres(vd, clock, ts);
}
Code:SYSCALL_DEFINE2(clock_gettime, const clockid_t, which_clock,
struct __kernel_timespec __user *, tp)
{
const struct k_clock *kc = clockid_to_kclock(which_clock);
struct timespec64 kernel_tp;
int error;
if (!kc)
return -EINVAL;
error = kc->clock_get_timespec(which_clock, &kernel_tp);
if (!error && put_timespec64(&kernel_tp, tp))
error = -EFAULT;
return error;
}The second path is vdso which doesn't incur syscall. However, it doesn't seem to support CLOCK_THREAD_CPUTIME_ID. My linux is built for x86. which path will it take and how to rationalize that in the source code? https://elixir.bootlin.com/linux/lat...eofday.c#L228:
Code:static __always_inline int
__cvdso_clock_gettime_common(const struct vdso_data *vd, clockid_t clock,
struct __kernel_timespec *ts)
{
u32 msk;
/* Check for negative values or invalid clocks */
if (unlikely((u32) clock >= MAX_CLOCKS))
return -1;
/*
* Convert the clockid to a bitmask and use it to check which
* clocks are handled in the VDSO directly.
*/
msk = 1U << clock;
if (likely(msk & VDSO_HRES))
vd = &vd[CS_HRES_COARSE];
else if (msk & VDSO_COARSE)
return do_coarse(&vd[CS_HRES_COARSE], clock, ts);
else if (msk & VDSO_RAW)
vd = &vd[CS_RAW];
else
return -1;
return do_hres(vd, clock, ts);
}