kern_resource.c (155444) | kern_resource.c (155534) |
---|---|
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> | 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 155444 2006-02-07 21:22:02Z phk $"); | 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 | 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 void calcru1(struct proc *p, struct rusage_ext *ruxp, 73 struct timeval *up, struct timeval *sp); | |
74static int donice(struct thread *td, struct proc *chgp, int n); 75static struct uidinfo *uilookup(uid_t uid); 76 77/* 78 * Resource controls and accounting. 79 */ 80 81#ifndef _SYS_SYSPROTO_H_ --- 607 unchanged lines hidden (view full) --- 689 p = td->td_proc; 690 PROC_LOCK(p); 691 lim_rlimit(p, uap->which, &rlim); 692 PROC_UNLOCK(p); 693 error = copyout(&rlim, uap->rlp, sizeof(struct rlimit)); 694 return (error); 695} 696 | 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 |
697/* 698 * Transform the running time and tick information in proc p into user, 699 * system, and interrupt time usage. 700 */ | |
701void | 695void |
702calcru(p, up, sp) | 696calccru(p, up, sp) |
703 struct proc *p; 704 struct timeval *up; 705 struct timeval *sp; 706{ | 697 struct proc *p; 698 struct timeval *up; 699 struct timeval *sp; 700{ |
707 uint64_t bt; 708 struct rusage_ext rux; 709 struct thread *td; 710 int bt_valid; | |
711 712 PROC_LOCK_ASSERT(p, MA_OWNED); | 701 702 PROC_LOCK_ASSERT(p, MA_OWNED); |
713 mtx_assert(&sched_lock, MA_NOTOWNED); 714 bt_valid = 0; 715 bt = 0; 716 mtx_lock_spin(&sched_lock); 717 rux = p->p_rux; 718 FOREACH_THREAD_IN_PROC(p, td) { 719 if (TD_IS_RUNNING(td)) { 720 /* 721 * Adjust for the current time slice. This is 722 * actually fairly important since the error here is 723 * on the order of a time quantum which is much 724 * greater than the precision of binuptime(). 725 */ 726 KASSERT(td->td_oncpu != NOCPU, 727 ("%s: running thread has no CPU", __func__)); 728 if (!bt_valid) { 729 bt = cpu_ticks(); 730 bt_valid = 1; 731 } 732 /* 733 * XXX: Doesn't this mean that this quantum will 734 * XXX: get counted twice if calcru() is called 735 * XXX: from SIGINFO ? 736 */ 737 rux.rux_runtime += 738 (bt - pcpu_find(td->td_oncpu)->pc_switchtime); 739 } 740 } 741 mtx_unlock_spin(&sched_lock); 742 calcru1(p, &rux, up, sp); 743 p->p_rux.rux_uu = rux.rux_uu; 744 p->p_rux.rux_su = rux.rux_su; 745 p->p_rux.rux_iu = rux.rux_iu; | 703 calcru(p, up, sp); |
746} 747 | 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 |
|
748void | 712void |
749calccru(p, up, sp) 750 struct proc *p; 751 struct timeval *up; 752 struct timeval *sp; | 713calcru(struct proc *p, struct timeval *up, struct timeval *sp) |
753{ | 714{ |
754 755 PROC_LOCK_ASSERT(p, MA_OWNED); 756 calcru1(p, &p->p_crux, up, sp); 757} 758 759static void 760calcru1(p, ruxp, up, sp) 761 struct proc *p; 762 struct rusage_ext *ruxp; 763 struct timeval *up; 764 struct timeval *sp; 765{ | 715 struct thread *td; 716 struct rusage_ext *ruxp = &p->p_rux; 717 uint64_t u; |
766 /* {user, system, interrupt, total} {ticks, usec}; previous tu: */ 767 u_int64_t ut, uu, st, su, it, iu, tt, tu, ptu; 768 | 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 |
|
769 ut = ruxp->rux_uticks; 770 st = ruxp->rux_sticks; 771 it = ruxp->rux_iticks; | 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); |
|
772 tt = ut + st + it; 773 if (tt == 0) { 774 st = 1; 775 tt = 1; 776 } | 743 tt = ut + st + it; 744 if (tt == 0) { 745 st = 1; 746 tt = 1; 747 } |
777 tu = (ruxp->rux_runtime * 1000000LL) / cpu_tickrate(); | |
778 ptu = ruxp->rux_uu + ruxp->rux_su + ruxp->rux_iu; | 748 ptu = ruxp->rux_uu + ruxp->rux_su + ruxp->rux_iu; |
779 if (tu < ptu) { 780 printf( 781"calcru: runtime went backwards from %ju usec to %ju usec for pid %d (%s)\n", 782 (uintmax_t)ptu, (uintmax_t)tu, p->p_pid, p->p_comm); 783 tu = ptu; 784 } | |
785 if ((int64_t)tu < 0) { 786 printf("calcru: negative runtime of %jd usec for pid %d (%s)\n", 787 (intmax_t)tu, p->p_pid, p->p_comm); 788 tu = ptu; 789 } 790 791 /* Subdivide tu. */ 792 uu = (tu * ut) / tt; 793 su = (tu * st) / tt; 794 iu = tu - uu - su; | 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; |
795 | 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 |
796 /* Enforce monotonicity. */ 797 if (uu < ruxp->rux_uu || su < ruxp->rux_su || iu < ruxp->rux_iu) { 798 if (uu < ruxp->rux_uu) 799 uu = ruxp->rux_uu; 800 else if (uu + ruxp->rux_su + ruxp->rux_iu > tu) 801 uu = tu - ruxp->rux_su - ruxp->rux_iu; 802 if (st == 0) 803 su = ruxp->rux_su; --- 5 unchanged lines hidden (view full) --- 809 su = tu - uu - ruxp->rux_iu; 810 } 811 KASSERT(uu + su + ruxp->rux_iu <= tu, 812 ("calcru: monotonisation botch 1")); 813 iu = tu - uu - su; 814 KASSERT(iu >= ruxp->rux_iu, 815 ("calcru: monotonisation botch 2")); 816 } | 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 |
|
817 ruxp->rux_uu = uu; 818 ruxp->rux_su = su; 819 ruxp->rux_iu = iu; 820 821 up->tv_sec = uu / 1000000; 822 up->tv_usec = uu % 1000000; 823 sp->tv_sec = su / 1000000; 824 sp->tv_usec = su % 1000000; --- 357 unchanged lines hidden --- | 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 --- |