1/*- 2 * Copyright (c) 1982, 1986, 1991, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. --- 21 unchanged lines hidden (view full) --- 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)kern_resource.c 8.5 (Berkeley) 1/21/94 35 */ 36 37#include <sys/cdefs.h> |
38__FBSDID("$FreeBSD: head/sys/kern/kern_resource.c 155534 2006-02-11 09:33:07Z phk $"); |
39 40#include "opt_compat.h" 41 42#include <sys/param.h> 43#include <sys/systm.h> 44#include <sys/sysproto.h> 45#include <sys/file.h> 46#include <sys/kernel.h> --- 17 unchanged lines hidden (view full) --- 64 65static MALLOC_DEFINE(M_PLIMIT, "plimit", "plimit structures"); 66static MALLOC_DEFINE(M_UIDINFO, "uidinfo", "uidinfo structures"); 67#define UIHASH(uid) (&uihashtbl[(uid) & uihash]) 68static struct mtx uihashtbl_mtx; 69static LIST_HEAD(uihashhead, uidinfo) *uihashtbl; 70static u_long uihash; /* size of hash table - 1 */ 71 |
72static int donice(struct thread *td, struct proc *chgp, int n); 73static struct uidinfo *uilookup(uid_t uid); 74 75/* 76 * Resource controls and accounting. 77 */ 78 79#ifndef _SYS_SYSPROTO_H_ --- 607 unchanged lines hidden (view full) --- 687 p = td->td_proc; 688 PROC_LOCK(p); 689 lim_rlimit(p, uap->which, &rlim); 690 PROC_UNLOCK(p); 691 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit)); 692 return (error); 693} 694 |
695void |
696calccru(p, up, sp) |
697 struct proc *p; 698 struct timeval *up; 699 struct timeval *sp; 700{ |
701 702 PROC_LOCK_ASSERT(p, MA_OWNED); |
703 calcru(p, up, sp); |
704} 705 |
706/* 707 * Transform the running time and tick information in proc p into user, 708 * system, and interrupt time usage. If appropriate, include the current 709 * time slice on this CPU. 710 */ 711 |
712void |
713calcru(struct proc *p, struct timeval *up, struct timeval *sp) |
714{ |
715 struct thread *td; 716 struct rusage_ext *ruxp = &p->p_rux; 717 uint64_t u; |
718 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ 719 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; 720 |
721 PROC_LOCK_ASSERT(p, MA_OWNED); 722 mtx_assert(&sched_lock, MA_NOTOWNED); 723 mtx_lock_spin(&sched_lock); 724 if (curthread->td_proc == p) { 725 td = curthread; 726 u = cpu_ticks(); 727 ruxp->rux_runtime += (u - PCPU_GET(switchtime)); 728 PCPU_SET(switchtime, u); 729 ruxp->rux_uticks += td->td_uticks; 730 td->td_uticks = 0; 731 ruxp->rux_iticks += td->td_iticks; 732 td->td_iticks = 0; 733 ruxp->rux_sticks += td->td_sticks; 734 td->td_sticks = 0; 735 } 736 |
737 ut = ruxp->rux_uticks; 738 st = ruxp->rux_sticks; 739 it = ruxp->rux_iticks; |
740 tu = ruxp->rux_runtime; 741 mtx_unlock_spin(&sched_lock); 742 tu = cputick2usec(tu); |
743 tt = ut + st + it; 744 if (tt == 0) { 745 st = 1; 746 tt = 1; 747 } |
748 ptu = ruxp->rux_uu + ruxp->rux_su + ruxp->rux_iu; |
749 if ((int64_t)tu < 0) { 750 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n", 751 (intmax_t)tu, p->p_pid, p->p_comm); 752 tu = ptu; 753 } 754 755 /* Subdivide tu. */ 756 uu = (tu * ut) / tt; 757 su = (tu * st) / tt; 758 iu = tu - uu - su; |
759 if (tu < ptu) { 760 printf( 761"calcru: runtime went backwards from %ju usec to %ju usec for pid %d (%s)\n", 762 (uintmax_t)ptu, (uintmax_t)tu, p->p_pid, p->p_comm); 763 printf("u %ju:%ju/%ju s %ju:%ju/%ju i %ju:%ju/%ju\n", 764 (uintmax_t)ut, (uintmax_t)ruxp->rux_uu, uu, 765 (uintmax_t)st, (uintmax_t)ruxp->rux_su, su, 766 (uintmax_t)it, (uintmax_t)ruxp->rux_iu, iu); 767 tu = ptu; 768 } 769#if 0 |
770 /* Enforce monotonicity. */ 771 if (uu < ruxp->rux_uu || su < ruxp->rux_su || iu < ruxp->rux_iu) { 772 if (uu < ruxp->rux_uu) 773 uu = ruxp->rux_uu; 774 else if (uu + ruxp->rux_su + ruxp->rux_iu > tu) 775 uu = tu - ruxp->rux_su - ruxp->rux_iu; 776 if (st == 0) 777 su = ruxp->rux_su; --- 5 unchanged lines hidden (view full) --- 783 su = tu - uu - ruxp->rux_iu; 784 } 785 KASSERT(uu + su + ruxp->rux_iu <= tu, 786 ("calcru: monotonisation botch 1")); 787 iu = tu - uu - su; 788 KASSERT(iu >= ruxp->rux_iu, 789 ("calcru: monotonisation botch 2")); 790 } |
791 KASSERT(uu + su + iu <= tu, 792 ("calcru: monotisation botch 3")); 793#endif |
794 ruxp->rux_uu = uu; 795 ruxp->rux_su = su; 796 ruxp->rux_iu = iu; 797 798 up->tv_sec = uu / 1000000; 799 up->tv_usec = uu % 1000000; 800 sp->tv_sec = su / 1000000; 801 sp->tv_usec = su % 1000000; --- 357 unchanged lines hidden --- |