19b50d902SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
49b50d902SRodney W. Grimes * Copyright (c) 1990, 1993
59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved.
69b50d902SRodney W. Grimes *
79b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without
89b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions
99b50d902SRodney W. Grimes * are met:
109b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer.
129b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
149b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
169b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software
179b50d902SRodney W. Grimes * without specific prior written permission.
189b50d902SRodney W. Grimes *
199b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
209b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
219b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
229b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
239b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
249b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
259b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
269b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
279b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
289b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
299b50d902SRodney W. Grimes * SUCH DAMAGE.
309b50d902SRodney W. Grimes */
319b50d902SRodney W. Grimes
329b50d902SRodney W. Grimes #include <sys/param.h>
33929789b4SBruce Evans #include <sys/proc.h>
349b50d902SRodney W. Grimes #include <sys/time.h>
351f7d2501SKirk McKusick #include <sys/user.h>
369b50d902SRodney W. Grimes
379b50d902SRodney W. Grimes #include "extern.h"
389b50d902SRodney W. Grimes
399b50d902SRodney W. Grimes /*
409b50d902SRodney W. Grimes * Returns 1 if p2 is "better" than p1
419b50d902SRodney W. Grimes *
429b50d902SRodney W. Grimes * The algorithm for picking the "interesting" process is thus:
439b50d902SRodney W. Grimes *
449b50d902SRodney W. Grimes * 1) Only foreground processes are eligible - implied.
459b50d902SRodney W. Grimes * 2) Runnable processes are favored over anything else. The runner
469b50d902SRodney W. Grimes * with the highest cpu utilization is picked (p_estcpu). Ties are
479b50d902SRodney W. Grimes * broken by picking the highest pid.
489b50d902SRodney W. Grimes * 3) The sleeper with the shortest sleep time is next. With ties,
49929789b4SBruce Evans * we pick out just "short-term" sleepers (TDF_SINTR == 0).
509b50d902SRodney W. Grimes * 4) Further ties are broken by picking the highest pid.
519b50d902SRodney W. Grimes *
529b50d902SRodney W. Grimes * If you change this, be sure to consider making the change in the kernel
539b50d902SRodney W. Grimes * too (^T in kern/tty.c).
549b50d902SRodney W. Grimes *
559b50d902SRodney W. Grimes * TODO - consider whether pctcpu should be used.
569b50d902SRodney W. Grimes */
579b50d902SRodney W. Grimes
581f7d2501SKirk McKusick #define ISRUN(p) (((p)->ki_stat == SRUN) || ((p)->ki_stat == SIDL))
599b50d902SRodney W. Grimes #define TESTAB(a, b) ((a)<<1 | (b))
609b50d902SRodney W. Grimes #define ONLYA 2
619b50d902SRodney W. Grimes #define ONLYB 1
629b50d902SRodney W. Grimes #define BOTH 3
639b50d902SRodney W. Grimes
649b50d902SRodney W. Grimes int
proc_compare(struct kinfo_proc * p1,struct kinfo_proc * p2)65e8e649ccSJuli Mallett proc_compare(struct kinfo_proc *p1, struct kinfo_proc *p2)
669b50d902SRodney W. Grimes {
679b50d902SRodney W. Grimes
689b50d902SRodney W. Grimes if (p1 == NULL)
699b50d902SRodney W. Grimes return (1);
709b50d902SRodney W. Grimes /*
719b50d902SRodney W. Grimes * see if at least one of them is runnable
729b50d902SRodney W. Grimes */
739b50d902SRodney W. Grimes switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
749b50d902SRodney W. Grimes case ONLYA:
759b50d902SRodney W. Grimes return (0);
769b50d902SRodney W. Grimes case ONLYB:
779b50d902SRodney W. Grimes return (1);
789b50d902SRodney W. Grimes case BOTH:
799b50d902SRodney W. Grimes /*
809b50d902SRodney W. Grimes * tie - favor one with highest recent cpu utilization
819b50d902SRodney W. Grimes */
821f7d2501SKirk McKusick if (p2->ki_estcpu > p1->ki_estcpu)
839b50d902SRodney W. Grimes return (1);
841f7d2501SKirk McKusick if (p1->ki_estcpu > p2->ki_estcpu)
859b50d902SRodney W. Grimes return (0);
861f7d2501SKirk McKusick return (p2->ki_pid > p1->ki_pid); /* tie - return highest pid */
879b50d902SRodney W. Grimes }
889b50d902SRodney W. Grimes /*
899b50d902SRodney W. Grimes * weed out zombies
909b50d902SRodney W. Grimes */
911f7d2501SKirk McKusick switch (TESTAB(p1->ki_stat == SZOMB, p2->ki_stat == SZOMB)) {
929b50d902SRodney W. Grimes case ONLYA:
939b50d902SRodney W. Grimes return (1);
949b50d902SRodney W. Grimes case ONLYB:
959b50d902SRodney W. Grimes return (0);
969b50d902SRodney W. Grimes case BOTH:
971f7d2501SKirk McKusick return (p2->ki_pid > p1->ki_pid); /* tie - return highest pid */
989b50d902SRodney W. Grimes }
999b50d902SRodney W. Grimes /*
1009b50d902SRodney W. Grimes * pick the one with the smallest sleep time
1019b50d902SRodney W. Grimes */
1021f7d2501SKirk McKusick if (p2->ki_slptime > p1->ki_slptime)
1039b50d902SRodney W. Grimes return (0);
1041f7d2501SKirk McKusick if (p1->ki_slptime > p2->ki_slptime)
1059b50d902SRodney W. Grimes return (1);
1069b50d902SRodney W. Grimes /*
1079b50d902SRodney W. Grimes * favor one sleeping in a non-interruptible sleep
1089b50d902SRodney W. Grimes */
109b40ce416SJulian Elischer if (p1->ki_tdflags & TDF_SINTR && (p2->ki_tdflags & TDF_SINTR) == 0)
1109b50d902SRodney W. Grimes return (1);
111b40ce416SJulian Elischer if (p2->ki_tdflags & TDF_SINTR && (p1->ki_tdflags & TDF_SINTR) == 0)
1129b50d902SRodney W. Grimes return (0);
1131f7d2501SKirk McKusick return (p2->ki_pid > p1->ki_pid); /* tie - return highest pid */
1149b50d902SRodney W. Grimes }
115