From: Christoph Lameter POSIX clocks are to be implemented in the following way according to V3 of the Single Unix Specification: 1. CLOCK_PROCESS_CPUTIME_ID Implementations shall also support the special clockid_t value CLOCK_PROCESS_CPUTIME_ID, which represents the CPU-time clock of the calling process when invoking one of the clock_*() or timer_*() functions. For these clock IDs, the values returned by clock_gettime() and specified by clock_settime() represent the amount of execution time of the process associated with the clock. 2. CLOCK_THREAD_CPUTIME_ID Implementations shall also support the special clockid_t value CLOCK_THREAD_CPUTIME_ID, which represents the CPU-time clock of the calling thread when invoking one of the clock_*() or timer_*() functions. For these clock IDs, the values returned by clock_gettime() and specified by clock_settime() shall represent the amount of execution time of the thread associated with the clock. These times mentioned are CPU processing times and not the time that has passed since the startup of a process. Glibc currently provides its own implementation of these two clocks which is designed to return the time that passed since the startup of a process or a thread. Moreover Glibc's clocks are bound to CPU timers which is problematic when the frequency of the clock changes or the process is moved to a different processor whose cpu timer may not be fully synchronized to the cpu timer of the current CPU. This patchset results in a both clocks working reliably. This patch: * Add CLOCK_THREAD_CPUTIME and CLOCK_PROCESS_CPUTIME_ID processing * Add timer_create override for posix clocks * Complete implementation of nanosleep overrride for posix clocks * export posix clock registration in include/linux/posix-timers.h * Allow up to 16 posix clocks Signed-off-by: Christoph Lameter Signed-off-by: Andrew Morton --- 25-akpm/include/linux/posix-timers.h | 18 ++++ 25-akpm/include/linux/sched.h | 2 25-akpm/include/linux/time.h | 7 + 25-akpm/kernel/posix-timers.c | 130 +++++++++++++++++++++++++++++++---- 4 files changed, 140 insertions(+), 17 deletions(-) diff -puN include/linux/posix-timers.h~posix-compliant-cpu-clocks-v6-generic-kernel-patch include/linux/posix-timers.h --- 25/include/linux/posix-timers.h~posix-compliant-cpu-clocks-v6-generic-kernel-patch Fri Oct 1 14:07:23 2004 +++ 25-akpm/include/linux/posix-timers.h Fri Oct 1 14:15:10 2004 @@ -33,9 +33,10 @@ struct k_clock { struct k_clock_abs *abs_struct; int (*clock_set) (struct timespec * tp); int (*clock_get) (struct timespec * tp); - int (*nsleep) (int flags, - struct timespec * new_setting, - struct itimerspec * old_setting); + int (*timer_create) (int which_clock, struct sigevent __user *timer_event_spec, + timer_t __user * created_timer_id); + int (*nsleep) (int which_clock, int flags, + struct timespec * t); int (*timer_set) (struct k_itimer * timr, int flags, struct itimerspec * new_setting, struct itimerspec * old_setting); @@ -43,6 +44,16 @@ struct k_clock { void (*timer_get) (struct k_itimer * timr, struct itimerspec * cur_setting); }; + +void register_posix_clock(int clock_id, struct k_clock *new_clock); + +/* Error handlers for timer_create and nanosleep */ +int do_posix_clock_notimer_create(int which_clock, + struct sigevent __user *time_event_spec, + timer_t __user *created_timer_id); + +int do_posix_clock_nonanosleep(int which_clock, int flags, struct timespec * t); + struct now_struct { unsigned long jiffies; }; @@ -62,3 +73,4 @@ struct now_struct { } \ }while (0) #endif + diff -puN include/linux/sched.h~posix-compliant-cpu-clocks-v6-generic-kernel-patch include/linux/sched.h --- 25/include/linux/sched.h~posix-compliant-cpu-clocks-v6-generic-kernel-patch Fri Oct 1 14:07:23 2004 +++ 25-akpm/include/linux/sched.h Fri Oct 1 14:15:10 2004 @@ -293,6 +293,7 @@ struct signal_struct { /* POSIX.1b Interval Timers */ struct list_head posix_timers; + int process_clock_offset; /* for CLOCK_PROCESS_CPUTIME_ID */ /* job control IDs */ pid_t pgrp; @@ -622,6 +623,7 @@ struct task_struct { unsigned long utime, stime; unsigned long nvcsw, nivcsw; /* context switch counts */ struct timespec start_time; + int thread_clock_offset; /* offset to thread_clock for CLOCK_THREAD_CPUTIME_ID */ /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */ unsigned long min_flt, maj_flt; /* process credentials */ diff -puN include/linux/time.h~posix-compliant-cpu-clocks-v6-generic-kernel-patch include/linux/time.h --- 25/include/linux/time.h~posix-compliant-cpu-clocks-v6-generic-kernel-patch Fri Oct 1 14:07:23 2004 +++ 25-akpm/include/linux/time.h Fri Oct 1 14:15:10 2004 @@ -157,7 +157,12 @@ struct itimerval { #define CLOCK_REALTIME_HR 4 #define CLOCK_MONOTONIC_HR 5 -#define MAX_CLOCKS 6 +/* + * The IDs of various hardware clocks + */ + + +#define MAX_CLOCKS 16 #define CLOCKS_MASK (CLOCK_REALTIME | CLOCK_MONOTONIC | \ CLOCK_REALTIME_HR | CLOCK_MONOTONIC_HR) #define CLOCKS_MONO (CLOCK_MONOTONIC & CLOCK_MONOTONIC_HR) diff -puN kernel/posix-timers.c~posix-compliant-cpu-clocks-v6-generic-kernel-patch kernel/posix-timers.c --- 25/kernel/posix-timers.c~posix-compliant-cpu-clocks-v6-generic-kernel-patch Fri Oct 1 14:07:23 2004 +++ 25-akpm/kernel/posix-timers.c Fri Oct 1 14:15:10 2004 @@ -10,6 +10,10 @@ * 2004-06-01 Fix CLOCK_REALTIME clock/timer TIMER_ABSTIME bug. * Copyright (C) 2004 Boris Hu * + * 2004-07-27 Provide POSIX compliant clocks + * CLOCK_PROCESS_CPUTIME_ID and CLOCK_THREAD_CPUTIME_ID. + * by Christoph Lameter + * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or (at @@ -134,18 +138,10 @@ static spinlock_t idr_lock = SPIN_LOCK_U * resolution. Here we define the standard CLOCK_REALTIME as a * 1/HZ resolution clock. * - * CPUTIME & THREAD_CPUTIME: We are not, at this time, definding these - * two clocks (and the other process related clocks (Std - * 1003.1d-1999). The way these should be supported, we think, - * is to use large negative numbers for the two clocks that are - * pinned to the executing process and to use -pid for clocks - * pinned to particular pids. Calls which supported these clock - * ids would split early in the function. - * * RESOLUTION: Clock resolution is used to round up timer and interval * times, NOT to report clock times, which are reported with as * much resolution as the system can muster. In some cases this - * resolution may depend on the underlaying clock hardware and + * resolution may depend on the underlying clock hardware and * may not be quantifiable until run time, and only then is the * necessary code is written. The standard says we should say * something about this issue in the documentation... @@ -163,7 +159,7 @@ static spinlock_t idr_lock = SPIN_LOCK_U * * At this time all functions EXCEPT clock_nanosleep can be * redirected by the CLOCKS structure. Clock_nanosleep is in - * there, but the code ignors it. + * there, but the code ignores it. * * Permissions: It is assumed that the clock_settime() function defined * for each clock will take care of permission checks. Some @@ -198,8 +194,16 @@ static int do_posix_gettime(struct k_clo static u64 do_posix_clock_monotonic_gettime_parts( struct timespec *tp, struct timespec *mo); int do_posix_clock_monotonic_gettime(struct timespec *tp); -int do_posix_clock_monotonic_settime(struct timespec *tp); +static int do_posix_clock_monotonic_settime(struct timespec *tp); +static int do_posix_clock_process_gettime(struct timespec *tp); +static int do_posix_clock_process_settime(struct timespec *tp); +static int do_posix_clock_thread_gettime(struct timespec *tp); +static int do_posix_clock_thread_settime(struct timespec *tp); static struct k_itimer *lock_timer(timer_t timer_id, unsigned long *flags); +int do_posix_clock_notimer_create(int which_clock, + struct sigevent __user *time_event_spec, + timer_t __user *created_timer_id); +int do_posix_clock_nonanosleep(int which_clock, int flags, struct timespec * t); static inline void unlock_timer(struct k_itimer *timr, unsigned long flags) { @@ -219,6 +223,20 @@ static __init int init_posix_timers(void .clock_get = do_posix_clock_monotonic_gettime, .clock_set = do_posix_clock_monotonic_settime }; + struct k_clock clock_thread = {.res = CLOCK_REALTIME_RES, + .abs_struct = NULL, + .clock_get = do_posix_clock_thread_gettime, + .clock_set = do_posix_clock_thread_settime, + .timer_create = do_posix_clock_notimer_create, + .nsleep = do_posix_clock_nonanosleep + }; + struct k_clock clock_process = {.res = CLOCK_REALTIME_RES, + .abs_struct = NULL, + .clock_get = do_posix_clock_process_gettime, + .clock_set = do_posix_clock_process_settime, + .timer_create = do_posix_clock_notimer_create, + .nsleep = do_posix_clock_nonanosleep + }; #ifdef CONFIG_TIME_INTERPOLATION /* Clocks are more accurate with time interpolators */ @@ -227,6 +245,8 @@ static __init int init_posix_timers(void register_posix_clock(CLOCK_REALTIME, &clock_realtime); register_posix_clock(CLOCK_MONOTONIC, &clock_monotonic); + register_posix_clock(CLOCK_PROCESS_CPUTIME_ID, &clock_process); + register_posix_clock(CLOCK_THREAD_CPUTIME_ID, &clock_thread); posix_timers_cache = kmem_cache_create("posix_timers_cache", sizeof (struct k_itimer), 0, 0, NULL, NULL); @@ -578,6 +598,10 @@ sys_timer_create(clockid_t which_clock, !posix_clocks[which_clock].res) return -EINVAL; + if (posix_clocks[which_clock].timer_create) + return posix_clocks[which_clock].timer_create(which_clock, + timer_event_spec, created_timer_id); + new_timer = alloc_posix_timer(); if (unlikely(!new_timer)) return -EAGAIN; @@ -1223,11 +1247,88 @@ int do_posix_clock_monotonic_gettime(str return 0; } -int do_posix_clock_monotonic_settime(struct timespec *tp) +static int do_posix_clock_monotonic_settime(struct timespec *tp) { return -EINVAL; } +int do_posix_clock_notimer_create(int which_clock, + struct sigevent __user *timer_event_spec, + timer_t __user *created_timer_id) { + return -EINVAL; +} + +int do_posix_clock_nonanosleep(int which_lock, int flags,struct timespec * t) { +/* Single Unix specficiation says to return ENOTSUP but we do not have that */ + return -EINVAL; +} + +/* + * Single Unix Specification V3: + * + * Implementations shall also support the special clockid_t value + * CLOCK_THREAD_CPUTIME_ID, which represents the CPU-time clock of the calling + * thread when invoking one of the clock_*() or timer_*() functions. For these + * clock IDs, the values returned by clock_gettime() and specified by + * clock_settime() shall represent the amount of execution time of the thread + * associated with the clock. + */ +static int do_posix_clock_thread_gettime(struct timespec *tp) +{ + jiffies_to_timespec(current->utime + current->stime + + current->thread_clock_offset, tp); + return 0; +} + +static int do_posix_clock_thread_settime(struct timespec *tp) +{ + current->thread_clock_offset = timespec_to_jiffies(tp) + - current->utime - current->stime; + return 0; +} + +/* + * Single Unix Specification V3: + * + * Implementations shall also support the special clockid_t value + * CLOCK_PROCESS_CPUTIME_ID, which represents the CPU-time clock of the + * calling process when invoking one of the clock_*() or timer_*() functions. + * For these clock IDs, the values returned by clock_gettime() and specified + * by clock_settime() represent the amount of execution time of the process + * associated with the clock. + */ + +static unsigned long process_ticks(void) { + unsigned long ticks; + task_t *t; + + spin_lock(¤t->sighand->siglock); + /* The signal structure is shared between all threads */ + ticks = current->signal->utime + current->signal->stime; + + /* Add up the cpu time for all the still running threads of this process */ + t = current; + do { + ticks += t->utime + t->stime; + t = next_thread(t); + } while (t != current); + + spin_unlock(¤t->sighand->siglock); + return ticks; +} + +static int do_posix_clock_process_gettime(struct timespec *tp) +{ + jiffies_to_timespec(current->signal->process_clock_offset + process_ticks(), tp); + return 0; +} + +static int do_posix_clock_process_settime(struct timespec *tp) +{ + current->signal->process_clock_offset = timespec_to_jiffies(tp) - process_ticks(); + return 0; +} + asmlinkage long sys_clock_settime(clockid_t which_clock, const struct timespec __user *tp) { @@ -1414,7 +1515,10 @@ sys_clock_nanosleep(clockid_t which_cloc if ((unsigned) t.tv_nsec >= NSEC_PER_SEC || t.tv_sec < 0) return -EINVAL; - ret = do_clock_nanosleep(which_clock, flags, &t); + if (posix_clocks[which_clock].nsleep) + ret = posix_clocks[which_clock].nsleep(which_clock, flags, &t); + else + ret = do_clock_nanosleep(which_clock, flags, &t); /* * Do this here as do_clock_nanosleep does not have the real address */ _