From 3f0806aa1fc8c8dc2f294421d558472faec7fd3f Mon Sep 17 00:00:00 2001 From: davidxu Date: Fri, 17 Aug 2012 02:26:31 +0000 Subject: Implement syscall clock_getcpuclockid2, so we can get a clock id for process, thread or others we want to support. Use the syscall to implement POSIX API clock_getcpuclock and pthread_getcpuclockid. PR: 168417 --- sys/compat/freebsd32/freebsd32_syscall.h | 1 + sys/compat/freebsd32/freebsd32_syscalls.c | 2 +- sys/compat/freebsd32/freebsd32_sysent.c | 2 +- sys/compat/freebsd32/freebsd32_systrace_args.c | 30 +++++ sys/compat/freebsd32/syscalls.master | 3 +- sys/kern/init_sysent.c | 2 +- sys/kern/kern_time.c | 149 +++++++++++++++++++++++-- sys/kern/syscalls.c | 2 +- sys/kern/syscalls.master | 3 +- sys/kern/systrace_args.c | 30 +++++ sys/sys/syscall.h | 1 + sys/sys/syscall.mk | 1 + sys/sys/sysproto.h | 7 ++ sys/sys/time.h | 7 ++ sys/sys/unistd.h | 2 +- 15 files changed, 225 insertions(+), 17 deletions(-) (limited to 'sys') diff --git a/sys/compat/freebsd32/freebsd32_syscall.h b/sys/compat/freebsd32/freebsd32_syscall.h index b1684a1..240f1ca 100644 --- a/sys/compat/freebsd32/freebsd32_syscall.h +++ b/sys/compat/freebsd32/freebsd32_syscall.h @@ -210,6 +210,7 @@ #define FREEBSD32_SYS_ffclock_getcounter 241 #define FREEBSD32_SYS_ffclock_setestimate 242 #define FREEBSD32_SYS_ffclock_getestimate 243 +#define FREEBSD32_SYS_clock_getcpuclockid2 247 #define FREEBSD32_SYS_minherit 250 #define FREEBSD32_SYS_rfork 251 #define FREEBSD32_SYS_openbsd_poll 252 diff --git a/sys/compat/freebsd32/freebsd32_syscalls.c b/sys/compat/freebsd32/freebsd32_syscalls.c index 18ade01..75e4d22 100644 --- a/sys/compat/freebsd32/freebsd32_syscalls.c +++ b/sys/compat/freebsd32/freebsd32_syscalls.c @@ -257,7 +257,7 @@ const char *freebsd32_syscallnames[] = { "#244", /* 244 = nosys */ "#245", /* 245 = nosys */ "#246", /* 246 = nosys */ - "#247", /* 247 = nosys */ + "clock_getcpuclockid2", /* 247 = clock_getcpuclockid2 */ "#248", /* 248 = ntp_gettime */ "#249", /* 249 = nosys */ "minherit", /* 250 = minherit */ diff --git a/sys/compat/freebsd32/freebsd32_sysent.c b/sys/compat/freebsd32/freebsd32_sysent.c index f657cc6..334c520 100644 --- a/sys/compat/freebsd32/freebsd32_sysent.c +++ b/sys/compat/freebsd32/freebsd32_sysent.c @@ -294,7 +294,7 @@ struct sysent freebsd32_sysent[] = { { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 244 = nosys */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 245 = nosys */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 246 = nosys */ - { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 247 = nosys */ + { AS(clock_getcpuclockid2_args), (sy_call_t *)sys_clock_getcpuclockid2, AUE_NULL, NULL, 0, 0, 0, SY_THR_STATIC }, /* 247 = clock_getcpuclockid2 */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 248 = ntp_gettime */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 249 = nosys */ { AS(minherit_args), (sy_call_t *)sys_minherit, AUE_MINHERIT, NULL, 0, 0, 0, SY_THR_STATIC }, /* 250 = minherit */ diff --git a/sys/compat/freebsd32/freebsd32_systrace_args.c b/sys/compat/freebsd32/freebsd32_systrace_args.c index c7a43d0..e5588d0 100644 --- a/sys/compat/freebsd32/freebsd32_systrace_args.c +++ b/sys/compat/freebsd32/freebsd32_systrace_args.c @@ -1224,6 +1224,15 @@ systrace_args(int sysnum, void *params, uint64_t *uarg, int *n_args) *n_args = 1; break; } + /* clock_getcpuclockid2 */ + case 247: { + struct clock_getcpuclockid2_args *p = params; + iarg[0] = p->id; /* id_t */ + iarg[1] = p->which; /* int */ + uarg[2] = (intptr_t) p->clock_id; /* clockid_t * */ + *n_args = 3; + break; + } /* minherit */ case 250: { struct minherit_args *p = params; @@ -4991,6 +5000,22 @@ systrace_entry_setargdesc(int sysnum, int ndx, char *desc, size_t descsz) break; }; break; + /* clock_getcpuclockid2 */ + case 247: + switch(ndx) { + case 0: + p = "id_t"; + break; + case 1: + p = "int"; + break; + case 2: + p = "clockid_t *"; + break; + default: + break; + }; + break; /* minherit */ case 250: switch(ndx) { @@ -8890,6 +8915,11 @@ systrace_return_setargdesc(int sysnum, int ndx, char *desc, size_t descsz) if (ndx == 0 || ndx == 1) p = "int"; break; + /* clock_getcpuclockid2 */ + case 247: + if (ndx == 0 || ndx == 1) + p = "int"; + break; /* minherit */ case 250: if (ndx == 0 || ndx == 1) diff --git a/sys/compat/freebsd32/syscalls.master b/sys/compat/freebsd32/syscalls.master index 97f09d0..478558a 100644 --- a/sys/compat/freebsd32/syscalls.master +++ b/sys/compat/freebsd32/syscalls.master @@ -457,7 +457,8 @@ 244 AUE_NULL UNIMPL nosys 245 AUE_NULL UNIMPL nosys 246 AUE_NULL UNIMPL nosys -247 AUE_NULL UNIMPL nosys +247 AUE_NULL NOPROTO { int clock_getcpuclockid2(id_t id,\ + int which, clockid_t *clock_id); } 248 AUE_NULL UNIMPL ntp_gettime 249 AUE_NULL UNIMPL nosys ; syscall numbers initially used in OpenBSD diff --git a/sys/kern/init_sysent.c b/sys/kern/init_sysent.c index 4a4328a..721143d 100644 --- a/sys/kern/init_sysent.c +++ b/sys/kern/init_sysent.c @@ -281,7 +281,7 @@ struct sysent sysent[] = { { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 244 = nosys */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 245 = nosys */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 246 = nosys */ - { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 247 = nosys */ + { AS(clock_getcpuclockid2_args), (sy_call_t *)sys_clock_getcpuclockid2, AUE_NULL, NULL, 0, 0, 0, SY_THR_STATIC }, /* 247 = clock_getcpuclockid2 */ { AS(ntp_gettime_args), (sy_call_t *)sys_ntp_gettime, AUE_NULL, NULL, 0, 0, SYF_CAPENABLED, SY_THR_STATIC }, /* 248 = ntp_gettime */ { 0, (sy_call_t *)nosys, AUE_NULL, NULL, 0, 0, 0, SY_THR_ABSENT }, /* 249 = nosys */ { AS(minherit_args), (sy_call_t *)sys_minherit, AUE_MINHERIT, NULL, 0, 0, SYF_CAPENABLED, SY_THR_STATIC }, /* 250 = minherit */ diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c index 4ba6117..c0e7831 100644 --- a/sys/kern/kern_time.c +++ b/sys/kern/kern_time.c @@ -58,6 +58,12 @@ __FBSDID("$FreeBSD$"); #include #define MAX_CLOCKS (CLOCK_MONOTONIC+1) +#define CPUCLOCK_BIT 0x80000000 +#define CPUCLOCK_PROCESS_BIT 0x40000000 +#define CPUCLOCK_ID_MASK (~(CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT)) +#define MAKE_THREAD_CPUCLOCK(tid) (CPUCLOCK_BIT|(tid)) +#define MAKE_PROCESS_CPUCLOCK(pid) \ + (CPUCLOCK_BIT|CPUCLOCK_PROCESS_BIT|(pid)) static struct kclock posix_clocks[MAX_CLOCKS]; static uma_zone_t itimer_zone = NULL; @@ -165,6 +171,52 @@ settime(struct thread *td, struct timeval *tv) } #ifndef _SYS_SYSPROTO_H_ +struct clock_getcpuclockid2_args { + id_t id; + int which, + clockid_t *clock_id; +}; +#endif +/* ARGSUSED */ +int +sys_clock_getcpuclockid2(struct thread *td, struct clock_getcpuclockid2_args *uap) +{ + clockid_t clk_id; + struct proc *p; + pid_t pid; + lwpid_t tid; + int error; + + switch(uap->which) { + case CPUCLOCK_WHICH_PID: + if (uap->id != 0) { + p = pfind(uap->id); + if (p == NULL) + return (ESRCH); + error = p_cansee(td, p); + PROC_UNLOCK(p); + if (error) + return (error); + pid = uap->id; + } else { + pid = td->td_proc->p_pid; + } + clk_id = MAKE_PROCESS_CPUCLOCK(pid); + break; + case CPUCLOCK_WHICH_TID: + if (uap->id == 0) + tid = td->td_tid; + else + tid = uap->id; + clk_id = MAKE_THREAD_CPUCLOCK(tid); + break; + default: + return (EINVAL); + } + return (copyout(&clk_id, uap->clock_id, sizeof(clockid_t))); +} + +#ifndef _SYS_SYSPROTO_H_ struct clock_gettime_args { clockid_t clock_id; struct timespec *tp; @@ -184,12 +236,85 @@ sys_clock_gettime(struct thread *td, struct clock_gettime_args *uap) return (error); } +static inline void +cputick2timespec(uint64_t runtime, struct timespec *ats) +{ + runtime = cputick2usec(runtime); + ats->tv_sec = runtime / 1000000; + ats->tv_nsec = runtime % 1000000 * 1000; +} + +static void +get_thread_cputime(struct thread *targettd, struct timespec *ats) +{ + uint64_t runtime, curtime, switchtime; + + if (targettd == NULL) { /* current thread */ + critical_enter(); + switchtime = PCPU_GET(switchtime); + curtime = cpu_ticks(); + runtime = curthread->td_runtime; + critical_exit(); + runtime += curtime - switchtime; + } else { + thread_lock(targettd); + runtime = targettd->td_runtime; + thread_unlock(targettd); + } + cputick2timespec(runtime, ats); +} + +static void +get_process_cputime(struct proc *targetp, struct timespec *ats) +{ + uint64_t runtime; + struct rusage ru; + + PROC_SLOCK(targetp); + rufetch(targetp, &ru); + runtime = targetp->p_rux.rux_runtime; + PROC_SUNLOCK(targetp); + cputick2timespec(runtime, ats); +} + +static int +get_cputime(struct thread *td, clockid_t clock_id, struct timespec *ats) +{ + struct proc *p, *p2; + struct thread *td2; + lwpid_t tid; + pid_t pid; + int error; + + p = td->td_proc; + if ((clock_id & CPUCLOCK_PROCESS_BIT) == 0) { + tid = clock_id & CPUCLOCK_ID_MASK; + td2 = tdfind(tid, p->p_pid); + if (td2 == NULL) + return (EINVAL); + get_thread_cputime(td2, ats); + PROC_UNLOCK(td2->td_proc); + } else { + pid = clock_id & CPUCLOCK_ID_MASK; + p2 = pfind(pid); + if (p2 == NULL) + return (EINVAL); + error = p_cansee(td, p2); + if (error) { + PROC_UNLOCK(p2); + return (EINVAL); + } + get_process_cputime(p2, ats); + PROC_UNLOCK(p2); + } + return (0); +} + int kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) { struct timeval sys, user; struct proc *p; - uint64_t runtime, curtime, switchtime; p = td->td_proc; switch (clock_id) { @@ -232,17 +357,17 @@ kern_clock_gettime(struct thread *td, clockid_t clock_id, struct timespec *ats) ats->tv_nsec = 0; break; case CLOCK_THREAD_CPUTIME_ID: - critical_enter(); - switchtime = PCPU_GET(switchtime); - curtime = cpu_ticks(); - runtime = td->td_runtime; - critical_exit(); - runtime = cputick2usec(runtime + curtime - switchtime); - ats->tv_sec = runtime / 1000000; - ats->tv_nsec = runtime % 1000000 * 1000; + get_thread_cputime(NULL, ats); + break; + case CLOCK_PROCESS_CPUTIME_ID: + PROC_LOCK(p); + get_process_cputime(p, ats); + PROC_UNLOCK(p); break; default: - return (EINVAL); + if ((int)clock_id >= 0) + return (EINVAL); + return (get_cputime(td, clock_id, ats)); } return (0); } @@ -336,12 +461,16 @@ kern_clock_getres(struct thread *td, clockid_t clock_id, struct timespec *ts) ts->tv_nsec = 0; break; case CLOCK_THREAD_CPUTIME_ID: + case CLOCK_PROCESS_CPUTIME_ID: + cputime: /* sync with cputick2usec */ ts->tv_nsec = 1000000 / cpu_tickrate(); if (ts->tv_nsec == 0) ts->tv_nsec = 1000; break; default: + if ((int)clock_id < 0) + goto cputime; return (EINVAL); } return (0); diff --git a/sys/kern/syscalls.c b/sys/kern/syscalls.c index a2ea5c8..34829a6 100644 --- a/sys/kern/syscalls.c +++ b/sys/kern/syscalls.c @@ -254,7 +254,7 @@ const char *syscallnames[] = { "#244", /* 244 = nosys */ "#245", /* 245 = nosys */ "#246", /* 246 = nosys */ - "#247", /* 247 = nosys */ + "clock_getcpuclockid2", /* 247 = clock_getcpuclockid2 */ "ntp_gettime", /* 248 = ntp_gettime */ "#249", /* 249 = nosys */ "minherit", /* 250 = minherit */ diff --git a/sys/kern/syscalls.master b/sys/kern/syscalls.master index f62dad7..5cb90f8 100644 --- a/sys/kern/syscalls.master +++ b/sys/kern/syscalls.master @@ -464,7 +464,8 @@ 244 AUE_NULL UNIMPL nosys 245 AUE_NULL UNIMPL nosys 246 AUE_NULL UNIMPL nosys -247 AUE_NULL UNIMPL nosys +247 AUE_NULL STD { int clock_getcpuclockid2(id_t id,\ + int which, clockid_t *clock_id); } 248 AUE_NULL STD { int ntp_gettime(struct ntptimeval *ntvp); } 249 AUE_NULL UNIMPL nosys ; syscall numbers initially used in OpenBSD diff --git a/sys/kern/systrace_args.c b/sys/kern/systrace_args.c index 522954b..4c57237 100644 --- a/sys/kern/systrace_args.c +++ b/sys/kern/systrace_args.c @@ -1358,6 +1358,15 @@ systrace_args(int sysnum, void *params, uint64_t *uarg, int *n_args) *n_args = 1; break; } + /* clock_getcpuclockid2 */ + case 247: { + struct clock_getcpuclockid2_args *p = params; + iarg[0] = p->id; /* id_t */ + iarg[1] = p->which; /* int */ + uarg[2] = (intptr_t) p->clock_id; /* clockid_t * */ + *n_args = 3; + break; + } /* ntp_gettime */ case 248: { struct ntp_gettime_args *p = params; @@ -5432,6 +5441,22 @@ systrace_entry_setargdesc(int sysnum, int ndx, char *desc, size_t descsz) break; }; break; + /* clock_getcpuclockid2 */ + case 247: + switch(ndx) { + case 0: + p = "id_t"; + break; + case 1: + p = "int"; + break; + case 2: + p = "clockid_t *"; + break; + default: + break; + }; + break; /* ntp_gettime */ case 248: switch(ndx) { @@ -9464,6 +9489,11 @@ systrace_return_setargdesc(int sysnum, int ndx, char *desc, size_t descsz) if (ndx == 0 || ndx == 1) p = "int"; break; + /* clock_getcpuclockid2 */ + case 247: + if (ndx == 0 || ndx == 1) + p = "int"; + break; /* ntp_gettime */ case 248: if (ndx == 0 || ndx == 1) diff --git a/sys/sys/syscall.h b/sys/sys/syscall.h index 321b70a..5a9e7cf 100644 --- a/sys/sys/syscall.h +++ b/sys/sys/syscall.h @@ -219,6 +219,7 @@ #define SYS_ffclock_getcounter 241 #define SYS_ffclock_setestimate 242 #define SYS_ffclock_getestimate 243 +#define SYS_clock_getcpuclockid2 247 #define SYS_ntp_gettime 248 #define SYS_minherit 250 #define SYS_rfork 251 diff --git a/sys/sys/syscall.mk b/sys/sys/syscall.mk index 7d9e06c..fe1332d 100644 --- a/sys/sys/syscall.mk +++ b/sys/sys/syscall.mk @@ -171,6 +171,7 @@ MIASM = \ ffclock_getcounter.o \ ffclock_setestimate.o \ ffclock_getestimate.o \ + clock_getcpuclockid2.o \ ntp_gettime.o \ minherit.o \ rfork.o \ diff --git a/sys/sys/sysproto.h b/sys/sys/sysproto.h index d0163c6..ca17ea1 100644 --- a/sys/sys/sysproto.h +++ b/sys/sys/sysproto.h @@ -736,6 +736,11 @@ struct ffclock_setestimate_args { struct ffclock_getestimate_args { char cest_l_[PADL_(struct ffclock_estimate *)]; struct ffclock_estimate * cest; char cest_r_[PADR_(struct ffclock_estimate *)]; }; +struct clock_getcpuclockid2_args { + char id_l_[PADL_(id_t)]; id_t id; char id_r_[PADR_(id_t)]; + char which_l_[PADL_(int)]; int which; char which_r_[PADR_(int)]; + char clock_id_l_[PADL_(clockid_t *)]; clockid_t * clock_id; char clock_id_r_[PADR_(clockid_t *)]; +}; struct ntp_gettime_args { char ntvp_l_[PADL_(struct ntptimeval *)]; struct ntptimeval * ntvp; char ntvp_r_[PADR_(struct ntptimeval *)]; }; @@ -1907,6 +1912,7 @@ int sys_nanosleep(struct thread *, struct nanosleep_args *); int sys_ffclock_getcounter(struct thread *, struct ffclock_getcounter_args *); int sys_ffclock_setestimate(struct thread *, struct ffclock_setestimate_args *); int sys_ffclock_getestimate(struct thread *, struct ffclock_getestimate_args *); +int sys_clock_getcpuclockid2(struct thread *, struct clock_getcpuclockid2_args *); int sys_ntp_gettime(struct thread *, struct ntp_gettime_args *); int sys_minherit(struct thread *, struct minherit_args *); int sys_rfork(struct thread *, struct rfork_args *); @@ -2597,6 +2603,7 @@ int freebsd7_shmctl(struct thread *, struct freebsd7_shmctl_args *); #define SYS_AUE_ffclock_getcounter AUE_NULL #define SYS_AUE_ffclock_setestimate AUE_NULL #define SYS_AUE_ffclock_getestimate AUE_NULL +#define SYS_AUE_clock_getcpuclockid2 AUE_NULL #define SYS_AUE_ntp_gettime AUE_NULL #define SYS_AUE_minherit AUE_MINHERIT #define SYS_AUE_rfork AUE_RFORK diff --git a/sys/sys/time.h b/sys/sys/time.h index 82bbd97..2890d1f 100644 --- a/sys/sys/time.h +++ b/sys/sys/time.h @@ -266,6 +266,7 @@ struct clockinfo { #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */ #define CLOCK_SECOND 13 /* FreeBSD-specific. */ #define CLOCK_THREAD_CPUTIME_ID 14 +#define CLOCK_PROCESS_CPUTIME_ID 15 #endif #ifndef TIMER_ABSTIME @@ -273,6 +274,11 @@ struct clockinfo { #define TIMER_ABSTIME 0x1 /* absolute timer */ #endif +#if __BSD_VISIBLE +#define CPUCLOCK_WHICH_PID 0 +#define CPUCLOCK_WHICH_TID 1 +#endif + #ifdef _KERNEL /* @@ -344,6 +350,7 @@ int utimes(const char *, const struct timeval *); #if __BSD_VISIBLE int adjtime(const struct timeval *, struct timeval *); +int clock_getcpuclockid2(id_t, int, clockid_t *); int futimes(int, const struct timeval *); int futimesat(int, const char *, const struct timeval [2]); int lutimes(const char *, const struct timeval *); diff --git a/sys/sys/unistd.h b/sys/sys/unistd.h index c558e8b..d6d92ab 100644 --- a/sys/sys/unistd.h +++ b/sys/sys/unistd.h @@ -53,7 +53,7 @@ #define _POSIX_ASYNCHRONOUS_IO 0 #define _POSIX_CHOWN_RESTRICTED 1 #define _POSIX_CLOCK_SELECTION (-1) -#define _POSIX_CPUTIME (-1) +#define _POSIX_CPUTIME 200112L #define _POSIX_FSYNC 200112L #define _POSIX_IPV6 0 #define _POSIX_JOB_CONTROL 1 -- cgit v1.1