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 * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
30128231Sbde#if 0
31128231Sbde#ifndef lint
32128231Sbdestatic char sccsid[] = "@(#)proc_compare.c	8.2 (Berkeley) 9/23/93";
33128231Sbde#endif /* not lint */
34128231Sbde#endif
35128231Sbde
3687674Smarkm#include <sys/cdefs.h>
3787674Smarkm__FBSDID("$FreeBSD$");
3887674Smarkm
391590Srgrimes#include <sys/param.h>
40128231Sbde#include <sys/proc.h>
411590Srgrimes#include <sys/time.h>
4269896Smckusick#include <sys/user.h>
431590Srgrimes
441590Srgrimes#include "extern.h"
451590Srgrimes
461590Srgrimes/*
471590Srgrimes * Returns 1 if p2 is "better" than p1
481590Srgrimes *
491590Srgrimes * The algorithm for picking the "interesting" process is thus:
501590Srgrimes *
511590Srgrimes *	1) Only foreground processes are eligible - implied.
521590Srgrimes *	2) Runnable processes are favored over anything else.  The runner
531590Srgrimes *	   with the highest cpu utilization is picked (p_estcpu).  Ties are
541590Srgrimes *	   broken by picking the highest pid.
551590Srgrimes *	3) The sleeper with the shortest sleep time is next.  With ties,
56128231Sbde *	   we pick out just "short-term" sleepers (TDF_SINTR == 0).
571590Srgrimes *	4) Further ties are broken by picking the highest pid.
581590Srgrimes *
591590Srgrimes * If you change this, be sure to consider making the change in the kernel
601590Srgrimes * too (^T in kern/tty.c).
611590Srgrimes *
621590Srgrimes * TODO - consider whether pctcpu should be used.
631590Srgrimes */
641590Srgrimes
65243183Sed#define	ISRUN(p)	(((p)->ki_stat == SRUN) || ((p)->ki_stat == SIDL))
66243183Sed#define	TESTAB(a, b)    ((a)<<1 | (b))
67243183Sed#define	ONLYA   2
68243183Sed#define	ONLYB   1
69243183Sed#define	BOTH    3
701590Srgrimes
711590Srgrimesint
7297981Sjmallettproc_compare(struct kinfo_proc *p1, struct kinfo_proc *p2)
731590Srgrimes{
741590Srgrimes
751590Srgrimes	if (p1 == NULL)
761590Srgrimes		return (1);
771590Srgrimes	/*
781590Srgrimes	 * see if at least one of them is runnable
791590Srgrimes	 */
801590Srgrimes	switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
811590Srgrimes	case ONLYA:
821590Srgrimes		return (0);
831590Srgrimes	case ONLYB:
841590Srgrimes		return (1);
851590Srgrimes	case BOTH:
861590Srgrimes		/*
871590Srgrimes		 * tie - favor one with highest recent cpu utilization
881590Srgrimes		 */
8969896Smckusick		if (p2->ki_estcpu > p1->ki_estcpu)
901590Srgrimes			return (1);
9169896Smckusick		if (p1->ki_estcpu > p2->ki_estcpu)
921590Srgrimes			return (0);
93128231Sbde		return (p2->ki_pid > p1->ki_pid); /* tie - return highest pid */
941590Srgrimes	}
951590Srgrimes	/*
96243183Sed	 * weed out zombies
971590Srgrimes	 */
9869896Smckusick	switch (TESTAB(p1->ki_stat == SZOMB, p2->ki_stat == SZOMB)) {
991590Srgrimes	case ONLYA:
1001590Srgrimes		return (1);
1011590Srgrimes	case ONLYB:
1021590Srgrimes		return (0);
1031590Srgrimes	case BOTH:
10469896Smckusick		return (p2->ki_pid > p1->ki_pid); /* tie - return highest pid */
1051590Srgrimes	}
1061590Srgrimes	/*
1071590Srgrimes	 * pick the one with the smallest sleep time
1081590Srgrimes	 */
10969896Smckusick	if (p2->ki_slptime > p1->ki_slptime)
1101590Srgrimes		return (0);
11169896Smckusick	if (p1->ki_slptime > p2->ki_slptime)
1121590Srgrimes		return (1);
1131590Srgrimes	/*
1141590Srgrimes	 * favor one sleeping in a non-interruptible sleep
1151590Srgrimes	 */
11683366Sjulian	if (p1->ki_tdflags & TDF_SINTR && (p2->ki_tdflags & TDF_SINTR) == 0)
1171590Srgrimes		return (1);
11883366Sjulian	if (p2->ki_tdflags & TDF_SINTR && (p1->ki_tdflags & TDF_SINTR) == 0)
1191590Srgrimes		return (0);
12087674Smarkm	return (p2->ki_pid > p1->ki_pid);	/* tie - return highest pid */
1211590Srgrimes}
122