proc_compare.c revision 97981
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1990, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
3487674Smarkm#include <sys/cdefs.h>
3587674Smarkm
3687674Smarkm__FBSDID("$FreeBSD: head/usr.bin/w/proc_compare.c 97981 2002-06-07 01:41:54Z jmallett $");
3787674Smarkm
381590Srgrimes#ifndef lint
3987674Smarkmstatic const char sccsid[] = "@(#)proc_compare.c	8.2 (Berkeley) 9/23/93";
4069896Smckusick#endif
411590Srgrimes
421590Srgrimes#include <sys/param.h>
431590Srgrimes#include <sys/time.h>
4469896Smckusick#include <sys/user.h>
451590Srgrimes
461590Srgrimes#include "extern.h"
471590Srgrimes
481590Srgrimes/*
491590Srgrimes * Returns 1 if p2 is "better" than p1
501590Srgrimes *
511590Srgrimes * The algorithm for picking the "interesting" process is thus:
521590Srgrimes *
531590Srgrimes *	1) Only foreground processes are eligible - implied.
541590Srgrimes *	2) Runnable processes are favored over anything else.  The runner
551590Srgrimes *	   with the highest cpu utilization is picked (p_estcpu).  Ties are
561590Srgrimes *	   broken by picking the highest pid.
571590Srgrimes *	3) The sleeper with the shortest sleep time is next.  With ties,
5871592Sjhb *	   we pick out just "short-term" sleepers (PS_SINTR == 0).
591590Srgrimes *	4) Further ties are broken by picking the highest pid.
601590Srgrimes *
611590Srgrimes * If you change this, be sure to consider making the change in the kernel
621590Srgrimes * too (^T in kern/tty.c).
631590Srgrimes *
641590Srgrimes * TODO - consider whether pctcpu should be used.
651590Srgrimes */
661590Srgrimes
6787674Smarkm#include <sys/cdefs.h>
6887674Smarkm
6987674Smarkm__FBSDID("$FreeBSD: head/usr.bin/w/proc_compare.c 97981 2002-06-07 01:41:54Z jmallett $");
7087674Smarkm
7169896Smckusick#define ISRUN(p)	(((p)->ki_stat == SRUN) || ((p)->ki_stat == SIDL))
721590Srgrimes#define TESTAB(a, b)    ((a)<<1 | (b))
731590Srgrimes#define ONLYA   2
741590Srgrimes#define ONLYB   1
751590Srgrimes#define BOTH    3
761590Srgrimes
771590Srgrimesint
7897981Sjmallettproc_compare(struct kinfo_proc *p1, struct kinfo_proc *p2)
791590Srgrimes{
801590Srgrimes
811590Srgrimes	if (p1 == NULL)
821590Srgrimes		return (1);
831590Srgrimes	/*
841590Srgrimes	 * see if at least one of them is runnable
851590Srgrimes	 */
861590Srgrimes	switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
871590Srgrimes	case ONLYA:
881590Srgrimes		return (0);
891590Srgrimes	case ONLYB:
901590Srgrimes		return (1);
911590Srgrimes	case BOTH:
921590Srgrimes		/*
931590Srgrimes		 * tie - favor one with highest recent cpu utilization
941590Srgrimes		 */
9569896Smckusick		if (p2->ki_estcpu > p1->ki_estcpu)
961590Srgrimes			return (1);
9769896Smckusick		if (p1->ki_estcpu > p2->ki_estcpu)
981590Srgrimes			return (0);
9969896Smckusick		return (p2->ki_pid > p1->ki_pid);	/* tie - return highest pid */
1001590Srgrimes	}
1011590Srgrimes	/*
1021590Srgrimes 	 * weed out zombies
1031590Srgrimes	 */
10469896Smckusick	switch (TESTAB(p1->ki_stat == SZOMB, p2->ki_stat == SZOMB)) {
1051590Srgrimes	case ONLYA:
1061590Srgrimes		return (1);
1071590Srgrimes	case ONLYB:
1081590Srgrimes		return (0);
1091590Srgrimes	case BOTH:
11069896Smckusick		return (p2->ki_pid > p1->ki_pid); /* tie - return highest pid */
1111590Srgrimes	}
1121590Srgrimes	/*
1131590Srgrimes	 * pick the one with the smallest sleep time
1141590Srgrimes	 */
11569896Smckusick	if (p2->ki_slptime > p1->ki_slptime)
1161590Srgrimes		return (0);
11769896Smckusick	if (p1->ki_slptime > p2->ki_slptime)
1181590Srgrimes		return (1);
1191590Srgrimes	/*
1201590Srgrimes	 * favor one sleeping in a non-interruptible sleep
1211590Srgrimes	 */
12283366Sjulian	if (p1->ki_tdflags & TDF_SINTR && (p2->ki_tdflags & TDF_SINTR) == 0)
1231590Srgrimes		return (1);
12483366Sjulian	if (p2->ki_tdflags & TDF_SINTR && (p1->ki_tdflags & TDF_SINTR) == 0)
1251590Srgrimes		return (0);
12687674Smarkm	return (p2->ki_pid > p1->ki_pid);	/* tie - return highest pid */
1271590Srgrimes}
128