calctsru() can be simplified.  Its length makes it look more
complicated than it really is.  We're converting from ticks to
nanoseconds and storing nanoseconds in a timespec:

- Remove the check for zero.  Pointless.

- Convert from ticks to nanoseconds inline.  No intermediate
  variables.

- Use NSEC_TO_TIMSPEC() to abbreviate the nsec -> timespec conversion.

ok?

Index: kern_resource.c
===================================================================
RCS file: /cvs/src/sys/kern/kern_resource.c,v
retrieving revision 1.69
diff -u -p -r1.69 kern_resource.c
--- kern_resource.c     25 Sep 2020 20:24:32 -0000      1.69
+++ kern_resource.c     25 Oct 2020 17:05:57 -0000
@@ -47,6 +47,7 @@
 #include <sys/ktrace.h>
 #include <sys/sched.h>
 #include <sys/signalvar.h>
+#include <sys/time.h>
 
 #include <sys/mount.h>
 #include <sys/syscallargs.h>
@@ -408,34 +409,12 @@ void
 calctsru(struct tusage *tup, struct timespec *up, struct timespec *sp,
     struct timespec *ip)
 {
-       u_quad_t st, ut, it;
-       int freq;
+       int freq = stathz ? stathz : hz;
 
-       st = tup->tu_sticks;
-       ut = tup->tu_uticks;
-       it = tup->tu_iticks;
-
-       if (st + ut + it == 0) {
-               timespecclear(up);
-               timespecclear(sp);
-               if (ip != NULL)
-                       timespecclear(ip);
-               return;
-       }
-
-       freq = stathz ? stathz : hz;
-
-       st = st * 1000000000 / freq;
-       sp->tv_sec = st / 1000000000;
-       sp->tv_nsec = st % 1000000000;
-       ut = ut * 1000000000 / freq;
-       up->tv_sec = ut / 1000000000;
-       up->tv_nsec = ut % 1000000000;
-       if (ip != NULL) {
-               it = it * 1000000000 / freq;
-               ip->tv_sec = it / 1000000000;
-               ip->tv_nsec = it % 1000000000;
-       }
+       NSEC_TO_TIMESPEC(tup->tu_uticks * 1000000000 / freq, up);
+       NSEC_TO_TIMESPEC(tup->tu_sticks * 1000000000 / freq, sp);
+       if (ip != NULL)
+               NSEC_TO_TIMESPEC(tup->tu_iticks * 1000000000 / freq, ip);
 }
 
 void

Reply via email to