From owner-p4-projects@FreeBSD.ORG Fri Nov 12 16:08:26 2010 Return-Path: Delivered-To: p4-projects@freebsd.org Received: by hub.freebsd.org (Postfix, from userid 32767) id A9D0610656A3; Fri, 12 Nov 2010 16:08:26 +0000 (UTC) Delivered-To: perforce@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 601011065674 for ; Fri, 12 Nov 2010 16:08:26 +0000 (UTC) (envelope-from trasz@freebsd.org) Received: from skunkworks.freebsd.org (skunkworks.freebsd.org [IPv6:2001:4f8:fff6::2d]) by mx1.freebsd.org (Postfix) with ESMTP id 315928FC18 for ; Fri, 12 Nov 2010 16:08:26 +0000 (UTC) Received: from skunkworks.freebsd.org (localhost [127.0.0.1]) by skunkworks.freebsd.org (8.14.4/8.14.4) with ESMTP id oACG8QnD080480 for ; Fri, 12 Nov 2010 16:08:26 GMT (envelope-from trasz@freebsd.org) Received: (from perforce@localhost) by skunkworks.freebsd.org (8.14.4/8.14.4/Submit) id oACG8Q9j080477 for perforce@freebsd.org; Fri, 12 Nov 2010 16:08:26 GMT (envelope-from trasz@freebsd.org) Date: Fri, 12 Nov 2010 16:08:26 GMT Message-Id: <201011121608.oACG8Q9j080477@skunkworks.freebsd.org> X-Authentication-Warning: skunkworks.freebsd.org: perforce set sender to trasz@freebsd.org using -f From: Edward Tomasz Napierala To: Perforce Change Reviews Precedence: bulk Cc: Subject: PERFORCE change 185695 for review X-BeenThere: p4-projects@freebsd.org X-Mailman-Version: 2.1.5 List-Id: p4 projects tree changes List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 12 Nov 2010 16:08:26 -0000 http://p4web.freebsd.org/@@185695?ac=10 Change 185695 by trasz@trasz_victim on 2010/11/12 16:07:20 Implement simple CPU throttling (%cpu limit). Affected files ... .. //depot/projects/soc2009/trasz_limits/sys/kern/kern_resource.c#51 edit .. //depot/projects/soc2009/trasz_limits/sys/sys/proc.h#26 edit Differences ... ==== //depot/projects/soc2009/trasz_limits/sys/kern/kern_resource.c#51 (text+ko) ==== @@ -624,21 +624,60 @@ } static void +rusage_throttle(struct thread *td, int throttle) +{ + u_char oldpri; + u_char newpri; + int type; + + if (throttle) { + td->td_flags |= TDF_THROTTLED; + newpri = PRI_MIN_IDLE; + type = RTP_PRIO_IDLE; + } else if (td->td_flags & TDF_THROTTLED) { + td->td_flags &= ~TDF_THROTTLED; + newpri = PRI_MIN_TIMESHARE; + type = RTP_PRIO_NORMAL; + } else + return; + + /* Mostly copied from rtp_to_pri(). */ + sched_class(td, type); /* XXX fix */ + oldpri = td->td_user_pri; + sched_user_prio(td, newpri); + if (TD_IS_RUNNING(td) || TD_CAN_RUN(td)) + sched_prio(td, td->td_user_pri); /* XXX dubious */ + if (TD_ON_UPILOCK(td) && oldpri != newpri) + umtx_pi_adjust(td, oldpri); +} + +static void rusage_cpu_task_fn(void *arg, int pending) { int pctcpu; struct thread *td; struct proc *p; struct timeval wallclock; + uint64_t pctcpu_limit; sx_slock(&allproc_lock); FOREACH_PROC_IN_SYSTEM(p) { + pctcpu_limit = rusage_get_limit(p, RUSAGE_PCTCPU); PROC_SLOCK(p); pctcpu = 0; FOREACH_THREAD_IN_PROC(p, td) { ruxagg(p, td); thread_lock(td); pctcpu += sched_pctcpu(td); + /* + * We are making this decision based on data from + * the previous run. The assumption is that this runs + * so often it doesn't matter. + */ + if (pctcpu > pctcpu_limit) + rusage_throttle(td, 1); + else + rusage_throttle(td, 0); thread_unlock(td); } PROC_SUNLOCK(p); ==== //depot/projects/soc2009/trasz_limits/sys/sys/proc.h#26 (text+ko) ==== @@ -354,7 +354,7 @@ #define TDF_NEEDRESCHED 0x00010000 /* Thread needs to yield. */ #define TDF_NEEDSIGCHK 0x00020000 /* Thread may need signal delivery. */ #define TDF_NOLOAD 0x00040000 /* Ignore during load avg calculations. */ -#define TDF_UNUSED19 0x00080000 /* --available-- */ +#define TDF_THROTTLED 0x00080000 /* Throttled due to %cpu usage */ #define TDF_THRWAKEUP 0x00100000 /* Libthr thread must not suspend itself. */ #define TDF_UNUSED21 0x00200000 /* --available-- */ #define TDF_SWAPINREQ 0x00400000 /* Swapin request due to wakeup. */