| 959 | } |
| 960 | |
| 961 | static void |
| 962 | calcru1(struct proc *p, struct rusage_ext *ruxp, struct timeval *up, |
| 963 | struct timeval *sp) |
| 964 | { |
| 965 | /* {user, system, interrupt, total} {ticks, usec}: */ |
| 966 | uint64_t ut, uu, st, su, it, tt, tu; |
| 967 | |
| 968 | ut = ruxp->rux_uticks; |
| 969 | st = ruxp->rux_sticks; |
| 970 | it = ruxp->rux_iticks; |
| 971 | tt = ut + st + it; |
| 972 | if (tt == 0) { |
| 973 | /* Avoid divide by zero */ |
| 974 | st = 1; |
| 975 | tt = 1; |
| 976 | } |
| 977 | tu = cputick2usec(ruxp->rux_runtime); |
| 978 | if ((int64_t)tu < 0) { |
| 979 | /* XXX: this should be an assert /phk */ |
| 980 | printf("calcru: negative runtime of %jd usec for pid %d (%s)\n", |
| 981 | (intmax_t)tu, p->p_pid, p->p_comm); |
| 982 | tu = ruxp->rux_tu; |
| 983 | } |
| 984 | |
| 985 | /* Subdivide tu. Avoid overflow in the multiplications. */ |
| 986 | if (__predict_true(tu <= ((uint64_t)1 << 38) && tt <= (1 << 26))) { |
| 987 | /* Up to 76 hours when stathz is 128. */ |
| 988 | uu = (tu * ut) / tt; |
| 989 | su = (tu * st) / tt; |
| 990 | } else { |
| 991 | uu = mul64_by_fraction(tu, ut, tt); |
| 992 | su = mul64_by_fraction(tu, st, tt); |
| 993 | } |
| 994 | |
| 995 | if (tu >= ruxp->rux_tu) { |
| 996 | /* |
| 997 | * The normal case, time increased. |
| 998 | * Enforce monotonicity of bucketed numbers. |
| 999 | */ |
| 1000 | if (uu < ruxp->rux_uu) |
| 1001 | uu = ruxp->rux_uu; |
| 1002 | if (su < ruxp->rux_su) |
| 1003 | su = ruxp->rux_su; |
| 1004 | } else if (tu + 3 > ruxp->rux_tu || 101 * tu > 100 * ruxp->rux_tu) { |
| 1005 | /* |
| 1006 | * When we calibrate the cputicker, it is not uncommon to |
| 1007 | * see the presumably fixed frequency increase slightly over |
| 1008 | * time as a result of thermal stabilization and NTP |
| 1009 | * discipline (of the reference clock). We therefore ignore |
| 1010 | * a bit of backwards slop because we expect to catch up |
| 1011 | * shortly. We use a 3 microsecond limit to catch low |
| 1012 | * counts and a 1% limit for high counts. |
| 1013 | */ |
| 1014 | uu = ruxp->rux_uu; |
| 1015 | su = ruxp->rux_su; |
| 1016 | tu = ruxp->rux_tu; |
| 1017 | } else { /* tu < ruxp->rux_tu */ |
| 1018 | /* |
no test coverage detected