From 94c38e45691bfc7020490efddf82c55c25ceeb99 Mon Sep 17 00:00:00 2001 From: claudio Date: Tue, 29 Aug 2023 16:19:34 +0000 Subject: [PATCH] Remove p_rtime from struct proc and replace it by passing the timespec as argument to the tuagg_locked function. - Remove incorrect use of p_rtime in other parts of the tree. p_rtime was almost always 0 so including it in any sum did not alter the result. - In main() the update of time can be further simplified since at that time only the primary cpu is running. - Add missing nanouptime() call in cpu_hatch() for hppa - Rename tuagg_unlocked to tuagg_locked like it is done in the rest of the tree. OK cheloha@ dlg@ --- sys/arch/hppa/dev/cpu.c | 6 +++++- sys/kern/init_main.c | 10 +++------- sys/kern/kern_exit.c | 12 ++++++++++-- sys/kern/kern_resource.c | 18 +++++++++--------- sys/kern/kern_sched.c | 7 +------ sys/kern/kern_time.c | 4 +--- sys/kern/sched_bsd.c | 6 +++--- sys/sys/proc.h | 3 +-- sys/sys/resourcevar.h | 4 ++-- 9 files changed, 35 insertions(+), 35 deletions(-) diff --git a/sys/arch/hppa/dev/cpu.c b/sys/arch/hppa/dev/cpu.c index bc778f69d12..777bbd44587 100644 --- a/sys/arch/hppa/dev/cpu.c +++ b/sys/arch/hppa/dev/cpu.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cpu.c,v 1.45 2023/06/15 22:18:07 cheloha Exp $ */ +/* $OpenBSD: cpu.c,v 1.46 2023/08/29 16:19:34 claudio Exp $ */ /* * Copyright (c) 1998-2003 Michael Shalayeff @@ -263,6 +263,10 @@ cpu_hatch(void) while (!start_secondary_cpu) ; + s = splhigh(); + nanouptime(&ci->ci_schedstate.spc_runtime); + splx(s); + SCHED_LOCK(s); cpu_switchto(NULL, sched_chooseproc()); } diff --git a/sys/kern/init_main.c b/sys/kern/init_main.c index 4cf78a2f60d..59b15fe04c1 100644 --- a/sys/kern/init_main.c +++ b/sys/kern/init_main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: init_main.c,v 1.321 2023/06/15 22:18:06 cheloha Exp $ */ +/* $OpenBSD: init_main.c,v 1.322 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: init_main.c,v 1.84.4.1 1996/06/02 09:08:06 mrg Exp $ */ /* @@ -481,16 +481,12 @@ main(void *framep) /* * Now can look at time, having had a chance to verify the time - * from the file system. Reset p->p_rtime as it may have been - * munched in mi_switch() after the time got set. + * from the file system. */ LIST_FOREACH(pr, &allprocess, ps_list) { nanouptime(&pr->ps_start); - TAILQ_FOREACH(p, &pr->ps_threads, p_thr_link) { - nanouptime(&p->p_cpu->ci_schedstate.spc_runtime); - timespecclear(&p->p_rtime); - } } + nanouptime(&curcpu()->ci_schedstate.spc_runtime); uvm_swap_init(); diff --git a/sys/kern/kern_exit.c b/sys/kern/kern_exit.c index 85c5d58dc23..2863d1806d0 100644 --- a/sys/kern/kern_exit.c +++ b/sys/kern/kern_exit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_exit.c,v 1.211 2023/04/25 18:14:06 claudio Exp $ */ +/* $OpenBSD: kern_exit.c,v 1.212 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: kern_exit.c,v 1.39 1996/04/22 01:38:25 christos Exp $ */ /* @@ -118,6 +118,7 @@ exit1(struct proc *p, int xexit, int xsig, int flags) { struct process *pr, *qr, *nqr; struct rusage *rup; + struct timespec ts; int s; atomic_setbits_int(&p->p_flag, P_WEXIT); @@ -298,7 +299,14 @@ exit1(struct proc *p, int xexit, int xsig, int flags) /* add thread's accumulated rusage into the process's total */ ruadd(rup, &p->p_ru); - tuagg(pr, p); + nanouptime(&ts); + if (timespeccmp(&ts, &curcpu()->ci_schedstate.spc_runtime, <)) + timespecclear(&ts); + else + timespecsub(&ts, &curcpu()->ci_schedstate.spc_runtime, &ts); + SCHED_LOCK(s); + tuagg_locked(pr, p, &ts); + SCHED_UNLOCK(s); /* * clear %cpu usage during swap diff --git a/sys/kern/kern_resource.c b/sys/kern/kern_resource.c index 98bcd6ba436..9e756af4026 100644 --- a/sys/kern/kern_resource.c +++ b/sys/kern/kern_resource.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_resource.c,v 1.77 2023/02/04 19:33:03 cheloha Exp $ */ +/* $OpenBSD: kern_resource.c,v 1.78 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: kern_resource.c,v 1.38 1996/10/23 07:19:38 matthias Exp $ */ /*- @@ -64,7 +64,7 @@ struct plimit *lim_copy(struct plimit *); struct plimit *lim_write_begin(void); void lim_write_commit(struct plimit *); -void tuagg_sub(struct tusage *, struct proc *); +void tuagg_sub(struct tusage *, struct proc *, const struct timespec *); /* * Patchable maximum data and stack limits. @@ -368,9 +368,10 @@ sys_getrlimit(struct proc *p, void *v, register_t *retval) } void -tuagg_sub(struct tusage *tup, struct proc *p) +tuagg_sub(struct tusage *tup, struct proc *p, const struct timespec *ts) { - timespecadd(&tup->tu_runtime, &p->p_rtime, &tup->tu_runtime); + if (ts != NULL) + timespecadd(&tup->tu_runtime, ts, &tup->tu_runtime); tup->tu_uticks += p->p_uticks; tup->tu_sticks += p->p_sticks; tup->tu_iticks += p->p_iticks; @@ -381,11 +382,10 @@ tuagg_sub(struct tusage *tup, struct proc *p) * totals for the thread and process */ void -tuagg_unlocked(struct process *pr, struct proc *p) +tuagg_locked(struct process *pr, struct proc *p, const struct timespec *ts) { - tuagg_sub(&pr->ps_tu, p); - tuagg_sub(&p->p_tu, p); - timespecclear(&p->p_rtime); + tuagg_sub(&pr->ps_tu, p, ts); + tuagg_sub(&p->p_tu, p, ts); p->p_uticks = 0; p->p_sticks = 0; p->p_iticks = 0; @@ -397,7 +397,7 @@ tuagg(struct process *pr, struct proc *p) int s; SCHED_LOCK(s); - tuagg_unlocked(pr, p); + tuagg_locked(pr, p, NULL); SCHED_UNLOCK(s); } diff --git a/sys/kern/kern_sched.c b/sys/kern/kern_sched.c index c0e66583e89..4d776259630 100644 --- a/sys/kern/kern_sched.c +++ b/sys/kern/kern_sched.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_sched.c,v 1.86 2023/08/14 08:33:24 mpi Exp $ */ +/* $OpenBSD: kern_sched.c,v 1.87 2023/08/29 16:19:34 claudio Exp $ */ /* * Copyright (c) 2007, 2008 Artur Grabowski * @@ -230,14 +230,9 @@ void sched_exit(struct proc *p) { struct schedstate_percpu *spc = &curcpu()->ci_schedstate; - struct timespec ts; struct proc *idle; int s; - nanouptime(&ts); - timespecsub(&ts, &spc->spc_runtime, &ts); - timespecadd(&p->p_rtime, &ts, &p->p_rtime); - if (ISSET(spc->spc_schedflags, SPCF_ITIMER)) { atomic_clearbits_int(&spc->spc_schedflags, SPCF_ITIMER); clockintr_cancel(spc->spc_itimer); diff --git a/sys/kern/kern_time.c b/sys/kern/kern_time.c index 4a21d54eb83..50e9285262e 100644 --- a/sys/kern/kern_time.c +++ b/sys/kern/kern_time.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kern_time.c,v 1.164 2023/08/05 20:07:55 cheloha Exp $ */ +/* $OpenBSD: kern_time.c,v 1.165 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: kern_time.c,v 1.20 1996/02/18 11:57:06 fvdl Exp $ */ /* @@ -130,13 +130,11 @@ clock_gettime(struct proc *p, clockid_t clock_id, struct timespec *tp) nanouptime(tp); timespecsub(tp, &curcpu()->ci_schedstate.spc_runtime, tp); timespecadd(tp, &p->p_p->ps_tu.tu_runtime, tp); - timespecadd(tp, &p->p_rtime, tp); break; case CLOCK_THREAD_CPUTIME_ID: nanouptime(tp); timespecsub(tp, &curcpu()->ci_schedstate.spc_runtime, tp); timespecadd(tp, &p->p_tu.tu_runtime, tp); - timespecadd(tp, &p->p_rtime, tp); break; default: /* check for clock from pthread_getcpuclockid() */ diff --git a/sys/kern/sched_bsd.c b/sys/kern/sched_bsd.c index f9f2b7d76bd..465624e589d 100644 --- a/sys/kern/sched_bsd.c +++ b/sys/kern/sched_bsd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sched_bsd.c,v 1.83 2023/08/19 11:14:11 claudio Exp $ */ +/* $OpenBSD: sched_bsd.c,v 1.84 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: kern_synch.c,v 1.37 1996/04/22 01:38:37 christos Exp $ */ /*- @@ -385,13 +385,13 @@ mi_switch(void) (long long)spc->spc_runtime.tv_sec, spc->spc_runtime.tv_nsec); #endif + timespecclear(&ts); } else { timespecsub(&ts, &spc->spc_runtime, &ts); - timespecadd(&p->p_rtime, &ts, &p->p_rtime); } /* add the time counts for this thread to the process's total */ - tuagg_unlocked(pr, p); + tuagg_locked(pr, p, &ts); /* Stop any optional clock interrupts. */ if (ISSET(spc->spc_schedflags, SPCF_ITIMER)) { diff --git a/sys/sys/proc.h b/sys/sys/proc.h index 7d2a0da025b..4f72d899753 100644 --- a/sys/sys/proc.h +++ b/sys/sys/proc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: proc.h,v 1.347 2023/08/05 20:07:56 cheloha Exp $ */ +/* $OpenBSD: proc.h,v 1.348 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: proc.h,v 1.44 1996/04/22 01:23:21 christos Exp $ */ /*- @@ -354,7 +354,6 @@ struct proc { struct rusage p_ru; /* Statistics */ struct tusage p_tu; /* accumulated times. */ - struct timespec p_rtime; /* Real time. */ struct plimit *p_limit; /* [l] read ref. of p_p->ps_limit */ struct kcov_dev *p_kd; /* kcov device handle */ diff --git a/sys/sys/resourcevar.h b/sys/sys/resourcevar.h index d25840445a6..b3fc0476b01 100644 --- a/sys/sys/resourcevar.h +++ b/sys/sys/resourcevar.h @@ -1,4 +1,4 @@ -/* $OpenBSD: resourcevar.h,v 1.27 2023/07/25 18:16:19 cheloha Exp $ */ +/* $OpenBSD: resourcevar.h,v 1.28 2023/08/29 16:19:34 claudio Exp $ */ /* $NetBSD: resourcevar.h,v 1.12 1995/11/22 23:01:53 cgd Exp $ */ /* @@ -67,7 +67,7 @@ extern uint32_t profclock_period; void addupc_intr(struct proc *, u_long, u_long); void addupc_task(struct proc *, u_long, u_int); void profclock(struct clockintr *, void *); -void tuagg_unlocked(struct process *, struct proc *); +void tuagg_locked(struct process *, struct proc *, const struct timespec *); void tuagg(struct process *, struct proc *); struct tusage; void calctsru(struct tusage *, struct timespec *, struct timespec *, -- 2.20.1